From: Christoph Lameter <cl@linux.com>
To: Pekka Enberg <penberg@kernel.org>
Cc: linux-mm@kvack.org, David Rientjes <rientjes@google.com>,
Matt Mackall <mpm@selenic.com>,
Glauber Costa <glommer@parallels.com>,
Joonsoo Kim <js1304@gmail.com>, Alex Shi <alex.shi@intel.com>
Subject: [RFC] Common code 04/12] slabs: Extract common code for kmem_cache_create
Date: Fri, 18 May 2012 11:19:10 -0500 [thread overview]
Message-ID: <20120518161929.264565121@linux.com> (raw)
In-Reply-To: 20120518161906.207356777@linux.com
[-- Attachment #1: common_kmem_cache_checks --]
[-- Type: text/plain, Size: 9749 bytes --]
Kmem_cache_create does a variety of sanity checks but those
vary depending on the allocator. Use the strictest tests and put them into
a slab_common file. Make the tests conditional on CONFIG_DEBUG_VM.
This patch has the effect of adding sanity checks for SLUB and SLOB
under CONFIG_DEBUG_VM and removes the checks in SLAB for !CONFIG_DEBUG_VM.
Signed-off-by: Christoph Lameter <cl@linux.com>
---
include/linux/slab.h | 4 +++
mm/Makefile | 2 -
mm/slab.c | 24 ++++++------------
mm/slab_common.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
mm/slob.c | 8 ++----
mm/slub.c | 11 --------
6 files changed, 85 insertions(+), 31 deletions(-)
Index: linux-2.6/mm/slab.c
===================================================================
--- linux-2.6.orig/mm/slab.c 2012-05-11 09:36:35.308445605 -0500
+++ linux-2.6/mm/slab.c 2012-05-11 09:43:33.160436947 -0500
@@ -1585,7 +1585,7 @@ void __init kmem_cache_init(void)
* bug.
*/
- sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
+ sizes[INDEX_AC].cs_cachep = __kmem_cache_create(names[INDEX_AC].name,
sizes[INDEX_AC].cs_size,
ARCH_KMALLOC_MINALIGN,
ARCH_KMALLOC_FLAGS|SLAB_PANIC,
@@ -1593,7 +1593,7 @@ void __init kmem_cache_init(void)
if (INDEX_AC != INDEX_L3) {
sizes[INDEX_L3].cs_cachep =
- kmem_cache_create(names[INDEX_L3].name,
+ __kmem_cache_create(names[INDEX_L3].name,
sizes[INDEX_L3].cs_size,
ARCH_KMALLOC_MINALIGN,
ARCH_KMALLOC_FLAGS|SLAB_PANIC,
@@ -1611,14 +1611,14 @@ void __init kmem_cache_init(void)
* allow tighter packing of the smaller caches.
*/
if (!sizes->cs_cachep) {
- sizes->cs_cachep = kmem_cache_create(names->name,
+ sizes->cs_cachep = __kmem_cache_create(names->name,
sizes->cs_size,
ARCH_KMALLOC_MINALIGN,
ARCH_KMALLOC_FLAGS|SLAB_PANIC,
NULL);
}
#ifdef CONFIG_ZONE_DMA
- sizes->cs_dmacachep = kmem_cache_create(
+ sizes->cs_dmacachep = __kmem_cache_create(
names->name_dma,
sizes->cs_size,
ARCH_KMALLOC_MINALIGN,
@@ -2247,7 +2247,7 @@ static int __init_refok setup_cpu_cache(
}
/**
- * kmem_cache_create - Create a cache.
+ * __kmem_cache_create - Create a cache.
* @name: A string which is used in /proc/slabinfo to identify this cache.
* @size: The size of objects to be created in this cache.
* @align: The required alignment for the objects.
@@ -2274,7 +2274,7 @@ static int __init_refok setup_cpu_cache(
* as davem.
*/
struct kmem_cache *
-kmem_cache_create (const char *name, size_t size, size_t align,
+__kmem_cache_create (const char *name, size_t size, size_t align,
unsigned long flags, void (*ctor)(void *))
{
size_t left_over, slab_size, ralign;
@@ -2415,7 +2415,7 @@ kmem_cache_create (const char *name, siz
/* Get cache's description obj. */
cachep = kmem_cache_zalloc(&cache_cache, gfp);
if (!cachep)
- goto oops;
+ return NULL;
cachep->nodelists = (struct kmem_list3 **)&cachep->array[nr_cpu_ids];
#if DEBUG
@@ -2471,8 +2471,7 @@ kmem_cache_create (const char *name, siz
printk(KERN_ERR
"kmem_cache_create: couldn't create cache %s.\n", name);
kmem_cache_free(&cache_cache, cachep);
- cachep = NULL;
- goto oops;
+ return NULL;
}
slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
+ sizeof(struct slab), align);
@@ -2530,8 +2529,7 @@ kmem_cache_create (const char *name, siz
if (setup_cpu_cache(cachep, gfp)) {
__kmem_cache_destroy(cachep);
- cachep = NULL;
- goto oops;
+ return NULL;
}
if (flags & SLAB_DEBUG_OBJECTS) {
@@ -2547,16 +2545,12 @@ kmem_cache_create (const char *name, siz
/* cache setup completed, link it into the list */
list_add(&cachep->list, &cache_chain);
oops:
- if (!cachep && (flags & SLAB_PANIC))
- panic("kmem_cache_create(): failed to create slab `%s'\n",
- name);
if (slab_is_available()) {
mutex_unlock(&cache_chain_mutex);
put_online_cpus();
}
return cachep;
}
-EXPORT_SYMBOL(kmem_cache_create);
#if DEBUG
static void check_irq_off(void)
Index: linux-2.6/mm/slab_common.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6/mm/slab_common.c 2012-05-11 09:43:33.160436947 -0500
@@ -0,0 +1,67 @@
+/*
+ * Slab allocator functions that are independent of the allocator strategy
+ *
+ * (C) 2012 Christoph Lameter <cl@linux.com>
+ */
+#include <linux/slab.h>
+
+#include <linux/mm.h>
+#include <linux/poison.h>
+#include <linux/interrupt.h>
+#include <linux/memory.h>
+#include <linux/compiler.h>
+#include <linux/module.h>
+
+#include <asm/cacheflush.h>
+#include <asm/tlbflush.h>
+#include <asm/page.h>
+
+/*
+ * kmem_cache_create - Create a cache.
+ * @name: A string which is used in /proc/slabinfo to identify this cache.
+ * @size: The size of objects to be created in this cache.
+ * @align: The required alignment for the objects.
+ * @flags: SLAB flags
+ * @ctor: A constructor for the objects.
+ *
+ * Returns a ptr to the cache on success, NULL on failure.
+ * Cannot be called within a interrupt, but can be interrupted.
+ * The @ctor is run when new pages are allocated by the cache.
+ *
+ * The flags are
+ *
+ * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
+ * to catch references to uninitialised memory.
+ *
+ * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
+ * for buffer overruns.
+ *
+ * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
+ * cacheline. This can be beneficial if you're counting cycles as closely
+ * as davem.
+ */
+
+struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
+ unsigned long flags, void (*ctor)(void *))
+{
+ struct kmem_cache *s = NULL;
+
+#ifdef CONFIG_DEBUG_VM
+ if (!name || in_interrupt() || size < sizeof(void *) ||
+ size > KMALLOC_MAX_SIZE) {
+ printk(KERN_ERR "kmem_cache_create(%s) integrity check"
+ " failed\n", name);
+ goto out;
+ }
+#endif
+
+ s = __kmem_cache_create(name, size, align, flags, ctor);
+
+out:
+ if (!s && (flags & SLAB_PANIC))
+ panic("kmem_cache_create: Failed to create slab '%s'\n", name);
+
+ return s;
+}
+EXPORT_SYMBOL(kmem_cache_create);
+
Index: linux-2.6/mm/slub.c
===================================================================
--- linux-2.6.orig/mm/slub.c 2012-05-11 09:36:35.320445605 -0500
+++ linux-2.6/mm/slub.c 2012-05-11 09:43:33.164436947 -0500
@@ -3921,15 +3921,12 @@ static struct kmem_cache *find_mergeable
return NULL;
}
-struct kmem_cache *kmem_cache_create(const char *name, size_t size,
+struct kmem_cache *__kmem_cache_create(const char *name, size_t size,
size_t align, unsigned long flags, void (*ctor)(void *))
{
struct kmem_cache *s;
char *n;
- if (WARN_ON(!name))
- return NULL;
-
down_write(&slub_lock);
s = find_mergeable(size, align, flags, name, ctor);
if (s) {
@@ -3973,14 +3970,8 @@ struct kmem_cache *kmem_cache_create(con
}
err:
up_write(&slub_lock);
-
- if (flags & SLAB_PANIC)
- panic("Cannot create slabcache %s\n", name);
- else
- s = NULL;
return s;
}
-EXPORT_SYMBOL(kmem_cache_create);
#ifdef CONFIG_SMP
/*
Index: linux-2.6/mm/slob.c
===================================================================
--- linux-2.6.orig/mm/slob.c 2012-05-11 09:42:52.032437799 -0500
+++ linux-2.6/mm/slob.c 2012-05-11 09:43:33.164436947 -0500
@@ -538,7 +538,7 @@ size_t ksize(const void *block)
}
EXPORT_SYMBOL(ksize);
-struct kmem_cache *kmem_cache_create(const char *name, size_t size,
+struct kmem_cache *__kmem_cache_create(const char *name, size_t size,
size_t align, unsigned long flags, void (*ctor)(void *))
{
struct kmem_cache *c;
@@ -561,13 +561,11 @@ struct kmem_cache *kmem_cache_create(con
c->align = ARCH_SLAB_MINALIGN;
if (c->align < align)
c->align = align;
- } else if (flags & SLAB_PANIC)
- panic("Cannot create slab cache %s\n", name);
- kmemleak_alloc(c, sizeof(struct kmem_cache), 1, GFP_KERNEL);
+ kmemleak_alloc(c, sizeof(struct kmem_cache), 1, GFP_KERNEL);
+ }
return c;
}
-EXPORT_SYMBOL(kmem_cache_create);
void kmem_cache_destroy(struct kmem_cache *c)
{
Index: linux-2.6/mm/Makefile
===================================================================
--- linux-2.6.orig/mm/Makefile 2012-05-11 09:36:35.328445605 -0500
+++ linux-2.6/mm/Makefile 2012-05-11 09:43:33.164436947 -0500
@@ -13,7 +13,7 @@ obj-y := filemap.o mempool.o oom_kill.
readahead.o swap.o truncate.o vmscan.o shmem.o \
prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \
page_isolation.o mm_init.o mmu_context.o percpu.o \
- $(mmu-y)
+ slab_common.o $(mmu-y)
obj-y += init-mm.o
ifdef CONFIG_NO_BOOTMEM
Index: linux-2.6/include/linux/slab.h
===================================================================
--- linux-2.6.orig/include/linux/slab.h 2012-05-11 09:36:35.292445608 -0500
+++ linux-2.6/include/linux/slab.h 2012-05-11 09:43:33.164436947 -0500
@@ -117,6 +117,10 @@ int kmem_cache_shrink(struct kmem_cache
void kmem_cache_free(struct kmem_cache *, void *);
unsigned int kmem_cache_size(struct kmem_cache *);
+/* Slab internal function */
+struct kmem_cache *__kmem_cache_create(const char *, size_t, size_t,
+ unsigned long,
+ void (*)(void *));
/*
* Please use this macro to create slab caches. Simply specify the
* name of the structure and maybe some flags that are listed above.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2012-05-18 16:19 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-18 16:19 [RFC] Common code 00/12] Sl[auo]b: Common functionality V2 Christoph Lameter
2012-05-18 16:19 ` [RFC] Common code 01/12] [slob] define page struct fields used in mm_types.h Christoph Lameter
2012-05-21 8:41 ` Glauber Costa
2012-05-22 14:22 ` JoonSoo Kim
2012-05-22 17:42 ` Christoph Lameter
2012-05-23 14:28 ` JoonSoo Kim
2012-05-18 16:19 ` [RFC] Common code 02/12] [slab]: Use page struct fields instead of casting Christoph Lameter
2012-05-22 14:28 ` JoonSoo Kim
2012-05-18 16:19 ` [RFC] Common code 03/12] Extract common fields from struct kmem_cache Christoph Lameter
2012-05-21 9:21 ` Glauber Costa
2012-05-22 14:39 ` JoonSoo Kim
2012-05-18 16:19 ` Christoph Lameter [this message]
2012-05-21 9:24 ` [RFC] Common code 04/12] slabs: Extract common code for kmem_cache_create Glauber Costa
2012-05-21 13:48 ` Christoph Lameter
2012-05-22 15:08 ` JoonSoo Kim
2012-05-22 15:23 ` Glauber Costa
2012-05-22 17:49 ` Christoph Lameter
2012-05-18 16:19 ` [RFC] Common code 05/12] slabs: Common definition for boot state of the slab allocators Christoph Lameter
2012-05-21 9:26 ` Glauber Costa
2012-05-22 15:34 ` JoonSoo Kim
2012-05-18 16:19 ` [RFC] Common code 06/12] slabs: Use a common mutex definition Christoph Lameter
2012-05-22 15:44 ` JoonSoo Kim
2012-05-18 16:19 ` [RFC] Common code 07/12] slabs: Move kmem_cache_create mutex handling to common code Christoph Lameter
2012-05-22 16:19 ` JoonSoo Kim
2012-05-22 17:51 ` Christoph Lameter
2012-05-18 16:19 ` [RFC] Common code 09/12] slabs: Extract a common function for kmem_cache_destroy Christoph Lameter
2012-05-21 9:34 ` Glauber Costa
2012-05-21 18:13 ` Christoph Lameter
2012-05-21 19:19 ` Glauber Costa
2012-05-21 19:31 ` Christoph Lameter
2012-05-21 20:06 ` Glauber Costa
2012-05-21 20:54 ` Christoph Lameter
2012-05-23 15:39 ` JoonSoo Kim
2012-05-23 15:42 ` Christoph Lameter
2012-05-18 16:19 ` [RFC] Common code 10/12] sl[aub]: Use the name "kmem_cache" for the slab cache with the kmem_cache structure Christoph Lameter
2012-05-21 9:35 ` Glauber Costa
2012-05-23 16:17 ` JoonSoo Kim
2012-05-18 16:19 ` [RFC] Common code 11/12] Move freeing of kmem_cache structure to common code Christoph Lameter
2012-05-23 16:17 ` JoonSoo Kim
2012-05-18 16:19 ` [RFC] Common code 12/12] [slauob]: Get rid of __kmem_cache_destroy Christoph Lameter
2012-05-23 16:42 ` JoonSoo Kim
[not found] ` <20120518161931.570041085@linux.com>
2012-05-21 9:27 ` [RFC] Common code 08/12] slabs: list addition move to slab_common Glauber Costa
2012-05-22 16:22 ` JoonSoo Kim
2012-05-21 9:38 ` [RFC] Common code 00/12] Sl[auo]b: Common functionality V2 Glauber Costa
2012-05-21 13:51 ` Christoph Lameter
2012-05-21 14:15 ` Glauber Costa
2012-06-11 2:25 ` Alex Shi
2012-06-29 3:17 ` Alex Shi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120518161929.264565121@linux.com \
--to=cl@linux.com \
--cc=alex.shi@intel.com \
--cc=glommer@parallels.com \
--cc=js1304@gmail.com \
--cc=linux-mm@kvack.org \
--cc=mpm@selenic.com \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).