slab: fix DEBUG_SLAB build
[deliverable/linux.git] / include / linux / slab_def.h
CommitLineData
2e892f43
CL
1#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>
039ca4e7
LZ
17
18#include <trace/events/kmem.h>
2e892f43 19
8eae985f
PE
20/*
21 * struct kmem_cache
22 *
23 * manages a cache.
24 */
25
26struct kmem_cache {
27/* 1) per-cpu data, touched during every alloc/free */
28 struct array_cache *array[NR_CPUS];
29/* 2) Cache tunables. Protected by cache_chain_mutex */
30 unsigned int batchcount;
31 unsigned int limit;
32 unsigned int shared;
33
34 unsigned int buffer_size;
35 u32 reciprocal_buffer_size;
36/* 3) touched by every alloc & free from the backend */
37
38 unsigned int flags; /* constant flags */
39 unsigned int num; /* # of objs per slab */
40
41/* 4) cache_grow/shrink */
42 /* order of pgs per slab (2^n) */
43 unsigned int gfporder;
44
45 /* force GFP flags, e.g. GFP_DMA */
46 gfp_t gfpflags;
47
48 size_t colour; /* cache colouring range */
49 unsigned int colour_off; /* colour offset */
50 struct kmem_cache *slabp_cache;
51 unsigned int slab_size;
52 unsigned int dflags; /* dynamic flags */
53
54 /* constructor func */
55 void (*ctor)(void *obj);
56
57/* 5) cache creation/removal */
58 const char *name;
59 struct list_head next;
60
61/* 6) statistics */
62#ifdef CONFIG_DEBUG_SLAB
63 unsigned long num_active;
64 unsigned long num_allocations;
65 unsigned long high_mark;
66 unsigned long grown;
67 unsigned long reaped;
68 unsigned long errors;
69 unsigned long max_freeable;
70 unsigned long node_allocs;
71 unsigned long node_frees;
72 unsigned long node_overflow;
73 atomic_t allochit;
74 atomic_t allocmiss;
75 atomic_t freehit;
76 atomic_t freemiss;
77
78 /*
79 * If debugging is enabled, then the allocator can add additional
80 * fields and/or padding to every object. buffer_size contains the total
81 * object size including these internal fields, the following two
82 * variables contain the offset to the user object and its size.
83 */
84 int obj_offset;
85 int obj_size;
86#endif /* CONFIG_DEBUG_SLAB */
87
88 /*
89 * We put nodelists[] at the end of kmem_cache, because we want to size
90 * this array to nr_node_ids slots instead of MAX_NUMNODES
91 * (see kmem_cache_init())
92 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
93 * is statically defined, so we reserve the max number of nodes.
94 */
95 struct kmem_list3 *nodelists[MAX_NUMNODES];
96 /*
97 * Do not add fields after nodelists[]
98 */
99};
100
2e892f43
CL
101/* Size description struct for general caches. */
102struct cache_sizes {
103 size_t cs_size;
104 struct kmem_cache *cs_cachep;
4b51d669 105#ifdef CONFIG_ZONE_DMA
2e892f43 106 struct kmem_cache *cs_dmacachep;
4b51d669 107#endif
2e892f43
CL
108};
109extern struct cache_sizes malloc_sizes[];
110
6193a2ff
PM
111void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
112void *__kmalloc(size_t size, gfp_t flags);
113
0f24f128 114#ifdef CONFIG_TRACING
85beb586
SR
115extern void *kmem_cache_alloc_trace(size_t size,
116 struct kmem_cache *cachep, gfp_t flags);
36555751
EGM
117extern size_t slab_buffer_size(struct kmem_cache *cachep);
118#else
119static __always_inline void *
85beb586 120kmem_cache_alloc_trace(size_t size, struct kmem_cache *cachep, gfp_t flags)
2e892f43 121{
36555751
EGM
122 return kmem_cache_alloc(cachep, flags);
123}
124static inline size_t slab_buffer_size(struct kmem_cache *cachep)
125{
126 return 0;
127}
128#endif
129
130static __always_inline void *kmalloc(size_t size, gfp_t flags)
131{
132 struct kmem_cache *cachep;
133 void *ret;
134
2e892f43
CL
135 if (__builtin_constant_p(size)) {
136 int i = 0;
6cb8f913
CL
137
138 if (!size)
139 return ZERO_SIZE_PTR;
140
2e892f43
CL
141#define CACHE(x) \
142 if (size <= x) \
143 goto found; \
144 else \
145 i++;
1c61fc40 146#include <linux/kmalloc_sizes.h>
2e892f43 147#undef CACHE
1cf3eb2f 148 return NULL;
2e892f43 149found:
4b51d669
CL
150#ifdef CONFIG_ZONE_DMA
151 if (flags & GFP_DMA)
36555751
EGM
152 cachep = malloc_sizes[i].cs_dmacachep;
153 else
4b51d669 154#endif
36555751
EGM
155 cachep = malloc_sizes[i].cs_cachep;
156
85beb586 157 ret = kmem_cache_alloc_trace(size, cachep, flags);
36555751
EGM
158
159 return ret;
2e892f43
CL
160 }
161 return __kmalloc(size, flags);
162}
163
2e892f43
CL
164#ifdef CONFIG_NUMA
165extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
6193a2ff 166extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
2e892f43 167
0f24f128 168#ifdef CONFIG_TRACING
85beb586
SR
169extern void *kmem_cache_alloc_node_trace(size_t size,
170 struct kmem_cache *cachep,
171 gfp_t flags,
172 int nodeid);
36555751
EGM
173#else
174static __always_inline void *
85beb586
SR
175kmem_cache_alloc_node_trace(size_t size,
176 struct kmem_cache *cachep,
177 gfp_t flags,
178 int nodeid)
36555751
EGM
179{
180 return kmem_cache_alloc_node(cachep, flags, nodeid);
181}
182#endif
183
184static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
2e892f43 185{
36555751 186 struct kmem_cache *cachep;
36555751 187
2e892f43
CL
188 if (__builtin_constant_p(size)) {
189 int i = 0;
6cb8f913
CL
190
191 if (!size)
192 return ZERO_SIZE_PTR;
193
2e892f43
CL
194#define CACHE(x) \
195 if (size <= x) \
196 goto found; \
197 else \
198 i++;
1c61fc40 199#include <linux/kmalloc_sizes.h>
2e892f43 200#undef CACHE
1cf3eb2f 201 return NULL;
2e892f43 202found:
4b51d669
CL
203#ifdef CONFIG_ZONE_DMA
204 if (flags & GFP_DMA)
36555751
EGM
205 cachep = malloc_sizes[i].cs_dmacachep;
206 else
4b51d669 207#endif
36555751
EGM
208 cachep = malloc_sizes[i].cs_cachep;
209
85beb586 210 return kmem_cache_alloc_node_trace(size, cachep, flags, node);
2e892f43
CL
211 }
212 return __kmalloc_node(size, flags, node);
213}
214
215#endif /* CONFIG_NUMA */
216
217#endif /* _LINUX_SLAB_DEF_H */
This page took 0.592758 seconds and 5 git commands to generate.