blob: 281600b30100bf21d6a43d813f5d53995a27019d [file] [log] [blame]
Christoph Lameter039363f2012-07-06 15:25:10 -05001/*
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>
Christoph Lameter20cea962012-07-06 15:25:13 -050014#include <linux/cpu.h>
15#include <linux/uaccess.h>
Christoph Lameter039363f2012-07-06 15:25:10 -050016#include <asm/cacheflush.h>
17#include <asm/tlbflush.h>
18#include <asm/page.h>
19
Christoph Lameter97d06602012-07-06 15:25:11 -050020#include "slab.h"
21
22enum slab_state slab_state;
Christoph Lameter18004c52012-07-06 15:25:12 -050023LIST_HEAD(slab_caches);
24DEFINE_MUTEX(slab_mutex);
Christoph Lameter97d06602012-07-06 15:25:11 -050025
Christoph Lameter039363f2012-07-06 15:25:10 -050026/*
27 * kmem_cache_create - Create a cache.
28 * @name: A string which is used in /proc/slabinfo to identify this cache.
29 * @size: The size of objects to be created in this cache.
30 * @align: The required alignment for the objects.
31 * @flags: SLAB flags
32 * @ctor: A constructor for the objects.
33 *
34 * Returns a ptr to the cache on success, NULL on failure.
35 * Cannot be called within a interrupt, but can be interrupted.
36 * The @ctor is run when new pages are allocated by the cache.
37 *
38 * The flags are
39 *
40 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
41 * to catch references to uninitialised memory.
42 *
43 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
44 * for buffer overruns.
45 *
46 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
47 * cacheline. This can be beneficial if you're counting cycles as closely
48 * as davem.
49 */
50
51struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
52 unsigned long flags, void (*ctor)(void *))
53{
54 struct kmem_cache *s = NULL;
55
Christoph Lameter20cea962012-07-06 15:25:13 -050056 get_online_cpus();
57 mutex_lock(&slab_mutex);
58
59#ifdef CONFIG_DEBUG_VM
Andrew Morton455ce9e2012-08-14 14:53:22 -070060 if (!name || in_interrupt() || size < sizeof(void *) ||
61 size > KMALLOC_MAX_SIZE) {
62 printk(KERN_ERR "kmem_cache_create(%s) integrity check"
63 " failed\n", name);
64 goto oops;
65 }
66
Christoph Lameter20cea962012-07-06 15:25:13 -050067 list_for_each_entry(s, &slab_caches, list) {
68 char tmp;
69 int res;
70
71 /*
72 * This happens when the module gets unloaded and doesn't
73 * destroy its slab cache and no-one else reuses the vmalloc
74 * area of the module. Print a warning.
75 */
76 res = probe_kernel_address(s->name, tmp);
77 if (res) {
78 printk(KERN_ERR
79 "Slab cache with size %d has lost its name\n",
80 s->object_size);
81 continue;
82 }
83
84 if (!strcmp(s->name, name)) {
85 printk(KERN_ERR "kmem_cache_create(%s): Cache name"
86 " already exists.\n",
87 name);
88 dump_stack();
89 s = NULL;
90 goto oops;
91 }
92 }
93
94 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
95#endif
96
Christoph Lameter039363f2012-07-06 15:25:10 -050097 s = __kmem_cache_create(name, size, align, flags, ctor);
98
Shuah Khan73a11802012-07-13 17:12:05 -060099#ifdef CONFIG_DEBUG_VM
Christoph Lameter20cea962012-07-06 15:25:13 -0500100oops:
Shuah Khan73a11802012-07-13 17:12:05 -0600101#endif
Christoph Lameter20cea962012-07-06 15:25:13 -0500102 mutex_unlock(&slab_mutex);
103 put_online_cpus();
104
Christoph Lameter039363f2012-07-06 15:25:10 -0500105 if (!s && (flags & SLAB_PANIC))
106 panic("kmem_cache_create: Failed to create slab '%s'\n", name);
107
108 return s;
109}
110EXPORT_SYMBOL(kmem_cache_create);
Christoph Lameter97d06602012-07-06 15:25:11 -0500111
112int slab_is_available(void)
113{
114 return slab_state >= UP;
115}