Commit 0a31bd5f authored by Christoph Lameter's avatar Christoph Lameter Committed by Linus Torvalds

KMEM_CACHE(): simplify slab cache creation

This patch provides a new macro

KMEM_CACHE(<struct>, <flags>)

to simplify slab creation. KMEM_CACHE creates a slab with the name of the
struct, with the size of the struct and with the alignment of the struct.
Additional slab flags may be specified if necessary.

Example

struct test_slab {
	int a,b,c;
	struct list_head;
} __cacheline_aligned_in_smp;

test_slab_cache = KMEM_CACHE(test_slab, SLAB_PANIC)

will create a new slab named "test_slab" of the size sizeof(struct
test_slab) and aligned to the alignment of test slab.  If it fails then we
panic.
Signed-off-by: default avatarChristoph Lameter <clameter@sgi.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 5af60839
...@@ -2090,13 +2090,11 @@ static void cfq_slab_kill(void) ...@@ -2090,13 +2090,11 @@ static void cfq_slab_kill(void)
static int __init cfq_slab_setup(void) static int __init cfq_slab_setup(void)
{ {
cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0, cfq_pool = KMEM_CACHE(cfq_queue, 0);
NULL, NULL);
if (!cfq_pool) if (!cfq_pool)
goto fail; goto fail;
cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool", cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
if (!cfq_ioc_pool) if (!cfq_ioc_pool)
goto fail; goto fail;
......
...@@ -68,10 +68,8 @@ static void aio_queue_work(struct kioctx *); ...@@ -68,10 +68,8 @@ static void aio_queue_work(struct kioctx *);
*/ */
static int __init aio_setup(void) static int __init aio_setup(void)
{ {
kiocb_cachep = kmem_cache_create("kiocb", sizeof(struct kiocb), kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
kioctx_cachep = kmem_cache_create("kioctx", sizeof(struct kioctx),
0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
aio_wq = create_workqueue("aio"); aio_wq = create_workqueue("aio");
......
...@@ -1193,8 +1193,7 @@ static void __init biovec_init_slabs(void) ...@@ -1193,8 +1193,7 @@ static void __init biovec_init_slabs(void)
static int __init init_bio(void) static int __init init_bio(void)
{ {
bio_slab = kmem_cache_create("bio", sizeof(struct bio), 0, bio_slab = KMEM_CACHE(bio, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
biovec_init_slabs(); biovec_init_slabs();
......
...@@ -2052,12 +2052,8 @@ static void __init dcache_init(unsigned long mempages) ...@@ -2052,12 +2052,8 @@ static void __init dcache_init(unsigned long mempages)
* but it is probably not worth it because of the cache nature * but it is probably not worth it because of the cache nature
* of the dcache. * of the dcache.
*/ */
dentry_cache = kmem_cache_create("dentry_cache", dentry_cache = KMEM_CACHE(dentry,
sizeof(struct dentry), SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
0,
(SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
SLAB_MEM_SPREAD),
NULL, NULL);
set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory); set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
......
...@@ -57,6 +57,18 @@ unsigned int kmem_cache_size(struct kmem_cache *); ...@@ -57,6 +57,18 @@ unsigned int kmem_cache_size(struct kmem_cache *);
const char *kmem_cache_name(struct kmem_cache *); const char *kmem_cache_name(struct kmem_cache *);
int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr); int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
/*
* Please use this macro to create slab caches. Simply specify the
* name of the structure and maybe some flags that are listed above.
*
* The alignment of the struct determines object alignment. If you
* f.e. add ____cacheline_aligned_in_smp to the struct declaration
* then the objects will be properly aligned in SMP configurations.
*/
#define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
sizeof(struct __struct), __alignof__(struct __struct),\
(__flags), NULL, NULL)
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node); extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
#else #else
......
...@@ -31,11 +31,7 @@ __setup("nodelayacct", delayacct_setup_disable); ...@@ -31,11 +31,7 @@ __setup("nodelayacct", delayacct_setup_disable);
void delayacct_init(void) void delayacct_init(void)
{ {
delayacct_cache = kmem_cache_create("delayacct_cache", delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC);
sizeof(struct task_delay_info),
0,
SLAB_PANIC,
NULL, NULL);
delayacct_tsk_init(&init_task); delayacct_tsk_init(&init_task);
} }
......
...@@ -412,7 +412,5 @@ void __init pidmap_init(void) ...@@ -412,7 +412,5 @@ void __init pidmap_init(void)
set_bit(0, init_pid_ns.pidmap[0].page); set_bit(0, init_pid_ns.pidmap[0].page);
atomic_dec(&init_pid_ns.pidmap[0].nr_free); atomic_dec(&init_pid_ns.pidmap[0].nr_free);
pid_cachep = kmem_cache_create("pid", sizeof(struct pid), pid_cachep = KMEM_CACHE(pid, SLAB_PANIC);
__alignof__(struct pid),
SLAB_PANIC, NULL, NULL);
} }
...@@ -2636,9 +2636,5 @@ __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma) ...@@ -2636,9 +2636,5 @@ __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
void __init signals_init(void) void __init signals_init(void)
{ {
sigqueue_cachep = sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
kmem_cache_create("sigqueue",
sizeof(struct sigqueue),
__alignof__(struct sigqueue),
SLAB_PANIC, NULL, NULL);
} }
...@@ -524,9 +524,7 @@ void __init taskstats_init_early(void) ...@@ -524,9 +524,7 @@ void __init taskstats_init_early(void)
{ {
unsigned int i; unsigned int i;
taskstats_cache = kmem_cache_create("taskstats_cache", taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
sizeof(struct taskstats),
0, SLAB_PANIC, NULL, NULL);
for_each_possible_cpu(i) { for_each_possible_cpu(i) {
INIT_LIST_HEAD(&(per_cpu(listener_array, i).list)); INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
init_rwsem(&(per_cpu(listener_array, i).sem)); init_rwsem(&(per_cpu(listener_array, i).sem));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment