Only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
Subsystems (2)
cover Only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
While SLAB_OBJ_EXT_IN_OBJ allows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.
As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
So let's take a conservative approach and restrict SLAB_OBJ_EXT_IN_OBJ to caches that are already unmergeable for other reasons.
Ideally this should be part of the slab/for-7.0/obj_metadata branch, but due to a conflict with slab/for-7.0/sheaves, I based it on slab/for-next (be36abd97e52).
Patch 1 factors out mergeability logic to slab_args_unmergeable(). This is used to determine mergeability before the cache is created.
Patch 2 uses the new function to allow SLAB_OBJ_EXT_IN_OBJ only when merging is not allowed for other reasons.
Harry Yoo (2):
mm/slab: factor out slab_args_unmergeable()
mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
mm/slab.h | 1 + mm/slab_common.c | 27 +++++++++++++++++---------- mm/slub.c | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 mm/slab: factor out slab_args_unmergeable()
slab_mergeable() determines whether a slab cache can be merged, but it should not be used when the cache is not fully created yet.
Extract the pre-cache-creation mergeability checks into slab_args_unmergeable(), which evaluates kmem_cache_args, slab flags, and slab_nomerge to determine if a cache will be mergeable before it is created.
--- mm/slab_common.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)
mm/slab_common.c +18 -10
@@ -174,24 +174,32 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static struct kmem_cache *find_mergeable (unsigned int size,slab_flags_t flags, -const char *name,struct kmem_cache_args *args) +static bool slab_args_unmergeable (struct kmem_cache_args *args, +slab_flags_t flags) { -struct kmem_cache *s; -unsigned int align; -if (slab_nomerge) -return NULL ; +return true;if (args->ctor) -return NULL ; +return true;if (IS_ENABLED (CONFIG_HARDENED_USERCOPY ) && args->usersize) -return NULL ; - - flags =kmem_cache_flags (flags, name); +return true;if (flags &SLAB_NEVER_MERGE ) +return true; + +return false; +} + +static struct kmem_cache *find_mergeable (unsigned int size,slab_flags_t flags, +const char *name,struct kmem_cache_args *args) +{ +struct kmem_cache *s; +unsigned int align; + + flags =kmem_cache_flags (flags, name); +if (slab_args_unmergeable (args, flags))return NULL ; size =ALIGN (size,sizeof (void *));
Signature · 1 line
2.43.0
Re: [PATCH V1 1/2] mm/slab: factor out slab_args_unmergeable()
Quoted 22 lines · Harry Yoo
slab_mergeable()determines whether a slab cache can be merged, but it should not be used when the cache is not fully created yet.Extract the pre-cache-creation mergeability checks into
slab_args_unmergeable(), which evaluateskmem_cache_args, slab flags, andslab_nomergeto determine if a cache will be mergeable before it is created.Signed-off-by Harry Yoo <[email protected]>--- mm/slab_common.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)mm/slab_common.c +0
-1@@ -174,24 +174,32 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static struct kmem_cache *find_mergeable(unsigned int size, slab_flags_t flags,
mm/slab_common.c int slab_unmergeable(struct kmem_cache *s) · line 181
Quoted 10 lines
mm/slab_common.c +3
-5- const char *name, struct kmem_cache_args *args) +static bool slab_args_unmergeable (struct kmem_cache_args *args, +slab_flags_t flags) { - struct kmem_cache *s; -unsigned int align; -if (slab_nomerge) -return NULL ; +return true;
I think this still logically belongs to find_mergeable() and not here, so moved there. Agreed?
Quoted 27 lines
mm/slab_common.c +15
-4if (args->ctor) -return NULL ; +return true;if (IS_ENABLED (CONFIG_HARDENED_USERCOPY ) && args->usersize) -return NULL ; - - flags =kmem_cache_flags (flags, name); +return true;if (flags &SLAB_NEVER_MERGE ) +return true; + +return false; +} + +static struct kmem_cache *find_mergeable (unsigned int size,slab_flags_t flags, +const char *name,struct kmem_cache_args *args) +{ +struct kmem_cache *s; +unsigned int align; + + flags =kmem_cache_flags (flags, name); +if (slab_args_unmergeable (args, flags))return NULL ; size =ALIGN (size,sizeof (void *));
Re: [PATCH V1 1/2] mm/slab: factor out slab_args_unmergeable()
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · Harry Yoo
slab_mergeable()determines whether a slab cache can be merged, but it should not be used when the cache is not fully created yet.Extract the pre-cache-creation mergeability checks into
slab_args_unmergeable(), which evaluateskmem_cache_args, slab flags, andslab_nomergeto determine if a cache will be mergeable before it is created.Signed-off-by Harry Yoo <[email protected]>--- mm/slab_common.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)mm/slab_common.c +0
-0@@ -174,24 +174,32 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; }
Quoted 14 lines
mm/slab_common.c int slab_unmergeable(struct kmem_cache *s) · line 181
Quoted 11 lines
mm/slab_common.c +3
-6-static struct kmem_cache *find_mergeable (unsigned int size,slab_flags_t flags, -const char *name,struct kmem_cache_args *args) +static bool slab_args_unmergeable (struct kmem_cache_args *args, +slab_flags_t flags) { -struct kmem_cache *s; -unsigned int align; -if (slab_nomerge) -return NULL ; +return true;I think this still logically belongs to
find_mergeable()and not here, so moved there. Agreed?
But then patch 2/2 should be modified to check slab_nomerge then?
And even slab_unmergeable() does slab_nomerge check, so perhaps it's more consistent to do it in slab_args_unmergeable().
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH V1 1/2] mm/slab: factor out slab_args_unmergeable()
Quoted 38 lines · Harry Yoo
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · Harry Yoo
slab_mergeable()determines whether a slab cache can be merged, but it should not be used when the cache is not fully created yet.Extract the pre-cache-creation mergeability checks into
slab_args_unmergeable(), which evaluateskmem_cache_args, slab flags, andslab_nomergeto determine if a cache will be mergeable before it is created.Signed-off-by Harry Yoo <[email protected]>--- mm/slab_common.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)mm/slab_common.c +0
-0@@ -174,24 +174,32 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; }Quoted 14 lines
mm/slab_common.c int slab_unmergeable(struct kmem_cache *s) · line 181
Quoted 11 lines
mm/slab_common.c +3
-6-static struct kmem_cache *find_mergeable (unsigned int size,slab_flags_t flags, -const char *name,struct kmem_cache_args *args) +static bool slab_args_unmergeable (struct kmem_cache_args *args, +slab_flags_t flags) { -struct kmem_cache *s; -unsigned int align; -if (slab_nomerge) -return NULL ; +return true;I think this still logically belongs to
find_mergeable()and not here, so moved there. Agreed?
Quoted 1 line
But then patch 2/2 should be modified to check
slab_nomergethen?
Yeah I would do that as well,
Quoted 2 lines
And even
slab_unmergeable()doesslab_nomergecheck, so perhaps it's more consistent to do it inslab_args_unmergeable().
But this is true so I'll leave both as you did, thanks!
PATCH 2/2 mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
In reply to Harry YooWhile SLAB_OBJ_EXT_IN_OBJ allows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.
As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
Restrict SLAB_OBJ_EXT_IN_OBJ to caches that are already unmergeable for other reasons (e.g., those with constructors or SLAB_TYPESAFE_BY_RCU).
--- mm/slab.h | 1 + mm/slab_common.c | 3 +-- mm/slub.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-)
mm/slab.h +1 -0
@@ -388,6 +388,7 @@ extern void create_boot_cache(struct kmem_cache *, const char *name, unsigned int useroffset, unsigned int usersize);int slab_unmergeable (struct kmem_cache *s); +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags);slab_flags_t kmem_cache_flags (slab_flags_t flags,const char *name);
mm/slab_common.c +1 -2
@@ -174,8 +174,7 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static bool slab_args_unmergeable (struct kmem_cache_args *args, -slab_flags_t flags) +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags) {if (slab_nomerge)return true;
mm/slub.c +2 -1
@@ -7676,7 +7676,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ aligned_size =ALIGN (size, s->align);#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) -if (aligned_size - size >=sizeof (struct slabobj_ext )) +if (slab_args_unmergeable (args, s->flags) && + (aligned_size - size >=sizeof (struct slabobj_ext ))) s->flags |=SLAB_OBJ_EXT_IN_OBJ ;#endif size = aligned_size;
Signature · 1 line
2.43.0
Hao Li
reviewerRe: [PATCH V1 2/2] mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
Quoted 50 lines · Harry Yoo
While
SLAB_OBJ_EXT_IN_OBJallows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
Restrict
SLAB_OBJ_EXT_IN_OBJto caches that are already unmergeable for other reasons (e.g., those with constructors orSLAB_TYPESAFE_BY_RCU).Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Harry Yoo <[email protected]>--- mm/slab.h | 1 + mm/slab_common.c | 3 +-- mm/slub.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-)mm/slab.h +1
-0@@ -388,6 +388,7 @@ extern void create_boot_cache(struct kmem_cache *, const char *name, unsigned int useroffset, unsigned int usersize);int slab_unmergeable (struct kmem_cache *s); +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags);slab_flags_t kmem_cache_flags (slab_flags_t flags,const char *name);mm/slab_common.c +1
-2@@ -174,8 +174,7 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static bool slab_args_unmergeable (struct kmem_cache_args *args, -slab_flags_t flags) +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags) {if (slab_nomerge)return true;mm/slub.c +0
-0
mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 7681
Quoted 7 lines
mm/slub.c +2
-1@@ -7676,7 +7676,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ aligned_size =ALIGN (size, s->align);#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) -if (aligned_size - size >=sizeof (struct slabobj_ext )) +if (slab_args_unmergeable (args, s->flags) && + (aligned_size - size >=sizeof (struct slabobj_ext ))) s->flags |=SLAB_OBJ_EXT_IN_OBJ ;
Hi Harry,
This patch looks reasonable to me. I just noticed a minor point that I wanted to bring up:
It seems a bit self-referential that SLAB_NEVER_MERGE already includes SLAB_OBJ_EXT_IN_OBJ, but we're using SLAB_NEVER_MERGE to decide whether to set SLAB_OBJ_EXT_IN_OBJ.
Do you think it might be helpful to add a comment here for better clarity?
Signature · 2 lines
Thanks, Hao
Quoted 5 lines
mm/slub.c +0
-0#endif size = aligned_size;Signature · 1 line
2.43.0
Re: [PATCH V1 2/2] mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
Quoted 61 lines · Hao Li
Quoted 50 lines · Harry Yoo
While
SLAB_OBJ_EXT_IN_OBJallows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
Restrict
SLAB_OBJ_EXT_IN_OBJto caches that are already unmergeable for other reasons (e.g., those with constructors orSLAB_TYPESAFE_BY_RCU).Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Harry Yoo <[email protected]>--- mm/slab.h | 1 + mm/slab_common.c | 3 +-- mm/slub.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-)mm/slab.h +1
-0@@ -388,6 +388,7 @@ extern void create_boot_cache(struct kmem_cache *, const char *name, unsigned int useroffset, unsigned int usersize);int slab_unmergeable (struct kmem_cache *s); +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags);slab_flags_t kmem_cache_flags (slab_flags_t flags,const char *name);mm/slab_common.c +1
-2@@ -174,8 +174,7 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static bool slab_args_unmergeable (struct kmem_cache_args *args, -slab_flags_t flags) +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags) {if (slab_nomerge)return true;mm/slub.c +0
-0mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 7681
Quoted 7 lines
mm/slub.c +2
-1@@ -7676,7 +7676,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ aligned_size =ALIGN (size, s->align);#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) -if (aligned_size - size >=sizeof (struct slabobj_ext )) +if (slab_args_unmergeable (args, s->flags) && + (aligned_size - size >=sizeof (struct slabobj_ext ))) s->flags |=SLAB_OBJ_EXT_IN_OBJ ;Hi Harry,
Quoted 6 lines
This patch looks reasonable to me. I just noticed a minor point that I wanted to bring up:
It seems a bit self-referential that
SLAB_NEVER_MERGEalready includesSLAB_OBJ_EXT_IN_OBJ, but we're usingSLAB_NEVER_MERGEto decide whether to setSLAB_OBJ_EXT_IN_OBJ.
Hi Hao, thanks for bringing it up!
Quoted 1 line
Do you think it might be helpful to add a comment here for better clarity?
Hmm but I'm not sure what should be clarified here. (perhaps because I wrote it).
Checking SLAB_OBJ_EXT_IN_OBJ (as part of SLAB_NEVER_MERGE) before setting SLAB_OBJ_EXT_IN_OBJ should be fine (because it's not set before we set it), and once you set it, it should prevent merging.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V1 2/2] mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
Quoted 71 lines · Harry Yoo
Quoted 61 lines · Hao Li
Quoted 50 lines · Harry Yoo
While
SLAB_OBJ_EXT_IN_OBJallows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
Restrict
SLAB_OBJ_EXT_IN_OBJto caches that are already unmergeable for other reasons (e.g., those with constructors orSLAB_TYPESAFE_BY_RCU).Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Harry Yoo <[email protected]>--- mm/slab.h | 1 + mm/slab_common.c | 3 +-- mm/slub.c | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-)mm/slab.h +1
-0@@ -388,6 +388,7 @@ extern void create_boot_cache(struct kmem_cache *, const char *name, unsigned int useroffset, unsigned int usersize);int slab_unmergeable (struct kmem_cache *s); +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags);slab_flags_t kmem_cache_flags (slab_flags_t flags,const char *name);mm/slab_common.c +1
-2@@ -174,8 +174,7 @@ int slab_unmergeable(struct kmem_cache *s)return 0 ; } -static bool slab_args_unmergeable (struct kmem_cache_args *args, -slab_flags_t flags) +bool slab_args_unmergeable (struct kmem_cache_args *args,slab_flags_t flags) {if (slab_nomerge)return true;mm/slub.c +0
-0mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 7681
Quoted 7 lines
mm/slub.c +2
-1@@ -7676,7 +7676,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ aligned_size =ALIGN (size, s->align);#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) -if (aligned_size - size >=sizeof (struct slabobj_ext )) +if (slab_args_unmergeable (args, s->flags) && + (aligned_size - size >=sizeof (struct slabobj_ext ))) s->flags |=SLAB_OBJ_EXT_IN_OBJ ;Hi Harry,
Quoted 6 lines
This patch looks reasonable to me. I just noticed a minor point that I wanted to bring up:
It seems a bit self-referential that
SLAB_NEVER_MERGEalready includesSLAB_OBJ_EXT_IN_OBJ, but we're usingSLAB_NEVER_MERGEto decide whether to setSLAB_OBJ_EXT_IN_OBJ.Hi Hao, thanks for bringing it up!
Quoted 8 lines
Quoted 1 line
Do you think it might be helpful to add a comment here for better clarity?
Hmm but I'm not sure what should be clarified here. (perhaps because I wrote it).
Checking
SLAB_OBJ_EXT_IN_OBJ(as part ofSLAB_NEVER_MERGE) before settingSLAB_OBJ_EXT_IN_OBJshould be fine (because it's not set before we set it), and once you set it, it should prevent merging.
Yeah, s->flags currently doesn't have SLAB_OBJ_EXT_IN_OBJ, and the functionality here is totally fine. I just happened to notice this while reading through the code, and from a semantic perspective, it made me pause for a moment. It looks like we're checking if s->flags contains SLAB_OBJ_EXT_IN_OBJ (among other flags) to decide whether we can set SLAB_OBJ_EXT_IN_OBJ. Maybe we could add a small comment like: "SLAB_OBJ_EXT_IN_OBJ hasn't been set yet here; this is just checking for other unmergeable reasons."
Of course, this is just a small thought, it's perfectly fine to leave it as is-I just thought it might help slightly.
Signature · 2 lines
Thanks, Hao
Quoted 4 lines
Signature · 3 lines
-- Cheers, Harry / Hyeonggon
Re: [PATCH V1 2/2] mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
Quoted 44 lines · Hao Li
Quoted 26 lines · Harry Yoo
Quoted 16 lines · Hao Li
mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 7681
Quoted 12 lines · Harry Yoo
mm/slub.c +2
-1@@ -7676,7 +7676,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ aligned_size =ALIGN (size, s->align);#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) -if (aligned_size - size >=sizeof (struct slabobj_ext )) +if (slab_args_unmergeable (args, s->flags) && + (aligned_size - size >=sizeof (struct slabobj_ext ))) s->flags |=SLAB_OBJ_EXT_IN_OBJ ;Hi Harry,
Quoted 6 lines
This patch looks reasonable to me. I just noticed a minor point that I wanted to bring up:
It seems a bit self-referential that
SLAB_NEVER_MERGEalready includesSLAB_OBJ_EXT_IN_OBJ, but we're usingSLAB_NEVER_MERGEto decide whether to setSLAB_OBJ_EXT_IN_OBJ.Hi Hao, thanks for bringing it up!
Quoted 8 lines
Quoted 1 line
Do you think it might be helpful to add a comment here for better clarity?
Hmm but I'm not sure what should be clarified here. (perhaps because I wrote it).
Checking
SLAB_OBJ_EXT_IN_OBJ(as part ofSLAB_NEVER_MERGE) before settingSLAB_OBJ_EXT_IN_OBJshould be fine (because it's not set before we set it), and once you set it, it should prevent merging.Yeah,
s->flagscurrently doesn't haveSLAB_OBJ_EXT_IN_OBJ, and the functionality here is totally fine. I just happened to notice this while reading through the code, and from a semantic perspective, it made me pause for a moment. It looks like we're checking ifs->flagscontainsSLAB_OBJ_EXT_IN_OBJ(among other flags) to decide whether we can setSLAB_OBJ_EXT_IN_OBJ. Maybe we could add a small comment like: "SLAB_OBJ_EXT_IN_OBJhasn't been set yet here; this is just checking for other unmergeable reasons."
Quoted 2 lines
Of course, this is just a small thought, it's perfectly fine to leave it as is-I just thought it might help slightly.
I think it's probably okay to leave it as-is for now. But thanks for bringing this up!
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V1 2/2] mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches
Quoted 45 lines · Harry Yoo
Quoted 44 lines · Hao Li
Quoted 26 lines · Harry Yoo
Quoted 16 lines · Hao Li
mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 7681
Quoted 12 lines · Harry Yoo
mm/slub.c +2
-1@@ -7676,7 +7676,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ aligned_size =ALIGN (size, s->align);#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) -if (aligned_size - size >=sizeof (struct slabobj_ext )) +if (slab_args_unmergeable (args, s->flags) && + (aligned_size - size >=sizeof (struct slabobj_ext ))) s->flags |=SLAB_OBJ_EXT_IN_OBJ ;Hi Harry,
Quoted 6 lines
This patch looks reasonable to me. I just noticed a minor point that I wanted to bring up:
It seems a bit self-referential that
SLAB_NEVER_MERGEalready includesSLAB_OBJ_EXT_IN_OBJ, but we're usingSLAB_NEVER_MERGEto decide whether to setSLAB_OBJ_EXT_IN_OBJ.Hi Hao, thanks for bringing it up!
Quoted 8 lines
Quoted 1 line
Do you think it might be helpful to add a comment here for better clarity?
Hmm but I'm not sure what should be clarified here. (perhaps because I wrote it).
Checking
SLAB_OBJ_EXT_IN_OBJ(as part ofSLAB_NEVER_MERGE) before settingSLAB_OBJ_EXT_IN_OBJshould be fine (because it's not set before we set it), and once you set it, it should prevent merging.Yeah,
s->flagscurrently doesn't haveSLAB_OBJ_EXT_IN_OBJ, and the functionality here is totally fine. I just happened to notice this while reading through the code, and from a semantic perspective, it made me pause for a moment. It looks like we're checking ifs->flagscontainsSLAB_OBJ_EXT_IN_OBJ(among other flags) to decide whether we can setSLAB_OBJ_EXT_IN_OBJ. Maybe we could add a small comment like: "SLAB_OBJ_EXT_IN_OBJhasn't been set yet here; this is just checking for other unmergeable reasons."
Quoted 6 lines
Quoted 2 lines
Of course, this is just a small thought, it's perfectly fine to leave it as is-I just thought it might help slightly.
I think it's probably okay to leave it as-is for now. But thanks for bringing this up!
Ah, no problem, thanks!
Signature · 2 lines
Thanks, Hao
Vlastimil Babka
AppliedQuoted 18 lines · Harry Yoo
While
SLAB_OBJ_EXT_IN_OBJallows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
So let's take a conservative approach and restrict
SLAB_OBJ_EXT_IN_OBJto caches that are already unmergeable for other reasons.Ideally this should be part of the slab/for-7.0/obj_metadata branch, but due to a conflict with slab/for-7.0/sheaves, I based it on slab/for-next (
be36abd97e52).Patch 1 factors out mergeability logic to
slab_args_unmergeable(). This is used to determine mergeability before the cache is created.
Quoted 6 lines
Patch 2 uses the new function to allow
SLAB_OBJ_EXT_IN_OBJonly when merging is not allowed for other reasons.Harry Yoo (2):
mm/slab: factor outslab_args_unmergeable()
mm/slab: only allowSLAB_OBJ_EXT_IN_OBJfor unmergeable caches
Thanks, applied to slab/for-next and reorganized the sub-branches a bit to prevent conflicts.
Quoted 5 lines
mm/slab.h | 1 + mm/slab_common.c | 27 +++++++++++++++++---------- mm/slub.c | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-)
Quoted 7 lines · Harry Yoo
While
SLAB_OBJ_EXT_IN_OBJallows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
Is this motivated by a production issue or a more a correctness thing?
It's somewhat tangential, but we've had practical problems with slab merging and ended up disabling it in the Meta fleet. It makes it difficult to identify culprits when there is a footprint regression in merged caches. IIRC there was at least one instance where it merged wildly different lifetimes, which raises fragmentation concerns. In the end we turned it off and noted no meaningful usage difference.
So I'm curious if there are cases where it tangibly helps. And I wonder whether default y makes sense given its observability and predictability implications.
To enable, or not to enable slab merging? That is the question (was: Re: [PATCH V1 0/2] Only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches)
Quoted 8 lines · Johannes Weiner
Quoted 7 lines · Harry Yoo
While
SLAB_OBJ_EXT_IN_OBJallows to reduce memory overhead to account slab objects, it prevents slab merging because merging can change the metadata layout.As pointed out Vlastimil Babka, disabling merging solely for this memory optimization may not be a net win, because disabling slab merging tends to increase overall memory usage.
Hi Joahnnes,
Quoted 1 line
Is this motivated by a production issue or a more a correctness thing?
It's more of a correctness thing :)
Quoted 4 lines
It's somewhat tangential, but we've had practical problems with slab merging and ended up disabling it in the Meta fleet. It makes it difficult to identify culprits when there is a footprint regression in merged caches.
Oh, yeah. slab merging makes it harder to investigate which caches are actually contributing to the regression.
Quoted 2 lines
IIRC there was at least one instance where it merged wildly different lifetimes, which raises fragmentation concerns.
That is a valid concern indeed.
Quoted 1 line
In the end we turned it off and noted no meaningful usage difference.
That's very interesting observation, I was thinking slab merging should have some benefit in saving memory...
Quoted 3 lines
So I'm curious if there are cases where it tangibly helps. And I wonder whether default y makes sense given its observability and predictability implications.
/me digs some old email threads
Julian Pidancet tried to change the default behavior a few years ago.
v2: https://lore.kernel.org/linux-mm/[email protected]
v1: https://lore.kernel.org/linux-mm/[email protected]
Christoph Lameter at that time had a concern about its impact on systems with larger pages.
https://lore.kernel.org/linux-mm/[email protected]
David Rientjes measured the difference between enabling vs. disabling slab merging in memory usage / performance on some benchmarks.
https://lore.kernel.org/linux-mm/[email protected]
The observation was that SReclaimable was only slightly increased (which probably isn't a big deal as it's reclaimable), but there were some performance regressions when disabling slab merging, most notably a -18% will-it-scale.context_switch1_per_thread_ops regression on skylake (but not on cascade lake, presumably due to differences in μarch).
Looks like at that time it was concluded that disabling slab merging by default probably wasn't a clear-cut decision, given that it showed a measurable regression on benchmarks and without knowing which real-world workloads would be affected by corner cases.
Signature · 2 lines
Cheers, Harry / Hyeonggon