mm/slub: clean up create_unique_id()
Subsystems (2)
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
@@ -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
Quoted 42 lines · Chao Yu
From: Chao Yu <[email protected]>
As Christophe JAILLET suggested:
In
create_unique_id(),"looks that
ID_STR_LENGTHcould even be reduced to 32 or 16.The 2nd
BUG_ONat 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. reduceID_STR_LENGTHto 32, as the buffer size should be enough;
2. removeBUG_ON()and return error if check condition is true.Suggested-by Christophe JAILLET <[email protected]>Fixes 81819f0fc828 ("SLUB core")Signed-off-by Chao Yu <[email protected]>--- mm/slub.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)mm/slub.c +1
-1@@ -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
Quoted 14 lines · Chao Yu
From: Chao Yu <[email protected]>
As Christophe JAILLET suggested:
In
create_unique_id(),"looks that
ID_STR_LENGTHcould even be reduced to 32 or 16.The 2nd
BUG_ONat 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. reduceID_STR_LENGTHto 32, as the buffer size should be enough;
2. removeBUG_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
Suggested-by Christophe JAILLET <[email protected]>Fixes 81819f0fc828 ("SLUB core")Signed-off-by Chao Yu <[email protected]>--- mm/slub.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)mm/slub.c +1
-1@@ -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; }
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_LENGTHcould even be reduced to 32 or 16.The 2nd
BUG_ONat 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. reduceID_STR_LENGTHto 32, as the buffer size should be enough;
2. removeBUG_ON()and return error if check condition is true.I'd leave a
WARN_ONthere 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
Suggested-by Christophe JAILLET <[email protected]>Fixes 81819f0fc828 ("SLUB core")Signed-off-by Chao Yu <[email protected]>--- mm/slub.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)mm/slub.c +1
-1@@ -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 usesnprintf()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; }