Skip to content

Commit b2e7456

Browse files
braunertehcaster
authored andcommitted
slab: create kmem_cache_create() compatibility layer
Use _Generic() to create a compatibility layer that type switches on the third argument to either call __kmem_cache_create() or __kmem_cache_create_args(). If NULL is passed for the struct kmem_cache_args argument use default args making porting for callers that don't care about additional arguments easy. Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Jens Axboe <[email protected]> Signed-off-by: Christian Brauner <[email protected]> Reviewed-by: Mike Rapoport (Microsoft) <[email protected]> Reviewed-by: Roman Gushchin <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent 199cd13 commit b2e7456

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

include/linux/slab.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,10 @@ struct kmem_cache *__kmem_cache_create_args(const char *name,
262262
unsigned int object_size,
263263
struct kmem_cache_args *args,
264264
slab_flags_t flags);
265-
struct kmem_cache *kmem_cache_create(const char *name, unsigned int size,
266-
unsigned int align, slab_flags_t flags,
267-
void (*ctor)(void *));
265+
266+
struct kmem_cache *__kmem_cache_create(const char *name, unsigned int size,
267+
unsigned int align, slab_flags_t flags,
268+
void (*ctor)(void *));
268269
struct kmem_cache *kmem_cache_create_usercopy(const char *name,
269270
unsigned int size, unsigned int align,
270271
slab_flags_t flags,
@@ -273,6 +274,28 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,
273274
struct kmem_cache *kmem_cache_create_rcu(const char *name, unsigned int size,
274275
unsigned int freeptr_offset,
275276
slab_flags_t flags);
277+
278+
/* If NULL is passed for @args, use this variant with default arguments. */
279+
static inline struct kmem_cache *
280+
__kmem_cache_default_args(const char *name, unsigned int size,
281+
struct kmem_cache_args *args,
282+
slab_flags_t flags)
283+
{
284+
struct kmem_cache_args kmem_default_args = {};
285+
286+
/* Make sure we don't get passed garbage. */
287+
if (WARN_ON_ONCE(args))
288+
return ERR_PTR(-EINVAL);
289+
290+
return __kmem_cache_create_args(name, size, &kmem_default_args, flags);
291+
}
292+
293+
#define kmem_cache_create(__name, __object_size, __args, ...) \
294+
_Generic((__args), \
295+
struct kmem_cache_args *: __kmem_cache_create_args, \
296+
void *: __kmem_cache_default_args, \
297+
default: __kmem_cache_create)(__name, __object_size, __args, __VA_ARGS__)
298+
276299
void kmem_cache_destroy(struct kmem_cache *s);
277300
int kmem_cache_shrink(struct kmem_cache *s);
278301

mm/slab_common.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ kmem_cache_create_usercopy(const char *name, unsigned int size,
383383
EXPORT_SYMBOL(kmem_cache_create_usercopy);
384384

385385
/**
386-
* kmem_cache_create - Create a cache.
386+
* __kmem_cache_create - Create a cache.
387387
* @name: A string which is used in /proc/slabinfo to identify this cache.
388388
* @size: The size of objects to be created in this cache.
389389
* @align: The required alignment for the objects.
@@ -407,9 +407,9 @@ EXPORT_SYMBOL(kmem_cache_create_usercopy);
407407
*
408408
* Return: a pointer to the cache on success, NULL on failure.
409409
*/
410-
struct kmem_cache *
411-
kmem_cache_create(const char *name, unsigned int size, unsigned int align,
412-
slab_flags_t flags, void (*ctor)(void *))
410+
struct kmem_cache *__kmem_cache_create(const char *name, unsigned int size,
411+
unsigned int align, slab_flags_t flags,
412+
void (*ctor)(void *))
413413
{
414414
struct kmem_cache_args kmem_args = {
415415
.align = align,
@@ -418,7 +418,7 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align,
418418

419419
return __kmem_cache_create_args(name, size, &kmem_args, flags);
420420
}
421-
EXPORT_SYMBOL(kmem_cache_create);
421+
EXPORT_SYMBOL(__kmem_cache_create);
422422

423423
/**
424424
* kmem_cache_create_rcu - Create a SLAB_TYPESAFE_BY_RCU cache.

0 commit comments

Comments
 (0)