* [PATCH 0/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed groups and ownership updates
@ 2026-11-16 9:42 Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 1/4] sysfs: check for SYSFS_GROUP_INVISIBLE using equality comparison Dmitry Torokhov
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2026-11-16 9:42 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Jakub Kicinski, Fernando Fernandez Mancera
Cc: driver-core, linux-kernel
DEFINE_SYSFS_GROUP_VISIBLE() and DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE()
evaluate the group visibility callback only for the first attribute and
return SYSFS_GROUP_INVISIBLE when the entire group should be hidden.
While internal_create_group() checks for this return value before
creating or updating a named group's subdirectory, it skips that check
for unnamed attribute groups and proceeds directly to create_files(). As
a result, returning SYSFS_GROUP_INVISIBLE for an unnamed group only
skips the first attribute while still creating all remaining regular and
binary attributes, and leaves them behind on sysfs_update_group().
In addition, sysfs_group_change_owner() checks SYSFS_GROUP_INVISIBLE too
late inside sysfs_group_attrs_change_owner(), failing with -ENOENT when
looking up the nonexistent subdirectory of a hidden named group and
falling through to binary attributes when breaking out of the regular
attributes loop. Furthermore, SYSFS_GROUP_INVISIBLE is currently tested
and masked with bitwise operations even though it is a standalone
sentinel value rather than a permission bit.
Switch SYSFS_GROUP_INVISIBLE checks to equality comparisons, and check
group visibility upfront in both sysfs_group_change_owner() and
internal_create_group() for named and unnamed groups alike. Drop the
now-redundant SYSFS_GROUP_INVISIBLE checks from create_files() so that
returning it for attributes after the first one triggers the
invalid-permissions warning, and simplify error cleanup in
create_files().
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
Dmitry Torokhov (4):
sysfs: check for SYSFS_GROUP_INVISIBLE using equality comparison
sysfs: check group visibility upfront in sysfs_group_change_owner()
sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups
sysfs: simplify error handling in create_files()
fs/sysfs/group.c | 93 ++++++++++++++++++++++++++++-----------------------
include/linux/sysfs.h | 52 ++++++++++++++++------------
2 files changed, 81 insertions(+), 64 deletions(-)
---
base-commit: 84abb955b1015477aa11a96e2f3ac1b938b77f48
change-id: 20261115-sysfs-group-invisible-443ba270fd0a
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/4] sysfs: check for SYSFS_GROUP_INVISIBLE using equality comparison
2026-11-16 9:42 [PATCH 0/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed groups and ownership updates Dmitry Torokhov
@ 2026-11-16 9:42 ` Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 2/4] sysfs: check group visibility upfront in sysfs_group_change_owner() Dmitry Torokhov
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2026-11-16 9:42 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Jakub Kicinski, Fernando Fernandez Mancera
Cc: driver-core, linux-kernel
Unlike SYSFS_PREALLOC, which is a permission modifier bit OR'd with a
file mode, SYSFS_GROUP_INVISIBLE is a standalone sentinel return value
indicating that the entire attribute group should be hidden.
Replace bitwise checks and flag masking of SYSFS_GROUP_INVISIBLE with
direct equality comparisons so that any invalid combination of
SYSFS_GROUP_INVISIBLE with permission bits is not silently accepted and
instead triggers the invalid-permissions warning.
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
fs/sysfs/group.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index 4e1e4f18a166..cc03229f8a94 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -69,8 +69,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
mode = grp->is_visible(kobj, *attr, i);
else
mode = grp->is_visible_const(kobj, *attr, i);
- mode &= ~SYSFS_GROUP_INVISIBLE;
- if (!mode)
+ if (!mode || mode == SYSFS_GROUP_INVISIBLE)
continue;
}
@@ -100,8 +99,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
(*bin_attr)->attr.name);
if (grp->is_bin_visible) {
mode = grp->is_bin_visible(kobj, *bin_attr, i);
- mode &= ~SYSFS_GROUP_INVISIBLE;
- if (!mode)
+ if (!mode || mode == SYSFS_GROUP_INVISIBLE)
continue;
}
if (grp->bin_size)
@@ -151,7 +149,7 @@ static int internal_create_group(struct kobject *kobj, int update,
if (grp->name) {
umode_t mode = __first_visible(grp, kobj);
- if (mode & SYSFS_GROUP_INVISIBLE)
+ if (mode == SYSFS_GROUP_INVISIBLE)
mode = 0;
else
mode = S_IRWXU | S_IRUGO | S_IXUGO;
@@ -522,7 +520,7 @@ 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)
+ if (mode == SYSFS_GROUP_INVISIBLE)
break;
if (!mode)
continue;
@@ -544,7 +542,7 @@ 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)
+ if (mode == SYSFS_GROUP_INVISIBLE)
break;
if (!mode)
continue;
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/4] sysfs: check group visibility upfront in sysfs_group_change_owner()
2026-11-16 9:42 [PATCH 0/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed groups and ownership updates Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 1/4] sysfs: check for SYSFS_GROUP_INVISIBLE using equality comparison Dmitry Torokhov
@ 2026-11-16 9:42 ` Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 3/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 4/4] sysfs: simplify error handling in create_files() Dmitry Torokhov
3 siblings, 0 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2026-11-16 9:42 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Jakub Kicinski, Fernando Fernandez Mancera
Cc: driver-core, linux-kernel
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@gmail.com>
---
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups
2026-11-16 9:42 [PATCH 0/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed groups and ownership updates Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 1/4] sysfs: check for SYSFS_GROUP_INVISIBLE using equality comparison Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 2/4] sysfs: check group visibility upfront in sysfs_group_change_owner() Dmitry Torokhov
@ 2026-11-16 9:42 ` Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 4/4] sysfs: simplify error handling in create_files() Dmitry Torokhov
3 siblings, 0 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2026-11-16 9:42 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Jakub Kicinski, Fernando Fernandez Mancera
Cc: driver-core, linux-kernel
DEFINE_SYSFS_GROUP_VISIBLE() and DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE()
evaluate the group visibility callback only for the first attribute and
return SYSFS_GROUP_INVISIBLE when the group should be hidden. While
named attribute groups check this flag before creating or updating the
group subdirectory, unnamed attribute groups skip that check and proceed
directly to file creation.
As a result, skipping the entry that returns SYSFS_GROUP_INVISIBLE
during file creation hides just the first attribute while still creating
all remaining regular and binary attributes, and updating an unnamed
group to be invisible leaves those attributes behind.
Resolve the target node first and check group visibility in
internal_create_group() for both named and unnamed groups, removing any
existing attributes (and the subdirectory for named groups) when
updating and returning early before creating directories or files. Drop
the now-redundant SYSFS_GROUP_INVISIBLE checks from create_files() so
that the invalid-permissions warning triggers if SYSFS_GROUP_INVISIBLE
is mistakenly returned for attributes after the first one, and update
the attribute group documentation accordingly.
Fixes: d87c295f599c ("sysfs: Introduce a mechanism to hide static attribute_groups")
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
fs/sysfs/group.c | 66 +++++++++++++++++++++++++++++++--------------------
include/linux/sysfs.h | 52 +++++++++++++++++++++++-----------------
2 files changed, 70 insertions(+), 48 deletions(-)
diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c
index 290e70034329..699c2f9e8926 100644
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -69,7 +69,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
mode = grp->is_visible(kobj, *attr, i);
else
mode = grp->is_visible_const(kobj, *attr, i);
- if (!mode || mode == SYSFS_GROUP_INVISIBLE)
+ if (!mode)
continue;
}
@@ -99,7 +99,7 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
(*bin_attr)->attr.name);
if (grp->is_bin_visible) {
mode = grp->is_bin_visible(kobj, *bin_attr, i);
- if (!mode || mode == SYSFS_GROUP_INVISIBLE)
+ if (!mode)
continue;
}
if (grp->bin_size)
@@ -145,42 +145,56 @@ static int internal_create_group(struct kobject *kobj, int update,
return 0;
}
- kobject_get_ownership(kobj, &uid, &gid);
if (grp->name) {
- umode_t mode = __first_visible(grp, kobj);
-
- if (mode == SYSFS_GROUP_INVISIBLE)
- mode = 0;
- else
- mode = S_IRWXU | S_IRUGO | S_IXUGO;
-
if (update) {
kn = kernfs_find_and_get(kobj->sd, grp->name);
if (!kn) {
pr_debug("attr grp %s/%s not created yet\n",
kobj->name, grp->name);
- /* may have been invisible prior to this update */
+ /*
+ * The group may have been invisible prior to
+ * this update; treat it as a new creation if
+ * it becomes visible (or a no-op if it stays
+ * invisible).
+ */
update = 0;
- } else if (!mode) {
- sysfs_remove_group(kobj, grp);
- kernfs_put(kn);
- return 0;
}
}
+ } else {
+ kn = kobj->sd;
+ }
- if (!update) {
- if (!mode)
- return 0;
- kn = kernfs_create_dir_ns(kobj->sd, grp->name, mode,
- uid, gid, kobj, NULL);
- if (IS_ERR(kn)) {
- if (PTR_ERR(kn) == -EEXIST)
- sysfs_warn_dup(kobj->sd, grp->name);
- return PTR_ERR(kn);
+ /*
+ * If the group is hidden, remove any existing attributes (and the
+ * subdirectory for a previously visible named group) when updating,
+ * and return early.
+ */
+ if (__first_visible(grp, kobj) == SYSFS_GROUP_INVISIBLE) {
+ if (update) {
+ remove_files(kn, grp);
+ if (grp->name) {
+ kernfs_remove(kn);
+ kernfs_put(kn);
}
}
- } else {
- kn = kobj->sd;
+ return 0;
+ }
+
+ kobject_get_ownership(kobj, &uid, &gid);
+
+ /*
+ * Create the subdirectory on initial creation or when a previously
+ * hidden named group becomes visible on update.
+ */
+ if (grp->name && !update) {
+ kn = kernfs_create_dir_ns(kobj->sd, grp->name,
+ S_IRWXU | S_IRUGO | S_IXUGO,
+ uid, gid, kobj, NULL);
+ if (IS_ERR(kn)) {
+ if (PTR_ERR(kn) == -EEXIST)
+ sysfs_warn_dup(kobj->sd, grp->name);
+ return PTR_ERR(kn);
+ }
}
kernfs_get(kn);
diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h
index b1a3a1e6ad09..bd9a1f910bfa 100644
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -68,28 +68,33 @@ do { \
* struct attribute_group - data structure used to declare an attribute group.
* @name: Optional: Attribute group name
* If specified, the attribute group will be created in a
- * new subdirectory with this name. Additionally when a
- * group is named, @is_visible and @is_bin_visible may
- * return SYSFS_GROUP_INVISIBLE to control visibility of
- * the directory itself.
+ * new subdirectory with this name.
* @is_visible: Optional: Function to return permissions associated with an
* attribute of the group. Will be called repeatedly for
* each non-binary attribute in the group. Only read/write
- * permissions as well as SYSFS_PREALLOC are accepted. Must
- * return 0 if an attribute is not visible. The returned
- * value will replace static permissions defined in struct
- * attribute. Use SYSFS_GROUP_VISIBLE() when assigning this
- * callback to specify separate _group_visible() and
- * _attr_visible() handlers.
+ * permissions as well as SYSFS_PREALLOC are accepted,
+ * except for the first attribute where
+ * SYSFS_GROUP_INVISIBLE may be returned to hide the entire
+ * group (both the subdirectory for named groups and all
+ * attributes for unnamed groups). Must return 0 if an
+ * attribute is not visible. The returned value will
+ * replace static permissions defined in struct attribute.
+ * Use SYSFS_GROUP_VISIBLE() when assigning this callback to
+ * specify separate _group_visible() and _attr_visible()
+ * handlers.
* @is_bin_visible:
* Optional: Function to return permissions associated with a
* binary attribute of the group. Will be called repeatedly
* for each binary attribute in the group. Only read/write
- * permissions as well as SYSFS_PREALLOC (and the
- * visibility flags for named groups) are accepted. Must
- * return 0 if a binary attribute is not visible. The
- * returned value will replace static permissions defined
- * in struct bin_attribute. If @is_visible is not set, Use
+ * permissions as well as SYSFS_PREALLOC are accepted,
+ * except for the first binary attribute (when the group has
+ * no regular attributes or neither @is_visible nor
+ * @is_visible_const is set), where SYSFS_GROUP_INVISIBLE
+ * may be returned to hide the entire group. Must return 0
+ * if a binary attribute is not visible. The returned value
+ * will replace static permissions defined in struct
+ * bin_attribute. When group visibility is not already
+ * controlled by @is_visible/@is_visible_const, use
* SYSFS_GROUP_VISIBLE() when assigning this callback to
* specify separate _group_visible() and _attr_visible()
* handlers.
@@ -128,11 +133,13 @@ struct attribute_group {
/*
* DEFINE_SYSFS_GROUP_VISIBLE(name):
* A helper macro to pair with the assignment of ".is_visible =
- * SYSFS_GROUP_VISIBLE(name)", that arranges for the directory
- * associated with a named attribute_group to optionally be hidden.
- * This allows for static declaration of attribute_groups, and the
+ * SYSFS_GROUP_VISIBLE(name)", that arranges for an entire
+ * attribute_group to optionally be hidden (both the directory for
+ * named groups and all attributes for unnamed groups). This allows
+ * for static declaration of attribute_groups, and the
* simplification of attribute visibility lifetime that implies,
- * without polluting sysfs with empty attribute directories.
+ * without polluting sysfs with empty attribute directories or
+ * unwanted attributes.
* Ex.
*
* static umode_t example_attr_visible(struct kobject *kobj,
@@ -208,9 +215,10 @@ struct attribute_group {
/*
* Same as DEFINE_SYSFS_GROUP_VISIBLE, but for groups with only binary
- * attributes. If an attribute_group defines both text and binary
- * attributes, the group visibility is determined by the function
- * specified to is_visible() not is_bin_visible()
+ * attributes (or without is_visible()/is_visible_const()). If an
+ * attribute_group defines both regular and binary attributes and sets
+ * is_visible() or is_visible_const(), group visibility is determined by
+ * that callback rather than is_bin_visible().
*/
#define DEFINE_SYSFS_BIN_GROUP_VISIBLE(name) \
static inline umode_t sysfs_group_visible_##name( \
--
2.56.0.679.gc527c65306-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 4/4] sysfs: simplify error handling in create_files()
2026-11-16 9:42 [PATCH 0/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed groups and ownership updates Dmitry Torokhov
` (2 preceding siblings ...)
2026-11-16 9:42 ` [PATCH 3/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups Dmitry Torokhov
@ 2026-11-16 9:42 ` Dmitry Torokhov
3 siblings, 0 replies; 5+ messages in thread
From: Dmitry Torokhov @ 2026-11-16 9:42 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Johan Hovold, Jakub Kicinski, Fernando Fernandez Mancera
Cc: driver-core, linux-kernel
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@gmail.com>
---
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-16 9:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 9:42 [PATCH 0/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed groups and ownership updates Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 1/4] sysfs: check for SYSFS_GROUP_INVISIBLE using equality comparison Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 2/4] sysfs: check group visibility upfront in sysfs_group_change_owner() Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 3/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups Dmitry Torokhov
2026-11-16 9:42 ` [PATCH 4/4] sysfs: simplify error handling in create_files() Dmitry Torokhov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®