Skip to contentSkip to conversation

slab: Warn on duplicate cache names when DEBUG_VM=y

PATCH In review Pedro Falcato 4 participants 9 messages Aug 7, 2024 – Aug 26, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 slab: Warn on duplicate cache names when DEBUG_VM=y

Duplicate slab cache names can create havoc for userspace tooling that expects slab cache names to be unique. This is a reasonable expectation.

Sadly, too many duplicate name problems are out there in the wild, so simply pr_warn instead of pr_err() + failing the sanity check.

---
v2:
- Replace the pr_err() + failure with a simple pr_warn

Contrary to Vlastimil's suggestion, we don't seem to require a refcount
== 0 check, because shutdown_cache already synchronously deletes the cache from the list (even reusing the same list_head for TYPESAFE_BY_RCU).

 mm/slab_common.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
mm/slab_common.c +18 -0
index 40b582a014b..1abe6a577d5 100644
@@ -88,6 +88,19 @@ unsigned int kmem_cache_size(struct kmem_cache *s)
 EXPORT_SYMBOL(kmem_cache_size);
 
 #ifdef CONFIG_DEBUG_VM
+
+static bool kmem_cache_is_duplicate_name(const char *name)
+{
+	struct kmem_cache *s;
+
+	list_for_each_entry(s, &slab_caches, list) {
+		if (!strcmp(s->name, name))
+			return true;
+	}
+
+	return false;
+}
+
 static int kmem_cache_sanity_check(const char *name, unsigned int size)
 {
 	if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
@@ -95,6 +108,11 @@ static int kmem_cache_sanity_check(const char *name, unsigned int size)
 		return -EINVAL;
 	}
 
+	if (kmem_cache_is_duplicate_name(name)) {
+		/* Duplicate names will confuse slabtop, et al */
+		pr_warn("%s: name %s already exists as a cache\n", __func__, name);
+	}
+
 	WARN_ON(strchr(name, ' '));	/* It confuses parsers */
 	return 0;
 }
Signature · 1 line
2.46.0
#1 lore raw
Quoted 8 lines · Pedro Falcato

Duplicate slab cache names can create havoc for userspace tooling that expects slab cache names to be unique. This is a reasonable expectation.

Sadly, too many duplicate name problems are out there in the wild, so simply pr_warn instead of pr_err() + failing the sanity check.

Quoted 7 lines

---
v2:
- Replace the pr_err() + failure with a simple pr_warn

Contrary to Vlastimil's suggestion, we don't seem to require a refcount
== 0 check, because shutdown_cache already synchronously deletes the cache from the list (even reusing the same list_head for TYPESAFE_BY_RCU).

It's not deleted when leaked objects are detected and the destroy is aborted. But in this series that should no longer happen so then it will be fine:

https://lore.kernel.org/all/[email protected]/

Quoted 40 lines
 mm/slab_common.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
mm/slab_common.c +18 -0
index 40b582a014b..1abe6a577d5 100644
@@ -88,6 +88,19 @@ unsigned int kmem_cache_size(struct kmem_cache *s)
 EXPORT_SYMBOL(kmem_cache_size);
 
 #ifdef CONFIG_DEBUG_VM
+
+static bool kmem_cache_is_duplicate_name(const char *name)
+{
+	struct kmem_cache *s;
+
+	list_for_each_entry(s, &slab_caches, list) {
+		if (!strcmp(s->name, name))
+			return true;
+	}
+
+	return false;
+}
+
 static int kmem_cache_sanity_check(const char *name, unsigned int size)
 {
 	if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
@@ -95,6 +108,11 @@ static int kmem_cache_sanity_check(const char *name, unsigned int size)
 		return -EINVAL;
 	}
 
+	if (kmem_cache_is_duplicate_name(name)) {
+		/* Duplicate names will confuse slabtop, et al */
+		pr_warn("%s: name %s already exists as a cache\n", __func__, name);
+	}
+
 	WARN_ON(strchr(name, ' '));	/* It confuses parsers */
 	return 0;
 }
reviewer Acked-by Christoph Lameter on v2 1/1
#2 lore raw
In reply to Pedro Falcato
Quoted 2 lines · Pedro Falcato

Duplicate slab cache names can create havoc for userspace tooling that expects slab cache names to be unique. This is a reasonable expectation.

Yes that is reasonable. This is done during slab creation and so is not a performance sensitive operation. The sanity check could be done even without CONFIG_DEBUG_VM

#3 lore raw
Quoted 8 lines · Christoph Lameter (Ampere)
Quoted 2 lines · Pedro Falcato

Duplicate slab cache names can create havoc for userspace tooling that expects slab cache names to be unique. This is a reasonable expectation.

Yes that is reasonable. This is done during slab creation and so is not a performance sensitive operation. The sanity check could be done even without CONFIG_DEBUG_VM

We can perhaps move it outside CONFIG_DEBUG_VM in few cycles.

