[PATCH 3/4] kobject: Allow the constification of kobject attributes

From: Thomas Weißschuh

Date: Fri May 29 2026 - 13:40:08 EST


Allow kobject attribute to reside in read-only memory.
Both const and non-const attributes are handled by the utility macros
and attributes can be migrated one-by-one.

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
include/linux/kobject.h | 57 +++++++++++++++++++++++++++++++++++++++++++------
lib/kobject.c | 8 +++++--
2 files changed, 57 insertions(+), 8 deletions(-)

diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index 975a5361fb60..2c5b386d441c 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -138,17 +138,62 @@ struct kset_uevent_ops {

struct kobj_attribute {
struct attribute attr;
- ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
- char *buf);
- ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
- const char *buf, size_t count);
+ __SYSFS_FUNCTION_ALTERNATIVE(
+ ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf);
+ ssize_t (*show_const)(struct kobject *kobj, const struct kobj_attribute *attr,
+ char *buf);
+ );
+ __SYSFS_FUNCTION_ALTERNATIVE(
+ ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
+ const char *buf, size_t count);
+ ssize_t (*store_const)(struct kobject *kobj, const struct kobj_attribute *attr,
+ const char *buf, size_t count);
+ );
};

+typedef ssize_t __kobj_show_handler_const(struct kobject *kobj, const struct kobj_attribute *attr,
+ char *buf);
+typedef ssize_t __kobj_store_handler_const(struct kobject *kobj, const struct kobj_attribute *attr,
+ const char *buf, size_t count);
+
+#ifdef CONFIG_CFI
+
+#define __KOBJ_ATTR_SHOW_STORE(_show, _store) \
+ .show = _Generic(_show, \
+ __kobj_show_handler_const * : NULL, \
+ default : _show \
+ ), \
+ .show_const = _Generic(_show, \
+ __kobj_show_handler_const * : _show, \
+ default : NULL \
+ ), \
+ .store = _Generic(_store, \
+ __kobj_store_handler_const * : NULL, \
+ default : _store \
+ ), \
+ .store_const = _Generic(_store, \
+ __kobj_store_handler_const * : _store, \
+ default : NULL \
+ ),
+
+#else
+
+#define __KOBJ_ATTR_SHOW_STORE(_show, _store) \
+ .show = _Generic(_show, \
+ __kobj_show_handler_const * : (void *)_show, \
+ default : _show \
+ ), \
+ .store = _Generic(_store, \
+ __kobj_store_handler_const * : (void *)_store, \
+ default : _store \
+ ), \
+
+#endif
+
#define __KOBJ_ATTR(_name, _mode, _show, _store) { \
.attr = { .name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
- .show = _show, \
- .store = _store, \
+ __KOBJ_ATTR_SHOW_STORE(_show, _store) \
}

#define __KOBJ_ATTR_RO_MODE(_name, _mode) { \
diff --git a/lib/kobject.c b/lib/kobject.c
index 9c9ff0f5175f..e7b010a989fb 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -823,9 +823,11 @@ static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
struct kobj_attribute *kattr;
ssize_t ret = -EIO;

- kattr = container_of(attr, struct kobj_attribute, attr);
+ kattr = container_of_const(attr, struct kobj_attribute, attr);
if (kattr->show)
ret = kattr->show(kobj, kattr, buf);
+ else if (kattr->show_const)
+ ret = kattr->show_const(kobj, kattr, buf);
return ret;
}

@@ -835,9 +837,11 @@ static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
struct kobj_attribute *kattr;
ssize_t ret = -EIO;

- kattr = container_of(attr, struct kobj_attribute, attr);
+ kattr = container_of_const(attr, struct kobj_attribute, attr);
if (kattr->store)
ret = kattr->store(kobj, kattr, buf, count);
+ else if (kattr->store_const)
+ ret = kattr->store_const(kobj, kattr, buf, count);
return ret;
}


--
2.54.0