[PATCH] kheaders: Simplify attribute through sysfs_bin_attr_simple_read()

Thomas Weißschuh posted 1 patch 1 year ago
kernel/kheaders.c | 19 +++----------------
1 file changed, 3 insertions(+), 16 deletions(-)
[PATCH] kheaders: Simplify attribute through sysfs_bin_attr_simple_read()
Posted by Thomas Weißschuh 1 year ago
The utility function from the sysfs core is sufficient to implement this
attribute. Make use of it.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
__BIN_ATTR() is used over the normal struct initializer because it is
shorter and can also handle the transition to const struct bin_attr
callbacks.
If you prefer, I can switch that back, but it will introduce a trivial
conflict with [0].

Note: There is no formal maintainer for that source file.

[0] https://lore.kernel.org/lkml/20241205-sysfs-const-bin_attr-simple-v1-0-4a4e4ced71e3@weissschuh.net/
---
 kernel/kheaders.c | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)

diff --git a/kernel/kheaders.c b/kernel/kheaders.c
index 42163c9e94e557c5043e2ac7987b567f23087ee2..de25d913e9473f14be70fb0f602c0202af4d92c9 100644
--- a/kernel/kheaders.c
+++ b/kernel/kheaders.c
@@ -29,25 +29,12 @@ asm (
 extern char kernel_headers_data[];
 extern char kernel_headers_data_end[];
 
-static ssize_t
-ikheaders_read(struct file *file,  struct kobject *kobj,
-	       struct bin_attribute *bin_attr,
-	       char *buf, loff_t off, size_t len)
-{
-	memcpy(buf, &kernel_headers_data[off], len);
-	return len;
-}
-
-static struct bin_attribute kheaders_attr __ro_after_init = {
-	.attr = {
-		.name = "kheaders.tar.xz",
-		.mode = 0444,
-	},
-	.read = &ikheaders_read,
-};
+static struct bin_attribute kheaders_attr __ro_after_init =
+	__BIN_ATTR(kheaders.tar.xz, 0444, sysfs_bin_attr_simple_read, NULL, 0);
 
 static int __init ikheaders_init(void)
 {
+	kheaders_attr.private = kernel_headers_data;
 	kheaders_attr.size = (kernel_headers_data_end -
 			      kernel_headers_data);
 	return sysfs_create_bin_file(kernel_kobj, &kheaders_attr);

---
base-commit: 2d8308bf5b67dff50262d8a9260a50113b3628c6
change-id: 20241215-sysfs-const-bin_attr-kheaders-440887f23892

Best regards,
-- 
Thomas Weißschuh <linux@weissschuh.net>

Re: [PATCH] kheaders: Simplify attribute through sysfs_bin_attr_simple_read()
Posted by Masahiro Yamada 12 months ago
On Mon, Dec 16, 2024 at 1:50 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> The utility function from the sysfs core is sufficient to implement this
> attribute. Make use of it.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> __BIN_ATTR() is used over the normal struct initializer because it is
> shorter and can also handle the transition to const struct bin_attr

 __BIN_ATTR_SIMPLE_RO() is even shorter.





--
Best Regards
Masahiro Yamada