Commit | Line | Data |
---|---|---|
039363f3 CL |
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> | |
20cea968 CL |
14 | #include <linux/cpu.h> |
15 | #include <linux/uaccess.h> | |
039363f3 CL |
16 | #include <asm/cacheflush.h> |
17 | #include <asm/tlbflush.h> | |
18 | #include <asm/page.h> | |
19 | ||
97d06609 CL |
20 | #include "slab.h" |
21 | ||
22 | enum slab_state slab_state; | |
18004c5d CL |
23 | LIST_HEAD(slab_caches); |
24 | DEFINE_MUTEX(slab_mutex); | |
97d06609 | 25 | |
039363f3 CL |
26 | /* |
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 | ||
51 | struct 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 | ||
455ce9eb AM |
56 | get_online_cpus(); |
57 | mutex_lock(&slab_mutex); | |
58 | ||
039363f3 CL |
59 | #ifdef CONFIG_DEBUG_VM |
60 | if (!name || in_interrupt() || size < sizeof(void *) || | |
61 | size > KMALLOC_MAX_SIZE) { | |
62 | printk(KERN_ERR "kmem_cache_create(%s) integrity check" | |
455ce9eb AM |
63 | " failed\n", name); |
64 | goto oops; | |
039363f3 | 65 | } |
20cea968 | 66 | |
20cea968 CL |
67 | 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 | ||
039363f3 CL |
97 | s = __kmem_cache_create(name, size, align, flags, ctor); |
98 | ||
73a1180e | 99 | #ifdef CONFIG_DEBUG_VM |
20cea968 | 100 | oops: |
73a1180e | 101 | #endif |
20cea968 CL |
102 | mutex_unlock(&slab_mutex); |
103 | put_online_cpus(); | |
104 | ||
039363f3 CL |
105 | if (!s && (flags & SLAB_PANIC)) |
106 | panic("kmem_cache_create: Failed to create slab '%s'\n", name); | |
107 | ||
108 | return s; | |
109 | } | |
110 | EXPORT_SYMBOL(kmem_cache_create); | |
97d06609 CL |
111 | |
112 | int slab_is_available(void) | |
113 | { | |
114 | return slab_state >= UP; | |
115 | } |