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