[PATCH 2/4] sysfs: check group visibility upfront in sysfs_group_change_owner()
From: Dmitry Torokhov
Date: Mon Nov 16 2026 - 04:43:15 EST
For hidden named attribute groups, looking up the group subdirectory in
sysfs_group_change_owner() fails with -ENOENT before attribute
visibility is ever checked inside sysfs_group_attrs_change_owner(). In
addition, checking SYSFS_GROUP_INVISIBLE inside
sysfs_group_attrs_change_owner() still updates ownership on the
kobject's own directory node for hidden unnamed groups, and breaking out
of the regular attributes loop still falls through to binary attributes.
Check group visibility upfront in sysfs_group_change_owner() using
__first_visible() and return 0 immediately when SYSFS_GROUP_INVISIBLE is
returned. Remove the now-redundant SYSFS_GROUP_INVISIBLE checks from
sysfs_group_attrs_change_owner(), which has no other callers.
Fixes: c7fbb8218b4a ("sysfs: check visibility before changing group attribute ownership")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
fs/sysfs/group.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index cc03229f8a94..290e70034329 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -520,8 +520,6 @@ static int sysfs_group_attrs_change_owner(struct kobject *kobj,
mode = grp->is_visible(kobj, *attr, i);
else
mode = grp->is_visible_const(kobj, *attr, i);
- if (mode == SYSFS_GROUP_INVISIBLE)
- break;
if (!mode)
continue;
}
@@ -542,8 +540,6 @@ static int sysfs_group_attrs_change_owner(struct kobject *kobj,
for (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) {
if (grp->is_bin_visible) {
mode = grp->is_bin_visible(kobj, *bin_attr, i);
- if (mode == SYSFS_GROUP_INVISIBLE)
- break;
if (!mode)
continue;
}
@@ -585,6 +581,9 @@ int sysfs_group_change_owner(struct kobject *kobj,
if (!kobj->state_in_sysfs)
return -EINVAL;
+ if (__first_visible(grp, kobj) == SYSFS_GROUP_INVISIBLE)
+ return 0;
+
if (grp->name) {
grp_kn = kernfs_find_and_get(kobj->sd, grp->name);
} else {
--
2.56.0.679.gc527c65306-goog