]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/slab_common.c
mm/sl[aou]b: Move kmem_cache allocations into common code
[mirror_ubuntu-jammy-kernel.git] / mm / slab_common.c
CommitLineData
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
22enum slab_state slab_state;
18004c5d
CL
23LIST_HEAD(slab_caches);
24DEFINE_MUTEX(slab_mutex);
9b030cb8 25struct kmem_cache *kmem_cache;
97d06609 26
77be4b13
SK
27#ifdef CONFIG_DEBUG_VM
28static int kmem_cache_sanity_check(const char *name, size_t size)
039363f3
CL
29{
30 struct kmem_cache *s = NULL;
31
039363f3
CL
32 if (!name || in_interrupt() || size < sizeof(void *) ||
33 size > KMALLOC_MAX_SIZE) {
77be4b13
SK
34 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
35 return -EINVAL;
039363f3 36 }
b920536a 37
20cea968
CL
38 list_for_each_entry(s, &slab_caches, list) {
39 char tmp;
40 int res;
41
42 /*
43 * This happens when the module gets unloaded and doesn't
44 * destroy its slab cache and no-one else reuses the vmalloc
45 * area of the module. Print a warning.
46 */
47 res = probe_kernel_address(s->name, tmp);
48 if (res) {
77be4b13 49 pr_err("Slab cache with size %d has lost its name\n",
20cea968
CL
50 s->object_size);
51 continue;
52 }
53
54 if (!strcmp(s->name, name)) {
77be4b13
SK
55 pr_err("%s (%s): Cache name already exists.\n",
56 __func__, name);
20cea968
CL
57 dump_stack();
58 s = NULL;
77be4b13 59 return -EINVAL;
20cea968
CL
60 }
61 }
62
63 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
77be4b13
SK
64 return 0;
65}
66#else
67static inline int kmem_cache_sanity_check(const char *name, size_t size)
68{
69 return 0;
70}
20cea968
CL
71#endif
72
77be4b13
SK
73/*
74 * kmem_cache_create - Create a cache.
75 * @name: A string which is used in /proc/slabinfo to identify this cache.
76 * @size: The size of objects to be created in this cache.
77 * @align: The required alignment for the objects.
78 * @flags: SLAB flags
79 * @ctor: A constructor for the objects.
80 *
81 * Returns a ptr to the cache on success, NULL on failure.
82 * Cannot be called within a interrupt, but can be interrupted.
83 * The @ctor is run when new pages are allocated by the cache.
84 *
85 * The flags are
86 *
87 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
88 * to catch references to uninitialised memory.
89 *
90 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
91 * for buffer overruns.
92 *
93 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
94 * cacheline. This can be beneficial if you're counting cycles as closely
95 * as davem.
96 */
97
98struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
99 unsigned long flags, void (*ctor)(void *))
100{
101 struct kmem_cache *s = NULL;
686d550d 102 int err = 0;
db265eca 103 char *n;
039363f3 104
77be4b13
SK
105 get_online_cpus();
106 mutex_lock(&slab_mutex);
686d550d
CL
107
108 if (!kmem_cache_sanity_check(name, size) == 0)
109 goto out_locked;
110
111
db265eca
CL
112 n = kstrdup(name, GFP_KERNEL);
113 if (!n) {
114 err = -ENOMEM;
115 goto out_locked;
116 }
117
cbb79694
CL
118 s = __kmem_cache_alias(name, size, align, flags, ctor);
119 if (s)
120 goto out_locked;
121
278b1bb1 122 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
db265eca 123 if (s) {
278b1bb1
CL
124 err = __kmem_cache_create(s, n, size, align, flags, ctor);
125 if (!err)
126
db265eca 127 list_add(&s->list, &slab_caches);
686d550d 128
278b1bb1
CL
129 else {
130 kfree(n);
131 kmem_cache_free(kmem_cache, s);
132 }
133
db265eca
CL
134 } else {
135 kfree(n);
278b1bb1 136 err = -ENOMEM;
db265eca 137 }
7c9adf5a 138
686d550d 139out_locked:
20cea968
CL
140 mutex_unlock(&slab_mutex);
141 put_online_cpus();
142
686d550d
CL
143 if (err) {
144
145 if (flags & SLAB_PANIC)
146 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
147 name, err);
148 else {
149 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
150 name, err);
151 dump_stack();
152 }
153
154 return NULL;
155 }
039363f3 156
96d17b7b
CL
157 if (s->refcount == 1) {
158 err = sysfs_slab_add(s);
159 if (err)
160 printk(KERN_WARNING "kmem_cache_create(%s) failed to"
161 " create sysfs entry. Error %d\n",
162 name, err);
163 }
164
039363f3
CL
165 return s;
166}
167EXPORT_SYMBOL(kmem_cache_create);
97d06609 168
945cf2b6
CL
169void kmem_cache_destroy(struct kmem_cache *s)
170{
171 get_online_cpus();
172 mutex_lock(&slab_mutex);
173 s->refcount--;
174 if (!s->refcount) {
175 list_del(&s->list);
176
177 if (!__kmem_cache_shutdown(s)) {
178 if (s->flags & SLAB_DESTROY_BY_RCU)
179 rcu_barrier();
180
db265eca 181 kfree(s->name);
8f4c765c 182 kmem_cache_free(kmem_cache, s);
945cf2b6
CL
183 } else {
184 list_add(&s->list, &slab_caches);
185 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
186 s->name);
187 dump_stack();
188 }
189 }
190 mutex_unlock(&slab_mutex);
191 put_online_cpus();
192}
193EXPORT_SYMBOL(kmem_cache_destroy);
194
97d06609
CL
195int slab_is_available(void)
196{
197 return slab_state >= UP;
198}