samples/kobject: Constify kobject attributes

Demonstrate that 'const struct kobj_attribute' is now supported by the
kobject core.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://patch.msgid.link/20260707-sysfs-const-attr-kobj-attr-prep-v2-4-35ae1bc0f9ef@weissschuh.net
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Thomas Weißschuh
2026-07-11 20:38:01 +02:00
committed by Danilo Krummrich
parent ed96337a5b
commit 584afc86a1
+9 -9
View File
@@ -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;