From: "Thomas Weißschuh" <linux@weissschuh.net>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>
Cc: driver-core@lists.linux.dev, linux-kernel@vger.kernel.org,
"Thomas Weißschuh" <linux@weissschuh.net>
Subject: [PATCH v3 4/5] driver core: Allow the constification of device attributes
Date: Tue, 12 May 2026 18:39:14 +0200 [thread overview]
Message-ID: <20260512-sysfs-const-attr-device_attr-prep-v3-4-cb7c17b34d52@weissschuh.net> (raw)
In-Reply-To: <20260512-sysfs-const-attr-device_attr-prep-v3-0-cb7c17b34d52@weissschuh.net>
Allow device 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@weissschuh.net>
---
drivers/base/core.c | 12 ++++++---
include/linux/device.h | 69 +++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 65 insertions(+), 16 deletions(-)
diff --git a/drivers/base/core.c b/drivers/base/core.c
index bd2ddf2aab50..6e2ee6d55a0b 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2419,9 +2419,11 @@ static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
if (dev_attr->show)
ret = dev_attr->show(dev, dev_attr, buf);
+ else if (dev_attr->show_const)
+ ret = dev_attr->show_const(dev, dev_attr, buf);
if (ret >= (ssize_t)PAGE_SIZE) {
- printk("dev_attr_show: %pS returned bad count\n",
- dev_attr->show);
+ printk("dev_attr_show: %pS/%pS returned bad count\n",
+ dev_attr->show, dev_attr->show_const);
}
return ret;
}
@@ -2435,6 +2437,8 @@ static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
if (dev_attr->store)
ret = dev_attr->store(dev, dev_attr, buf, count);
+ else if (dev_attr->store_const)
+ ret = dev_attr->store_const(dev, dev_attr, buf, count);
return ret;
}
@@ -3047,10 +3051,10 @@ int device_create_file(struct device *dev,
int error = 0;
if (dev) {
- WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
+ WARN(((attr->attr.mode & S_IWUGO) && !(attr->store || attr->store_const)),
"Attribute %s: write permission without 'store'\n",
attr->attr.name);
- WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
+ WARN(((attr->attr.mode & S_IRUGO) && !(attr->show || attr->show_const)),
"Attribute %s: read permission without 'show'\n",
attr->attr.name);
error = sysfs_create_file(&dev->kobj, &attr->attr);
diff --git a/include/linux/device.h b/include/linux/device.h
index 17150b0dfcd6..354f54be02b9 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -104,10 +104,18 @@ struct device_type {
*/
struct device_attribute {
struct attribute attr;
- ssize_t (*show)(struct device *dev, struct device_attribute *attr,
- char *buf);
- ssize_t (*store)(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t count);
+ __SYSFS_FUNCTION_ALTERNATIVE(
+ ssize_t (*show)(struct device *dev, struct device_attribute *attr,
+ char *buf);
+ ssize_t (*show_const)(struct device *dev, const struct device_attribute *attr,
+ char *buf);
+ );
+ __SYSFS_FUNCTION_ALTERNATIVE(
+ ssize_t (*store)(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count);
+ ssize_t (*store_const)(struct device *dev, const struct device_attribute *attr,
+ const char *buf, size_t count);
+ );
};
/**
@@ -135,11 +143,50 @@ ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
char *buf);
+typedef ssize_t __device_show_handler_const(struct device *dev, const struct device_attribute *attr,
+ char *buf);
+typedef ssize_t __device_store_handler_const(struct device *dev, const struct device_attribute *attr,
+ const char *buf, size_t count);
+
+#ifdef CONFIG_CFI
+
+#define __DEVICE_ATTR_SHOW_STORE(_show, _store) \
+ .show = _Generic(_show, \
+ __device_show_handler_const * : NULL, \
+ default : _show \
+ ), \
+ .show_const = _Generic(_show, \
+ __device_show_handler_const * : _show, \
+ default : NULL \
+ ), \
+ .store = _Generic(_store, \
+ __device_store_handler_const * : NULL, \
+ default : _store \
+ ), \
+ .store_const = _Generic(_store, \
+ __device_store_handler_const * : _store, \
+ default : NULL \
+ ),
+
+#else
+
+#define __DEVICE_ATTR_SHOW_STORE(_show, _store) \
+ .show = _Generic(_show, \
+ __device_show_handler_const * : (void *)_show, \
+ default : _show \
+ ), \
+ .store = _Generic(_store, \
+ __device_store_handler_const * : (void *)_store, \
+ default : _store \
+ ), \
+
+#endif
+
+
#define __DEVICE_ATTR(_name, _mode, _show, _store) { \
.attr = {.name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
- .show = _show, \
- .store = _store, \
+ __DEVICE_ATTR_SHOW_STORE(_show, _store) \
}
#define __DEVICE_ATTR_RO_MODE(_name, _mode) \
@@ -161,8 +208,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define __DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
.attr = {.name = __stringify(_name), .mode = _mode, \
.ignore_lockdep = true }, \
- .show = _show, \
- .store = _store, \
+ __DEVICE_ATTR_SHOW_STORE(_show, _store) \
}
#else
#define __DEVICE_ATTR_IGNORE_LOCKDEP __DEVICE_ATTR
@@ -221,8 +267,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define DEVICE_ATTR_RW_NAMED(_name, _attrname) \
struct device_attribute dev_attr_##_name = { \
.attr = { .name = _attrname, .mode = 0644 }, \
- .show = _name##_show, \
- .store = _name##_store, \
+ __DEVICE_ATTR_SHOW_STORE(_name##_show, _name##_store) \
}
/**
@@ -255,7 +300,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define DEVICE_ATTR_RO_NAMED(_name, _attrname) \
struct device_attribute dev_attr_##_name = { \
.attr = { .name = _attrname, .mode = 0444 }, \
- .show = _name##_show, \
+ __DEVICE_ATTR_SHOW_STORE(_name##_show, NULL) \
}
/**
@@ -279,7 +324,7 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define DEVICE_ATTR_WO_NAMED(_name, _attrname) \
struct device_attribute dev_attr_##_name = { \
.attr = { .name = _attrname, .mode = 0200 }, \
- .store = _name##_store, \
+ __DEVICE_ATTR_SHOW_STORE(NULL, _name##_store) \
}
/**
--
2.54.0
next prev parent reply other threads:[~2026-05-12 16:39 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-12 16:39 [PATCH v3 0/5] " Thomas Weißschuh
2026-05-12 16:39 ` [PATCH v3 1/5] driver core: Delete DEVICE_ATTR_PREALLOC() Thomas Weißschuh
2026-05-12 16:39 ` [PATCH v3 2/5] driver core: Add low-level macros for device attributes Thomas Weißschuh
2026-05-12 16:39 ` [PATCH v3 3/5] driver core: Stop using generic sysfs " Thomas Weißschuh
2026-05-12 16:39 ` Thomas Weißschuh [this message]
2026-05-26 14:52 ` [PATCH v3 4/5] driver core: Allow the constification of " Mark Brown
2026-05-26 15:25 ` Mark Brown
2026-05-26 15:32 ` Danilo Krummrich
2026-05-26 15:38 ` Mark Brown
2026-05-12 16:39 ` [PATCH v3 5/5] driver core: Constify core " Thomas Weißschuh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260512-sysfs-const-attr-device_attr-prep-v3-4-cb7c17b34d52@weissschuh.net \
--to=linux@weissschuh.net \
--cc=dakr@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®