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>
24 enum slab_state slab_state;
25 LIST_HEAD(slab_caches);
26 DEFINE_MUTEX(slab_mutex);
27 struct kmem_cache *kmem_cache;
29 #ifdef CONFIG_DEBUG_VM
30 static int kmem_cache_sanity_check(const char *name, size_t size)
32 struct kmem_cache *s = NULL;
34 if (!name || in_interrupt() || size < sizeof(void *) ||
35 size > KMALLOC_MAX_SIZE) {
36 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
40 list_for_each_entry(s, &slab_caches, list) {
45 * This happens when the module gets unloaded and doesn't
46 * destroy its slab cache and no-one else reuses the vmalloc
47 * area of the module. Print a warning.
49 res = probe_kernel_address(s->name, tmp);
51 pr_err("Slab cache with size %d has lost its name\n",
56 if (!strcmp(s->name, name)) {
57 pr_err("%s (%s): Cache name already exists.\n",
65 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
69 static inline int kmem_cache_sanity_check(const char *name, size_t size)
76 * kmem_cache_create - Create a cache.
77 * @name: A string which is used in /proc/slabinfo to identify this cache.
78 * @size: The size of objects to be created in this cache.
79 * @align: The required alignment for the objects.
81 * @ctor: A constructor for the objects.
83 * Returns a ptr to the cache on success, NULL on failure.
84 * Cannot be called within a interrupt, but can be interrupted.
85 * The @ctor is run when new pages are allocated by the cache.
89 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
90 * to catch references to uninitialised memory.
92 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
93 * for buffer overruns.
95 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
96 * cacheline. This can be beneficial if you're counting cycles as closely
100 struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
101 unsigned long flags, void (*ctor)(void *))
103 struct kmem_cache *s = NULL;
107 mutex_lock(&slab_mutex);
109 if (!kmem_cache_sanity_check(name, size) == 0)
113 * Some allocators will constraint the set of valid flags to a subset
114 * of all flags. We expect them to define CACHE_CREATE_MASK in this
115 * case, and we'll just provide them with a sanitized version of the
118 flags &= CACHE_CREATE_MASK;
120 s = __kmem_cache_alias(name, size, align, flags, ctor);
124 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
126 s->object_size = s->size = size;
129 s->name = kstrdup(name, GFP_KERNEL);
131 kmem_cache_free(kmem_cache, s);
136 err = __kmem_cache_create(s, flags);
140 list_add(&s->list, &slab_caches);
144 kmem_cache_free(kmem_cache, s);
150 mutex_unlock(&slab_mutex);
155 if (flags & SLAB_PANIC)
156 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
159 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
169 EXPORT_SYMBOL(kmem_cache_create);
171 void kmem_cache_destroy(struct kmem_cache *s)
174 mutex_lock(&slab_mutex);
179 if (!__kmem_cache_shutdown(s)) {
180 mutex_unlock(&slab_mutex);
181 if (s->flags & SLAB_DESTROY_BY_RCU)
185 kmem_cache_free(kmem_cache, s);
187 list_add(&s->list, &slab_caches);
188 mutex_unlock(&slab_mutex);
189 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
194 mutex_unlock(&slab_mutex);
198 EXPORT_SYMBOL(kmem_cache_destroy);
200 int slab_is_available(void)
202 return slab_state >= UP;
206 /* Create a cache during boot when no slab services are available yet */
207 void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
213 s->size = s->object_size = size;
214 s->align = ARCH_KMALLOC_MINALIGN;
215 err = __kmem_cache_create(s, flags);
218 panic("Creation of kmalloc slab %s size=%zd failed. Reason %d\n",
221 s->refcount = -1; /* Exempt from merging for now */
224 struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
227 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
230 panic("Out of memory when creating slab %s\n", name);
232 create_boot_cache(s, name, size, flags);
233 list_add(&s->list, &slab_caches);
238 #endif /* !CONFIG_SLOB */
241 #ifdef CONFIG_SLABINFO
242 static void print_slabinfo_header(struct seq_file *m)
245 * Output format version, so at least we can change it
246 * without _too_ many complaints.
248 #ifdef CONFIG_DEBUG_SLAB
249 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
251 seq_puts(m, "slabinfo - version: 2.1\n");
253 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
254 "<objperslab> <pagesperslab>");
255 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
256 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
257 #ifdef CONFIG_DEBUG_SLAB
258 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
259 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
260 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
265 static void *s_start(struct seq_file *m, loff_t *pos)
269 mutex_lock(&slab_mutex);
271 print_slabinfo_header(m);
273 return seq_list_start(&slab_caches, *pos);
276 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
278 return seq_list_next(p, &slab_caches, pos);
281 static void s_stop(struct seq_file *m, void *p)
283 mutex_unlock(&slab_mutex);
286 static int s_show(struct seq_file *m, void *p)
288 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
289 struct slabinfo sinfo;
291 memset(&sinfo, 0, sizeof(sinfo));
292 get_slabinfo(s, &sinfo);
294 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
295 s->name, sinfo.active_objs, sinfo.num_objs, s->size,
296 sinfo.objects_per_slab, (1 << sinfo.cache_order));
298 seq_printf(m, " : tunables %4u %4u %4u",
299 sinfo.limit, sinfo.batchcount, sinfo.shared);
300 seq_printf(m, " : slabdata %6lu %6lu %6lu",
301 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
302 slabinfo_show_stats(m, s);
308 * slabinfo_op - iterator that generates /proc/slabinfo
318 * + further values on SMP and with statistics enabled
320 static const struct seq_operations slabinfo_op = {
327 static int slabinfo_open(struct inode *inode, struct file *file)
329 return seq_open(file, &slabinfo_op);
332 static const struct file_operations proc_slabinfo_operations = {
333 .open = slabinfo_open,
335 .write = slabinfo_write,
337 .release = seq_release,
340 static int __init slab_proc_init(void)
342 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
345 module_init(slab_proc_init);
346 #endif /* CONFIG_SLABINFO */