From 584afc86a1d44235ae4548d69efd40f649564233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Tue, 7 Jul 2026 18:56:35 +0200 Subject: [PATCH] samples/kobject: Constify kobject attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Demonstrate that 'const struct kobj_attribute' is now supported by the kobject core. Signed-off-by: Thomas Weißschuh Link: https://patch.msgid.link/20260707-sysfs-const-attr-kobj-attr-prep-v2-4-35ae1bc0f9ef@weissschuh.net Signed-off-by: Danilo Krummrich --- samples/kobject/kobject-example.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/samples/kobject/kobject-example.c b/samples/kobject/kobject-example.c index 7404f310dc45..36f3fbc47fe0 100644 --- a/samples/kobject/kobject-example.c +++ b/samples/kobject/kobject-example.c @@ -25,13 +25,13 @@ static int bar; /* * The "foo" file where a static variable is read from and written to. */ -static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr, +static ssize_t foo_show(struct kobject *kobj, const struct kobj_attribute *attr, char *buf) { return sysfs_emit(buf, "%d\n", foo); } -static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, +static ssize_t foo_store(struct kobject *kobj, const struct kobj_attribute *attr, const char *buf, size_t count) { int ret; @@ -44,14 +44,14 @@ static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, } /* Sysfs attributes cannot be world-writable. */ -static struct kobj_attribute foo_attribute = +static const struct kobj_attribute foo_attribute = __KOBJ_ATTR(foo, 0664, foo_show, foo_store); /* * More complex function where we determine which variable is being accessed by * looking at the attribute for the "baz" and "bar" files. */ -static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, +static ssize_t b_show(struct kobject *kobj, const struct kobj_attribute *attr, char *buf) { int var; @@ -63,7 +63,7 @@ static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, return sysfs_emit(buf, "%d\n", var); } -static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, +static ssize_t b_store(struct kobject *kobj, const struct kobj_attribute *attr, const char *buf, size_t count) { int var, ret; @@ -79,9 +79,9 @@ static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, return count; } -static struct kobj_attribute baz_attribute = +static const struct kobj_attribute baz_attribute = __KOBJ_ATTR(baz, 0664, b_show, b_store); -static struct kobj_attribute bar_attribute = +static const struct kobj_attribute bar_attribute = __KOBJ_ATTR(bar, 0664, b_show, b_store); @@ -89,7 +89,7 @@ static struct kobj_attribute bar_attribute = * Create a group of attributes so that we can create and destroy them all * at once. */ -static struct attribute *attrs[] = { +static const struct attribute *const attrs[] = { &foo_attribute.attr, &baz_attribute.attr, &bar_attribute.attr, @@ -103,7 +103,7 @@ static struct attribute *attrs[] = { * attribute group. */ static const struct attribute_group attr_group = { - .attrs = attrs, + .attrs_const = attrs, }; static struct kobject *example_kobj;