]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - mm/slab_common.c
2 * Slab allocator functions that are independent of the allocator strategy
4 * (C) 2012 Christoph Lameter <cl@linux.com>
6 #include <linux/slab.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>
14 #include <linux/cpu.h>
15 #include <linux/uaccess.h>
16 #include <asm/cacheflush.h>
17 #include <asm/tlbflush.h>
22 enum slab_state slab_state
;
23 LIST_HEAD(slab_caches
);
24 DEFINE_MUTEX(slab_mutex
);
26 #ifdef CONFIG_DEBUG_VM
27 static int kmem_cache_sanity_check(const char *name
, size_t size
)
29 struct kmem_cache
*s
= NULL
;
31 if (!name
|| in_interrupt() || size
< sizeof(void *) ||
32 size
> KMALLOC_MAX_SIZE
) {
33 pr_err("kmem_cache_create(%s) integrity check failed\n", name
);
37 list_for_each_entry(s
, &slab_caches
, list
) {
42 * This happens when the module gets unloaded and doesn't
43 * destroy its slab cache and no-one else reuses the vmalloc
44 * area of the module. Print a warning.
46 res
= probe_kernel_address(s
->name
, tmp
);
48 pr_err("Slab cache with size %d has lost its name\n",
53 if (!strcmp(s
->name
, name
)) {
54 pr_err("%s (%s): Cache name already exists.\n",
62 WARN_ON(strchr(name
, ' ')); /* It confuses parsers */
66 static inline int kmem_cache_sanity_check(const char *name
, size_t size
)
73 * kmem_cache_create - Create a cache.
74 * @name: A string which is used in /proc/slabinfo to identify this cache.
75 * @size: The size of objects to be created in this cache.
76 * @align: The required alignment for the objects.
78 * @ctor: A constructor for the objects.
80 * Returns a ptr to the cache on success, NULL on failure.
81 * Cannot be called within a interrupt, but can be interrupted.
82 * The @ctor is run when new pages are allocated by the cache.
86 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
87 * to catch references to uninitialised memory.
89 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
90 * for buffer overruns.
92 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
93 * cacheline. This can be beneficial if you're counting cycles as closely
97 struct kmem_cache
*kmem_cache_create(const char *name
, size_t size
, size_t align
,
98 unsigned long flags
, void (*ctor
)(void *))
100 struct kmem_cache
*s
= NULL
;
104 mutex_lock(&slab_mutex
);
106 if (!kmem_cache_sanity_check(name
, size
) == 0)
110 s
= __kmem_cache_create(name
, size
, align
, flags
, ctor
);
112 err
= -ENOSYS
; /* Until __kmem_cache_create returns code */
115 * Check if the slab has actually been created and if it was a
116 * real instatiation. Aliases do not belong on the list
118 if (s
&& s
->refcount
== 1)
119 list_add(&s
->list
, &slab_caches
);
122 mutex_unlock(&slab_mutex
);
127 if (flags
& SLAB_PANIC
)
128 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
131 printk(KERN_WARNING
"kmem_cache_create(%s) failed with error %d",
141 EXPORT_SYMBOL(kmem_cache_create
);
143 void kmem_cache_destroy(struct kmem_cache
*s
)
146 mutex_lock(&slab_mutex
);
151 if (!__kmem_cache_shutdown(s
)) {
152 if (s
->flags
& SLAB_DESTROY_BY_RCU
)
155 __kmem_cache_destroy(s
);
157 list_add(&s
->list
, &slab_caches
);
158 printk(KERN_ERR
"kmem_cache_destroy %s: Slab cache still has objects\n",
163 mutex_unlock(&slab_mutex
);
166 EXPORT_SYMBOL(kmem_cache_destroy
);
168 int slab_is_available(void)
170 return slab_state
>= UP
;