Skip to contentSkip to conversation

mm/slub: clean up create_unique_id()

PATCH Superseded Chao Yu 3 participants 4 messages Sep 18, 2022 – Sep 25, 2022

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Chao Yu

author
#0 lore raw

PATCH 1/1 mm/slub: clean up create_unique_id()

From: Chao Yu <[email protected]>

As Christophe JAILLET suggested:

In create_unique_id(),

"looks that ID_STR_LENGTH could even be reduced to 32 or 16.

The 2nd BUG_ON at the end of the function could certainly be just removed as well or remplaced by a:

        if (p > name + ID_STR_LENGTH - 1) {
                kfree(name);
                return -E<something>;
        }

"

According to above suggestion, let's do below cleanups:
1. reduce ID_STR_LENGTH to 32, as the buffer size should be enough;
2. remove BUG_ON() and return error if check condition is true.

---
 mm/slub.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/slub.c +5 -2
index 5ba6db62a5ab..a045c1ca8772 100644
@@ -5883,7 +5883,7 @@ static inline struct kset *cache_kset(struct kmem_cache *s)
 	return slab_kset;
 }
 
-#define ID_STR_LENGTH 64
+#define ID_STR_LENGTH 32
 
 /* Create a unique string id for a slab cache:
  *
@@ -5919,7 +5919,10 @@ static char *create_unique_id(struct kmem_cache *s)
 		*p++ = '-';
 	p += sprintf(p, "%07u", s->size);
 
-	BUG_ON(p > name + ID_STR_LENGTH - 1);
+	if (p > name + ID_STR_LENGTH - 1) {
+		kfree(name);
+		return ERR_PTR(-EINVAL);
+	}
 	return name;
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 42 lines · Chao Yu

From: Chao Yu <[email protected]>

As Christophe JAILLET suggested:

In create_unique_id(),

"looks that ID_STR_LENGTH could even be reduced to 32 or 16.

The 2nd BUG_ON at the end of the function could certainly be just removed as well or remplaced by a:

        if (p > name + ID_STR_LENGTH - 1) {
                kfree(name);
                return -E<something>;
        }

"

According to above suggestion, let's do below cleanups:
1. reduce ID_STR_LENGTH to 32, as the buffer size should be enough;
2. remove BUG_ON() and return error if check condition is true.

---
 mm/slub.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/slub.c +1 -1
index 5ba6db62a5ab..a045c1ca8772 100644
@@ -5883,7 +5883,7 @@ static inline struct kset *cache_kset(struct kmem_cache *s)
 	return slab_kset;
 }
 
-#define ID_STR_LENGTH 64
+#define ID_STR_LENGTH 32
 
 /* Create a unique string id for a slab cache:
  *

mm/slub.c static char *create_unique_id(struct kmem_cache *s) · line 5926

Quoted 9 lines
mm/slub.c +4 -1
@@ -5919,7 +5919,10 @@ static char *create_unique_id(struct kmem_cache *s)
 		*p++ = '-';
 	p += sprintf(p, "%07u", s->size);
 
-	BUG_ON(p > name + ID_STR_LENGTH - 1);
+	if (p > name + ID_STR_LENGTH - 1) {
+		kfree(name);
+		return ERR_PTR(-EINVAL);
+	}
 	return name;

IIUC 16 bytes will be still safe ;)

Anyway, for either size:

Looks good to me,

Thanks!

Quoted 5 lines
mm/slub.c +0 -0
 }
 
Signature · 1 line
2.25.1
Signature · 2 lines
Thanks,
Hyeonggon
#2 lore raw
In reply to Chao Yu
Quoted 14 lines · Chao Yu

From: Chao Yu <[email protected]>

As Christophe JAILLET suggested:

In create_unique_id(),

"looks that ID_STR_LENGTH could even be reduced to 32 or 16.

The 2nd BUG_ON at the end of the function could certainly be just removed as well or remplaced by a:

        if (p > name + ID_STR_LENGTH - 1) {
                kfree(name);
                return -E<something>;
        }
Quoted 5 lines

"

According to above suggestion, let's do below cleanups:
1. reduce ID_STR_LENGTH to 32, as the buffer size should be enough;
2. remove BUG_ON() and return error if check condition is true.

I'd leave a WARN_ON there as we really don't expect this to happen, so if it does, we should be loud about it and not silently fail.

Quoted 22 lines
---
 mm/slub.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/slub.c +1 -1
index 5ba6db62a5ab..a045c1ca8772 100644
@@ -5883,7 +5883,7 @@ static inline struct kset *cache_kset(struct kmem_cache *s)
 	return slab_kset;
 }
 
-#define ID_STR_LENGTH 64
+#define ID_STR_LENGTH 32
 
 /* Create a unique string id for a slab cache:
  *

mm/slub.c static char *create_unique_id(struct kmem_cache *s) · line 5920

Quoted 2 lines
mm/slub.c +0 -0
@@ -5919,7 +5919,10 @@ static char *create_unique_id(struct kmem_cache *s)
 		*p++ = '-';
 	p += sprintf(p, "%07u", s->size);

Hm but sprintf() will happily overflow, so if we only detect that afterwards, it's kinda too late to gracefully fail. Should use snprintf() then?

Quoted 8 lines
mm/slub.c +4 -1
-	BUG_ON(p > name + ID_STR_LENGTH - 1);
+	if (p > name + ID_STR_LENGTH - 1) {
+		kfree(name);
+		return ERR_PTR(-EINVAL);
+	}
 	return name;
 }
 
author
#3 lore raw
Quoted 17 lines · Vlastimil Babka
Quoted 16 lines · Chao Yu

From: Chao Yu <[email protected]>

As Christophe JAILLET suggested:

In create_unique_id(),

"looks that ID_STR_LENGTH could even be reduced to 32 or 16.

The 2nd BUG_ON at the end of the function could certainly be just removed as well or remplaced by a:

         if (p > name + ID_STR_LENGTH - 1) {
                 kfree(name);
                 return -E<something>;
         }

"

Quoted 6 lines
Quoted 3 lines

According to above suggestion, let's do below cleanups:
1. reduce ID_STR_LENGTH to 32, as the buffer size should be enough;
2. remove BUG_ON() and return error if check condition is true.

I'd leave a WARN_ON there as we really don't expect this to happen, so if it does, we should be loud about it and not silently fail.

Agreed.

Quoted 20 lines
Quoted 20 lines
---
  mm/slub.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
mm/slub.c +1 -1
index 5ba6db62a5ab..a045c1ca8772 100644
@@ -5883,7 +5883,7 @@ static inline struct kset *cache_kset(struct kmem_cache *s)
  	return slab_kset;
  }
 
-#define ID_STR_LENGTH 64
+#define ID_STR_LENGTH 32
 
Quoted 9 lines

mm/slub.c static char *create_unique_id(struct kmem_cache *s) · line 5920

Quoted 5 lines
mm/slub.c +0 -0
  /* Create a unique string id for a slab cache:
   *
@@ -5919,7 +5919,10 @@ static char *create_unique_id(struct kmem_cache *s)
  		*p++ = '-';
  	p += sprintf(p, "%07u", s->size);

Hm but sprintf() will happily overflow, so if we only detect that afterwards, it's kinda too late to gracefully fail. Should use snprintf() then?

Correct.

Updated in v2.

Thanks,

Quoted 10 lines
Quoted 9 lines
mm/slub.c +4 -1
-	BUG_ON(p > name + ID_STR_LENGTH - 1);
+	if (p > name + ID_STR_LENGTH - 1) {
+		kfree(name);
+		return ERR_PTR(-EINVAL);
+	}
  	return name;
  }