[PATCH 4/4] sysfs: simplify error handling in create_files()

From: Dmitry Torokhov

Date: Mon Nov 16 2026 - 04:43:24 EST


Both attribute creation loops in create_files() duplicate the same error
cleanup pattern by breaking out of the loop and checking the error
status afterward to remove created files.

Consolidate error cleanup into a single exit path that removes the
group's files and returns the error, returning 0 directly on success.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
fs/sysfs/group.c | 22 ++++++++++------------
1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index 699c2f9e8926..b90ba546dcda 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -51,10 +51,10 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
{
struct attribute *const *attr;
const struct bin_attribute *const *bin_attr;
- int error = 0, i;
+ int error, i;

if (grp->attrs) {
- for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
+ for (i = 0, attr = grp->attrs; *attr; i++, attr++) {
umode_t mode = (*attr)->mode;

/*
@@ -81,11 +81,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
error = sysfs_add_file_mode_ns(parent, *attr, mode, uid,
gid, NULL);
if (unlikely(error))
- break;
- }
- if (error) {
- remove_files(parent, grp);
- goto exit;
+ goto err_remove_files;
}
}

@@ -113,13 +109,15 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
error = sysfs_add_bin_file_mode_ns(parent, *bin_attr,
mode, size, uid, gid,
NULL);
- if (error)
- break;
+ if (unlikely(error))
+ goto err_remove_files;
}
- if (error)
- remove_files(parent, grp);
}
-exit:
+
+ return 0;
+
+err_remove_files:
+ remove_files(parent, grp);
return error;
}


--
2.56.0.679.gc527c65306-goog