]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
sysfs: introduce callback attribute_group::bin_size
authorThomas Weißschuh <linux@weissschuh.net>
Sun, 3 Nov 2024 17:03:31 +0000 (17:03 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 5 Nov 2024 13:00:28 +0000 (14:00 +0100)
Several drivers need to dynamically calculate the size of an binary
attribute. Currently this is done by assigning attr->size from the
is_bin_visible() callback.

This has drawbacks:
* It is not documented.
* A single attribute can be instantiated multiple times, overwriting the
  shared size field.
* It prevents the structure to be moved to read-only memory.

Introduce a new dedicated callback to calculate the size of the
attribute.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Acked-by: Krzysztof Wilczyński <kw@linux.com>
Link: https://lore.kernel.org/r/20241103-sysfs-const-bin_attr-v2-2-71110628844c@weissschuh.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/sysfs/group.c
include/linux/sysfs.h

index 45b2e92941da1f49dcc71af3781317c61480c956..8b01a7eda5fb3239e138372417d01967c7a3f122 100644 (file)
@@ -98,6 +98,8 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
                                if (!mode)
                                        continue;
                        }
+                       if (grp->bin_size)
+                               size = grp->bin_size(kobj, *bin_attr, i);
 
                        WARN(mode & ~(SYSFS_PREALLOC | 0664),
                             "Attribute %s: Invalid permissions 0%o\n",
index c4e64dc112063f7cb89bf66059d0338716089e87..4746cccb95898b24df6f53de9421ea7649b5568f 100644 (file)
@@ -87,6 +87,11 @@ do {                                                 \
  *             SYSFS_GROUP_VISIBLE() when assigning this callback to
  *             specify separate _group_visible() and _attr_visible()
  *             handlers.
+ * @bin_size:
+ *             Optional: Function to return the size of a binary attribute
+ *             of the group. Will be called repeatedly for each binary
+ *             attribute in the group. Overwrites the size field embedded
+ *             inside the attribute itself.
  * @attrs:     Pointer to NULL terminated list of attributes.
  * @bin_attrs: Pointer to NULL terminated list of binary attributes.
  *             Either attrs or bin_attrs or both must be provided.
@@ -97,6 +102,9 @@ struct attribute_group {
                                              struct attribute *, int);
        umode_t                 (*is_bin_visible)(struct kobject *,
                                                  struct bin_attribute *, int);
+       size_t                  (*bin_size)(struct kobject *,
+                                           const struct bin_attribute *,
+                                           int);
        struct attribute        **attrs;
        struct bin_attribute    **bin_attrs;
 };