]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/slab_common.c
mm/sl[au]b: Move slabinfo processing to slab_common.c
[mirror_ubuntu-bionic-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
112
cbb79694
CL
113 s = __kmem_cache_alias(name, size, align, flags, ctor);
114 if (s)
115 goto out_locked;
116
278b1bb1 117 s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
db265eca 118 if (s) {
8a13a4cc
CL
119 s->object_size = s->size = size;
120 s->align = align;
121 s->ctor = ctor;
122 s->name = kstrdup(name, GFP_KERNEL);
123 if (!s->name) {
124 kmem_cache_free(kmem_cache, s);
125 err = -ENOMEM;
126 goto out_locked;
127 }
128
129 err = __kmem_cache_create(s, flags);
cce89f4f 130 if (!err) {
278b1bb1 131
cce89f4f 132 s->refcount = 1;
db265eca 133 list_add(&s->list, &slab_caches);
686d550d 134
cce89f4f 135 } else {
8a13a4cc 136 kfree(s->name);
278b1bb1
CL
137 kmem_cache_free(kmem_cache, s);
138 }
8a13a4cc 139 } else
278b1bb1 140 err = -ENOMEM;
7c9adf5a 141
686d550d 142out_locked:
20cea968
CL
143 mutex_unlock(&slab_mutex);
144 put_online_cpus();
145
686d550d
CL
146 if (err) {
147
148 if (flags & SLAB_PANIC)
149 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
150 name, err);
151 else {
152 printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d",
153 name, err);
154 dump_stack();
155 }
156
157 return NULL;
158 }
039363f3
CL
159
160 return s;
161}
162EXPORT_SYMBOL(kmem_cache_create);
97d06609 163
945cf2b6
CL
164void kmem_cache_destroy(struct kmem_cache *s)
165{
166 get_online_cpus();
167 mutex_lock(&slab_mutex);
168 s->refcount--;
169 if (!s->refcount) {
170 list_del(&s->list);
171
172 if (!__kmem_cache_shutdown(s)) {
210ed9de 173 mutex_unlock(&slab_mutex);
945cf2b6
CL
174 if (s->flags & SLAB_DESTROY_BY_RCU)
175 rcu_barrier();
176
db265eca 177 kfree(s->name);
8f4c765c 178 kmem_cache_free(kmem_cache, s);
945cf2b6
CL
179 } else {
180 list_add(&s->list, &slab_caches);
210ed9de 181 mutex_unlock(&slab_mutex);
945cf2b6
CL
182 printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n",
183 s->name);
184 dump_stack();
185 }
210ed9de
JK
186 } else {
187 mutex_unlock(&slab_mutex);
945cf2b6 188 }
945cf2b6
CL
189 put_online_cpus();
190}
191EXPORT_SYMBOL(kmem_cache_destroy);
192
97d06609
CL
193int slab_is_available(void)
194{
195 return slab_state >= UP;
196}
b7454ad3
GC
197
198#ifdef CONFIG_SLABINFO
199static void *s_start(struct seq_file *m, loff_t *pos)
200{
201 loff_t n = *pos;
202
203 mutex_lock(&slab_mutex);
204 if (!n)
205 print_slabinfo_header(m);
206
207 return seq_list_start(&slab_caches, *pos);
208}
209
210static void *s_next(struct seq_file *m, void *p, loff_t *pos)
211{
212 return seq_list_next(p, &slab_caches, pos);
213}
214
215static void s_stop(struct seq_file *m, void *p)
216{
217 mutex_unlock(&slab_mutex);
218}
219
220static int s_show(struct seq_file *m, void *p)
221{
222 return slabinfo_show(m, p);
223}
224
225/*
226 * slabinfo_op - iterator that generates /proc/slabinfo
227 *
228 * Output layout:
229 * cache-name
230 * num-active-objs
231 * total-objs
232 * object size
233 * num-active-slabs
234 * total-slabs
235 * num-pages-per-slab
236 * + further values on SMP and with statistics enabled
237 */
238static const struct seq_operations slabinfo_op = {
239 .start = s_start,
240 .next = s_next,
241 .stop = s_stop,
242 .show = s_show,
243};
244
245static int slabinfo_open(struct inode *inode, struct file *file)
246{
247 return seq_open(file, &slabinfo_op);
248}
249
250static const struct file_operations proc_slabinfo_operations = {
251 .open = slabinfo_open,
252 .read = seq_read,
253 .write = slabinfo_write,
254 .llseek = seq_lseek,
255 .release = seq_release,
256};
257
258static int __init slab_proc_init(void)
259{
260 proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
261 return 0;
262}
263module_init(slab_proc_init);
264#endif /* CONFIG_SLABINFO */