Skip to contentSkip to conversation

sysfs: don't remove existing directory on update failure

PATCH In review Greg Kroah-Hartman 3 participants 3 messages May 20 Driver core

lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 1/1 sysfs: don't remove existing directory on update failure

When sysfs_update_group() is called for a named group and create_files() fails (e.g. -ENOMEM), internal_create_group() calls kernfs_remove(kn) on the group directory. In the update path, kn was obtained via kernfs_find_and_get() and refers to a directory that already existed before this call. Removing it silently destroys a sysfs group that the caller did not create.

Only remove the directory if we created it ourselves. On update failure the directory remains as it is left empty by remove_files() inside create_files(), but can be repopulated by a retry.

Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Rajat Jain <[email protected]>
Fixes c855cf2759d2 ("sysfs: Fix internal_create_group() for named group updates")
Cc stable <[email protected]>
Assisted-by gkh_clanker_t1000
---
 fs/sysfs/group.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/sysfs/group.c +1 -1
index 182e54e575ee..4e1e4f18a166 100644
@@ -188,7 +188,7 @@ static int internal_create_group(struct kobject *kobj, int update,
 	kernfs_get(kn);
 	error = create_files(kn, kobj, uid, gid, grp, update);
 	if (error) {
-		if (grp->name)
+		if (grp->name && !update)
 			kernfs_remove(kn);
 	}
 	kernfs_put(kn);
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Greg Kroah-Hartman

When sysfs_update_group() is called for a named group and create_files() fails (e.g. -ENOMEM), internal_create_group() calls kernfs_remove(kn) on the group directory. In the update path, kn was obtained via kernfs_find_and_get() and refers to a directory that already existed before this call. Removing it silently destroys a sysfs group that the caller did not create.

Only remove the directory if we created it ourselves. On update failure the directory remains as it is left empty by remove_files() inside create_files(), but can be repopulated by a retry.

Quoted 7 lines
Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Rajat Jain <[email protected]>
Fixes c855cf2759d2 ("sysfs: Fix internal_create_group() for named group updates")
Cc stable <[email protected]>
Assisted-by gkh_clanker_t1000

No issues found, so

Quoted 20 lines
---
 fs/sysfs/group.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/sysfs/group.c +1 -2
index 182e54e575ee..4e1e4f18a166 100644
@@ -188,7 +188,7 @@ static int internal_create_group(struct kobject *kobj, int update,
        kernfs_get(kn);
        error = create_files(kn, kobj, uid, gid, grp, update);
        if (error) {
-               if (grp->name)
+               if (grp->name && !update)
                        kernfs_remove(kn);
        }
        kernfs_put(kn);
--

2.54.0

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Greg Kroah-Hartman
Quoted 11 lines · Greg Kroah-Hartman

When sysfs_update_group() is called for a named group and create_files() fails (e.g. -ENOMEM), internal_create_group() calls kernfs_remove(kn) on the group directory. In the update path, kn was obtained via kernfs_find_and_get() and refers to a directory that already existed before this call. Removing it silently destroys a sysfs group that the caller did not create.

Only remove the directory if we created it ourselves. On update failure the directory remains as it is left empty by remove_files() inside create_files(), but can be repopulated by a retry.

Quoted 7 lines
Cc "Rafael J. Wysocki" <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Rajat Jain <[email protected]>
Fixes c855cf2759d2 ("sysfs: Fix internal_create_group() for named group updates")
Cc stable <[email protected]>
Assisted-by gkh_clanker_t1000