blob: b705be7faa4861e41aceda053e3f04ec39114566 [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>
Glauber Costab7454ad2012-10-19 18:20:25 +040016#include <linux/seq_file.h>
17#include <linux/proc_fs.h>
Christoph Lameter039363f2012-07-06 15:25:10 -050018#include <asm/cacheflush.h>
19#include <asm/tlbflush.h>
20#include <asm/page.h>
21
Christoph Lameter97d06602012-07-06 15:25:11 -050022#include "slab.h"
23
24enum slab_state slab_state;
Christoph Lameter18004c52012-07-06 15:25:12 -050025LIST_HEAD(slab_caches);
26DEFINE_MUTEX(slab_mutex);
Christoph Lameter9b030cb2012-09-05 00:20:33 +000027struct kmem_cache *kmem_cache;
Christoph Lameter97d06602012-07-06 15:25:11 -050028
Shuah Khan77be4b12012-08-16 00:09:46 -070029#ifdef CONFIG_DEBUG_VM
30static int kmem_cache_sanity_check(const char *name, size_t size)
31{
32 struct kmem_cache *s = NULL;
33
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);
37 return -EINVAL;
38 }
39
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) {
51 pr_err("Slab cache with size %d has lost its name\n",
52 s->object_size);
53 continue;
54 }
55
56 if (!strcmp(s->name, name)) {
57 pr_err("%s (%s): Cache name already exists.\n",
58 __func__, name);
59 dump_stack();
60 s = NULL;
61 return -EINVAL;
62 }
63 }
64
65 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
66 return 0;
67}
68#else
69static inline int kmem_cache_sanity_check(const char *name, size_t size)
70{
71 return 0;
72}
73#endif
74
Christoph Lameter039363f2012-07-06 15:25:10 -050075/*
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;
Christoph Lameter686d5502012-09-05 00:20:33 +0000104 int err = 0;
Christoph Lameter039363f2012-07-06 15:25:10 -0500105
Pekka Enbergb9205362012-08-16 10:12:18 +0300106 get_online_cpus();
107 mutex_lock(&slab_mutex);
Christoph Lameter686d5502012-09-05 00:20:33 +0000108
109 if (!kmem_cache_sanity_check(name, size) == 0)
110 goto out_locked;
111
Glauber Costad8843922012-10-17 15:36:51 +0400112 /*
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;
Christoph Lameter686d5502012-09-05 00:20:33 +0000119
Christoph Lametercbb79692012-09-05 00:18:32 +0000120 s = __kmem_cache_alias(name, size, align, flags, ctor);
121 if (s)
122 goto out_locked;
123
Christoph Lameter278b1bb2012-09-05 00:20:34 +0000124 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
Christoph Lameterdb265ec2012-09-04 23:18:33 +0000125 if (s) {
Christoph Lameter8a13a4c2012-09-04 23:18:33 +0000126 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);
Christoph Lametercce89f42012-09-04 23:38:33 +0000137 if (!err) {
Christoph Lameter278b1bb2012-09-05 00:20:34 +0000138
Christoph Lametercce89f42012-09-04 23:38:33 +0000139 s->refcount = 1;
Christoph Lameterdb265ec2012-09-04 23:18:33 +0000140 list_add(&s->list, &slab_caches);
141
Christoph Lametercce89f42012-09-04 23:38:33 +0000142 } else {
Christoph Lameter8a13a4c2012-09-04 23:18:33 +0000143 kfree(s->name);
Christoph Lameter278b1bb2012-09-05 00:20:34 +0000144 kmem_cache_free(kmem_cache, s);
145 }
Christoph Lameter8a13a4c2012-09-04 23:18:33 +0000146 } else
Christoph Lameter278b1bb2012-09-05 00:20:34 +0000147 err = -ENOMEM;
Christoph Lameter7c9adf52012-09-04 23:38:33 +0000148
Christoph Lameter686d5502012-09-05 00:20:33 +0000149out_locked:
Christoph Lameter20cea962012-07-06 15:25:13 -0500150 mutex_unlock(&slab_mutex);
151 put_online_cpus();
152
Christoph Lameter686d5502012-09-05 00:20:33 +0000153 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 }
Christoph Lameter039363f2012-07-06 15:25:10 -0500166
167 return s;
168}
169EXPORT_SYMBOL(kmem_cache_create);
Christoph Lameter97d06602012-07-06 15:25:11 -0500170
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000171void 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)) {
Jiri Kosina210ed9d2012-10-08 09:26:01 +0200180 mutex_unlock(&slab_mutex);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000181 if (s->flags & SLAB_DESTROY_BY_RCU)
182 rcu_barrier();
183
Christoph Lameterdb265ec2012-09-04 23:18:33 +0000184 kfree(s->name);
Christoph Lameter8f4c7652012-09-05 00:18:32 +0000185 kmem_cache_free(kmem_cache, s);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000186 } else {
187 list_add(&s->list, &slab_caches);
Jiri Kosina210ed9d2012-10-08 09:26:01 +0200188 mutex_unlock(&slab_mutex);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000189 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
190 s->name);
191 dump_stack();
192 }
Jiri Kosina210ed9d2012-10-08 09:26:01 +0200193 } else {
194 mutex_unlock(&slab_mutex);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000195 }
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000196 put_online_cpus();
197}
198EXPORT_SYMBOL(kmem_cache_destroy);
199
Christoph Lameter97d06602012-07-06 15:25:11 -0500200int slab_is_available(void)
201{
202 return slab_state >= UP;
203}
Glauber Costab7454ad2012-10-19 18:20:25 +0400204
205#ifdef CONFIG_SLABINFO
Glauber Costabcee6e22012-10-19 18:20:26 +0400206static void print_slabinfo_header(struct seq_file *m)
207{
208 /*
209 * Output format version, so at least we can change it
210 * without _too_ many complaints.
211 */
212#ifdef CONFIG_DEBUG_SLAB
213 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
214#else
215 seq_puts(m, "slabinfo - version: 2.1\n");
216#endif
217 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
218 "<objperslab> <pagesperslab>");
219 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
220 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
221#ifdef CONFIG_DEBUG_SLAB
222 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
223 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
224 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
225#endif
226 seq_putc(m, '\n');
227}
228
Glauber Costab7454ad2012-10-19 18:20:25 +0400229static void *s_start(struct seq_file *m, loff_t *pos)
230{
231 loff_t n = *pos;
232
233 mutex_lock(&slab_mutex);
234 if (!n)
235 print_slabinfo_header(m);
236
237 return seq_list_start(&slab_caches, *pos);
238}
239
240static void *s_next(struct seq_file *m, void *p, loff_t *pos)
241{
242 return seq_list_next(p, &slab_caches, pos);
243}
244
245static void s_stop(struct seq_file *m, void *p)
246{
247 mutex_unlock(&slab_mutex);
248}
249
250static int s_show(struct seq_file *m, void *p)
251{
Glauber Costa0d7561c2012-10-19 18:20:27 +0400252 struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
253 struct slabinfo sinfo;
254
255 memset(&sinfo, 0, sizeof(sinfo));
256 get_slabinfo(s, &sinfo);
257
258 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
259 s->name, sinfo.active_objs, sinfo.num_objs, s->size,
260 sinfo.objects_per_slab, (1 << sinfo.cache_order));
261
262 seq_printf(m, " : tunables %4u %4u %4u",
263 sinfo.limit, sinfo.batchcount, sinfo.shared);
264 seq_printf(m, " : slabdata %6lu %6lu %6lu",
265 sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
266 slabinfo_show_stats(m, s);
267 seq_putc(m, '\n');
268 return 0;
Glauber Costab7454ad2012-10-19 18:20:25 +0400269}
270
271/*
272 * slabinfo_op - iterator that generates /proc/slabinfo
273 *
274 * Output layout:
275 * cache-name
276 * num-active-objs
277 * total-objs
278 * object size
279 * num-active-slabs
280 * total-slabs
281 * num-pages-per-slab
282 * + further values on SMP and with statistics enabled
283 */
284static const struct seq_operations slabinfo_op = {
285 .start = s_start,
286 .next = s_next,
287 .stop = s_stop,
288 .show = s_show,
289};
290
291static int slabinfo_open(struct inode *inode, struct file *file)
292{
293 return seq_open(file, &slabinfo_op);
294}
295
296static const struct file_operations proc_slabinfo_operations = {
297 .open = slabinfo_open,
298 .read = seq_read,
299 .write = slabinfo_write,
300 .llseek = seq_lseek,
301 .release = seq_release,
302};
303
304static int __init slab_proc_init(void)
305{
306 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
307 return 0;
308}
309module_init(slab_proc_init);
310#endif /* CONFIG_SLABINFO */