blob: aa3ca5bb01b55a097f1397a6a92728f81b3167a5 [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>
Christoph Lameter039363f2012-07-06 15:25:10 -050016#include <asm/cacheflush.h>
17#include <asm/tlbflush.h>
18#include <asm/page.h>
19
Christoph Lameter97d06602012-07-06 15:25:11 -050020#include "slab.h"
21
22enum slab_state slab_state;
Christoph Lameter18004c52012-07-06 15:25:12 -050023LIST_HEAD(slab_caches);
24DEFINE_MUTEX(slab_mutex);
Christoph Lameter97d06602012-07-06 15:25:11 -050025
Christoph Lameter039363f2012-07-06 15:25:10 -050026/*
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
51struct 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
Christoph Lameter20cea962012-07-06 15:25:13 -050056#ifdef CONFIG_DEBUG_VM
Andrew Morton455ce9e2012-08-14 14:53:22 -070057 if (!name || in_interrupt() || size < sizeof(void *) ||
58 size > KMALLOC_MAX_SIZE) {
59 printk(KERN_ERR "kmem_cache_create(%s) integrity check"
Pekka Enbergb9205362012-08-16 10:12:18 +030060 " failed\n", name);
61 goto out;
Andrew Morton455ce9e2012-08-14 14:53:22 -070062 }
Pekka Enbergb9205362012-08-16 10:12:18 +030063#endif
Andrew Morton455ce9e2012-08-14 14:53:22 -070064
Pekka Enbergb9205362012-08-16 10:12:18 +030065 get_online_cpus();
66 mutex_lock(&slab_mutex);
67
68#ifdef CONFIG_DEBUG_VM
Christoph Lameter20cea962012-07-06 15:25:13 -050069 list_for_each_entry(s, &slab_caches, list) {
70 char tmp;
71 int res;
72
73 /*
74 * This happens when the module gets unloaded and doesn't
75 * destroy its slab cache and no-one else reuses the vmalloc
76 * area of the module. Print a warning.
77 */
78 res = probe_kernel_address(s->name, tmp);
79 if (res) {
80 printk(KERN_ERR
81 "Slab cache with size %d has lost its name\n",
82 s->object_size);
83 continue;
84 }
85
86 if (!strcmp(s->name, name)) {
87 printk(KERN_ERR "kmem_cache_create(%s): Cache name"
88 " already exists.\n",
89 name);
90 dump_stack();
91 s = NULL;
92 goto oops;
93 }
94 }
95
96 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
97#endif
98
Christoph Lameter039363f2012-07-06 15:25:10 -050099 s = __kmem_cache_create(name, size, align, flags, ctor);
100
Shuah Khan73a11802012-07-13 17:12:05 -0600101#ifdef CONFIG_DEBUG_VM
Christoph Lameter20cea962012-07-06 15:25:13 -0500102oops:
Shuah Khan73a11802012-07-13 17:12:05 -0600103#endif
Christoph Lameter20cea962012-07-06 15:25:13 -0500104 mutex_unlock(&slab_mutex);
105 put_online_cpus();
106
Pekka Enbergb9205362012-08-16 10:12:18 +0300107#ifdef CONFIG_DEBUG_VM
108out:
109#endif
Christoph Lameter039363f2012-07-06 15:25:10 -0500110 if (!s && (flags & SLAB_PANIC))
111 panic("kmem_cache_create: Failed to create slab '%s'\n", name);
112
113 return s;
114}
115EXPORT_SYMBOL(kmem_cache_create);
Christoph Lameter97d06602012-07-06 15:25:11 -0500116
117int slab_is_available(void)
118{
119 return slab_state >= UP;
120}