From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Johan Hovold <johan@kernel.org>,
Jakub Kicinski <kuba@kernel.org>,
Fernando Fernandez Mancera <fmancera@suse.de>
Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH 3/4] sysfs: support SYSFS_GROUP_INVISIBLE for unnamed attribute groups
Date: Mon, 16 Nov 2026 01:42:55 -0800 [thread overview]
Message-ID: <20261116-sysfs-group-invisible-v1-3-94cd173a7d9a@gmail.com> (raw)
In-Reply-To: <20261116-sysfs-group-invisible-v1-0-94cd173a7d9a@gmail.com>
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
next prev parent reply other threads:[~2026-11-16 9:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-11-16 9:42 ` [PATCH 4/4] sysfs: simplify error handling in create_files() Dmitry Torokhov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116-sysfs-group-invisible-v1-3-94cd173a7d9a@gmail.com \
--to=dmitry.torokhov@gmail.com \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=fmancera@suse.de \
--cc=gregkh@linuxfoundation.org \
--cc=johan@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®