2 * Slab allocator functions that are independent of the allocator strategy
4 * (C) 2012 Christoph Lameter <cl@linux.com>
6 #include <linux/slab.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>
21 #include <linux/memcontrol.h>
25 enum slab_state slab_state;
26 LIST_HEAD(slab_caches);
27 DEFINE_MUTEX(slab_mutex);
28 struct kmem_cache *kmem_cache;
30 #ifdef CONFIG_DEBUG_VM
31 static int kmem_cache_sanity_check(struct mem_cgroup *memcg, const char *name,
34 struct kmem_cache *s = NULL;
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);
42 list_for_each_entry(s, &slab_caches, list) {
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.
51 res = probe_kernel_address(s->name, tmp);
53 pr_err("Slab cache with size %d has lost its name\n",
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.
64 if (!memcg && !strcmp(s->name, name)) {
65 pr_err("%s (%s): Cache name already exists.\n",
73 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
77 static inline int kmem_cache_sanity_check(struct mem_cgroup *memcg,
78 const char *name, size_t size)
84 #ifdef CONFIG_MEMCG_KMEM
85 int memcg_update_all_caches(int num_memcgs)
89 mutex_lock(&slab_mutex);
91 list_for_each_entry(s, &slab_caches, list) {
92 if (!is_root_cache(s))
95 ret = memcg_update_cache_size(s, num_memcgs);
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.
105 memcg_update_array_size(num_memcgs);
107 mutex_unlock(&slab_mutex);
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.
118 * @ctor: A constructor for the objects.
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.
126 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
127 * to catch references to uninitialised memory.
129 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
130 * for buffer overruns.
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
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 *))
141 struct kmem_cache *s = NULL;
145 mutex_lock(&slab_mutex);
147 if (!kmem_cache_sanity_check(memcg, name, size) == 0)
151 * Some allocators will constraint the set of valid flags to a subset
152 * of all flags. We expect them to define CACHE_CREATE_MASK in this
153 * case, and we'll just provide them with a sanitized version of the
156 flags &= CACHE_CREATE_MASK;
158 s = __kmem_cache_alias(memcg, name, size, align, flags, ctor);
162 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
164 s->object_size = s->size = size;
168 if (memcg_register_cache(memcg, s)) {
169 kmem_cache_free(kmem_cache, s);
174 s->name = kstrdup(name, GFP_KERNEL);
176 kmem_cache_free(kmem_cache, s);
181 err = __kmem_cache_create(s, flags);
184 list_add(&s->list, &slab_caches);
185 memcg_cache_list_add(memcg, s);
188 kmem_cache_free(kmem_cache, s);
194 mutex_unlock(&slab_mutex);
199 if (flags & SLAB_PANIC)
200 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
203 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
215 kmem_cache_create(const char *name, size_t size, size_t align,
216 unsigned long flags, void (*ctor)(void *))
218 return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor);
220 EXPORT_SYMBOL(kmem_cache_create);
222 void kmem_cache_destroy(struct kmem_cache *s)
224 /* Destroy all the children caches if we aren't a memcg cache */
225 kmem_cache_destroy_memcg_children(s);
228 mutex_lock(&slab_mutex);
233 if (!__kmem_cache_shutdown(s)) {
234 mutex_unlock(&slab_mutex);
235 if (s->flags & SLAB_DESTROY_BY_RCU)
238 memcg_release_cache(s);
240 kmem_cache_free(kmem_cache, s);
242 list_add(&s->list, &slab_caches);
243 mutex_unlock(&slab_mutex);
244 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
249 mutex_unlock(&slab_mutex);
253 EXPORT_SYMBOL(kmem_cache_destroy);
255 int slab_is_available(void)
257 return slab_state >= UP;
260 #ifdef CONFIG_SLABINFO
261 void print_slabinfo_header(struct seq_file *m)
264 * Output format version, so at least we can change it
265 * without _too_ many complaints.
267 #ifdef CONFIG_DEBUG_SLAB
268 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
270 seq_puts(m, "slabinfo - version: 2.1\n");
272 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
273 "<objperslab> <pagesperslab>");
274 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
275 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
276 #ifdef CONFIG_DEBUG_SLAB
277 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
278 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
279 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
284 static void *s_start(struct seq_file *m, loff_t *pos)
288 mutex_lock(&slab_mutex);
290 print_slabinfo_header(m);
292 return seq_list_start(&slab_caches, *pos);
295 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
297 return seq_list_next(p, &slab_caches, pos);
300 static void s_stop(struct seq_file *m, void *p)
302 mutex_unlock(&slab_mutex);
306 memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
308 struct kmem_cache *c;
309 struct slabinfo sinfo;
312 if (!is_root_cache(s))
315 for_each_memcg_cache_index(i) {
316 c = cache_from_memcg(s, i);
320 memset(&sinfo, 0, sizeof(sinfo));
321 get_slabinfo(c, &sinfo);
323 info->active_slabs += sinfo.active_slabs;
324 info->num_slabs += sinfo.num_slabs;
325 info->shared_avail += sinfo.shared_avail;
326 info->active_objs += sinfo.active_objs;
327 info->num_objs += sinfo.num_objs;
331 int cache_show(struct kmem_cache *s, struct seq_file *m)
333 struct slabinfo sinfo;
335 memset(&sinfo, 0, sizeof(sinfo));
336 get_slabinfo(s, &sinfo);
338 memcg_accumulate_slabinfo(s, &sinfo);
340 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
341 cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size,
342 sinfo.objects_per_slab, (1 << sinfo.cache_order));
344 seq_printf(m, " : tunables %4u %4u %4u",
345 sinfo.limit, sinfo.batchcount, sinfo.shared);
346 seq_printf(m, " : slabdata %6lu %6lu %6lu",
347 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
348 slabinfo_show_stats(m, s);
353 static int s_show(struct seq_file *m, void *p)
355 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
357 if (!is_root_cache(s))
359 return cache_show(s, m);
363 * slabinfo_op - iterator that generates /proc/slabinfo
373 * + further values on SMP and with statistics enabled
375 static const struct seq_operations slabinfo_op = {
382 static int slabinfo_open(struct inode *inode, struct file *file)
384 return seq_open(file, &slabinfo_op);
387 static const struct file_operations proc_slabinfo_operations = {
388 .open = slabinfo_open,
390 .write = slabinfo_write,
392 .release = seq_release,
395 static int __init slab_proc_init(void)
397 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
400 module_init(slab_proc_init);
401 #endif /* CONFIG_SLABINFO */