Skip to content

Commit 0a31bd5

Browse files
Christoph LameterLinus Torvalds
authored andcommitted
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: Christoph Lameter <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 5af6083 commit 0a31bd5

File tree

9 files changed

+23
-32
lines changed

9 files changed

+23
-32
lines changed

block/cfq-iosched.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,13 +2090,11 @@ static void cfq_slab_kill(void)
20902090

20912091
static int __init cfq_slab_setup(void)
20922092
{
2093-
cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2094-
NULL, NULL);
2093+
cfq_pool = KMEM_CACHE(cfq_queue, 0);
20952094
if (!cfq_pool)
20962095
goto fail;
20972096

2098-
cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2099-
sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2097+
cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
21002098
if (!cfq_ioc_pool)
21012099
goto fail;
21022100

fs/aio.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ static void aio_queue_work(struct kioctx *);
6868
*/
6969
static int __init aio_setup(void)
7070
{
71-
kiocb_cachep = kmem_cache_create("kiocb", sizeof(struct kiocb),
72-
0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
73-
kioctx_cachep = kmem_cache_create("kioctx", sizeof(struct kioctx),
74-
0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
71+
kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
72+
kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
7573

7674
aio_wq = create_workqueue("aio");
7775

fs/bio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,7 @@ static void __init biovec_init_slabs(void)
11931193

11941194
static int __init init_bio(void)
11951195
{
1196-
bio_slab = kmem_cache_create("bio", sizeof(struct bio), 0,
1197-
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1196+
bio_slab = KMEM_CACHE(bio, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
11981197

11991198
biovec_init_slabs();
12001199

fs/dcache.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,12 +2052,8 @@ static void __init dcache_init(unsigned long mempages)
20522052
* but it is probably not worth it because of the cache nature
20532053
* of the dcache.
20542054
*/
2055-
dentry_cache = kmem_cache_create("dentry_cache",
2056-
sizeof(struct dentry),
2057-
0,
2058-
(SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2059-
SLAB_MEM_SPREAD),
2060-
NULL, NULL);
2055+
dentry_cache = KMEM_CACHE(dentry,
2056+
SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
20612057

20622058
set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
20632059

include/linux/slab.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ unsigned int kmem_cache_size(struct kmem_cache *);
5757
const char *kmem_cache_name(struct kmem_cache *);
5858
int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
5959

60+
/*
61+
* Please use this macro to create slab caches. Simply specify the
62+
* name of the structure and maybe some flags that are listed above.
63+
*
64+
* The alignment of the struct determines object alignment. If you
65+
* f.e. add ____cacheline_aligned_in_smp to the struct declaration
66+
* then the objects will be properly aligned in SMP configurations.
67+
*/
68+
#define KMEM_CACHE(__struct, __flags) kmem_cache_create(#__struct,\
69+
sizeof(struct __struct), __alignof__(struct __struct),\
70+
(__flags), NULL, NULL)
71+
6072
#ifdef CONFIG_NUMA
6173
extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
6274
#else

kernel/delayacct.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ __setup("nodelayacct", delayacct_setup_disable);
3131

3232
void delayacct_init(void)
3333
{
34-
delayacct_cache = kmem_cache_create("delayacct_cache",
35-
sizeof(struct task_delay_info),
36-
0,
37-
SLAB_PANIC,
38-
NULL, NULL);
34+
delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC);
3935
delayacct_tsk_init(&init_task);
4036
}
4137

kernel/pid.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,5 @@ void __init pidmap_init(void)
412412
set_bit(0, init_pid_ns.pidmap[0].page);
413413
atomic_dec(&init_pid_ns.pidmap[0].nr_free);
414414

415-
pid_cachep = kmem_cache_create("pid", sizeof(struct pid),
416-
__alignof__(struct pid),
417-
SLAB_PANIC, NULL, NULL);
415+
pid_cachep = KMEM_CACHE(pid, SLAB_PANIC);
418416
}

kernel/signal.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,9 +2636,5 @@ __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
26362636

26372637
void __init signals_init(void)
26382638
{
2639-
sigqueue_cachep =
2640-
kmem_cache_create("sigqueue",
2641-
sizeof(struct sigqueue),
2642-
__alignof__(struct sigqueue),
2643-
SLAB_PANIC, NULL, NULL);
2639+
sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
26442640
}

kernel/taskstats.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,7 @@ void __init taskstats_init_early(void)
524524
{
525525
unsigned int i;
526526

527-
taskstats_cache = kmem_cache_create("taskstats_cache",
528-
sizeof(struct taskstats),
529-
0, SLAB_PANIC, NULL, NULL);
527+
taskstats_cache = KMEM_CACHE(taskstats, SLAB_PANIC);
530528
for_each_possible_cpu(i) {
531529
INIT_LIST_HEAD(&(per_cpu(listener_array, i).list));
532530
init_rwsem(&(per_cpu(listener_array, i).sem));

0 commit comments

Comments
 (0)