Skip to contentSkip to conversation

Only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches

PATCH Applied Harry Yoo 4 participants 14 messages Jan 27 – Feb 5

Subsystems (2)
lore
Share & tools
lore

Harry Yoo

author
#0 lore raw

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
author
#1 lore raw

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
index 2e80d323f550..904414c3ebb8 100644
@@ -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
#2 lore raw

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 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 +0 -1
index 2e80d323f550..904414c3ebb8 100644
@@ -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 -4
 	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 *));
author
#3 lore raw

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 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 +0 -0
index 2e80d323f550..904414c3ebb8 100644
@@ -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
#4 lore raw

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 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 +0 -0
index 2e80d323f550..904414c3ebb8 100644
@@ -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_nomerge then?

Yeah I would do that as well,

Quoted 2 lines

And even slab_unmergeable() does slab_nomerge check, so perhaps it's more consistent to do it in slab_args_unmergeable().

But this is true so I'll leave both as you did, thanks!

author
#5 lore raw

PATCH 2/2 mm/slab: only allow SLAB_OBJ_EXT_IN_OBJ for unmergeable caches

In reply to Harry Yoo

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.

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
index 8593c506cbf1..a5c4f981ee8b 100644
@@ -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
index 904414c3ebb8..d5a70a831a2a 100644
@@ -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
index ae9af184a18b..0581847e7dac 100644
@@ -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
reviewer
#6 lore raw

Re: [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_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
index 8593c506cbf1..a5c4f981ee8b 100644
@@ -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
index 904414c3ebb8..d5a70a831a2a 100644
@@ -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
index ae9af184a18b..0581847e7dac 100644

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
author
#7 lore raw

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_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
index 8593c506cbf1..a5c4f981ee8b 100644
@@ -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
index 904414c3ebb8..d5a70a831a2a 100644
@@ -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
index ae9af184a18b..0581847e7dac 100644

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,

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_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.

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
reviewer
#8 lore raw

Re: [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_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
index 8593c506cbf1..a5c4f981ee8b 100644
@@ -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
index 904414c3ebb8..d5a70a831a2a 100644
@@ -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
index ae9af184a18b..0581847e7dac 100644

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,

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_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.

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 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.

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
author
#9 lore raw

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
index ae9af184a18b..0581847e7dac 100644
@@ -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_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.

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 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.

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."

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
reviewer
#10 lore raw

Re: [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
index ae9af184a18b..0581847e7dac 100644
@@ -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_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.

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 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.

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."

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
#11 lore raw
In reply to Harry Yoo
Quoted 18 lines · Harry Yoo

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.

Quoted 6 lines

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

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(-)
#12 lore raw
In reply to Harry Yoo
Quoted 7 lines · Harry Yoo

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.

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.

author
#13 lore raw

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_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.

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