]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/slab_common.c
slab: Use the new create_boot_cache function to simplify bootstrap
[mirror_ubuntu-zesty-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>
b7454ad3
GC
16#include <linux/seq_file.h>
17#include <linux/proc_fs.h>
039363f3
CL
18#include <asm/cacheflush.h>
19#include <asm/tlbflush.h>
20#include <asm/page.h>
21
97d06609
CL
22#include "slab.h"
23
24enum slab_state slab_state;
18004c5d
CL
25LIST_HEAD(slab_caches);
26DEFINE_MUTEX(slab_mutex);
9b030cb8 27struct kmem_cache *kmem_cache;
97d06609 28
77be4b13
SK
29#ifdef CONFIG_DEBUG_VM
30static int kmem_cache_sanity_check(const char *name, size_t size)
039363f3
CL
31{
32 struct kmem_cache *s = NULL;
33
039363f3
CL
34 if (!name || in_interrupt() || size < sizeof(void *) ||
35 size > KMALLOC_MAX_SIZE) {
77be4b13
SK
36 pr_err("kmem_cache_create(%s) integrity check failed\n", name);
37 return -EINVAL;
039363f3 38 }
b920536a 39
20cea968
CL
40 list_for_each_entry(s, &slab_caches, list) {
41 char tmp;
42 int res;
43
44 /*
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.
48 */
49 res = probe_kernel_address(s->name, tmp);
50 if (res) {
77be4b13 51 pr_err("Slab cache with size %d has lost its name\n",
20cea968
CL
52 s->object_size);
53 continue;
54 }
55
56 if (!strcmp(s->name, name)) {
77be4b13
SK
57 pr_err("%s (%s): Cache name already exists.\n",
58 __func__, name);
20cea968
CL
59 dump_stack();
60 s = NULL;
77be4b13 61 return -EINVAL;
20cea968
CL
62 }
63 }
64
65 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
77be4b13
SK
66 return 0;
67}
68#else
69static inline int kmem_cache_sanity_check(const char *name, size_t size)
70{
71 return 0;
72}
20cea968
CL
73#endif
74
77be4b13
SK
75/*
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.
80 * @flags: SLAB flags
81 * @ctor: A constructor for the objects.
82 *
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.
86 *
87 * The flags are
88 *
89 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
90 * to catch references to uninitialised memory.
91 *
92 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
93 * for buffer overruns.
94 *
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
97 * as davem.
98 */
99
100struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
101 unsigned long flags, void (*ctor)(void *))
102{
103 struct kmem_cache *s = NULL;
686d550d 104 int err = 0;
039363f3 105
77be4b13
SK
106 get_online_cpus();
107 mutex_lock(&slab_mutex);
686d550d
CL
108
109 if (!kmem_cache_sanity_check(name, size) == 0)
110 goto out_locked;
111
d8843922
GC
112 /*
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
116 * passed flags.
117 */
118 flags &= CACHE_CREATE_MASK;
686d550d 119
cbb79694
CL
120 s = __kmem_cache_alias(name, size, align, flags, ctor);
121 if (s)
122 goto out_locked;
123
278b1bb1 124 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
db265eca 125 if (s) {
8a13a4cc
CL
126 s->object_size = s->size = size;
127 s->align = align;
128 s->ctor = ctor;
129 s->name = kstrdup(name, GFP_KERNEL);
130 if (!s->name) {
131 kmem_cache_free(kmem_cache, s);
132 err = -ENOMEM;
133 goto out_locked;
134 }
135
136 err = __kmem_cache_create(s, flags);
cce89f4f 137 if (!err) {
278b1bb1 138
cce89f4f 139 s->refcount = 1;
db265eca 140 list_add(&s->list, &slab_caches);
686d550d 141
cce89f4f 142 } else {
8a13a4cc 143 kfree(s->name);
278b1bb1
CL
144 kmem_cache_free(kmem_cache, s);
145 }
8a13a4cc 146 } else
278b1bb1 147 err = -ENOMEM;
7c9adf5a 148
686d550d 149out_locked:
20cea968
CL
150 mutex_unlock(&slab_mutex);
151 put_online_cpus();
152
686d550d
CL
153 if (err) {
154
155 if (flags & SLAB_PANIC)
156 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
157 name, err);
158 else {
159 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
160 name, err);
161 dump_stack();
162 }
163
164 return NULL;
165 }
039363f3
CL
166
167 return s;
168}
169EXPORT_SYMBOL(kmem_cache_create);
97d06609 170
945cf2b6
CL
171void kmem_cache_destroy(struct kmem_cache *s)
172{
173 get_online_cpus();
174 mutex_lock(&slab_mutex);
175 s->refcount--;
176 if (!s->refcount) {
177 list_del(&s->list);
178
179 if (!__kmem_cache_shutdown(s)) {
210ed9de 180 mutex_unlock(&slab_mutex);
945cf2b6
CL
181 if (s->flags & SLAB_DESTROY_BY_RCU)
182 rcu_barrier();
183
db265eca 184 kfree(s->name);
8f4c765c 185 kmem_cache_free(kmem_cache, s);
945cf2b6
CL
186 } else {
187 list_add(&s->list, &slab_caches);
210ed9de 188 mutex_unlock(&slab_mutex);
945cf2b6
CL
189 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
190 s->name);
191 dump_stack();
192 }
210ed9de
JK
193 } else {
194 mutex_unlock(&slab_mutex);
945cf2b6 195 }
945cf2b6
CL
196 put_online_cpus();
197}
198EXPORT_SYMBOL(kmem_cache_destroy);
199
97d06609
CL
200int slab_is_available(void)
201{
202 return slab_state >= UP;
203}
b7454ad3 204
45530c44
CL
205#ifndef CONFIG_SLOB
206/* Create a cache during boot when no slab services are available yet */
207void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size,
208 unsigned long flags)
209{
210 int err;
211
212 s->name = name;
213 s->size = s->object_size = size;
214 s->align = ARCH_KMALLOC_MINALIGN;
215 err = __kmem_cache_create(s, flags);
216
217 if (err)
218 panic("Creation of kmalloc slab %s size=%zd failed. Reason %d\n",
219 name, size, err);
220
221 s->refcount = -1; /* Exempt from merging for now */
222}
223
224struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
225 unsigned long flags)
226{
227 struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
228
229 if (!s)
230 panic("Out of memory when creating slab %s\n", name);
231
232 create_boot_cache(s, name, size, flags);
233 list_add(&s->list, &slab_caches);
234 s->refcount = 1;
235 return s;
236}
237
238#endif /* !CONFIG_SLOB */
239
240
b7454ad3 241#ifdef CONFIG_SLABINFO
bcee6e2a
GC
242static void print_slabinfo_header(struct seq_file *m)
243{
244 /*
245 * Output format version, so at least we can change it
246 * without _too_ many complaints.
247 */
248#ifdef CONFIG_DEBUG_SLAB
249 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
250#else
251 seq_puts(m, "slabinfo - version: 2.1\n");
252#endif
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>");
261#endif
262 seq_putc(m, '\n');
263}
264
b7454ad3
GC
265static void *s_start(struct seq_file *m, loff_t *pos)
266{
267 loff_t n = *pos;
268
269 mutex_lock(&slab_mutex);
270 if (!n)
271 print_slabinfo_header(m);
272
273 return seq_list_start(&slab_caches, *pos);
274}
275
276static void *s_next(struct seq_file *m, void *p, loff_t *pos)
277{
278 return seq_list_next(p, &slab_caches, pos);
279}
280
281static void s_stop(struct seq_file *m, void *p)
282{
283 mutex_unlock(&slab_mutex);
284}
285
286static int s_show(struct seq_file *m, void *p)
287{
0d7561c6
GC
288 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
289 struct slabinfo sinfo;
290
291 memset(&sinfo, 0, sizeof(sinfo));
292 get_slabinfo(s, &sinfo);
293
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));
297
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);
303 seq_putc(m, '\n');
304 return 0;
b7454ad3
GC
305}
306
307/*
308 * slabinfo_op - iterator that generates /proc/slabinfo
309 *
310 * Output layout:
311 * cache-name
312 * num-active-objs
313 * total-objs
314 * object size
315 * num-active-slabs
316 * total-slabs
317 * num-pages-per-slab
318 * + further values on SMP and with statistics enabled
319 */
320static const struct seq_operations slabinfo_op = {
321 .start = s_start,
322 .next = s_next,
323 .stop = s_stop,
324 .show = s_show,
325};
326
327static int slabinfo_open(struct inode *inode, struct file *file)
328{
329 return seq_open(file, &slabinfo_op);
330}
331
332static const struct file_operations proc_slabinfo_operations = {
333 .open = slabinfo_open,
334 .read = seq_read,
335 .write = slabinfo_write,
336 .llseek = seq_lseek,
337 .release = seq_release,
338};
339
340static int __init slab_proc_init(void)
341{
342 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
343 return 0;
344}
345module_init(slab_proc_init);
346#endif /* CONFIG_SLABINFO */