mm/slab_common: commonize slab merge logic
[deliverable/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 #define CREATE_TRACE_POINTS
24 #include <trace/events/kmem.h>
25
26 #include "slab.h"
27
28 enum slab_state slab_state;
29 LIST_HEAD(slab_caches);
30 DEFINE_MUTEX(slab_mutex);
31 struct kmem_cache *kmem_cache;
32
33 /*
34 * Set of flags that will prevent slab merging
35 */
36 #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
37 SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
38 SLAB_FAILSLAB)
39
40 #define SLAB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
41 SLAB_CACHE_DMA | SLAB_NOTRACK)
42
43 /*
44 * Merge control. If this is set then no merging of slab caches will occur.
45 * (Could be removed. This was introduced to pacify the merge skeptics.)
46 */
47 static int slab_nomerge;
48
49 static int __init setup_slab_nomerge(char *str)
50 {
51 slab_nomerge = 1;
52 return 1;
53 }
54
55 #ifdef CONFIG_SLUB
56 __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
57 #endif
58
59 __setup("slab_nomerge", setup_slab_nomerge);
60
61 /*
62 * Determine the size of a slab object
63 */
64 unsigned int kmem_cache_size(struct kmem_cache *s)
65 {
66 return s->object_size;
67 }
68 EXPORT_SYMBOL(kmem_cache_size);
69
70 #ifdef CONFIG_DEBUG_VM
71 static int kmem_cache_sanity_check(const char *name, size_t size)
72 {
73 struct kmem_cache *s = NULL;
74
75 if (!name || in_interrupt() || size < sizeof(void *) ||
76 size > KMALLOC_MAX_SIZE) {
77 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
78 return -EINVAL;
79 }
80
81 list_for_each_entry(s, &slab_caches, list) {
82 char tmp;
83 int res;
84
85 /*
86 * This happens when the module gets unloaded and doesn't
87 * destroy its slab cache and no-one else reuses the vmalloc
88 * area of the module. Print a warning.
89 */
90 res = probe_kernel_address(s->name, tmp);
91 if (res) {
92 pr_err("Slab cache with size %d has lost its name\n",
93 s->object_size);
94 continue;
95 }
96
97 #if !defined(CONFIG_SLUB)
98 if (!strcmp(s->name, name)) {
99 pr_err("%s (%s): Cache name already exists.\n",
100 __func__, name);
101 dump_stack();
102 s = NULL;
103 return -EINVAL;
104 }
105 #endif
106 }
107
108 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
109 return 0;
110 }
111 #else
112 static inline int kmem_cache_sanity_check(const char *name, size_t size)
113 {
114 return 0;
115 }
116 #endif
117
118 #ifdef CONFIG_MEMCG_KMEM
119 int memcg_update_all_caches(int num_memcgs)
120 {
121 struct kmem_cache *s;
122 int ret = 0;
123 mutex_lock(&slab_mutex);
124
125 list_for_each_entry(s, &slab_caches, list) {
126 if (!is_root_cache(s))
127 continue;
128
129 ret = memcg_update_cache_size(s, num_memcgs);
130 /*
131 * See comment in memcontrol.c, memcg_update_cache_size:
132 * Instead of freeing the memory, we'll just leave the caches
133 * up to this point in an updated state.
134 */
135 if (ret)
136 goto out;
137 }
138
139 memcg_update_array_size(num_memcgs);
140 out:
141 mutex_unlock(&slab_mutex);
142 return ret;
143 }
144 #endif
145
146 /*
147 * Find a mergeable slab cache
148 */
149 int slab_unmergeable(struct kmem_cache *s)
150 {
151 if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
152 return 1;
153
154 if (!is_root_cache(s))
155 return 1;
156
157 if (s->ctor)
158 return 1;
159
160 /*
161 * We may have set a slab to be unmergeable during bootstrap.
162 */
163 if (s->refcount < 0)
164 return 1;
165
166 return 0;
167 }
168
169 struct kmem_cache *find_mergeable(size_t size, size_t align,
170 unsigned long flags, const char *name, void (*ctor)(void *))
171 {
172 struct kmem_cache *s;
173
174 if (slab_nomerge || (flags & SLAB_NEVER_MERGE))
175 return NULL;
176
177 if (ctor)
178 return NULL;
179
180 size = ALIGN(size, sizeof(void *));
181 align = calculate_alignment(flags, align, size);
182 size = ALIGN(size, align);
183 flags = kmem_cache_flags(size, flags, name, NULL);
184
185 list_for_each_entry(s, &slab_caches, list) {
186 if (slab_unmergeable(s))
187 continue;
188
189 if (size > s->size)
190 continue;
191
192 if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
193 continue;
194 /*
195 * Check if alignment is compatible.
196 * Courtesy of Adrian Drzewiecki
197 */
198 if ((s->size & ~(align - 1)) != s->size)
199 continue;
200
201 if (s->size - size >= sizeof(void *))
202 continue;
203
204 return s;
205 }
206 return NULL;
207 }
208
209 /*
210 * Figure out what the alignment of the objects will be given a set of
211 * flags, a user specified alignment and the size of the objects.
212 */
213 unsigned long calculate_alignment(unsigned long flags,
214 unsigned long align, unsigned long size)
215 {
216 /*
217 * If the user wants hardware cache aligned objects then follow that
218 * suggestion if the object is sufficiently large.
219 *
220 * The hardware cache alignment cannot override the specified
221 * alignment though. If that is greater then use it.
222 */
223 if (flags & SLAB_HWCACHE_ALIGN) {
224 unsigned long ralign = cache_line_size();
225 while (size <= ralign / 2)
226 ralign /= 2;
227 align = max(align, ralign);
228 }
229
230 if (align < ARCH_SLAB_MINALIGN)
231 align = ARCH_SLAB_MINALIGN;
232
233 return ALIGN(align, sizeof(void *));
234 }
235
236 static struct kmem_cache *
237 do_kmem_cache_create(char *name, size_t object_size, size_t size, size_t align,
238 unsigned long flags, void (*ctor)(void *),
239 struct mem_cgroup *memcg, struct kmem_cache *root_cache)
240 {
241 struct kmem_cache *s;
242 int err;
243
244 err = -ENOMEM;
245 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
246 if (!s)
247 goto out;
248
249 s->name = name;
250 s->object_size = object_size;
251 s->size = size;
252 s->align = align;
253 s->ctor = ctor;
254
255 err = memcg_alloc_cache_params(memcg, s, root_cache);
256 if (err)
257 goto out_free_cache;
258
259 err = __kmem_cache_create(s, flags);
260 if (err)
261 goto out_free_cache;
262
263 s->refcount = 1;
264 list_add(&s->list, &slab_caches);
265 out:
266 if (err)
267 return ERR_PTR(err);
268 return s;
269
270 out_free_cache:
271 memcg_free_cache_params(s);
272 kfree(s);
273 goto out;
274 }
275
276 /*
277 * kmem_cache_create - Create a cache.
278 * @name: A string which is used in /proc/slabinfo to identify this cache.
279 * @size: The size of objects to be created in this cache.
280 * @align: The required alignment for the objects.
281 * @flags: SLAB flags
282 * @ctor: A constructor for the objects.
283 *
284 * Returns a ptr to the cache on success, NULL on failure.
285 * Cannot be called within a interrupt, but can be interrupted.
286 * The @ctor is run when new pages are allocated by the cache.
287 *
288 * The flags are
289 *
290 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
291 * to catch references to uninitialised memory.
292 *
293 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
294 * for buffer overruns.
295 *
296 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
297 * cacheline. This can be beneficial if you're counting cycles as closely
298 * as davem.
299 */
300 struct kmem_cache *
301 kmem_cache_create(const char *name, size_t size, size_t align,
302 unsigned long flags, void (*ctor)(void *))
303 {
304 struct kmem_cache *s;
305 char *cache_name;
306 int err;
307
308 get_online_cpus();
309 get_online_mems();
310
311 mutex_lock(&slab_mutex);
312
313 err = kmem_cache_sanity_check(name, size);
314 if (err) {
315 s = NULL; /* suppress uninit var warning */
316 goto out_unlock;
317 }
318
319 /*
320 * Some allocators will constraint the set of valid flags to a subset
321 * of all flags. We expect them to define CACHE_CREATE_MASK in this
322 * case, and we'll just provide them with a sanitized version of the
323 * passed flags.
324 */
325 flags &= CACHE_CREATE_MASK;
326
327 s = __kmem_cache_alias(name, size, align, flags, ctor);
328 if (s)
329 goto out_unlock;
330
331 cache_name = kstrdup(name, GFP_KERNEL);
332 if (!cache_name) {
333 err = -ENOMEM;
334 goto out_unlock;
335 }
336
337 s = do_kmem_cache_create(cache_name, size, size,
338 calculate_alignment(flags, align, size),
339 flags, ctor, NULL, NULL);
340 if (IS_ERR(s)) {
341 err = PTR_ERR(s);
342 kfree(cache_name);
343 }
344
345 out_unlock:
346 mutex_unlock(&slab_mutex);
347
348 put_online_mems();
349 put_online_cpus();
350
351 if (err) {
352 if (flags & SLAB_PANIC)
353 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
354 name, err);
355 else {
356 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
357 name, err);
358 dump_stack();
359 }
360 return NULL;
361 }
362 return s;
363 }
364 EXPORT_SYMBOL(kmem_cache_create);
365
366 #ifdef CONFIG_MEMCG_KMEM
367 /*
368 * memcg_create_kmem_cache - Create a cache for a memory cgroup.
369 * @memcg: The memory cgroup the new cache is for.
370 * @root_cache: The parent of the new cache.
371 * @memcg_name: The name of the memory cgroup (used for naming the new cache).
372 *
373 * This function attempts to create a kmem cache that will serve allocation
374 * requests going from @memcg to @root_cache. The new cache inherits properties
375 * from its parent.
376 */
377 struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
378 struct kmem_cache *root_cache,
379 const char *memcg_name)
380 {
381 struct kmem_cache *s = NULL;
382 char *cache_name;
383
384 get_online_cpus();
385 get_online_mems();
386
387 mutex_lock(&slab_mutex);
388
389 cache_name = kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
390 memcg_cache_id(memcg), memcg_name);
391 if (!cache_name)
392 goto out_unlock;
393
394 s = do_kmem_cache_create(cache_name, root_cache->object_size,
395 root_cache->size, root_cache->align,
396 root_cache->flags, root_cache->ctor,
397 memcg, root_cache);
398 if (IS_ERR(s)) {
399 kfree(cache_name);
400 s = NULL;
401 }
402
403 out_unlock:
404 mutex_unlock(&slab_mutex);
405
406 put_online_mems();
407 put_online_cpus();
408
409 return s;
410 }
411
412 static int memcg_cleanup_cache_params(struct kmem_cache *s)
413 {
414 int rc;
415
416 if (!s->memcg_params ||
417 !s->memcg_params->is_root_cache)
418 return 0;
419
420 mutex_unlock(&slab_mutex);
421 rc = __memcg_cleanup_cache_params(s);
422 mutex_lock(&slab_mutex);
423
424 return rc;
425 }
426 #else
427 static int memcg_cleanup_cache_params(struct kmem_cache *s)
428 {
429 return 0;
430 }
431 #endif /* CONFIG_MEMCG_KMEM */
432
433 void slab_kmem_cache_release(struct kmem_cache *s)
434 {
435 kfree(s->name);
436 kmem_cache_free(kmem_cache, s);
437 }
438
439 void kmem_cache_destroy(struct kmem_cache *s)
440 {
441 get_online_cpus();
442 get_online_mems();
443
444 mutex_lock(&slab_mutex);
445
446 s->refcount--;
447 if (s->refcount)
448 goto out_unlock;
449
450 if (memcg_cleanup_cache_params(s) != 0)
451 goto out_unlock;
452
453 if (__kmem_cache_shutdown(s) != 0) {
454 printk(KERN_ERR "kmem_cache_destroy %s: "
455 "Slab cache still has objects\n", s->name);
456 dump_stack();
457 goto out_unlock;
458 }
459
460 list_del(&s->list);
461
462 mutex_unlock(&slab_mutex);
463 if (s->flags & SLAB_DESTROY_BY_RCU)
464 rcu_barrier();
465
466 memcg_free_cache_params(s);
467 #ifdef SLAB_SUPPORTS_SYSFS
468 sysfs_slab_remove(s);
469 #else
470 slab_kmem_cache_release(s);
471 #endif
472 goto out;
473
474 out_unlock:
475 mutex_unlock(&slab_mutex);
476 out:
477 put_online_mems();
478 put_online_cpus();
479 }
480 EXPORT_SYMBOL(kmem_cache_destroy);
481
482 /**
483 * kmem_cache_shrink - Shrink a cache.
484 * @cachep: The cache to shrink.
485 *
486 * Releases as many slabs as possible for a cache.
487 * To help debugging, a zero exit status indicates all slabs were released.
488 */
489 int kmem_cache_shrink(struct kmem_cache *cachep)
490 {
491 int ret;
492
493 get_online_cpus();
494 get_online_mems();
495 ret = __kmem_cache_shrink(cachep);
496 put_online_mems();
497 put_online_cpus();
498 return ret;
499 }
500 EXPORT_SYMBOL(kmem_cache_shrink);
501
502 int slab_is_available(void)
503 {
504 return slab_state >= UP;
505 }
506
507 #ifndef CONFIG_SLOB
508 /* Create a cache during boot when no slab services are available yet */
509 void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
510 unsigned long flags)
511 {
512 int err;
513
514 s->name = name;
515 s->size = s->object_size = size;
516 s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size);
517 err = __kmem_cache_create(s, flags);
518
519 if (err)
520 panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n",
521 name, size, err);
522
523 s->refcount = -1; /* Exempt from merging for now */
524 }
525
526 struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
527 unsigned long flags)
528 {
529 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
530
531 if (!s)
532 panic("Out of memory when creating slab %s\n", name);
533
534 create_boot_cache(s, name, size, flags);
535 list_add(&s->list, &slab_caches);
536 s->refcount = 1;
537 return s;
538 }
539
540 struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
541 EXPORT_SYMBOL(kmalloc_caches);
542
543 #ifdef CONFIG_ZONE_DMA
544 struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1];
545 EXPORT_SYMBOL(kmalloc_dma_caches);
546 #endif
547
548 /*
549 * Conversion table for small slabs sizes / 8 to the index in the
550 * kmalloc array. This is necessary for slabs < 192 since we have non power
551 * of two cache sizes there. The size of larger slabs can be determined using
552 * fls.
553 */
554 static s8 size_index[24] = {
555 3, /* 8 */
556 4, /* 16 */
557 5, /* 24 */
558 5, /* 32 */
559 6, /* 40 */
560 6, /* 48 */
561 6, /* 56 */
562 6, /* 64 */
563 1, /* 72 */
564 1, /* 80 */
565 1, /* 88 */
566 1, /* 96 */
567 7, /* 104 */
568 7, /* 112 */
569 7, /* 120 */
570 7, /* 128 */
571 2, /* 136 */
572 2, /* 144 */
573 2, /* 152 */
574 2, /* 160 */
575 2, /* 168 */
576 2, /* 176 */
577 2, /* 184 */
578 2 /* 192 */
579 };
580
581 static inline int size_index_elem(size_t bytes)
582 {
583 return (bytes - 1) / 8;
584 }
585
586 /*
587 * Find the kmem_cache structure that serves a given size of
588 * allocation
589 */
590 struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
591 {
592 int index;
593
594 if (unlikely(size > KMALLOC_MAX_SIZE)) {
595 WARN_ON_ONCE(!(flags & __GFP_NOWARN));
596 return NULL;
597 }
598
599 if (size <= 192) {
600 if (!size)
601 return ZERO_SIZE_PTR;
602
603 index = size_index[size_index_elem(size)];
604 } else
605 index = fls(size - 1);
606
607 #ifdef CONFIG_ZONE_DMA
608 if (unlikely((flags & GFP_DMA)))
609 return kmalloc_dma_caches[index];
610
611 #endif
612 return kmalloc_caches[index];
613 }
614
615 /*
616 * Create the kmalloc array. Some of the regular kmalloc arrays
617 * may already have been created because they were needed to
618 * enable allocations for slab creation.
619 */
620 void __init create_kmalloc_caches(unsigned long flags)
621 {
622 int i;
623
624 /*
625 * Patch up the size_index table if we have strange large alignment
626 * requirements for the kmalloc array. This is only the case for
627 * MIPS it seems. The standard arches will not generate any code here.
628 *
629 * Largest permitted alignment is 256 bytes due to the way we
630 * handle the index determination for the smaller caches.
631 *
632 * Make sure that nothing crazy happens if someone starts tinkering
633 * around with ARCH_KMALLOC_MINALIGN
634 */
635 BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
636 (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
637
638 for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
639 int elem = size_index_elem(i);
640
641 if (elem >= ARRAY_SIZE(size_index))
642 break;
643 size_index[elem] = KMALLOC_SHIFT_LOW;
644 }
645
646 if (KMALLOC_MIN_SIZE >= 64) {
647 /*
648 * The 96 byte size cache is not used if the alignment
649 * is 64 byte.
650 */
651 for (i = 64 + 8; i <= 96; i += 8)
652 size_index[size_index_elem(i)] = 7;
653
654 }
655
656 if (KMALLOC_MIN_SIZE >= 128) {
657 /*
658 * The 192 byte sized cache is not used if the alignment
659 * is 128 byte. Redirect kmalloc to use the 256 byte cache
660 * instead.
661 */
662 for (i = 128 + 8; i <= 192; i += 8)
663 size_index[size_index_elem(i)] = 8;
664 }
665 for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
666 if (!kmalloc_caches[i]) {
667 kmalloc_caches[i] = create_kmalloc_cache(NULL,
668 1 << i, flags);
669 }
670
671 /*
672 * Caches that are not of the two-to-the-power-of size.
673 * These have to be created immediately after the
674 * earlier power of two caches
675 */
676 if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
677 kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
678
679 if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
680 kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
681 }
682
683 /* Kmalloc array is now usable */
684 slab_state = UP;
685
686 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
687 struct kmem_cache *s = kmalloc_caches[i];
688 char *n;
689
690 if (s) {
691 n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
692
693 BUG_ON(!n);
694 s->name = n;
695 }
696 }
697
698 #ifdef CONFIG_ZONE_DMA
699 for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
700 struct kmem_cache *s = kmalloc_caches[i];
701
702 if (s) {
703 int size = kmalloc_size(i);
704 char *n = kasprintf(GFP_NOWAIT,
705 "dma-kmalloc-%d", size);
706
707 BUG_ON(!n);
708 kmalloc_dma_caches[i] = create_kmalloc_cache(n,
709 size, SLAB_CACHE_DMA | flags);
710 }
711 }
712 #endif
713 }
714 #endif /* !CONFIG_SLOB */
715
716 /*
717 * To avoid unnecessary overhead, we pass through large allocation requests
718 * directly to the page allocator. We use __GFP_COMP, because we will need to
719 * know the allocation order to free the pages properly in kfree.
720 */
721 void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
722 {
723 void *ret;
724 struct page *page;
725
726 flags |= __GFP_COMP;
727 page = alloc_kmem_pages(flags, order);
728 ret = page ? page_address(page) : NULL;
729 kmemleak_alloc(ret, size, 1, flags);
730 return ret;
731 }
732 EXPORT_SYMBOL(kmalloc_order);
733
734 #ifdef CONFIG_TRACING
735 void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
736 {
737 void *ret = kmalloc_order(size, flags, order);
738 trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
739 return ret;
740 }
741 EXPORT_SYMBOL(kmalloc_order_trace);
742 #endif
743
744 #ifdef CONFIG_SLABINFO
745
746 #ifdef CONFIG_SLAB
747 #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR)
748 #else
749 #define SLABINFO_RIGHTS S_IRUSR
750 #endif
751
752 void print_slabinfo_header(struct seq_file *m)
753 {
754 /*
755 * Output format version, so at least we can change it
756 * without _too_ many complaints.
757 */
758 #ifdef CONFIG_DEBUG_SLAB
759 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
760 #else
761 seq_puts(m, "slabinfo - version: 2.1\n");
762 #endif
763 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
764 "<objperslab> <pagesperslab>");
765 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
766 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
767 #ifdef CONFIG_DEBUG_SLAB
768 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
769 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
770 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
771 #endif
772 seq_putc(m, '\n');
773 }
774
775 static void *s_start(struct seq_file *m, loff_t *pos)
776 {
777 loff_t n = *pos;
778
779 mutex_lock(&slab_mutex);
780 if (!n)
781 print_slabinfo_header(m);
782
783 return seq_list_start(&slab_caches, *pos);
784 }
785
786 void *slab_next(struct seq_file *m, void *p, loff_t *pos)
787 {
788 return seq_list_next(p, &slab_caches, pos);
789 }
790
791 void slab_stop(struct seq_file *m, void *p)
792 {
793 mutex_unlock(&slab_mutex);
794 }
795
796 static void
797 memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
798 {
799 struct kmem_cache *c;
800 struct slabinfo sinfo;
801 int i;
802
803 if (!is_root_cache(s))
804 return;
805
806 for_each_memcg_cache_index(i) {
807 c = cache_from_memcg_idx(s, i);
808 if (!c)
809 continue;
810
811 memset(&sinfo, 0, sizeof(sinfo));
812 get_slabinfo(c, &sinfo);
813
814 info->active_slabs += sinfo.active_slabs;
815 info->num_slabs += sinfo.num_slabs;
816 info->shared_avail += sinfo.shared_avail;
817 info->active_objs += sinfo.active_objs;
818 info->num_objs += sinfo.num_objs;
819 }
820 }
821
822 int cache_show(struct kmem_cache *s, struct seq_file *m)
823 {
824 struct slabinfo sinfo;
825
826 memset(&sinfo, 0, sizeof(sinfo));
827 get_slabinfo(s, &sinfo);
828
829 memcg_accumulate_slabinfo(s, &sinfo);
830
831 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
832 cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
833 sinfo.objects_per_slab, (1 << sinfo.cache_order));
834
835 seq_printf(m, " : tunables %4u %4u %4u",
836 sinfo.limit, sinfo.batchcount, sinfo.shared);
837 seq_printf(m, " : slabdata %6lu %6lu %6lu",
838 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
839 slabinfo_show_stats(m, s);
840 seq_putc(m, '\n');
841 return 0;
842 }
843
844 static int s_show(struct seq_file *m, void *p)
845 {
846 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
847
848 if (!is_root_cache(s))
849 return 0;
850 return cache_show(s, m);
851 }
852
853 /*
854 * slabinfo_op - iterator that generates /proc/slabinfo
855 *
856 * Output layout:
857 * cache-name
858 * num-active-objs
859 * total-objs
860 * object size
861 * num-active-slabs
862 * total-slabs
863 * num-pages-per-slab
864 * + further values on SMP and with statistics enabled
865 */
866 static const struct seq_operations slabinfo_op = {
867 .start = s_start,
868 .next = slab_next,
869 .stop = slab_stop,
870 .show = s_show,
871 };
872
873 static int slabinfo_open(struct inode *inode, struct file *file)
874 {
875 return seq_open(file, &slabinfo_op);
876 }
877
878 static const struct file_operations proc_slabinfo_operations = {
879 .open = slabinfo_open,
880 .read = seq_read,
881 .write = slabinfo_write,
882 .llseek = seq_lseek,
883 .release = seq_release,
884 };
885
886 static int __init slab_proc_init(void)
887 {
888 proc_create("slabinfo", SLABINFO_RIGHTS, NULL,
889 &proc_slabinfo_operations);
890 return 0;
891 }
892 module_init(slab_proc_init);
893 #endif /* CONFIG_SLABINFO */
894
895 static __always_inline void *__do_krealloc(const void *p, size_t new_size,
896 gfp_t flags)
897 {
898 void *ret;
899 size_t ks = 0;
900
901 if (p)
902 ks = ksize(p);
903
904 if (ks >= new_size)
905 return (void *)p;
906
907 ret = kmalloc_track_caller(new_size, flags);
908 if (ret && p)
909 memcpy(ret, p, ks);
910
911 return ret;
912 }
913
914 /**
915 * __krealloc - like krealloc() but don't free @p.
916 * @p: object to reallocate memory for.
917 * @new_size: how many bytes of memory are required.
918 * @flags: the type of memory to allocate.
919 *
920 * This function is like krealloc() except it never frees the originally
921 * allocated buffer. Use this if you don't want to free the buffer immediately
922 * like, for example, with RCU.
923 */
924 void *__krealloc(const void *p, size_t new_size, gfp_t flags)
925 {
926 if (unlikely(!new_size))
927 return ZERO_SIZE_PTR;
928
929 return __do_krealloc(p, new_size, flags);
930
931 }
932 EXPORT_SYMBOL(__krealloc);
933
934 /**
935 * krealloc - reallocate memory. The contents will remain unchanged.
936 * @p: object to reallocate memory for.
937 * @new_size: how many bytes of memory are required.
938 * @flags: the type of memory to allocate.
939 *
940 * The contents of the object pointed to are preserved up to the
941 * lesser of the new and old sizes. If @p is %NULL, krealloc()
942 * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
943 * %NULL pointer, the object pointed to is freed.
944 */
945 void *krealloc(const void *p, size_t new_size, gfp_t flags)
946 {
947 void *ret;
948
949 if (unlikely(!new_size)) {
950 kfree(p);
951 return ZERO_SIZE_PTR;
952 }
953
954 ret = __do_krealloc(p, new_size, flags);
955 if (ret && p != ret)
956 kfree(p);
957
958 return ret;
959 }
960 EXPORT_SYMBOL(krealloc);
961
962 /**
963 * kzfree - like kfree but zero memory
964 * @p: object to free memory of
965 *
966 * The memory of the object @p points to is zeroed before freed.
967 * If @p is %NULL, kzfree() does nothing.
968 *
969 * Note: this function zeroes the whole allocated buffer which can be a good
970 * deal bigger than the requested buffer size passed to kmalloc(). So be
971 * careful when using this function in performance sensitive code.
972 */
973 void kzfree(const void *p)
974 {
975 size_t ks;
976 void *mem = (void *)p;
977
978 if (unlikely(ZERO_OR_NULL_PTR(mem)))
979 return;
980 ks = ksize(mem);
981 memset(mem, 0, ks);
982 kfree(mem);
983 }
984 EXPORT_SYMBOL(kzfree);
985
986 /* Tracepoints definitions. */
987 EXPORT_TRACEPOINT_SYMBOL(kmalloc);
988 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
989 EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
990 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
991 EXPORT_TRACEPOINT_SYMBOL(kfree);
992 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
This page took 0.051055 seconds and 6 git commands to generate.