Skip to content

Commit 3dbe2ba

Browse files
braunertehcaster
authored andcommitted
slab: pass struct kmem_cache_args to do_kmem_cache_create()
and initialize most things in do_kmem_cache_create(). In a follow-up patch we'll remove rcu_freeptr_offset from struct kmem_cache. Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Jens Axboe <[email protected]> Reviewed-by: Mike Rapoport (Microsoft) <[email protected]> Reviewed-by: Vlastimil Babka <[email protected]> Signed-off-by: Christian Brauner <[email protected]> Reviewed-by: Roman Gushchin <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent fc0eac5 commit 3dbe2ba

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

mm/slab.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ kmalloc_slab(size_t size, kmem_buckets *b, gfp_t flags, unsigned long caller)
424424
gfp_t kmalloc_fix_flags(gfp_t flags);
425425

426426
/* Functions provided by the slab allocators */
427-
int do_kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
427+
int do_kmem_cache_create(struct kmem_cache *s, const char *name,
428+
unsigned int size, struct kmem_cache_args *args,
429+
slab_flags_t flags);
428430

429431
void __init kmem_cache_init(void);
430432
extern void create_boot_cache(struct kmem_cache *, const char *name,

mm/slab_common.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,7 @@ static struct kmem_cache *create_cache(const char *name,
224224
s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
225225
if (!s)
226226
goto out;
227-
228-
s->name = name;
229-
s->size = s->object_size = object_size;
230-
if (args->use_freeptr_offset)
231-
s->rcu_freeptr_offset = args->freeptr_offset;
232-
else
233-
s->rcu_freeptr_offset = UINT_MAX;
234-
s->align = args->align;
235-
s->ctor = args->ctor;
236-
#ifdef CONFIG_HARDENED_USERCOPY
237-
s->useroffset = args->useroffset;
238-
s->usersize = args->usersize;
239-
#endif
240-
err = do_kmem_cache_create(s, flags);
227+
err = do_kmem_cache_create(s, name, object_size, args, flags);
241228
if (err)
242229
goto out_free_cache;
243230

@@ -788,9 +775,7 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name,
788775
{
789776
int err;
790777
unsigned int align = ARCH_KMALLOC_MINALIGN;
791-
792-
s->name = name;
793-
s->size = s->object_size = size;
778+
struct kmem_cache_args kmem_args = {};
794779

795780
/*
796781
* kmalloc caches guarantee alignment of at least the largest
@@ -799,14 +784,14 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name,
799784
*/
800785
if (flags & SLAB_KMALLOC)
801786
align = max(align, 1U << (ffs(size) - 1));
802-
s->align = calculate_alignment(flags, align, size);
787+
kmem_args.align = calculate_alignment(flags, align, size);
803788

804789
#ifdef CONFIG_HARDENED_USERCOPY
805-
s->useroffset = useroffset;
806-
s->usersize = usersize;
790+
kmem_args.useroffset = useroffset;
791+
kmem_args.usersize = usersize;
807792
#endif
808793

809-
err = do_kmem_cache_create(s, flags);
794+
err = do_kmem_cache_create(s, name, size, &kmem_args, flags);
810795

811796
if (err)
812797
panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",

mm/slub.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5843,14 +5843,29 @@ __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
58435843
return s;
58445844
}
58455845

5846-
int do_kmem_cache_create(struct kmem_cache *s, slab_flags_t flags)
5846+
int do_kmem_cache_create(struct kmem_cache *s, const char *name,
5847+
unsigned int size, struct kmem_cache_args *args,
5848+
slab_flags_t flags)
58475849
{
58485850
int err = -EINVAL;
58495851

5852+
s->name = name;
5853+
s->size = s->object_size = size;
5854+
58505855
s->flags = kmem_cache_flags(flags, s->name);
58515856
#ifdef CONFIG_SLAB_FREELIST_HARDENED
58525857
s->random = get_random_long();
58535858
#endif
5859+
if (args->use_freeptr_offset)
5860+
s->rcu_freeptr_offset = args->freeptr_offset;
5861+
else
5862+
s->rcu_freeptr_offset = UINT_MAX;
5863+
s->align = args->align;
5864+
s->ctor = args->ctor;
5865+
#ifdef CONFIG_HARDENED_USERCOPY
5866+
s->useroffset = args->useroffset;
5867+
s->usersize = args->usersize;
5868+
#endif
58545869

58555870
if (!calculate_sizes(s))
58565871
goto out;

0 commit comments

Comments
 (0)