Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 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> |
Christoph Lameter | 20cea96 | 2012-07-06 15:25:13 -0500 | [diff] [blame] | 14 | #include <linux/cpu.h> |
| 15 | #include <linux/uaccess.h> |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 16 | #include <asm/cacheflush.h> |
| 17 | #include <asm/tlbflush.h> |
| 18 | #include <asm/page.h> |
| 19 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 20 | #include "slab.h" |
| 21 | |
| 22 | enum slab_state slab_state; |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 23 | LIST_HEAD(slab_caches); |
| 24 | DEFINE_MUTEX(slab_mutex); |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 25 | struct kmem_cache *kmem_cache; |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 26 | |
Shuah Khan | 77be4b1 | 2012-08-16 00:09:46 -0700 | [diff] [blame] | 27 | #ifdef CONFIG_DEBUG_VM |
| 28 | static int kmem_cache_sanity_check(const char *name, size_t size) |
| 29 | { |
| 30 | struct kmem_cache *s = NULL; |
| 31 | |
| 32 | if (!name || in_interrupt() || size < sizeof(void *) || |
| 33 | size > KMALLOC_MAX_SIZE) { |
| 34 | pr_err("kmem_cache_create(%s) integrity check failed\n", name); |
| 35 | return -EINVAL; |
| 36 | } |
| 37 | |
| 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) { |
| 49 | pr_err("Slab cache with size %d has lost its name\n", |
| 50 | s->object_size); |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | if (!strcmp(s->name, name)) { |
| 55 | pr_err("%s (%s): Cache name already exists.\n", |
| 56 | __func__, name); |
| 57 | dump_stack(); |
| 58 | s = NULL; |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | WARN_ON(strchr(name, ' ')); /* It confuses parsers */ |
| 64 | return 0; |
| 65 | } |
| 66 | #else |
| 67 | static inline int kmem_cache_sanity_check(const char *name, size_t size) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | #endif |
| 72 | |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 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 | |
| 98 | struct 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; |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 102 | int err = 0; |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 103 | |
Pekka Enberg | b920536 | 2012-08-16 10:12:18 +0300 | [diff] [blame] | 104 | get_online_cpus(); |
| 105 | mutex_lock(&slab_mutex); |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 106 | |
| 107 | if (!kmem_cache_sanity_check(name, size) == 0) |
| 108 | goto out_locked; |
| 109 | |
| 110 | |
Christoph Lameter | cbb7969 | 2012-09-05 00:18:32 +0000 | [diff] [blame] | 111 | s = __kmem_cache_alias(name, size, align, flags, ctor); |
| 112 | if (s) |
| 113 | goto out_locked; |
| 114 | |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 115 | s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL); |
Christoph Lameter | db265ec | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 116 | if (s) { |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 117 | s->object_size = s->size = size; |
| 118 | s->align = align; |
| 119 | s->ctor = ctor; |
| 120 | s->name = kstrdup(name, GFP_KERNEL); |
| 121 | if (!s->name) { |
| 122 | kmem_cache_free(kmem_cache, s); |
| 123 | err = -ENOMEM; |
| 124 | goto out_locked; |
| 125 | } |
| 126 | |
| 127 | err = __kmem_cache_create(s, flags); |
Christoph Lameter | cce89f4 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 128 | if (!err) { |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 129 | |
Christoph Lameter | cce89f4 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 130 | s->refcount = 1; |
Christoph Lameter | db265ec | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 131 | list_add(&s->list, &slab_caches); |
| 132 | |
Christoph Lameter | cce89f4 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 133 | } else { |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 134 | kfree(s->name); |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 135 | kmem_cache_free(kmem_cache, s); |
| 136 | } |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 137 | } else |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 138 | err = -ENOMEM; |
Christoph Lameter | 7c9adf5 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 139 | |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 140 | out_locked: |
Christoph Lameter | 20cea96 | 2012-07-06 15:25:13 -0500 | [diff] [blame] | 141 | mutex_unlock(&slab_mutex); |
| 142 | put_online_cpus(); |
| 143 | |
Christoph Lameter | 686d550 | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 144 | if (err) { |
| 145 | |
| 146 | if (flags & SLAB_PANIC) |
| 147 | panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n", |
| 148 | name, err); |
| 149 | else { |
| 150 | printk(KERN_WARNING "kmem_cache_create(%s) failed with error %d", |
| 151 | name, err); |
| 152 | dump_stack(); |
| 153 | } |
| 154 | |
| 155 | return NULL; |
| 156 | } |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 157 | |
| 158 | return s; |
| 159 | } |
| 160 | EXPORT_SYMBOL(kmem_cache_create); |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 161 | |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 162 | void kmem_cache_destroy(struct kmem_cache *s) |
| 163 | { |
| 164 | get_online_cpus(); |
| 165 | mutex_lock(&slab_mutex); |
| 166 | s->refcount--; |
| 167 | if (!s->refcount) { |
| 168 | list_del(&s->list); |
| 169 | |
| 170 | if (!__kmem_cache_shutdown(s)) { |
Jiri Kosina | 210ed9d | 2012-10-08 09:26:01 +0200 | [diff] [blame] | 171 | mutex_unlock(&slab_mutex); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 172 | if (s->flags & SLAB_DESTROY_BY_RCU) |
| 173 | rcu_barrier(); |
| 174 | |
Christoph Lameter | db265ec | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 175 | kfree(s->name); |
Christoph Lameter | 8f4c765 | 2012-09-05 00:18:32 +0000 | [diff] [blame] | 176 | kmem_cache_free(kmem_cache, s); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 177 | } else { |
| 178 | list_add(&s->list, &slab_caches); |
Jiri Kosina | 210ed9d | 2012-10-08 09:26:01 +0200 | [diff] [blame] | 179 | mutex_unlock(&slab_mutex); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 180 | printk(KERN_ERR "kmem_cache_destroy %s: Slab cache still has objects\n", |
| 181 | s->name); |
| 182 | dump_stack(); |
| 183 | } |
Jiri Kosina | 210ed9d | 2012-10-08 09:26:01 +0200 | [diff] [blame] | 184 | } else { |
| 185 | mutex_unlock(&slab_mutex); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 186 | } |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 187 | put_online_cpus(); |
| 188 | } |
| 189 | EXPORT_SYMBOL(kmem_cache_destroy); |
| 190 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 191 | int slab_is_available(void) |
| 192 | { |
| 193 | return slab_state >= UP; |
| 194 | } |