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 *),
140 struct kmem_cache *parent_cache)
142 struct kmem_cache *s = NULL;
146 mutex_lock(&slab_mutex);
148 if (!kmem_cache_sanity_check(memcg, name, size) == 0)
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
157 flags &= CACHE_CREATE_MASK;
159 s = __kmem_cache_alias(memcg, name, size, align, flags, ctor);
163 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
165 s->object_size = s->size = size;
169 if (memcg_register_cache(memcg, s, parent_cache)) {
170 kmem_cache_free(kmem_cache, s);
175 s->name = kstrdup(name, GFP_KERNEL);
177 kmem_cache_free(kmem_cache, s);
182 err = __kmem_cache_create(s, flags);
185 list_add(&s->list, &slab_caches);
186 memcg_cache_list_add(memcg, s);
189 kmem_cache_free(kmem_cache, s);
195 mutex_unlock(&slab_mutex);
200 if (flags & SLAB_PANIC)
201 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
204 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
216 kmem_cache_create(const char *name, size_t size, size_t align,
217 unsigned long flags, void (*ctor)(void *))
219 return kmem_cache_create_memcg(NULL, name, size, align, flags, ctor, NULL);
221 EXPORT_SYMBOL(kmem_cache_create);
223 void kmem_cache_destroy(struct kmem_cache *s)
225 /* Destroy all the children caches if we aren't a memcg cache */
226 kmem_cache_destroy_memcg_children(s);
229 mutex_lock(&slab_mutex);
234 if (!__kmem_cache_shutdown(s)) {
235 mutex_unlock(&slab_mutex);
236 if (s->flags & SLAB_DESTROY_BY_RCU)
239 memcg_release_cache(s);
241 kmem_cache_free(kmem_cache, s);
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",
250 mutex_unlock(&slab_mutex);
254 EXPORT_SYMBOL(kmem_cache_destroy);
256 int slab_is_available(void)
258 return slab_state >= UP;
261 #ifdef CONFIG_SLABINFO
262 void print_slabinfo_header(struct seq_file *m)
265 * Output format version, so at least we can change it
266 * without _too_ many complaints.
268 #ifdef CONFIG_DEBUG_SLAB
269 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
271 seq_puts(m, "slabinfo - version: 2.1\n");
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>");
285 static void *s_start(struct seq_file *m, loff_t *pos)
289 mutex_lock(&slab_mutex);
291 print_slabinfo_header(m);
293 return seq_list_start(&slab_caches, *pos);
296 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
298 return seq_list_next(p, &slab_caches, pos);
301 static void s_stop(struct seq_file *m, void *p)
303 mutex_unlock(&slab_mutex);
307 memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info)
309 struct kmem_cache *c;
310 struct slabinfo sinfo;
313 if (!is_root_cache(s))
316 for_each_memcg_cache_index(i) {
317 c = cache_from_memcg(s, i);
321 memset(&sinfo, 0, sizeof(sinfo));
322 get_slabinfo(c, &sinfo);
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;
332 int cache_show(struct kmem_cache *s, struct seq_file *m)
334 struct slabinfo sinfo;
336 memset(&sinfo, 0, sizeof(sinfo));
337 get_slabinfo(s, &sinfo);
339 memcg_accumulate_slabinfo(s, &sinfo);
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));
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);
354 static int s_show(struct seq_file *m, void *p)
356 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
358 if (!is_root_cache(s))
360 return cache_show(s, m);
364 * slabinfo_op - iterator that generates /proc/slabinfo
374 * + further values on SMP and with statistics enabled
376 static const struct seq_operations slabinfo_op = {
383 static int slabinfo_open(struct inode *inode, struct file *file)
385 return seq_open(file, &slabinfo_op);
388 static const struct file_operations proc_slabinfo_operations = {
389 .open = slabinfo_open,
391 .write = slabinfo_write,
393 .release = seq_release,
396 static int __init slab_proc_init(void)
398 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
401 module_init(slab_proc_init);
402 #endif /* CONFIG_SLABINFO */