fs,mm: add kmem_cache_create_rcu()
Subsystems (3)
Christian Brauner
maintainerv2 cover fs,mm: add kmem_cache_create_rcu()
When a kmem cache is created with SLAB_TYPESAFE_BY_RCU the free pointer must be located outside of the object because we don't know what part of the memory can safely be overwritten as it may be needed to prevent object recycling.
That has the consequence that SLAB_TYPESAFE_BY_RCU may end up adding a new cacheline. This is the case for .e.g, struct file. After having it shrunk down by 40 bytes and having it fit in three cachelines we still have SLAB_TYPESAFE_BY_RCU adding a fourth cacheline because it needs to accomodate the free pointer and is hardware cacheline aligned.
I tried to find ways to rectify this as struct file is pretty much everywhere and having it use less memory is a good thing. So here's a proposal.
I was hoping to get something to this effect into v6.12.
If we really want to switch to a struct to pass kmem_cache parameters I can do the preparatory patch to convert all kmem_cache_create() and kmem_cache_create_usercopy() callers to use a struct for initialization of course. I can do this as a preparatory work or as follow-up work to this series. Thoughts?
Thanks!
Christian
---
---
base-commit: 766508e7e2c5075eb744cb29b8cef6fa835b0344
change-id: 20240827-work-kmem_cache-rcu-f97e35600cc6
Christian Brauner
maintainerPATCH v2 1/3 mm: remove unused root_cache argument
That argument is unused so remove it.
--- mm/slab_common.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
mm/slab_common.c +2 -3
@@ -204,8 +204,7 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,static struct kmem_cache *create_cache (const char *name,unsigned int object_size,unsigned int align,slab_flags_t flags,unsigned int useroffset, -unsigned int usersize,void (*ctor)(void *), -struct kmem_cache *root_cache) +unsigned int usersize,void (*ctor)(void *)) {struct kmem_cache *s;int err;
@@ -334,7 +333,7 @@ kmem_cache_create_usercopy(const char *name, s =create_cache (cache_name, size,calculate_alignment (flags, align, size), - flags, useroffset, usersize, ctor,NULL ); + flags, useroffset, usersize, ctor);if (IS_ERR (s)) { err =PTR_ERR (s);kfree_const (cache_name);
Signature · 1 line
2.45.2
Christian Brauner
maintainerPATCH v2 2/3 mm: add kmem_cache_create_rcu()
In reply to Christian BraunerWhen a kmem cache is created with SLAB_TYPESAFE_BY_RCU the free pointer must be located outside of the object because we don't know what part of the memory can safely be overwritten as it may be needed to prevent object recycling.
That has the consequence that SLAB_TYPESAFE_BY_RCU may end up adding a new cacheline. This is the case for .e.g, struct file. After having it shrunk down by 40 bytes and having it fit in three cachelines we still have SLAB_TYPESAFE_BY_RCU adding a fourth cacheline because it needs to accomodate the free pointer and is hardware cacheline aligned.
I tried to find ways to rectify this as struct file is pretty much everywhere and having it use less memory is a good thing. So here's a proposal.
--- include/linux/slab.h | 9 ++++ mm/slab.h | 1 + mm/slab_common.c | 133 ++++++++++++++++++++++++++++++++++++--------------- mm/slub.c | 17 ++++--- 4 files changed, 114 insertions(+), 46 deletions(-)
include/linux/slab.h +9 -0
@@ -212,6 +212,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +/* +* freeptr_t represents a SLUB freelist pointer, which might be encoded +* and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. +*/ +typedef struct {unsigned long v; }freeptr_t ; +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. *
@@ -242,6 +248,9 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,slab_flags_t flags, unsigned int useroffset, unsigned int usersize, void (*ctor)(void *)); +struct kmem_cache *kmem_cache_create_rcu (const char *name,unsigned int size, +unsigned int freeptr_offset, +slab_flags_t flags);void kmem_cache_destroy (struct kmem_cache *s);int kmem_cache_shrink (struct kmem_cache *s);
mm/slab.h +1 -0
@@ -261,6 +261,7 @@ struct kmem_cache {unsigned int object_size;/* Object size without metadata */ struct reciprocal_value reciprocal_size;unsigned int offset;/* Free pointer offset */ +unsigned int rcu_freeptr_offset;/* Specific free pointer requested */ #ifdef CONFIG_SLUB_CPU_PARTIAL /* Number of per cpu partial objects to keep around */ unsigned int cpu_partial;
mm/slab_common.c +94 -39
184 lines not on this page · raw message
mm/slub.c +10 -7
@@ -465,12 +465,6 @@ static struct workqueue_struct *flushwq;* Core slab cache functions *******************************************************************/ -/* -* freeptr_t represents a SLUB freelist pointer, which might be encoded -* and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. -*/ -typedef struct {unsigned long v; }freeptr_t ; -/* * Returns freelist pointer (ptr). With hardening, this is obfuscated * with an XOR of the address where the pointer is held and a per-cache
@@ -5144,6 +5138,12 @@ static void set_cpu_partial(struct kmem_cache *s)#endif } +/* Was a valid freeptr offset requested? */ +static inline bool has_freeptr_offset (const struct kmem_cache *s) +{ +return s->rcu_freeptr_offset !=UINT_MAX ; +} +/* * calculate_sizes() determines the order and the distribution of data within * a slab object.
@@ -5189,7 +5189,8 @@ static int calculate_sizes(struct kmem_cache *s)*/ s->inuse = size; -if ((flags & (SLAB_TYPESAFE_BY_RCU |SLAB_POISON )) || s->ctor || +if (((flags &SLAB_TYPESAFE_BY_RCU ) && !has_freeptr_offset (s)) || + (flags &SLAB_POISON ) || s->ctor || ((flags &SLAB_RED_ZONE ) && (s->object_size <sizeof (void *) ||slub_debug_orig_size (s)))) {/*
@@ -5210,6 +5211,8 @@ static int calculate_sizes(struct kmem_cache *s)*/ s->offset = size; size +=sizeof (void *); + }else if ((flags &SLAB_TYPESAFE_BY_RCU ) &&has_freeptr_offset (s)) { + s->offset = s->rcu_freeptr_offset; }else {/* * Store freelist pointer near middle of object to keep
Signature · 1 line
2.45.2
Christian Brauner
maintainerPATCH v2 3/3 fs: use kmem_cache_create_rcu()
In reply to Christian BraunerSwitch to the new kmem_cache_create_rcu() helper which allows us to use a custom free pointer offset avoiding the need to have an external free pointer which would grow struct file behind our backs.
--- fs/file_table.c | 6 +++--- include/linux/fs.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-)
fs/file_table.c +3 -3
@@ -514,9 +514,9 @@ EXPORT_SYMBOL(__fput_sync);void __init files_init (void ) { - filp_cachep =kmem_cache_create ("filp" ,sizeof (struct file ),0 , -SLAB_TYPESAFE_BY_RCU |SLAB_HWCACHE_ALIGN | -SLAB_PANIC |SLAB_ACCOUNT ,NULL ); + filp_cachep =kmem_cache_create_rcu ("filp" ,sizeof (struct file ), + offsetof(struct file , f_freeptr), +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT );percpu_counter_init (&nr_files,0 ,GFP_KERNEL ); }
include/linux/fs.h +1 -0
@@ -1057,6 +1057,7 @@ struct file {struct callback_head f_task_work;struct llist_node f_llist;struct file_ra_state f_ra; +freeptr_t f_freeptr; };/* --- cacheline 3 boundary (192 bytes) --- */ } __randomize_layout
Signature · 1 line
2.45.2
Christian Brauner
maintainerQuoted 15 lines · Christian Brauner
When a kmem cache is created with
SLAB_TYPESAFE_BY_RCUthe free pointer must be located outside of the object because we don't know what part of the memory can safely be overwritten as it may be needed to prevent object recycling.That has the consequence that
SLAB_TYPESAFE_BY_RCUmay end up adding a new cacheline. This is the case for .e.g, struct file. After having it shrunk down by 40 bytes and having it fit in three cachelines we still haveSLAB_TYPESAFE_BY_RCUadding a fourth cacheline because it needs to accomodate the free pointer and is hardware cacheline aligned.I tried to find ways to rectify this as struct file is pretty much everywhere and having it use less memory is a good thing. So here's a proposal.
Quoted 7 lines
I was hoping to get something to this effect into v6.12.
If we really want to switch to a struct to pass
kmem_cacheparameters I can do the preparatory patch to convert allkmem_cache_create()andkmem_cache_create_usercopy()callers to use a struct for initialization of course. I can do this as a preparatory work or as follow-up work to this series. Thoughts?
So one thing I can do is to add:
struct kmem_cache_args {
.freeptr_offset,
.useroffset,
.flags,
.name,
};accompanied by:
int kmem_create_cache(struct kmem_cache_args *args);
and then switch both the filp cache and Jens' io_kiocb cache over to use these two helpers. Then we can convert other callers one by one.
@Vlastimil, @Jens, @Linus what do you think?
Re: [PATCH v2 2/3] mm: add kmem_cache_create_rcu()
In reply to Christian Braunermm/slab_common.c struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, · line 208
Quoted 9 lines · Christian Brauner
static struct
kmem_cache*create_cache(const char *name,mm/slab_common.c +4
-3- unsigned int object_size, unsigned int align, - slab_flags_t flags, unsigned int useroffset, - unsigned int usersize, void (*ctor)(void *)) + unsigned int object_size, unsigned int freeptr_offset, + unsigned int align, slab_flags_t flags, + unsigned int useroffset, unsigned int usersize, + void (*ctor)(void *))
I actually think it would be nicer at this point to have a "kmem_cache create structure", and not 8 individual arguments.
But this approach looks fine to me even without that.
Linus
Quoted 24 lines · Christian Brauner
Quoted 15 lines · Christian Brauner
When a kmem cache is created with
SLAB_TYPESAFE_BY_RCUthe free pointer must be located outside of the object because we don't know what part of the memory can safely be overwritten as it may be needed to prevent object recycling.That has the consequence that
SLAB_TYPESAFE_BY_RCUmay end up adding a new cacheline. This is the case for .e.g, struct file. After having it shrunk down by 40 bytes and having it fit in three cachelines we still haveSLAB_TYPESAFE_BY_RCUadding a fourth cacheline because it needs to accomodate the free pointer and is hardware cacheline aligned.I tried to find ways to rectify this as struct file is pretty much everywhere and having it use less memory is a good thing. So here's a proposal.
Quoted 7 lines
I was hoping to get something to this effect into v6.12.
If we really want to switch to a struct to pass
kmem_cacheparameters I can do the preparatory patch to convert allkmem_cache_create()andkmem_cache_create_usercopy()callers to use a struct for initialization of course. I can do this as a preparatory work or as follow-up work to this series. Thoughts?
Quoted 8 lines
So one thing I can do is to add:
struct kmem_cache_args { .freeptr_offset, .useroffset, .flags, .name, };
Hm basically everyone uses name, size and some flags, so how about we leave those as direct parameters and args is for the rest, and in most cases would be NULL.
Quoted 3 lines
accompanied by:
int
kmem_create_cache(struct kmem_cache_args *args);
I think we can't reuse the name with different parameters as long the old one exists?
Quoted 4 lines
and then switch both the filp cache and Jens'
io_kiocbcache over to use these two helpers. Then we can convert other callers one by one.@Vlastimil, @Jens, @Linus what do you think?
In the other thread you said it's best to leave such refactoring to maintainers and I agree and don't ask you to do the cleanup in order to get what you need (and we don't need to rush it either).
Re: [PATCH v2 2/3] mm: add kmem_cache_create_rcu()
In reply to Christian BraunerQuoted 11 lines · Christian Brauner
When a kmem cache is created with
SLAB_TYPESAFE_BY_RCUthe free pointer must be located outside of the object because we don't know what part of the memory can safely be overwritten as it may be needed to prevent object recycling.That has the consequence that
SLAB_TYPESAFE_BY_RCUmay end up adding a new cacheline. This is the case for .e.g, struct file. After having it shrunk down by 40 bytes and having it fit in three cachelines we still haveSLAB_TYPESAFE_BY_RCUadding a fourth cacheline because it needs to accomodate the free pointer and is hardware cacheline aligned.
Quoted 5 lines
I tried to find ways to rectify this as struct file is pretty much everywhere and having it use less memory is a good thing. So here's a proposal.
Signed-off-by Christian Brauner <[email protected]>
So logistically patch 3 needs stuff in the vfs tree and having 1+2 in slab tree and 3 in vfs that depends on 1+2 elsewhere is infeasible, so it will be easiest for whole series to be in vfs, right?
Quoted 39 lines
--- include/linux/slab.h | 9 ++++ mm/slab.h | 1 + mm/slab_common.c | 133 ++++++++++++++++++++++++++++++++++++--------------- mm/slub.c | 17 ++++--- 4 files changed, 114 insertions(+), 46 deletions(-)include/linux/slab.h +9
-0@@ -212,6 +212,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +/* +* freeptr_t represents a SLUB freelist pointer, which might be encoded +* and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. +*/ +typedef struct {unsigned long v; }freeptr_t ; +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. * @@ -242,6 +248,9 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,slab_flags_t flags, unsigned int useroffset, unsigned int usersize, void (*ctor)(void *)); +struct kmem_cache *kmem_cache_create_rcu (const char *name,unsigned int size, +unsigned int freeptr_offset, +slab_flags_t flags);void kmem_cache_destroy (struct kmem_cache *s);int kmem_cache_shrink (struct kmem_cache *s);mm/slab.h +0
-0
mm/slab.h struct kmem_cache { · line 264
Quoted 4 lines
mm/slab.h +1
-0@@ -261,6 +261,7 @@ struct kmem_cache {unsigned int object_size;/* Object size without metadata */ struct reciprocal_value reciprocal_size;unsigned int offset;/* Free pointer offset */ +unsigned int rcu_freeptr_offset;/* Specific free pointer requested */
More precisely something like:
Specific offset requested (if not UINT_MAX)
?
Quoted 22 lines
mm/slab.h +0
-0#ifdef CONFIG_SLUB_CPU_PARTIAL /* Number of per cpu partial objects to keep around */ unsigned int cpu_partial;mm/slab_common.c +4
-3@@ -202,9 +202,10 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, }static struct kmem_cache *create_cache (const char *name, -unsigned int object_size,unsigned int align, -slab_flags_t flags,unsigned int useroffset, -unsigned int usersize,void (*ctor)(void *)) +unsigned int object_size,unsigned int freeptr_offset, +unsigned int align,slab_flags_t flags, +unsigned int useroffset,unsigned int usersize, +void (*ctor)(void *)) {struct kmem_cache *s;int err;
mm/slab_common.c static struct kmem_cache *create_cache(const char *name, · line 217
Quoted 5 lines
mm/slab_common.c +2
-0@@ -212,6 +213,12 @@ static struct kmem_cache *create_cache(const char *name,if (WARN_ON (useroffset + usersize > object_size)) useroffset = usersize =0 ; + err = -EINVAL ; + if (freeptr_offset < UINT_MAX &&
freeptr_offset != UINT_MAX to be more obvious and match has_freeptr_offset() ?
mm/slab_common.c static struct kmem_cache *create_cache(const char *name, · line 219
Quoted 2 lines
mm/slab_common.c +2
-0+ (freeptr_offset >= object_size || + (freeptr_offset && !(flags & SLAB_TYPESAFE_BY_RCU))))
and here drop the "freeptr_offset &&" as zero is a valid value
instead we could want alignment to sizeof(freeptr_t) if we were paranoid?
mm/slab_common.c static struct kmem_cache *create_cache(const char *name, · line 220
Quoted 1 line
mm/slab_common.c +1
-0+ goto out;
The rest seems good to me now.
Christian Brauner
maintainerRe: [PATCH v2 2/3] mm: add kmem_cache_create_rcu()
Quoted 16 lines · Vlastimil Babka
Quoted 15 lines · Christian Brauner
When a kmem cache is created with
SLAB_TYPESAFE_BY_RCUthe free pointer must be located outside of the object because we don't know what part of the memory can safely be overwritten as it may be needed to prevent object recycling.That has the consequence that
SLAB_TYPESAFE_BY_RCUmay end up adding a new cacheline. This is the case for .e.g, struct file. After having it shrunk down by 40 bytes and having it fit in three cachelines we still haveSLAB_TYPESAFE_BY_RCUadding a fourth cacheline because it needs to accomodate the free pointer and is hardware cacheline aligned.I tried to find ways to rectify this as struct file is pretty much everywhere and having it use less memory is a good thing. So here's a proposal.
Quoted 5 lines
Quoted 1 line
Signed-off-by Christian Brauner <[email protected]>So logistically patch 3 needs stuff in the vfs tree and having 1+2 in slab tree and 3 in vfs that depends on 1+2 elsewhere is infeasible, so it will be easiest for whole series to be in vfs, right?
Yeah, that's fine by me.
Quoted 47 lines
Quoted 40 lines
--- include/linux/slab.h | 9 ++++ mm/slab.h | 1 + mm/slab_common.c | 133 ++++++++++++++++++++++++++++++++++++--------------- mm/slub.c | 17 ++++--- 4 files changed, 114 insertions(+), 46 deletions(-)include/linux/slab.h +9
-0@@ -212,6 +212,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +/* +* freeptr_t represents a SLUB freelist pointer, which might be encoded +* and not dereferenceable if CONFIG_SLAB_FREELIST_HARDENED is enabled. +*/ +typedef struct {unsigned long v; }freeptr_t ; +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. * @@ -242,6 +248,9 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,slab_flags_t flags, unsigned int useroffset, unsigned int usersize, void (*ctor)(void *)); +struct kmem_cache *kmem_cache_create_rcu (const char *name,unsigned int size, +unsigned int freeptr_offset, +slab_flags_t flags);void kmem_cache_destroy (struct kmem_cache *s);int kmem_cache_shrink (struct kmem_cache *s);mm/slab.h +0
-0mm/slab.h struct kmem_cache { · line 264
Quoted 4 lines
mm/slab.h +1
-0@@ -261,6 +261,7 @@ struct kmem_cache {unsigned int object_size;/* Object size without metadata */ struct reciprocal_value reciprocal_size;unsigned int offset;/* Free pointer offset */ +unsigned int rcu_freeptr_offset;/* Specific free pointer requested */ More precisely something like:
Quoted 2 lines
Specific offset requested (if not
UINT_MAX)
Yep, added that.
Quoted 28 lines
?
Quoted 25 lines
mm/slab.h +0
-0#ifdef CONFIG_SLUB_CPU_PARTIAL /* Number of per cpu partial objects to keep around */ unsigned int cpu_partial;mm/slab_common.c +4
-3@@ -202,9 +202,10 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, }static struct kmem_cache *create_cache (const char *name, -unsigned int object_size,unsigned int align, -slab_flags_t flags,unsigned int useroffset, -unsigned int usersize,void (*ctor)(void *)) +unsigned int object_size,unsigned int freeptr_offset, +unsigned int align,slab_flags_t flags, +unsigned int useroffset,unsigned int usersize, +void (*ctor)(void *)) {struct kmem_cache *s;int err;@@ -212,6 +213,12 @@ static struct kmem_cache *create_cache(const char *name,if (WARN_ON (useroffset + usersize > object_size)) useroffset = usersize =0 ;
Quoted 4 lines
mm/slab_common.c static struct kmem_cache *create_cache(const char *name, · line 217
Quoted 2 lines
mm/slab_common.c +2
-0+ err = -EINVAL ; + if (freeptr_offset < UINT_MAX &&
freeptr_offset!=UINT_MAXto be more obvious and matchhas_freeptr_offset()?
Done.
Quoted 5 lines
mm/slab_common.c static struct kmem_cache *create_cache(const char *name, · line 219
Quoted 3 lines
mm/slab_common.c +2
-0+ (freeptr_offset >= object_size || + (freeptr_offset && !(flags & SLAB_TYPESAFE_BY_RCU))))and here drop the "
freeptr_offset&&" as zero is a valid value
Yes, thank you.
Quoted 2 lines
instead we could want alignment to
sizeof(freeptr_t)if we were paranoid?
Added a check for that.
Quoted 4 lines
mm/slab_common.c static struct kmem_cache *create_cache(const char *name, · line 220
Quoted 2 lines
mm/slab_common.c +1
-0+ goto out;The rest seems good to me now.
Thanks for the review! v3 incoming
Christian Brauner
maintainerQuoted 5 lines
Quoted 1 line
@Vlastimil, @Jens, @Linus what do you think?
In the other thread you said it's best to leave such refactoring to maintainers and I agree and don't ask you to do the cleanup in order to get what you need (and we don't need to rush it either).
I mainly didn't want to wait for that huge refactor to be done before we get a workable solution for v6.12 since the patch isn't all that huge.
But I'm not shying away from cleanup work I'm the cause of.
So I'm happy to do it for v6.13 as a follow-up but I also don't want to steal this away from you because it'll probably be a fun task to get to port everything to struct kmem_cache_args and clean that all up.
Quoted 6 lines · Christian Brauner
Quoted 5 lines
Quoted 1 line
@Vlastimil, @Jens, @Linus what do you think?
In the other thread you said it's best to leave such refactoring to maintainers and I agree and don't ask you to do the cleanup in order to get what you need (and we don't need to rush it either).
Quoted 8 lines
I mainly didn't want to wait for that huge refactor to be done before we get a workable solution for v6.12 since the patch isn't all that huge.
But I'm not shying away from cleanup work I'm the cause of.
So I'm happy to do it for v6.13 as a follow-up but I also don't want to steal this away from you because it'll probably be a fun task to get to port everything to struct
kmem_cache_argsand clean that all up.
So if you want, be my guest :) Yeah could be a fun task, but I'll probably be too busy for a while now.