Skip to content

Commit bebf29b

Browse files
t-8chgregkh
authored andcommitted
sysfs: introduce callback attribute_group::bin_size
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 <[email protected]> Acked-by: Krzysztof Wilczyński <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0afcee1 commit bebf29b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

fs/sysfs/group.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
9898
if (!mode)
9999
continue;
100100
}
101+
if (grp->bin_size)
102+
size = grp->bin_size(kobj, *bin_attr, i);
101103

102104
WARN(mode & ~(SYSFS_PREALLOC | 0664),
103105
"Attribute %s: Invalid permissions 0%o\n",

include/linux/sysfs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ do { \
8787
* SYSFS_GROUP_VISIBLE() when assigning this callback to
8888
* specify separate _group_visible() and _attr_visible()
8989
* handlers.
90+
* @bin_size:
91+
* Optional: Function to return the size of a binary attribute
92+
* of the group. Will be called repeatedly for each binary
93+
* attribute in the group. Overwrites the size field embedded
94+
* inside the attribute itself.
9095
* @attrs: Pointer to NULL terminated list of attributes.
9196
* @bin_attrs: Pointer to NULL terminated list of binary attributes.
9297
* Either attrs or bin_attrs or both must be provided.
@@ -97,6 +102,9 @@ struct attribute_group {
97102
struct attribute *, int);
98103
umode_t (*is_bin_visible)(struct kobject *,
99104
struct bin_attribute *, int);
105+
size_t (*bin_size)(struct kobject *,
106+
const struct bin_attribute *,
107+
int);
100108
struct attribute **attrs;
101109
struct bin_attribute **bin_attrs;
102110
};

0 commit comments

Comments
 (0)