]> git.karo-electronics.de Git - karo-tx-linux.git/blob - mm/slab_common.c
Merge branch 'snd-bugfix' into tx53-devel
[karo-tx-linux.git] / mm / slab_common.c
1 /*
2  * Slab allocator functions that are independent of the allocator strategy
3  *
4  * (C) 2012 Christoph Lameter <cl@linux.com>
5  */
6 #include <linux/slab.h>
7
8 #include <linux/mm.h>
9 #include <linux/poison.h>
10 #include <linux/interrupt.h>
11 #include <linux/memory.h>
12 #include <linux/compiler.h>
13 #include <linux/module.h>
14 #include <linux/cpu.h>
15 #include <linux/uaccess.h>
16 #include <linux/seq_file.h>
17 #include <linux/proc_fs.h>
18 #include <asm/cacheflush.h>
19 #include <asm/tlbflush.h>
20 #include <asm/page.h>
21 #include <linux/memcontrol.h>
22
23 #include "slab.h"
24
25 enum slab_state slab_state;
26 LIST_HEAD(slab_caches);
27 DEFINE_MUTEX(slab_mutex);
28 struct kmem_cache *kmem_cache;
29
30 #ifdef CONFIG_DEBUG_VM
31 static int kmem_cache_sanity_check(struct mem_cgroup *memcg, const char *name,
32                                    size_t size)
33 {
34         struct kmem_cache *s = NULL;
35
36         if (!name || in_interrupt() || size < sizeof(void *) ||
37                 size > KMALLOC_MAX_SIZE) {
38                 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
39                 return -EINVAL;
40         }
41
42         list_for_each_entry(s, &slab_caches, list) {
43                 char tmp;
44                 int res;
45
46                 /*
47                  * This happens when the module gets unloaded and doesn't
48                  * destroy its slab cache and no-one else reuses the vmalloc
49                  * area of the module.  Print a warning.
50                  */
51                 res = probe_kernel_address(s->name, tmp);
52                 if (res) {
53                         pr_err("Slab cache with size %d has lost its name\n",
54                                s->object_size);
55                         continue;
56                 }
57
58                 /*
59                  * For simplicity, we won't check this in the list of memcg
60                  * caches. We have control over memcg naming, and if there
61                  * aren't duplicates in the global list, there won't be any
62                  * duplicates in the memcg lists as well.
63                  */
64                 if (!memcg && !strcmp(s->name, name)) {
65                         pr_err("%s (%s): Cache name already exists.\n",
66                                __func__, name);
67                         dump_stack();
68                         s = NULL;
69                         return -EINVAL;
70                 }
71         }
72
73         WARN_ON(strchr(name, ' '));     /* It confuses parsers */
74         return 0;
75 }
76 #else
77 static inline int kmem_cache_sanity_check(struct mem_cgroup *memcg,
78                                           const char *name, size_t size)
79 {
80         return 0;
81 }
82 #endif
83
84 #ifdef CONFIG_MEMCG_KMEM
85 int memcg_update_all_caches(int num_memcgs)
86 {
87         struct kmem_cache *s;
88         int ret = 0;
89         mutex_lock(&slab_mutex);
90
91         list_for_each_entry(s, &slab_caches, list) {
92                 if (!is_root_cache(s))
93                         continue;
94
95                 ret = memcg_update_cache_size(s, num_memcgs);
96                 /*
97                  * See comment in memcontrol.c, memcg_update_cache_size:
98                  * Instead of freeing the memory, we'll just leave the caches
99                  * up to this point in an updated state.
100                  */
101                 if (ret)
102                         goto out;
103         }
104
105         memcg_update_array_size(num_memcgs);
106 out:
107         mutex_unlock(&slab_mutex);
108         return ret;
109 }
110 #endif
111
112 /*
113  * kmem_cache_create - Create a cache.
114  * @name: A string which is used in /proc/slabinfo to identify this cache.
115  * @size: The size of objects to be created in this cache.
116  * @align: The required alignment for the objects.
117  * @flags: SLAB flags
118  * @ctor: A constructor for the objects.
119  *
120  * Returns a ptr to the cache on success, NULL on failure.
121  * Cannot be called within a interrupt, but can be interrupted.
122  * The @ctor is run when new pages are allocated by the cache.
123  *
124  * The flags are
125  *
126  * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
127  * to catch references to uninitialised memory.
128  *
129  * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
130  * for buffer overruns.
131  *
132  * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
133  * cacheline.  This can be beneficial if you're counting cycles as closely
134  * as davem.
135  */
136
137 struct kmem_cache *
138 kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size,
139                         size_t align, unsigned long flags, void (*ctor)(void *),
140                         struct kmem_cache *parent_cache)
141 {
142         struct kmem_cache *s = NULL;
143         int err = 0;
144
145         get_online_cpus();
146         mutex_lock(&slab_mutex);
147
148         if (!kmem_cache_sanity_check(memcg, name, size) == 0)
149                 goto out_locked;
150
151         /*
152          * Some allocators will constraint the set of valid flags to a subset
153          * of all flags. We expect them to define CACHE_CREATE_MASK in this
154          * case, and we'll just provide them with a sanitized version of the
155          * passed flags.
156          */
157         flags &= CACHE_CREATE_MASK;
158
159         s = __kmem_cache_alias(memcg, name, size, align, flags, ctor);
160         if (s)
161                 goto out_locked;
162
163         s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
164         if (s) {
165                 s->object_size = s->size = size;
166                 s->align = align;
167                 s->ctor = ctor;
168
169                 if (memcg_register_cache(memcg, s, parent_cache)) {
170                         kmem_cache_free(kmem_cache, s);
171                         err = -ENOMEM;
172                         goto out_locked;
173                 }
174
175                 s->name = kstrdup(name, GFP_KERNEL);
176                 if (!s->name) {
177                         kmem_cache_free(kmem_cache, s);
178                         err = -ENOMEM;
179                         goto out_locked;
180                 }
181
182                 err = __kmem_cache_create(s, flags);
183                 if (!err) {
184                         s->refcount = 1;
185                         list_add(&s->list, &slab_caches);
186                         memcg_cache_list_add(memcg, s);
187                 } else {
188                         kfree(s->name);
189                         kmem_cache_free(kmem_cache, s);
190                 }
191         } else
192                 err = -ENOMEM;
193
194 out_locked:
195         mutex_unlock(&slab_mutex);
196         put_online_cpus();
197
198         if (err) {
199
200                 if (flags & SLAB_PANIC)
201                         panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
202                                 name, err);
203                 else {
204                         printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
205                                 name, err);
206                         dump_stack();
207                 }
208
209                 return NULL;
210         }
211
212         return s;
213 }
214
215 struct kmem_cache *
216 kmem_cache_create(const char *name, size_t size, size_t align,
217                   unsigned long flags, void (*ctor)(void *))
218 {
219         return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor, NULL);
220 }
221 EXPORT_SYMBOL(kmem_cache_create);
222
223 void kmem_cache_destroy(struct kmem_cache *s)
224 {
225         /* Destroy all the children caches if we aren't a memcg cache */
226         kmem_cache_destroy_memcg_children(s);
227
228         get_online_cpus();
229         mutex_lock(&slab_mutex);
230         s->refcount--;
231         if (!s->refcount) {
232                 list_del(&s->list);
233
234                 if (!__kmem_cache_shutdown(s)) {
235                         mutex_unlock(&slab_mutex);
236                         if (s->flags & SLAB_DESTROY_BY_RCU)
237                                 rcu_barrier();
238
239                         memcg_release_cache(s);
240                         kfree(s->name);
241                         kmem_cache_free(kmem_cache, s);
242                 } else {
243                         list_add(&s->list, &slab_caches);
244                         mutex_unlock(&slab_mutex);
245                         printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
246                                 s->name);
247                         dump_stack();
248                 }
249         } else {
250                 mutex_unlock(&slab_mutex);
251         }
252         put_online_cpus();
253 }
254 EXPORT_SYMBOL(kmem_cache_destroy);
255
256 int slab_is_available(void)
257 {
258         return slab_state >= UP;
259 }
260
261 #ifdef CONFIG_SLABINFO
262 void print_slabinfo_header(struct seq_file *m)
263 {
264         /*
265          * Output format version, so at least we can change it
266          * without _too_ many complaints.
267          */
268 #ifdef CONFIG_DEBUG_SLAB
269         seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
270 #else
271         seq_puts(m, "slabinfo - version: 2.1\n");
272 #endif
273         seq_puts(m, "# name            <active_objs> <num_objs> <objsize> "
274                  "<objperslab> <pagesperslab>");
275         seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
276         seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
277 #ifdef CONFIG_DEBUG_SLAB
278         seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
279                  "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
280         seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
281 #endif
282         seq_putc(m, '\n');
283 }
284
285 static void *s_start(struct seq_file *m, loff_t *pos)
286 {
287         loff_t n = *pos;
288
289         mutex_lock(&slab_mutex);
290         if (!n)
291                 print_slabinfo_header(m);
292
293         return seq_list_start(&slab_caches, *pos);
294 }
295
296 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
297 {
298         return seq_list_next(p, &slab_caches, pos);
299 }
300
301 static void s_stop(struct seq_file *m, void *p)
302 {
303         mutex_unlock(&slab_mutex);
304 }
305
306 static void
307 memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
308 {
309         struct kmem_cache *c;
310         struct slabinfo sinfo;
311         int i;
312
313         if (!is_root_cache(s))
314                 return;
315
316         for_each_memcg_cache_index(i) {
317                 c = cache_from_memcg(s, i);
318                 if (!c)
319                         continue;
320
321                 memset(&sinfo, 0, sizeof(sinfo));
322                 get_slabinfo(c, &sinfo);
323
324                 info->active_slabs += sinfo.active_slabs;
325                 info->num_slabs += sinfo.num_slabs;
326                 info->shared_avail += sinfo.shared_avail;
327                 info->active_objs += sinfo.active_objs;
328                 info->num_objs += sinfo.num_objs;
329         }
330 }
331
332 int cache_show(struct kmem_cache *s, struct seq_file *m)
333 {
334         struct slabinfo sinfo;
335
336         memset(&sinfo, 0, sizeof(sinfo));
337         get_slabinfo(s, &sinfo);
338
339         memcg_accumulate_slabinfo(s, &sinfo);
340
341         seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
342                    cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
343                    sinfo.objects_per_slab, (1 << sinfo.cache_order));
344
345         seq_printf(m, " : tunables %4u %4u %4u",
346                    sinfo.limit, sinfo.batchcount, sinfo.shared);
347         seq_printf(m, " : slabdata %6lu %6lu %6lu",
348                    sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
349         slabinfo_show_stats(m, s);
350         seq_putc(m, '\n');
351         return 0;
352 }
353
354 static int s_show(struct seq_file *m, void *p)
355 {
356         struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
357
358         if (!is_root_cache(s))
359                 return 0;
360         return cache_show(s, m);
361 }
362
363 /*
364  * slabinfo_op - iterator that generates /proc/slabinfo
365  *
366  * Output layout:
367  * cache-name
368  * num-active-objs
369  * total-objs
370  * object size
371  * num-active-slabs
372  * total-slabs
373  * num-pages-per-slab
374  * + further values on SMP and with statistics enabled
375  */
376 static const struct seq_operations slabinfo_op = {
377         .start = s_start,
378         .next = s_next,
379         .stop = s_stop,
380         .show = s_show,
381 };
382
383 static int slabinfo_open(struct inode *inode, struct file *file)
384 {
385         return seq_open(file, &slabinfo_op);
386 }
387
388 static const struct file_operations proc_slabinfo_operations = {
389         .open           = slabinfo_open,
390         .read           = seq_read,
391         .write          = slabinfo_write,
392         .llseek         = seq_lseek,
393         .release        = seq_release,
394 };
395
396 static int __init slab_proc_init(void)
397 {
398         proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
399         return 0;
400 }
401 module_init(slab_proc_init);
402 #endif /* CONFIG_SLABINFO */