kobject: introduce kobject_is_added()
Subsystems (4)
PATCH 1/3 kobject: introduce kobject_is_added()
Add kobject_is_added() to avoid consumers from directly accessing the internal variables of kobject.
--- include/linux/kobject.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/kobject.h +5 -0
@@ -203,6 +203,11 @@ static inline const struct kobj_type *get_ktype(const struct kobject *kobj)return kobj->ktype; } +static inline int kobject_is_added (struct kobject *kobj) +{ +return kobj->state_in_sysfs; +} +extern struct kobject *kset_find_obj (struct kset *,const char *);/* The global /sys/kernel/ kobject for people to chain off of */
Signature · 1 line
2.35.1
PATCH 2/3 erofs: convert to use kobject_is_added()
Use kobject_is_added() instead of directly accessing the internal variables of kobject. BTW kill kobject_del() directly, because kobject_put() actually covers kobject removal automatically.
--- fs/erofs/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/erofs/sysfs.c +1 -2
@@ -240,8 +240,7 @@ void erofs_unregister_sysfs(struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb); -if (sbi->s_kobj.state_in_sysfs) { -kobject_del (&sbi->s_kobj); +if (kobject_is_added (&sbi->s_kobj)) {kobject_put (&sbi->s_kobj);wait_for_completion (&sbi->s_kobj_unregister); }
Signature · 1 line
2.35.1
Greg KH
maintainerRe: [PATCH 2/3] erofs: convert to use kobject_is_added()
Quoted 14 lines · Yangtao Li
Use
kobject_is_added()instead of directly accessing the internal variables of kobject. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/erofs/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)fs/erofs/sysfs.c +0
-0
fs/erofs/sysfs.c void erofs_unregister_sysfs(struct super_block *sb) · line 243
Quoted 6 lines
fs/erofs/sysfs.c +1
-2@@ -240,8 +240,7 @@ void erofs_unregister_sysfs(struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb); -if (sbi->s_kobj.state_in_sysfs) { -kobject_del (&sbi->s_kobj); +if (kobject_is_added (&sbi->s_kobj)) {
I do not understand why this check is even needed, I do not think it should be there at all as obviously the kobject was registered if it now needs to not be registered.
Meta-comment, we need to come up with a "filesystem kobject type" to get rid of lots of the boilerplate filesystem kobject logic as it's duplicated in every filesystem in tiny different ways and lots of times (like here), it's wrong.
kobjects were not designed to be "used raw" like this, ideally they would be wrapped in a subsystem that makes them easier to be used (like the driver model), but filesystems decided to use them and that usage just grew over the years. That's evolution for you...
thanks,
greg k-h
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
Hi Greg,
Quoted 18 lines · Greg KH
Quoted 17 lines · Yangtao Li
Use
kobject_is_added()instead of directly accessing the internal variables of kobject. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/erofs/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)fs/erofs/sysfs.c +0
-0@@ -240,8 +240,7 @@ void erofs_unregister_sysfs(struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb);
Quoted 7 lines
fs/erofs/sysfs.c void erofs_unregister_sysfs(struct super_block *sb) · line 243
Quoted 3 lines
fs/erofs/sysfs.c +1
-2-if (sbi->s_kobj.state_in_sysfs) { -kobject_del (&sbi->s_kobj); +if (kobject_is_added (&sbi->s_kobj)) {I do not understand why this check is even needed, I do not think it should be there at all as obviously the kobject was registered if it now needs to not be registered.
I think Yangtao sent a new patchset which missed the whole previous background discussions as below:
https://lore.kernel.org/r/[email protected]
It's needed because once a syzbot complaint as below:
https://lore.kernel.org/r/CAD-N9QXNx=p3-QoWzk6pCznF32CZy8kM3vvo8mamfZZ9CpUKdw@mail.gmail.com
I'd suggest including the previous backgrounds at least in the newer patchset, otherwise it makes me explain again and again...
Thanks,
Gao Xiang
Quoted 12 lines
Meta-comment, we need to come up with a "filesystem kobject type" to get rid of lots of the boilerplate filesystem kobject logic as it's duplicated in every filesystem in tiny different ways and lots of times (like here), it's wrong.
kobjects were not designed to be "used raw" like this, ideally they would be wrapped in a subsystem that makes them easier to be used (like the driver model), but filesystems decided to use them and that usage just grew over the years. That's evolution for you...> thanks,
greg k-h
Greg KH
maintainerRe: [PATCH 2/3] erofs: convert to use kobject_is_added()
Quoted 32 lines · Gao Xiang
Hi Greg,
Quoted 10 lines · Greg KH
Quoted 9 lines · Yangtao Li
Use
kobject_is_added()instead of directly accessing the internal variables of kobject. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/erofs/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)Quoted 14 lines
fs/erofs/sysfs.c void erofs_unregister_sysfs(struct super_block *sb) · line 242
Quoted 10 lines
fs/erofs/sysfs.c +1
-2@@ -240,8 +240,7 @@ void erofs_unregister_sysfs(struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb); -if (sbi->s_kobj.state_in_sysfs) { -kobject_del (&sbi->s_kobj); +if (kobject_is_added (&sbi->s_kobj)) {I do not understand why this check is even needed, I do not think it should be there at all as obviously the kobject was registered if it now needs to not be registered.
I think Yangtao sent a new patchset which missed the whole previous background discussions as below:
https://lore.kernel.org/r/[email protected]
Quoted 5 lines
It's needed because once a syzbot complaint as below:
https://lore.kernel.org/r/CAD-N9QXNx=p3-QoWzk6pCznF32CZy8kM3vvo8mamfZZ9CpUKdw@mail.gmail.comI'd suggest including the previous backgrounds at least in the newer patchset, otherwise it makes me explain again and again...
That would be good, as I do not think this is correct, it should be fixed in a different way, see my response to the zonefs patch in this series as a much simpler method to use.
thanks,
greg k-h
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
Quoted 36 lines · Greg KH
Quoted 35 lines · Gao Xiang
Hi Greg,
Quoted 10 lines · Greg KH
Quoted 9 lines · Yangtao Li
Use
kobject_is_added()instead of directly accessing the internal variables of kobject. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/erofs/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)Quoted 14 lines
fs/erofs/sysfs.c void erofs_unregister_sysfs(struct super_block *sb) · line 242
Quoted 10 lines
fs/erofs/sysfs.c +1
-2@@ -240,8 +240,7 @@ void erofs_unregister_sysfs(struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb); -if (sbi->s_kobj.state_in_sysfs) { -kobject_del (&sbi->s_kobj); +if (kobject_is_added (&sbi->s_kobj)) {I do not understand why this check is even needed, I do not think it should be there at all as obviously the kobject was registered if it now needs to not be registered.
I think Yangtao sent a new patchset which missed the whole previous background discussions as below:
https://lore.kernel.org/r/[email protected]It's needed because once a syzbot complaint as below:
https://lore.kernel.org/r/CAD-N9QXNx=p3-QoWzk6pCznF32CZy8kM3vvo8mamfZZ9CpUKdw@mail.gmail.com
Quoted 6 lines
Quoted 2 lines
I'd suggest including the previous backgrounds at least in the newer patchset, otherwise it makes me explain again and again...
That would be good, as I do not think this is correct, it should be fixed in a different way, see my response to the zonefs patch in this series as a much simpler method to use.
Yes, but here (sbi->s_kobj) is not a kobject pointer (also at a quick glance it seems that zonefs has similar code), and also we couldn't just check the sbi is NULL or not here only, since sbi is already non-NULL in this path and there are some others in sbi to free in other functions.
s_kobj could be changed into a pointer if needed. I'm all fine with either way since as you said, it's a boilerplate filesystem kobject logic duplicated from somewhere. Hopefully Yangtao could help take this task since he sent me patches about this multiple times.
Thanks,
Gao Xiang
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerRe: [PATCH 2/3] erofs: convert to use kobject_is_added()
Quoted 52 lines · Gao Xiang
Quoted 36 lines · Greg KH
Quoted 35 lines · Gao Xiang
Hi Greg,
Quoted 10 lines · Greg KH
Quoted 9 lines · Yangtao Li
Use
kobject_is_added()instead of directly accessing the internal variables of kobject. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/erofs/sysfs.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)Quoted 14 lines
fs/erofs/sysfs.c void erofs_unregister_sysfs(struct super_block *sb) · line 242
Quoted 10 lines
fs/erofs/sysfs.c +1
-2@@ -240,8 +240,7 @@ void erofs_unregister_sysfs(struct super_block *sb) {struct erofs_sb_info *sbi =EROFS_SB (sb); -if (sbi->s_kobj.state_in_sysfs) { -kobject_del (&sbi->s_kobj); +if (kobject_is_added (&sbi->s_kobj)) {I do not understand why this check is even needed, I do not think it should be there at all as obviously the kobject was registered if it now needs to not be registered.
I think Yangtao sent a new patchset which missed the whole previous background discussions as below:
https://lore.kernel.org/r/[email protected]It's needed because once a syzbot complaint as below:
https://lore.kernel.org/r/CAD-N9QXNx=p3-QoWzk6pCznF32CZy8kM3vvo8mamfZZ9CpUKdw@mail.gmail.comQuoted 6 lines
Quoted 2 lines
I'd suggest including the previous backgrounds at least in the newer patchset, otherwise it makes me explain again and again...
That would be good, as I do not think this is correct, it should be fixed in a different way, see my response to the zonefs patch in this series as a much simpler method to use.
Yes, but here (
sbi->s_kobj) is not a kobject pointer (also at a quick glance it seems that zonefs has similar code), and also we couldn't just check the sbi is NULL or not here only, since sbi is already non-NULL in this path and there are some others in sbi to free in other functions.
Quoted 4 lines
s_kobjcould be changed into a pointer if needed. I'm all fine with either way since as you said, it's a boilerplate filesystem kobject logic duplicated from somewhere. Hopefully Yangtao could help take this task since he sent me patches about this multiple times.
I made the same mistake with the zonefs code. If the kobject in this structure controls the lifespan of it (which makes it not a pointer, my mistake), then that whole memory chunk can't be valid anymore if the kobject registering function failed so you need to get rid of it then, not later.
thanks,
greg k-h
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
In reply to Greg KHQuoted 4 lines
Meta-comment, we need to come up with a "filesystem kobject type" to get rid of lots of the boilerplate filesystem kobject logic as it's duplicated in every filesystem in tiny different ways and lots of times (like here), it's wrong.
Can we add the following structure?
struct filesystem_kobject {
struct kobject kobject;
struct completion unregister;
};w/ it, we can simplify something:
1. merge init_completion and kobject_init_and_add
2. merge kobject_put and wait_for_completion
3. we can have a common release func for kobj_type release
MBR,
Yangtao
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
Quoted 12 lines
Quoted 4 lines
Meta-comment, we need to come up with a "filesystem kobject type" to get rid of lots of the boilerplate filesystem kobject logic as it's duplicated in every filesystem in tiny different ways and lots of times (like here), it's wrong.
Can we add the following structure?
struct filesystem_kobject { struct kobject kobject; struct completion unregister; };
Quoted 7 lines
w/ it, we can simplify something:
1. merge
init_completionandkobject_init_and_add2. merge
kobject_putandwait_for_completion3. we can have a common release func for
kobj_typerelease
It seems that the above ideas are not crazy enough, maybe we should do more. Any ideas and suggestions are very welcome.
MBR,
Yangtao
Greg KH
maintainerRe: [PATCH 2/3] erofs: convert to use kobject_is_added()
In reply to Yangtao LiQuoted 11 lines · Yangtao Li
Quoted 4 lines
Meta-comment, we need to come up with a "filesystem kobject type" to get rid of lots of the boilerplate filesystem kobject logic as it's duplicated in every filesystem in tiny different ways and lots of times (like here), it's wrong.
Can we add the following structure?
struct filesystem_kobject { struct kobject kobject; struct completion unregister; };
Ah, no, I see the problem.
The filesystem authors are treating the kobject NOT as the thing that handles the lifespan of the structure it is embedded in, but rather as something else (i.e. a convient place to put filesystem information to userspace.)
That isn't going to work, and as proof of that, the release callback should be a simple call to kfree(), NOT as a completion notification which then something else will go off and free the memory here. That implies that there are multiple reference counting structures happening on the same structure, which is not ok.
Either we let the kobject code properly handle the lifespan of the structure, OR we pull it out of the structure and just let it hang off as a separate structure (i.e. a pointer to something else.)
As the superblock lifespan rules ALREADY control the reference counting logic of the filesystem superblock structure, let's stick with that and just tack-on the kobject as a separate structure entirely.
Does that make sense? Let me do a quick pass at this for zonefs as that's pretty simple to show you what I mean...
thanks,
greg k-h
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
Hi Greg,
Quoted 5 lines
That isn't going to work, and as proof of that, the release callback should be a simple call to
kfree(), NOT as a completion notification which then something else will go off and free the memory here. That implies that there are multiple reference counting structures happening on the same structure, which is not ok.
The release() function did nothing inside, but we need to wait asynchronously...
Can we directly export the kobject_cleanup(kobj) interface so that kobj_type->release() doesn't have to do anything?
If do it, the use of init_completion, wait_for_completion, etc. will no longer be needed.
Quoted 2 lines
OR we pull it out of the structure and just let it hang off as a separate structure (i.e. a pointer to something else.)
Make something like sbi->s_kobj a pointer instead of data embedded in sbi? When kobject_init_and_add fails, call kobject_put(sbi->s_kobj), and assign sbi->s_kobj = NULL at the same time?
Thx,
Yangtao
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
In reply to Greg KHHi Greg,
Quoted 1 line
just let it hang off as a separate structure (i.e. a pointer to something else.)
I have made some attempts. According to my understanding, the reason why the filesystem needs to embed the kobj structure (not a pointer) is that the kobj_to_sbi method is required in the attr_store/attr_show method for subsequent data processing.
130 static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
131 const char *buf, size_t len)
132 {
133 struct erofs_sb_info *sbi = container_of(kobj, struct erofs_sb_info,
134 s_kobj);
If we turn the kobject in sbi into a pointer, then we need to insert a pointer to sbi in the kobject, or perform the following encapsulation.
struct filesystem_kobject {
struct kobject kobject;
void *private;
};Later, I thought I could send some demo code that strips the kobject in sbi into a pointer.
BTW, Now sysfs.c in many file systems is full of a lot of repetitive code, maybe we can abstract the common part? Like filesystem_attr、filesystem_kobject_ops、filesystem_kobject_ktype...
Thx,
Yangtao
Re: [PATCH 2/3] erofs: convert to use kobject_is_added()
In reply to Greg KHHi all,
Quoted 1 line
Later, I thought I could send some demo code that strips the kobject in sbi into a pointer.
I made the following modifications, not sure if I'm going the right way.
fs/erofs/internal.h +1 -2
@@ -165,8 +165,7 @@ struct erofs_sb_info {u32 feature_incompat;/* sysfs support */ -struct kobject s_kobj;/* /sys/fs/erofs/<devname> */ -struct completion s_kobj_unregister; +struct filesystem_kobject *f_kobj;/* fscache support */ struct fscache_volume *volume;
fs/erofs/sysfs.c +53 -17
118 lines not on this page · raw message
PATCH 3/3 zonefs: convert to use kobject_is_added()
In reply to Yangtao LiUse kobject_is_added() instead of local s_sysfs_registered variables. BTW kill kobject_del() directly, because kobject_put() actually covers kobject removal automatically.
--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)
fs/zonefs/sysfs.c +5 -6
@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; } - sbi->s_sysfs_registered = true; -return 0 ; }
@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered) +if (!sbi)return ; -kobject_del (&sbi->s_kobj); -kobject_put (&sbi->s_kobj); -wait_for_completion (&sbi->s_kobj_unregister); +if (kobject_is_added (&sbi->s_kobj)) { +kobject_put (&sbi->s_kobj); +wait_for_completion (&sbi->s_kobj_unregister); + } }int __init zonefs_sysfs_init (void )
fs/zonefs/zonefs.h +0 -1
@@ -238,7 +238,6 @@ struct zonefs_sb_info {unsigned int s_max_active_seq_files;atomic_t s_active_seq_files; -bool s_sysfs_registered;struct kobject s_kobj;struct completion s_kobj_unregister; };
Signature · 1 line
2.35.1
Greg KH
maintainerRe: [PATCH 3/3] zonefs: convert to use kobject_is_added()
Quoted 15 lines · Yangtao Li
Use
kobject_is_added()instead of locals_sysfs_registeredvariables. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)fs/zonefs/sysfs.c +0
-0
fs/zonefs/sysfs.c int zonefs_sysfs_register(struct super_block *sb) · line 104
Quoted 4 lines
fs/zonefs/sysfs.c +0
-1@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; } - sbi->s_sysfs_registered = true;
You know this, why do you need to have a variable tell you this or not?
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 113
Quoted 9 lines
fs/zonefs/sysfs.c +0
-2-return 0 ; }@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered)
How can either of these ever be true? Note, sbi should be passed here to this function, not the super block as that is now unregistered from the system. Looks like no one has really tested this codepath that much :(
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 112
Quoted 2 lines
fs/zonefs/sysfs.c +1
-0+if (!sbi)return ;
this can not ever be true, right?
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 114
Quoted 5 lines
fs/zonefs/sysfs.c +1
-3-kobject_del (&sbi->s_kobj); -kobject_put (&sbi->s_kobj); -wait_for_completion (&sbi->s_kobj_unregister); +if (kobject_is_added (&sbi->s_kobj)) {
Again, not needed.
thanks,
greg k-h
Re: [PATCH 3/3] zonefs: convert to use kobject_is_added()
Quoted 19 lines · Greg KH
Quoted 18 lines · Yangtao Li
Use
kobject_is_added()instead of locals_sysfs_registeredvariables. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)fs/zonefs/sysfs.c +0
-0@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; }
Quoted 3 lines
fs/zonefs/sysfs.c int zonefs_sysfs_register(struct super_block *sb) · line 104
Quoted 1 line
fs/zonefs/sysfs.c +0
-1- sbi->s_sysfs_registered = true;You know this, why do you need to have a variable tell you this or not?
If kobject_init_and_add() fails, zonefs_sysfs_register() returns an error and fill_super will also return that error. vfs will then call kill_super, which calls zonefs_sysfs_unregister(). For that case, we need to know that we actually added the kobj.
Quoted 16 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 113
Quoted 10 lines
fs/zonefs/sysfs.c +0
-2-return 0 ; }@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered)How can either of these ever be true? Note, sbi should be passed here to this function, not the super block as that is now unregistered from the system. Looks like no one has really tested this codepath that much :(
Quoted 4 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 112
Quoted 2 lines
fs/zonefs/sysfs.c +1
-0+if (!sbi)return ;this can not ever be true, right?
Yes it can, if someone attempt to mount a non zoned device. In that case, fill_super returns early without setting sb->s_fs_info but vfs still calls kill_super.
Quoted 13 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 114
Quoted 7 lines
fs/zonefs/sysfs.c +1
-3-kobject_del (&sbi->s_kobj); -kobject_put (&sbi->s_kobj); -wait_for_completion (&sbi->s_kobj_unregister); +if (kobject_is_added (&sbi->s_kobj)) {Again, not needed.
thanks,
greg k-h
Greg KH
maintainerRe: [PATCH 3/3] zonefs: convert to use kobject_is_added()
Quoted 20 lines · Damien Le Moal
Quoted 19 lines · Greg KH
Quoted 18 lines · Yangtao Li
Use
kobject_is_added()instead of locals_sysfs_registeredvariables. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)fs/zonefs/sysfs.c +0
-0@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; }
Quoted 8 lines
Quoted 3 lines
fs/zonefs/sysfs.c int zonefs_sysfs_register(struct super_block *sb) · line 104
Quoted 1 line
fs/zonefs/sysfs.c +0
-1- sbi->s_sysfs_registered = true;You know this, why do you need to have a variable tell you this or not?
If
kobject_init_and_add()fails,zonefs_sysfs_register()returns an error andfill_superwill also return that error. vfs will then callkill_super, which callszonefs_sysfs_unregister(). For that case, we need to know that we actually added the kobj.
Ok, but then why not just 0 out the kobject pointer here instead? That way you will always know if it's a valid pointer or not and you don't have to rely on some other variable? Use the one that you have already :)
And you really don't even need to check anything, just pass in NULL to kobject_del() and friends, it should handle it.
Quoted 15 lines
Quoted 15 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 113
Quoted 9 lines
fs/zonefs/sysfs.c +0
-2-return 0 ; }@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered)How can either of these ever be true? Note, sbi should be passed here to this function, not the super block as that is now unregistered from the system. Looks like no one has really tested this codepath that much :(
Quoted 8 lines
Quoted 4 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 112
Quoted 2 lines
fs/zonefs/sysfs.c +1
-0+if (!sbi)return ;this can not ever be true, right?
Yes it can, if someone attempt to mount a non zoned device. In that case,
fill_superreturns early without settingsb->s_fs_infobut vfs still callskill_super.
But you already had a sbi pointer in the place that this was called, so you "know" if you need to even call into here or not. You are having to look up the same pointer multiple times in this call chain, there's no need for that.
thanks,
greg k-h
Re: [PATCH 3/3] zonefs: convert to use kobject_is_added()
Quoted 25 lines · Greg KH
Quoted 24 lines · Damien Le Moal
Quoted 22 lines · Greg KH
Quoted 15 lines · Yangtao Li
Use
kobject_is_added()instead of locals_sysfs_registeredvariables. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)fs/zonefs/sysfs.c +0
-0fs/zonefs/sysfs.c int zonefs_sysfs_register(struct super_block *sb) · line 104
Quoted 4 lines
fs/zonefs/sysfs.c +0
-1@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; } - sbi->s_sysfs_registered = true;You know this, why do you need to have a variable tell you this or not?
Quoted 8 lines
Quoted 4 lines
If
kobject_init_and_add()fails,zonefs_sysfs_register()returns an error andfill_superwill also return that error. vfs will then callkill_super, which callszonefs_sysfs_unregister(). For that case, we need to know that we actually added the kobj.Ok, but then why not just 0 out the kobject pointer here instead? That way you will always know if it's a valid pointer or not and you don't have to rely on some other variable? Use the one that you have already :)
but sbi->s_kobj is the kobject itself, not a pointer. I can still zero it out in case of error to avoid using the added s_sysfs_registered bool. I would need to check a field of s_kobj though, which is not super clean and makes the code dependent on kobject internals. Not super nice in my opinion, unless I am missing something.
Quoted 22 lines
And you really don't even need to check anything, just pass in NULL to
kobject_del()and friends, it should handle it.>Quoted 20 lines
Quoted 19 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 113
Quoted 9 lines
fs/zonefs/sysfs.c +0
-2-return 0 ; }@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered)How can either of these ever be true? Note, sbi should be passed here to this function, not the super block as that is now unregistered from the system. Looks like no one has really tested this codepath that much :(
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 112
Quoted 2 lines
fs/zonefs/sysfs.c +1
-0+if (!sbi)return ;this can not ever be true, right?
Quoted 8 lines
Quoted 3 lines
Yes it can, if someone attempt to mount a non zoned device. In that case,
fill_superreturns early without settingsb->s_fs_infobut vfs still callskill_super.But you already had a sbi pointer in the place that this was called, so you "know" if you need to even call into here or not. You are having to look up the same pointer multiple times in this call chain, there's no need for that.
I am not following here. Either we check that we have sbi here in zonefs_sysfs_unregister(), or we conditionally call this function in zonefs_kill_super() with a "if (sbi)". Either way, we need to check since sbi can be NULL.
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerRe: [PATCH 3/3] zonefs: convert to use kobject_is_added()
Quoted 31 lines · Damien Le Moal
Quoted 30 lines · Greg KH
Quoted 28 lines · Damien Le Moal
Quoted 19 lines · Greg KH
Quoted 18 lines · Yangtao Li
Use
kobject_is_added()instead of locals_sysfs_registeredvariables. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)fs/zonefs/sysfs.c +0
-0@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; }Quoted 3 lines
fs/zonefs/sysfs.c int zonefs_sysfs_register(struct super_block *sb) · line 104
Quoted 1 line
fs/zonefs/sysfs.c +0
-1- sbi->s_sysfs_registered = true;You know this, why do you need to have a variable tell you this or not?
If
kobject_init_and_add()fails,zonefs_sysfs_register()returns an error andfill_superwill also return that error. vfs will then callkill_super, which callszonefs_sysfs_unregister(). For that case, we need to know that we actually added the kobj.
Quoted 5 lines
Quoted 3 lines
Ok, but then why not just 0 out the kobject pointer here instead? That way you will always know if it's a valid pointer or not and you don't have to rely on some other variable? Use the one that you have already :)
but
sbi->s_kobjis the kobject itself, not a pointer.
Then it should not be there if the kobject is not valid as it should have been freed when the kobject_init_and_add() call failed, right?
Quoted 5 lines
I can still zero it out in
case of error to avoid using the addeds_sysfs_registeredbool. I would need to check a field ofs_kobjthough, which is not super clean and makes the code dependent on kobject internals. Not super nice in my opinion, unless I am missing something.
See above, if a kobject fails to be registered, just remove the whole object as it's obviously "dead" now and you can not trust it.
Quoted 31 lines
Quoted 22 lines
And you really don't even need to check anything, just pass in NULL to
kobject_del()and friends, it should handle it.>Quoted 20 lines
Quoted 19 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 113
Quoted 9 lines
fs/zonefs/sysfs.c +0
-2-return 0 ; }@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered)How can either of these ever be true? Note, sbi should be passed here to this function, not the super block as that is now unregistered from the system. Looks like no one has really tested this codepath that much :(
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 112
Quoted 2 lines
fs/zonefs/sysfs.c +1
-0+if (!sbi)return ;this can not ever be true, right?
Quoted 8 lines
Quoted 3 lines
Yes it can, if someone attempt to mount a non zoned device. In that case,
fill_superreturns early without settingsb->s_fs_infobut vfs still callskill_super.But you already had a sbi pointer in the place that this was called, so you "know" if you need to even call into here or not. You are having to look up the same pointer multiple times in this call chain, there's no need for that.
Quoted 4 lines
I am not following here. Either we check that we have sbi here in
zonefs_sysfs_unregister(), or we conditionally call this function inzonefs_kill_super()with a "if (sbi)". Either way, we need to check since sbi can be NULL.
In zonefs_kill_super() you have get the spi at the top of the function, so use that, don't make zonefs_sysfs_unregister() have to compute it again.
But again, if the kobject fails to be registered, you have to treat the memory contained there as not valid and get rid of it as soon as possible.
thanks,
greg k-h
Re: [PATCH 3/3] zonefs: convert to use kobject_is_added()
Quoted 32 lines · Greg KH
Quoted 31 lines · Damien Le Moal
Quoted 30 lines · Greg KH
Quoted 28 lines · Damien Le Moal
Quoted 19 lines · Greg KH
Quoted 18 lines · Yangtao Li
Use
kobject_is_added()instead of locals_sysfs_registeredvariables. BTW killkobject_del()directly, becausekobject_put()actually covers kobject removal automatically.Signed-off-by Yangtao Li <[email protected]>--- fs/zonefs/sysfs.c | 11 +++++------ fs/zonefs/zonefs.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-)fs/zonefs/sysfs.c +0
-0@@ -101,8 +101,6 @@ int zonefs_sysfs_register(struct super_block *sb)return ret; }Quoted 3 lines
fs/zonefs/sysfs.c int zonefs_sysfs_register(struct super_block *sb) · line 104
Quoted 1 line
fs/zonefs/sysfs.c +0
-1- sbi->s_sysfs_registered = true;You know this, why do you need to have a variable tell you this or not?
If
kobject_init_and_add()fails,zonefs_sysfs_register()returns an error andfill_superwill also return that error. vfs will then callkill_super, which callszonefs_sysfs_unregister(). For that case, we need to know that we actually added the kobj.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Ok, but then why not just 0 out the kobject pointer here instead? That way you will always know if it's a valid pointer or not and you don't have to rely on some other variable? Use the one that you have already :)
but
sbi->s_kobjis the kobject itself, not a pointer.Then it should not be there if the kobject is not valid as it should have been freed when the
kobject_init_and_add()call failed, right?
What do you mean freed ? the kboject itself is a field of zonefs sbi. So the kobject gets freed together with sbi.
Quoted 8 lines
Quoted 5 lines
I can still zero it out in
case of error to avoid using the addeds_sysfs_registeredbool. I would need to check a field ofs_kobjthough, which is not super clean and makes the code dependent on kobject internals. Not super nice in my opinion, unless I am missing something.See above, if a kobject fails to be registered, just remove the whole object as it's obviously "dead" now and you can not trust it.
Well yes, that is what s_sysfs_registered indicates, that the kobject is not valid. I do not understand what you mean with "just remove the whole object".
Quoted 31 lines
Quoted 31 lines
Quoted 30 lines
And you really don't even need to check anything, just pass in NULL to
kobject_del()and friends, it should handle it.>Quoted 15 lines
Quoted 15 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 113
Quoted 9 lines
fs/zonefs/sysfs.c +0
-2-return 0 ; }@@ -110,12 +108,13 @@ void zonefs_sysfs_unregister(struct super_block *sb) {struct zonefs_sb_info *sbi =ZONEFS_SB (sb); -if (!sbi || !sbi->s_sysfs_registered)How can either of these ever be true? Note, sbi should be passed here to this function, not the super block as that is now unregistered from the system. Looks like no one has really tested this codepath that much :(
Quoted 8 lines
Quoted 4 lines
fs/zonefs/sysfs.c void zonefs_sysfs_unregister(struct super_block *sb) · line 112
Quoted 2 lines
fs/zonefs/sysfs.c +1
-0+if (!sbi)return ;this can not ever be true, right?
Yes it can, if someone attempt to mount a non zoned device. In that case,
fill_superreturns early without settingsb->s_fs_infobut vfs still callskill_super.But you already had a sbi pointer in the place that this was called, so you "know" if you need to even call into here or not. You are having to look up the same pointer multiple times in this call chain, there's no need for that.
Quoted 8 lines
Quoted 4 lines
I am not following here. Either we check that we have sbi here in
zonefs_sysfs_unregister(), or we conditionally call this function inzonefs_kill_super()with a "if (sbi)". Either way, we need to check since sbi can be NULL.In
zonefs_kill_super()you have get the spi at the top of the function, so use that, don't makezonefs_sysfs_unregister()have to compute it again.
That I can do, yes.
Quoted 4 lines
But again, if the kobject fails to be registered, you have to treat the memory contained there as not valid and get rid of it as soon as possible.
If the kobject add failed, we never touch it thanks to s_sysfs_registered. I still do not see the issue here.
Quoted 4 lines
thanks,
greg k-h
Greg KH
maintainerQuoted 13 lines · Yangtao Li
Add
kobject_is_added()to avoid consumers from directly accessing the internal variables of kobject.Signed-off-by Yangtao Li <[email protected]>--- include/linux/kobject.h | 5 +++++ 1 file changed, 5 insertions(+)include/linux/kobject.h +0
-0
include/linux/kobject.h static inline const struct kobj_type *get_ktype(const struct kobject *kobj) · line 210
Quoted 8 lines
include/linux/kobject.h +5
-0@@ -203,6 +203,11 @@ static inline const struct kobj_type *get_ktype(const struct kobject *kobj)return kobj->ktype; } +static inline int kobject_is_added (struct kobject *kobj) +{ +return kobj->state_in_sysfs; +} +
No, this implies that the caller is not doing something correctly as it should always know if it has added a kobject or not. Let me review the please where you used this to find the problems there...
thanks,
greg k-h