Quoted 1 line

Added to slab/for-next, thanks!

#4 lore raw
In reply to Pedro Falcato
Quoted 25 lines · Pedro Falcato
mm/slab_common.c +13 -0
index 40b582a014b..1abe6a577d5 100644
@@ -88,6 +88,19 @@ unsigned int kmem_cache_size(struct kmem_cache *s)
 EXPORT_SYMBOL(kmem_cache_size);
 
 #ifdef CONFIG_DEBUG_VM
+
+static bool kmem_cache_is_duplicate_name(const char *name)
+{
+	struct kmem_cache *s;
+
+	list_for_each_entry(s, &slab_caches, list) {
+		if (!strcmp(s->name, name))
+			return true;
+	}
+
+	return false;
+}
+
 static int kmem_cache_sanity_check(const char *name, unsigned int size)
 {
 	if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {

mm/slab_common.c static int kmem_cache_sanity_check(const char *name, unsigned int size) · line 113

Quoted 6 lines
mm/slab_common.c +3 -0
@@ -95,6 +108,11 @@ static int kmem_cache_sanity_check(const char *name, unsigned int size)
 		return -EINVAL;
 	}
 
+	if (kmem_cache_is_duplicate_name(name)) {
+		/* Duplicate names will confuse slabtop, et al */
+		pr_warn("%s: name %s already exists as a cache\n", __func__, name);

Shouldn't this be a full WARN_ON() instead of pr_warn()? I assume we'll be interested in who is adding the cache when the name already exists.

#5 lore raw
Quoted 31 lines · David Rientjes
Quoted 28 lines · Pedro Falcato
mm/slab_common.c +13 -0
index 40b582a014b..1abe6a577d5 100644
@@ -88,6 +88,19 @@ unsigned int kmem_cache_size(struct kmem_cache *s)
 EXPORT_SYMBOL(kmem_cache_size);
 
 #ifdef CONFIG_DEBUG_VM
+
+static bool kmem_cache_is_duplicate_name(const char *name)
+{
+     struct kmem_cache *s;
+
+     list_for_each_entry(s, &slab_caches, list) {
+             if (!strcmp(s->name, name))
+                     return true;
+     }
+
+     return false;
+}
+
 static int kmem_cache_sanity_check(const char *name, unsigned int size)
 {
      if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
@@ -95,6 +108,11 @@ static int kmem_cache_sanity_check(const char *name, unsigned int size)
              return -EINVAL;
      }
 
Quoted 7 lines

mm/slab_common.c static int kmem_cache_sanity_check(const char *name, unsigned int size) · line 113

Quoted 3 lines
mm/slab_common.c +3 -0
+     if (kmem_cache_is_duplicate_name(name)) {
+             /* Duplicate names will confuse slabtop, et al */
+             pr_warn("%s: name %s already exists as a cache\n", __func__, name);

Shouldn't this be a full WARN_ON() instead of pr_warn()? I assume we'll be interested in who is adding the cache when the name already exists.

panic_on_warn? :)

Personally I don't have anything against WARN_ON, but we've seen that panic_on_warn is a real thing on real systems, and DEBUG_VM is also set on real prod configs (like Fedora does/used to do). I've sent out one or two loose patches for problems I did find in my own testing around, but there may be many more (e.g some drivers may call kmem_cache_create repeatedly in some sort of callback, like 9pfs was doing when mounting; this is not greppable). And I'd guess grepping for cache names tends to be easy enough?

Signature · 1 line
Pedro
#6 lore raw
Quoted 36 lines · Pedro Falcato
Quoted 28 lines
Quoted 28 lines
mm/slab_common.c +13 -0
index 40b582a014b..1abe6a577d5 100644
@@ -88,6 +88,19 @@ unsigned int kmem_cache_size(struct kmem_cache *s)
 EXPORT_SYMBOL(kmem_cache_size);
 
 #ifdef CONFIG_DEBUG_VM
+
+static bool kmem_cache_is_duplicate_name(const char *name)
+{
+     struct kmem_cache *s;
+
+     list_for_each_entry(s, &slab_caches, list) {
+             if (!strcmp(s->name, name))
+                     return true;
+     }
+
+     return false;
+}
+
 static int kmem_cache_sanity_check(const char *name, unsigned int size)
 {
      if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
@@ -95,6 +108,11 @@ static int kmem_cache_sanity_check(const char *name, unsigned int size)
              return -EINVAL;
      }
 
Quoted 7 lines

mm/slab_common.c static int kmem_cache_sanity_check(const char *name, unsigned int size) · line 113

Quoted 3 lines
mm/slab_common.c +3 -0
+     if (kmem_cache_is_duplicate_name(name)) {
+             /* Duplicate names will confuse slabtop, et al */
+             pr_warn("%s: name %s already exists as a cache\n", __func__, name);

Shouldn't this be a full WARN_ON() instead of pr_warn()? I assume we'll be interested in who is adding the cache when the name already exists.

Quoted 2 lines

panic_on_warn? :)

Would get the problem fixed up pretty fast, no? :)

Quoted 9 lines

Personally I don't have anything against WARN_ON, but we've seen that panic_on_warn is a real thing on real systems, and DEBUG_VM is also set on real prod configs (like Fedora does/used to do). I've sent out one or two loose patches for problems I did find in my own testing around, but there may be many more (e.g some drivers may call kmem_cache_create repeatedly in some sort of callback, like 9pfs was doing when mounting; this is not greppable). And I'd guess grepping for cache names tends to be easy enough?

Can we add a dump_stack() to make this way easier instead of hiding who is creating the duplicate name?

#7 lore raw
Quoted 25 lines · David Rientjes
Quoted 10 lines · Pedro Falcato
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+     if (kmem_cache_is_duplicate_name(name)) {
+             /* Duplicate names will confuse slabtop, et al */
+             pr_warn("%s: name %s already exists as a cache\n", __func__, name);

Shouldn't this be a full WARN_ON() instead of pr_warn()? I assume we'll be interested in who is adding the cache when the name already exists.

panic_on_warn? :)

Would get the problem fixed up pretty fast, no? :)

