[PATCH 3/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups
From: Dmitry Torokhov
Date: Mon Nov 16 2026 - 04:43:13 EST
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@xxxxxxxxx>
---
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