mm: slub: remove dead and buggy code from sysfs_slab_add()
Subsystems (2)
PATCH 1/1 mm: slub: remove dead and buggy code from sysfs_slab_add()
The function sysfs_slab_add() has two callers:
One is slab_sysfs_init(), which first initializes slab_kset, and only when that succeeds sets slab_state to FULL, and then proceeds to call sysfs_slab_add() for all previously created slabs.
The other is __kmem_cache_create(), but only after a
if (slab_state <= UP) return 0;
check.
So in other words, sysfs_slab_add() is never called without slab_kset (aka the return value of cache_kset()) being non-NULL.
And this is just as well, because if we ever did take this path and called kobject_init(&s->kobj), and then later when called again from slab_sysfs_init() would end up calling kobject_init_and_add(), we would hit
if (kobj->state_initialized) {
/* do not error out as sometimes we can recover */
pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
dump_stack();
}in kobject.c.
--- mm/slub.c | 5 ----- 1 file changed, 5 deletions(-)
mm/slub.c +0 -5
@@ -5937,11 +5937,6 @@ static int sysfs_slab_add(struct kmem_cache *s)struct kset *kset =cache_kset (s);int unmergeable =slab_unmergeable (s); -if (!kset) { -kobject_init (&s->kobj, &slab_ktype); -return 0 ; - } -if (!unmergeable && disable_higher_order_debug && (slub_debug &DEBUG_METADATA_FLAGS )) unmergeable =1 ;
Signature · 1 line
2.37.2
Quoted 52 lines · Rasmus Villemoes
The function
sysfs_slab_add()has two callers:One is
slab_sysfs_init(), which first initializesslab_kset, and only when that succeeds setsslab_stateto FULL, and then proceeds to callsysfs_slab_add()for all previously created slabs.The other is
__kmem_cache_create(), but only after aif (slab_state <= UP) return 0;check.
So in other words,
sysfs_slab_add()is never called withoutslab_kset(aka the return value ofcache_kset()) being non-NULL.And this is just as well, because if we ever did take this path and called
kobject_init(&s->kobj), and then later when called again fromslab_sysfs_init()would end up callingkobject_init_and_add(), we would hitif (kobj->state_initialized) { /* do not error out as sometimes we can recover */ pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n", dump_stack(); }in
kobject.c.Signed-off-by Rasmus Villemoes <[email protected]>--- mm/slub.c | 5 ----- 1 file changed, 5 deletions(-)mm/slub.c +0
-5@@ -5937,11 +5937,6 @@ static int sysfs_slab_add(struct kmem_cache *s)struct kset *kset =cache_kset (s);int unmergeable =slab_unmergeable (s); -if (!kset) { -kobject_init (&s->kobj, &slab_ktype); -return 0 ; - } -if (!unmergeable && disable_higher_order_debug && (slub_debug &DEBUG_METADATA_FLAGS )) unmergeable =1 ;Signature · 1 line
2.37.2
I assumed that it's hit when SLUB failed to initialize slab_kset in slab_sysfs_init(). (Yeah, it is too unlikely, though....)
And obviously it's a bug if sysfs_slab_add() is called early than slab_sysfs_init().
Signature · 2 lines
Thanks, Hyeonggon
Quoted 43 lines · Hyeonggon Yoo
Quoted 42 lines · Rasmus Villemoes
The function
sysfs_slab_add()has two callers:One is
slab_sysfs_init(), which first initializesslab_kset, and only when that succeeds setsslab_stateto FULL, and then proceeds to callsysfs_slab_add()for all previously created slabs.The other is
__kmem_cache_create(), but only after aif (slab_state <= UP) return 0;check.
So in other words,
sysfs_slab_add()is never called withoutslab_kset(aka the return value ofcache_kset()) being non-NULL.And this is just as well, because if we ever did take this path and called
kobject_init(&s->kobj), and then later when called again fromslab_sysfs_init()would end up callingkobject_init_and_add(), we would hitif (kobj->state_initialized) { /* do not error out as sometimes we can recover */ pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n", dump_stack(); }in
kobject.c.Signed-off-by Rasmus Villemoes <[email protected]>--- mm/slub.c | 5 ----- 1 file changed, 5 deletions(-)mm/slub.c +0
-0@@ -5937,11 +5937,6 @@ static int sysfs_slab_add(struct kmem_cache *s)struct kset *kset =cache_kset (s);int unmergeable =slab_unmergeable (s);
Quoted 13 lines
Quoted 10 lines
mm/slub.c +0
-5-if (!kset) { -kobject_init (&s->kobj, &slab_ktype); -return 0 ; - } -if (!unmergeable && disable_higher_order_debug && (slub_debug &DEBUG_METADATA_FLAGS )) unmergeable =1 ;Signature · 1 line
2.37.2I assumed that it's hit when SLUB failed to initialize
slab_ksetinslab_sysfs_init(). (Yeah, it is too unlikely, though....)
No, it is not, because if the creation of slab_kset fails, slab_sysfs_init() returns early, and hence slab_state never transitions to FULL. I don't see anywhere else where slab_state could become FULL (of course in slab.c and slob.c, but those are not built when slub.c is), so I do believe my analysis in the commit log is correct.
Quoted 2 lines
And obviously it's a bug if
sysfs_slab_add()is called early thanslab_sysfs_init().
Yes, and that's already what the existing slab_state check guards.
Rasmus
Hyeonggon Yoo
Reviewed-byQuoted 58 lines · Rasmus Villemoes
Quoted 43 lines · Hyeonggon Yoo
Quoted 42 lines · Rasmus Villemoes
The function
sysfs_slab_add()has two callers:One is
slab_sysfs_init(), which first initializesslab_kset, and only when that succeeds setsslab_stateto FULL, and then proceeds to callsysfs_slab_add()for all previously created slabs.The other is
__kmem_cache_create(), but only after aif (slab_state <= UP) return 0;check.
So in other words,
sysfs_slab_add()is never called withoutslab_kset(aka the return value ofcache_kset()) being non-NULL.And this is just as well, because if we ever did take this path and called
kobject_init(&s->kobj), and then later when called again fromslab_sysfs_init()would end up callingkobject_init_and_add(), we would hitif (kobj->state_initialized) { /* do not error out as sometimes we can recover */ pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n", dump_stack(); }in
kobject.c.Signed-off-by Rasmus Villemoes <[email protected]>--- mm/slub.c | 5 ----- 1 file changed, 5 deletions(-)mm/slub.c +0
-0@@ -5937,11 +5937,6 @@ static int sysfs_slab_add(struct kmem_cache *s)struct kset *kset =cache_kset (s);int unmergeable =slab_unmergeable (s);Quoted 13 lines
Quoted 10 lines
mm/slub.c +0
-5-if (!kset) { -kobject_init (&s->kobj, &slab_ktype); -return 0 ; - } -if (!unmergeable && disable_higher_order_debug && (slub_debug &DEBUG_METADATA_FLAGS )) unmergeable =1 ;Signature · 1 line
2.37.2I assumed that it's hit when SLUB failed to initialize
slab_ksetinslab_sysfs_init(). (Yeah, it is too unlikely, though....)
Quoted 3 lines
No, it is not, because if the creation of
slab_ksetfails,slab_sysfs_init()returns early, and henceslab_statenever transitions to FULL.
Yeah, you are right ;-) I misread that.
Quoted 3 lines
I don't see anywhere else where
slab_statecould become FULL (of course inslab.candslob.c, but those are not built whenslub.cis), so I do believe my analysis in the commit log is correct.
Right.
Quoted 6 lines
Quoted 2 lines
And obviously it's a bug if
sysfs_slab_add()is called early thanslab_sysfs_init().Yes, and that's already what the existing
slab_statecheck guards.Rasmus
Looks good to me,
Thanks!
Signature · 2 lines
Thanks, Hyeonggon
Quoted 27 lines · Rasmus Villemoes
The function
sysfs_slab_add()has two callers:One is
slab_sysfs_init(), which first initializesslab_kset, and only when that succeeds setsslab_stateto FULL, and then proceeds to callsysfs_slab_add()for all previously created slabs.The other is
__kmem_cache_create(), but only after aif (slab_state <= UP) return 0;check.
So in other words,
sysfs_slab_add()is never called withoutslab_kset(aka the return value ofcache_kset()) being non-NULL.And this is just as well, because if we ever did take this path and called
kobject_init(&s->kobj), and then later when called again fromslab_sysfs_init()would end up callingkobject_init_and_add(), we would hitif (kobj->state_initialized) { /* do not error out as sometimes we can recover */ pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n", dump_stack(); }
Quoted 3 lines
in
kobject.c.Signed-off-by Rasmus Villemoes <[email protected]>
Quoted 27 lines · Rasmus Villemoes
The function
sysfs_slab_add()has two callers:One is
slab_sysfs_init(), which first initializesslab_kset, and only when that succeeds setsslab_stateto FULL, and then proceeds to callsysfs_slab_add()for all previously created slabs.The other is
__kmem_cache_create(), but only after aif (slab_state <= UP) return 0;check.
So in other words,
sysfs_slab_add()is never called withoutslab_kset(aka the return value ofcache_kset()) being non-NULL.And this is just as well, because if we ever did take this path and called
kobject_init(&s->kobj), and then later when called again fromslab_sysfs_init()would end up callingkobject_init_and_add(), we would hitif (kobj->state_initialized) { /* do not error out as sometimes we can recover */ pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n", dump_stack(); }
Quoted 3 lines
in
kobject.c.Signed-off-by Rasmus Villemoes <[email protected]>
Thanks, added to slab.git for-6.2/slub-sysfs
Quoted 20 lines
--- mm/slub.c | 5 ----- 1 file changed, 5 deletions(-)mm/slub.c +0
-5@@ -5937,11 +5937,6 @@ static int sysfs_slab_add(struct kmem_cache *s)struct kset *kset =cache_kset (s);int unmergeable =slab_unmergeable (s); -if (!kset) { -kobject_init (&s->kobj, &slab_ktype); -return 0 ; - } -if (!unmergeable && disable_higher_order_debug && (slub_debug &DEBUG_METADATA_FLAGS )) unmergeable =1 ;