Quoted 9 lines

Personally I don't have anything against WARN_ON, but we've seen that panic_on_warn is a real thing on real systems, and DEBUG_VM is also set on real prod configs (like Fedora does/used to do). I've sent out one or two loose patches for problems I did find in my own testing around, but there may be many more (e.g some drivers may call kmem_cache_create repeatedly in some sort of callback, like 9pfs was doing when mounting; this is not greppable). And I'd guess grepping for cache names tends to be easy enough?

Quoted 2 lines

Can we add a dump_stack() to make this way easier instead of hiding who is creating the duplicate name?

Bah, sorry for the delay.

I'm fully in favour of adding a dump_stack(), but it seems like hand coding WARN_ON a bit. Oh well.
If y'all agree, please squash this in (praying gmail doesn't mangle this diff, in any case it's a trivial change):

mm/slab_common.c +0 -0
index 1abe6a577d52..d183655e4b1b 100644
@@ -111,6 +111,7 @@ static int kmem_cache_sanity_check(const char

*name, unsigned int size)

       if (kmem_cache_is_duplicate_name(name)) {
               /* Duplicate names will confuse slabtop, et al */
               pr_warn("%s: name %s already exists as a cache\n",

__func__, name);
+ dump_stack_lvl(KERN_WARNING);
}

WARN_ON(strchr(name, ' ')); /* It confuses parsers */

#8 lore raw
Quoted 26 lines · Pedro Falcato
Quoted 25 lines · David Rientjes
Quoted 10 lines · Pedro Falcato
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+     if (kmem_cache_is_duplicate_name(name)) {
+             /* Duplicate names will confuse slabtop, et al */
+             pr_warn("%s: name %s already exists as a cache\n", __func__, name);

Shouldn't this be a full WARN_ON() instead of pr_warn()? I assume we'll be interested in who is adding the cache when the name already exists.

panic_on_warn? :)

Would get the problem fixed up pretty fast, no? :)

Quoted 9 lines

Personally I don't have anything against WARN_ON, but we've seen that panic_on_warn is a real thing on real systems, and DEBUG_VM is also set on real prod configs (like Fedora does/used to do). I've sent out one or two loose patches for problems I did find in my own testing around, but there may be many more (e.g some drivers may call kmem_cache_create repeatedly in some sort of callback, like 9pfs was doing when mounting; this is not greppable). And I'd guess grepping for cache names tends to be easy enough?

Quoted 8 lines
Quoted 2 lines

Can we add a dump_stack() to make this way easier instead of hiding who is creating the duplicate name?

Bah, sorry for the delay.

I'm fully in favour of adding a dump_stack(), but it seems like hand coding WARN_ON a bit. Oh well.

After some pondering I've decided we should just go with WARN_ON and not do a hand coded workaround for hypothetical people who run CONFIG_DEBUG_VM together with panic_on_warn, so I've updated the commit in the slab tree accordingly.

Quoted 17 lines

If y'all agree, please squash this in (praying gmail doesn't mangle this diff, in any case it's a trivial change):

mm/slab_common.c +1 -0
index 1abe6a577d52..d183655e4b1b 100644
@@ -111,6 +111,7 @@ static int kmem_cache_sanity_check(const char
 *name, unsigned int size)
       if (kmem_cache_is_duplicate_name(name)) {
               /* Duplicate names will confuse slabtop, et al */
               pr_warn("%s: name %s already exists as a cache\n",
 __func__, name);
+               dump_stack_lvl(KERN_WARNING);
       }

WARN_ON(strchr(name, ' ')); /* It confuses parsers */