]> git.ipfire.org Git - people/arne_f/kernel.git/blob - mm/slab_common.c
Merge tag 'v3.10.51' into linux-3.10.x-grsecurity-2.9.1
[people/arne_f/kernel.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 __read_only;
26 LIST_HEAD(slab_caches);
27 DEFINE_MUTEX(slab_mutex);
28 struct kmem_cache *kmem_cache;
29
30 #ifdef CONFIG_PAX_MEMORY_SANITIZE
31 bool pax_sanitize_slab __read_only = true;
32 static int __init pax_sanitize_slab_setup(char *str)
33 {
34 pax_sanitize_slab = !!simple_strtol(str, NULL, 0);
35 printk("%sabled PaX slab sanitization\n", pax_sanitize_slab ? "En" : "Dis");
36 return 1;
37 }
38 __setup("pax_sanitize_slab=", pax_sanitize_slab_setup);
39 #endif
40
41 #ifdef CONFIG_DEBUG_VM
42 static int kmem_cache_sanity_check(struct mem_cgroup *memcg, const char *name,
43 size_t size)
44 {
45 struct kmem_cache *s = NULL;
46
47 if (!name || in_interrupt() || size < sizeof(void *) ||
48 size > KMALLOC_MAX_SIZE) {
49 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
50 return -EINVAL;
51 }
52
53 list_for_each_entry(s, &slab_caches, list) {
54 char tmp;
55 int res;
56
57 /*
58 * This happens when the module gets unloaded and doesn't
59 * destroy its slab cache and no-one else reuses the vmalloc
60 * area of the module. Print a warning.
61 */
62 res = probe_kernel_address(s->name, tmp);
63 if (res) {
64 pr_err("Slab cache with size %d has lost its name\n",
65 s->object_size);
66 continue;
67 }
68
69 #if !defined(CONFIG_SLUB)
70 /*
71 * For simplicity, we won't check this in the list of memcg
72 * caches. We have control over memcg naming, and if there
73 * aren't duplicates in the global list, there won't be any
74 * duplicates in the memcg lists as well.
75 */
76 if (!memcg && !strcmp(s->name, name)) {
77 pr_err("%s (%s): Cache name already exists.\n",
78 __func__, name);
79 dump_stack();
80 s = NULL;
81 return -EINVAL;
82 }
83 #endif
84 }
85
86 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
87 return 0;
88 }
89 #else
90 static inline int kmem_cache_sanity_check(struct mem_cgroup *memcg,
91 const char *name, size_t size)
92 {
93 return 0;
94 }
95 #endif
96
97 #ifdef CONFIG_MEMCG_KMEM
98 int memcg_update_all_caches(int num_memcgs)
99 {
100 struct kmem_cache *s;
101 int ret = 0;
102 mutex_lock(&slab_mutex);
103
104 list_for_each_entry(s, &slab_caches, list) {
105 if (!is_root_cache(s))
106 continue;
107
108 ret = memcg_update_cache_size(s, num_memcgs);
109 /*
110 * See comment in memcontrol.c, memcg_update_cache_size:
111 * Instead of freeing the memory, we'll just leave the caches
112 * up to this point in an updated state.
113 */
114 if (ret)
115 goto out;
116 }
117
118 memcg_update_array_size(num_memcgs);
119 out:
120 mutex_unlock(&slab_mutex);
121 return ret;
122 }
123 #endif
124
125 /*
126 * Figure out what the alignment of the objects will be given a set of
127 * flags, a user specified alignment and the size of the objects.
128 */
129 unsigned long calculate_alignment(unsigned long flags,
130 unsigned long align, unsigned long size)
131 {
132 /*
133 * If the user wants hardware cache aligned objects then follow that
134 * suggestion if the object is sufficiently large.
135 *
136 * The hardware cache alignment cannot override the specified
137 * alignment though. If that is greater then use it.
138 */
139 if (flags & SLAB_HWCACHE_ALIGN) {
140 unsigned long ralign = cache_line_size();
141 while (size <= ralign / 2)
142 ralign /= 2;
143 align = max(align, ralign);
144 }
145
146 if (align < ARCH_SLAB_MINALIGN)
147 align = ARCH_SLAB_MINALIGN;
148
149 return ALIGN(align, sizeof(void *));
150 }
151
152
153 /*
154 * kmem_cache_create - Create a cache.
155 * @name: A string which is used in /proc/slabinfo to identify this cache.
156 * @size: The size of objects to be created in this cache.
157 * @align: The required alignment for the objects.
158 * @flags: SLAB flags
159 * @ctor: A constructor for the objects.
160 *
161 * Returns a ptr to the cache on success, NULL on failure.
162 * Cannot be called within a interrupt, but can be interrupted.
163 * The @ctor is run when new pages are allocated by the cache.
164 *
165 * The flags are
166 *
167 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
168 * to catch references to uninitialised memory.
169 *
170 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
171 * for buffer overruns.
172 *
173 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
174 * cacheline. This can be beneficial if you're counting cycles as closely
175 * as davem.
176 */
177
178 struct kmem_cache *
179 kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size,
180 size_t align, unsigned long flags, void (*ctor)(void *),
181 struct kmem_cache *parent_cache)
182 {
183 struct kmem_cache *s = NULL;
184 int err = 0;
185
186 get_online_cpus();
187 mutex_lock(&slab_mutex);
188
189 if (!kmem_cache_sanity_check(memcg, name, size) == 0)
190 goto out_locked;
191
192 /*
193 * Some allocators will constraint the set of valid flags to a subset
194 * of all flags. We expect them to define CACHE_CREATE_MASK in this
195 * case, and we'll just provide them with a sanitized version of the
196 * passed flags.
197 */
198 flags &= CACHE_CREATE_MASK;
199
200 s = __kmem_cache_alias(memcg, name, size, align, flags, ctor);
201 if (s)
202 goto out_locked;
203
204 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
205 if (s) {
206 s->object_size = s->size = size;
207 s->align = calculate_alignment(flags, align, size);
208 s->ctor = ctor;
209
210 if (memcg_register_cache(memcg, s, parent_cache)) {
211 kmem_cache_free(kmem_cache, s);
212 err = -ENOMEM;
213 goto out_locked;
214 }
215
216 s->name = kstrdup(name, GFP_KERNEL);
217 if (!s->name) {
218 kmem_cache_free(kmem_cache, s);
219 err = -ENOMEM;
220 goto out_locked;
221 }
222
223 err = __kmem_cache_create(s, flags);
224 if (!err) {
225 atomic_set(&s->refcount, 1);
226 list_add(&s->list, &slab_caches);
227 memcg_cache_list_add(memcg, s);
228 } else {
229 kfree(s->name);
230 kmem_cache_free(kmem_cache, s);
231 }
232 } else
233 err = -ENOMEM;
234
235 out_locked:
236 mutex_unlock(&slab_mutex);
237 put_online_cpus();
238
239 if (err) {
240
241 if (flags & SLAB_PANIC)
242 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
243 name, err);
244 else {
245 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
246 name, err);
247 dump_stack();
248 }
249
250 return NULL;
251 }
252
253 return s;
254 }
255
256 struct kmem_cache *
257 kmem_cache_create(const char *name, size_t size, size_t align,
258 unsigned long flags, void (*ctor)(void *))
259 {
260 return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor, NULL);
261 }
262 EXPORT_SYMBOL(kmem_cache_create);
263
264 void kmem_cache_destroy(struct kmem_cache *s)
265 {
266 /* Destroy all the children caches if we aren't a memcg cache */
267 kmem_cache_destroy_memcg_children(s);
268
269 get_online_cpus();
270 mutex_lock(&slab_mutex);
271 if (atomic_dec_and_test(&s->refcount)) {
272 list_del(&s->list);
273
274 if (!__kmem_cache_shutdown(s)) {
275 mutex_unlock(&slab_mutex);
276 if (s->flags & SLAB_DESTROY_BY_RCU)
277 rcu_barrier();
278
279 memcg_release_cache(s);
280 kfree(s->name);
281 kmem_cache_free(kmem_cache, s);
282 } else {
283 list_add(&s->list, &slab_caches);
284 mutex_unlock(&slab_mutex);
285 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
286 s->name);
287 dump_stack();
288 }
289 } else {
290 mutex_unlock(&slab_mutex);
291 }
292 put_online_cpus();
293 }
294 EXPORT_SYMBOL(kmem_cache_destroy);
295
296 int slab_is_available(void)
297 {
298 return slab_state >= UP;
299 }
300
301 #ifndef CONFIG_SLOB
302 /* Create a cache during boot when no slab services are available yet */
303 void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
304 unsigned long flags)
305 {
306 int err;
307
308 s->name = name;
309 s->size = s->object_size = size;
310 s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
311 err = __kmem_cache_create(s, flags);
312
313 if (err)
314 panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
315 name, size, err);
316
317 atomic_set(&s->refcount, -1); /* Exempt from merging for now */
318 }
319
320 struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
321 unsigned long flags)
322 {
323 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
324
325 if (!s)
326 panic("Out of memory when creating slab %s\n", name);
327
328 create_boot_cache(s, name, size, flags);
329 list_add(&s->list, &slab_caches);
330 atomic_set(&s->refcount, 1);
331 return s;
332 }
333
334 struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
335 EXPORT_SYMBOL(kmalloc_caches);
336
337 #ifdef CONFIG_ZONE_DMA
338 struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
339 EXPORT_SYMBOL(kmalloc_dma_caches);
340 #endif
341
342 #ifdef CONFIG_PAX_USERCOPY_SLABS
343 struct kmem_cache *kmalloc_usercopy_caches[KMALLOC_SHIFT_HIGH + 1];
344 EXPORT_SYMBOL(kmalloc_usercopy_caches);
345 #endif
346
347 /*
348 * Conversion table for small slabs sizes / 8 to the index in the
349 * kmalloc array. This is necessary for slabs < 192 since we have non power
350 * of two cache sizes there. The size of larger slabs can be determined using
351 * fls.
352 */
353 static s8 size_index[24] = {
354 3, /* 8 */
355 4, /* 16 */
356 5, /* 24 */
357 5, /* 32 */
358 6, /* 40 */
359 6, /* 48 */
360 6, /* 56 */
361 6, /* 64 */
362 1, /* 72 */
363 1, /* 80 */
364 1, /* 88 */
365 1, /* 96 */
366 7, /* 104 */
367 7, /* 112 */
368 7, /* 120 */
369 7, /* 128 */
370 2, /* 136 */
371 2, /* 144 */
372 2, /* 152 */
373 2, /* 160 */
374 2, /* 168 */
375 2, /* 176 */
376 2, /* 184 */
377 2 /* 192 */
378 };
379
380 static inline int size_index_elem(size_t bytes)
381 {
382 return (bytes - 1) / 8;
383 }
384
385 /*
386 * Find the kmem_cache structure that serves a given size of
387 * allocation
388 */
389 struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
390 {
391 int index;
392
393 if (size > KMALLOC_MAX_SIZE) {
394 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
395 return NULL;
396 }
397
398 if (size <= 192) {
399 if (!size)
400 return ZERO_SIZE_PTR;
401
402 index = size_index[size_index_elem(size)];
403 } else
404 index = fls(size - 1);
405
406 #ifdef CONFIG_ZONE_DMA
407 if (unlikely((flags & GFP_DMA)))
408 return kmalloc_dma_caches[index];
409
410 #endif
411
412 #ifdef CONFIG_PAX_USERCOPY_SLABS
413 if (unlikely((flags & GFP_USERCOPY)))
414 return kmalloc_usercopy_caches[index];
415
416 #endif
417
418 return kmalloc_caches[index];
419 }
420
421 /*
422 * Create the kmalloc array. Some of the regular kmalloc arrays
423 * may already have been created because they were needed to
424 * enable allocations for slab creation.
425 */
426 void __init create_kmalloc_caches(unsigned long flags)
427 {
428 int i;
429
430 /*
431 * Patch up the size_index table if we have strange large alignment
432 * requirements for the kmalloc array. This is only the case for
433 * MIPS it seems. The standard arches will not generate any code here.
434 *
435 * Largest permitted alignment is 256 bytes due to the way we
436 * handle the index determination for the smaller caches.
437 *
438 * Make sure that nothing crazy happens if someone starts tinkering
439 * around with ARCH_KMALLOC_MINALIGN
440 */
441 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
442 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
443
444 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
445 int elem = size_index_elem(i);
446
447 if (elem >= ARRAY_SIZE(size_index))
448 break;
449 size_index[elem] = KMALLOC_SHIFT_LOW;
450 }
451
452 if (KMALLOC_MIN_SIZE >= 64) {
453 /*
454 * The 96 byte size cache is not used if the alignment
455 * is 64 byte.
456 */
457 for (i = 64 + 8; i <= 96; i += 8)
458 size_index[size_index_elem(i)] = 7;
459
460 }
461
462 if (KMALLOC_MIN_SIZE >= 128) {
463 /*
464 * The 192 byte sized cache is not used if the alignment
465 * is 128 byte. Redirect kmalloc to use the 256 byte cache
466 * instead.
467 */
468 for (i = 128 + 8; i <= 192; i += 8)
469 size_index[size_index_elem(i)] = 8;
470 }
471 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
472 if (!kmalloc_caches[i]) {
473 kmalloc_caches[i] = create_kmalloc_cache(NULL,
474 1 << i, SLAB_USERCOPY | flags);
475 }
476
477 /*
478 * Caches that are not of the two-to-the-power-of size.
479 * These have to be created immediately after the
480 * earlier power of two caches
481 */
482 if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
483 kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, SLAB_USERCOPY | flags);
484
485 if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
486 kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, SLAB_USERCOPY | flags);
487 }
488
489 /* Kmalloc array is now usable */
490 slab_state = UP;
491
492 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
493 struct kmem_cache *s = kmalloc_caches[i];
494 char *n;
495
496 if (s) {
497 n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
498
499 BUG_ON(!n);
500 s->name = n;
501 }
502 }
503
504 #ifdef CONFIG_ZONE_DMA
505 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
506 struct kmem_cache *s = kmalloc_caches[i];
507
508 if (s) {
509 int size = kmalloc_size(i);
510 char *n = kasprintf(GFP_NOWAIT,
511 "dma-kmalloc-%d", size);
512
513 BUG_ON(!n);
514 kmalloc_dma_caches[i] = create_kmalloc_cache(n,
515 size, SLAB_CACHE_DMA | flags);
516 }
517 }
518 #endif
519
520 #ifdef CONFIG_PAX_USERCOPY_SLABS
521 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
522 struct kmem_cache *s = kmalloc_caches[i];
523
524 if (s) {
525 int size = kmalloc_size(i);
526 char *n = kasprintf(GFP_NOWAIT,
527 "usercopy-kmalloc-%d", size);
528
529 BUG_ON(!n);
530 kmalloc_usercopy_caches[i] = create_kmalloc_cache(n,
531 size, SLAB_USERCOPY | flags);
532 }
533 }
534 #endif
535
536 }
537 #endif /* !CONFIG_SLOB */
538
539
540 #ifdef CONFIG_SLABINFO
541 void print_slabinfo_header(struct seq_file *m)
542 {
543 /*
544 * Output format version, so at least we can change it
545 * without _too_ many complaints.
546 */
547 #ifdef CONFIG_DEBUG_SLAB
548 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
549 #else
550 seq_puts(m, "slabinfo - version: 2.1\n");
551 #endif
552 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
553 "<objperslab> <pagesperslab>");
554 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
555 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
556 #ifdef CONFIG_DEBUG_SLAB
557 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
558 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
559 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
560 #ifdef CONFIG_PAX_MEMORY_SANITIZE
561 seq_puts(m, " : pax <sanitized> <not_sanitized>");
562 #endif
563 #endif
564 seq_putc(m, '\n');
565 }
566
567 static void *s_start(struct seq_file *m, loff_t *pos)
568 {
569 loff_t n = *pos;
570
571 mutex_lock(&slab_mutex);
572 if (!n)
573 print_slabinfo_header(m);
574
575 return seq_list_start(&slab_caches, *pos);
576 }
577
578 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
579 {
580 return seq_list_next(p, &slab_caches, pos);
581 }
582
583 static void s_stop(struct seq_file *m, void *p)
584 {
585 mutex_unlock(&slab_mutex);
586 }
587
588 static void
589 memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
590 {
591 struct kmem_cache *c;
592 struct slabinfo sinfo;
593 int i;
594
595 if (!is_root_cache(s))
596 return;
597
598 for_each_memcg_cache_index(i) {
599 c = cache_from_memcg(s, i);
600 if (!c)
601 continue;
602
603 memset(&sinfo, 0, sizeof(sinfo));
604 get_slabinfo(c, &sinfo);
605
606 info->active_slabs += sinfo.active_slabs;
607 info->num_slabs += sinfo.num_slabs;
608 info->shared_avail += sinfo.shared_avail;
609 info->active_objs += sinfo.active_objs;
610 info->num_objs += sinfo.num_objs;
611 }
612 }
613
614 int cache_show(struct kmem_cache *s, struct seq_file *m)
615 {
616 struct slabinfo sinfo;
617
618 memset(&sinfo, 0, sizeof(sinfo));
619 get_slabinfo(s, &sinfo);
620
621 memcg_accumulate_slabinfo(s, &sinfo);
622
623 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
624 cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
625 sinfo.objects_per_slab, (1 << sinfo.cache_order));
626
627 seq_printf(m, " : tunables %4u %4u %4u",
628 sinfo.limit, sinfo.batchcount, sinfo.shared);
629 seq_printf(m, " : slabdata %6lu %6lu %6lu",
630 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
631 slabinfo_show_stats(m, s);
632 seq_putc(m, '\n');
633 return 0;
634 }
635
636 static int s_show(struct seq_file *m, void *p)
637 {
638 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
639
640 if (!is_root_cache(s))
641 return 0;
642 return cache_show(s, m);
643 }
644
645 /*
646 * slabinfo_op - iterator that generates /proc/slabinfo
647 *
648 * Output layout:
649 * cache-name
650 * num-active-objs
651 * total-objs
652 * object size
653 * num-active-slabs
654 * total-slabs
655 * num-pages-per-slab
656 * + further values on SMP and with statistics enabled
657 */
658 static const struct seq_operations slabinfo_op = {
659 .start = s_start,
660 .next = s_next,
661 .stop = s_stop,
662 .show = s_show,
663 };
664
665 static int slabinfo_open(struct inode *inode, struct file *file)
666 {
667 return seq_open(file, &slabinfo_op);
668 }
669
670 static const struct file_operations proc_slabinfo_operations = {
671 .open = slabinfo_open,
672 .read = seq_read,
673 .write = slabinfo_write,
674 .llseek = seq_lseek,
675 .release = seq_release,
676 };
677
678 static int __init slab_proc_init(void)
679 {
680 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
681 return 0;
682 }
683 module_init(slab_proc_init);
684 #endif /* CONFIG_SLABINFO */