mm, slab: Remove silly function slab_buffer_size()
[firefly-linux-kernel-4.4.55.git] / include / linux / slab_def.h
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>
17
18 /*
19  * struct kmem_cache
20  *
21  * manages a cache.
22  */
23
24 struct kmem_cache {
25 /* 1) Cache tunables. Protected by cache_chain_mutex */
26         unsigned int batchcount;
27         unsigned int limit;
28         unsigned int shared;
29
30         unsigned int size;
31         u32 reciprocal_buffer_size;
32 /* 2) touched by every alloc & free from the backend */
33
34         unsigned int flags;             /* constant flags */
35         unsigned int num;               /* # of objs per slab */
36
37 /* 3) cache_grow/shrink */
38         /* order of pgs per slab (2^n) */
39         unsigned int gfporder;
40
41         /* force GFP flags, e.g. GFP_DMA */
42         gfp_t allocflags;
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;
48
49         /* constructor func */
50         void (*ctor)(void *obj);
51
52 /* 4) cache creation/removal */
53         const char *name;
54         struct list_head list;
55         int refcount;
56         int object_size;
57         int align;
58
59 /* 5) statistics */
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
78          * fields and/or padding to every object. size contains the total
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;
83 #endif /* CONFIG_DEBUG_SLAB */
84
85 /* 6) per-cpu/per-node data, touched during every alloc/free */
86         /*
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
89          * (see kmem_cache_init())
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.
92          */
93         struct kmem_list3 **nodelists;
94         struct array_cache *array[NR_CPUS];
95         /*
96          * Do not add fields after array[]
97          */
98 };
99
100 /* Size description struct for general caches. */
101 struct cache_sizes {
102         size_t                  cs_size;
103         struct kmem_cache       *cs_cachep;
104 #ifdef CONFIG_ZONE_DMA
105         struct kmem_cache       *cs_dmacachep;
106 #endif
107 };
108 extern struct cache_sizes malloc_sizes[];
109
110 void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
111 void *__kmalloc(size_t size, gfp_t flags);
112
113 #ifdef CONFIG_TRACING
114 extern void *kmem_cache_alloc_trace(size_t size,
115                                     struct kmem_cache *cachep, gfp_t flags);
116 #else
117 static __always_inline void *
118 kmem_cache_alloc_trace(size_t size, struct kmem_cache *cachep, gfp_t flags)
119 {
120         return kmem_cache_alloc(cachep, flags);
121 }
122 #endif
123
124 static __always_inline void *kmalloc(size_t size, gfp_t flags)
125 {
126         struct kmem_cache *cachep;
127         void *ret;
128
129         if (__builtin_constant_p(size)) {
130                 int i = 0;
131
132                 if (!size)
133                         return ZERO_SIZE_PTR;
134
135 #define CACHE(x) \
136                 if (size <= x) \
137                         goto found; \
138                 else \
139                         i++;
140 #include <linux/kmalloc_sizes.h>
141 #undef CACHE
142                 return NULL;
143 found:
144 #ifdef CONFIG_ZONE_DMA
145                 if (flags & GFP_DMA)
146                         cachep = malloc_sizes[i].cs_dmacachep;
147                 else
148 #endif
149                         cachep = malloc_sizes[i].cs_cachep;
150
151                 ret = kmem_cache_alloc_trace(size, cachep, flags);
152
153                 return ret;
154         }
155         return __kmalloc(size, flags);
156 }
157
158 #ifdef CONFIG_NUMA
159 extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
160 extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
161
162 #ifdef CONFIG_TRACING
163 extern void *kmem_cache_alloc_node_trace(size_t size,
164                                          struct kmem_cache *cachep,
165                                          gfp_t flags,
166                                          int nodeid);
167 #else
168 static __always_inline void *
169 kmem_cache_alloc_node_trace(size_t size,
170                             struct kmem_cache *cachep,
171                             gfp_t flags,
172                             int nodeid)
173 {
174         return kmem_cache_alloc_node(cachep, flags, nodeid);
175 }
176 #endif
177
178 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
179 {
180         struct kmem_cache *cachep;
181
182         if (__builtin_constant_p(size)) {
183                 int i = 0;
184
185                 if (!size)
186                         return ZERO_SIZE_PTR;
187
188 #define CACHE(x) \
189                 if (size <= x) \
190                         goto found; \
191                 else \
192                         i++;
193 #include <linux/kmalloc_sizes.h>
194 #undef CACHE
195                 return NULL;
196 found:
197 #ifdef CONFIG_ZONE_DMA
198                 if (flags & GFP_DMA)
199                         cachep = malloc_sizes[i].cs_dmacachep;
200                 else
201 #endif
202                         cachep = malloc_sizes[i].cs_cachep;
203
204                 return kmem_cache_alloc_node_trace(size, cachep, flags, node);
205         }
206         return __kmalloc_node(size, flags, node);
207 }
208
209 #endif  /* CONFIG_NUMA */
210
211 #endif  /* _LINUX_SLAB_DEF_H */