The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/4] kobject: Allow the constification of kobject attributes
@ 2026-07-07 16:56 Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 1/4] kobject: Provide macros to initialize 'struct kobj_attribute' Thomas Weißschuh
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-07-07 16:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Andrew Morton
  Cc: Thomas Weißschuh, driver-core, linux-kernel

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@weissschuh.net>
---
Changes in v2:
- Fix the definition of __KOBJ_ATTR_RO_MODE()
- Rebase on v7.2-rc1
- Link to v1: https://patch.msgid.link/20260529-sysfs-const-attr-kobj-attr-prep-v1-0-7e667ba3bb85@weissschuh.net

To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: "Rafael J. Wysocki" <rafael@kernel.org>
To: Danilo Krummrich <dakr@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: driver-core@lists.linux.dev
Cc: linux-kernel@vger.kernel.org

---
Thomas Weißschuh (4):
      kobject: Provide macros to initialize 'struct kobj_attribute'
      samples/kobject: Switch to the new __KOBJ_ATTR() macro
      kobject: Allow the constification of kobject attributes
      samples/kobject: Constify kobject attributes

 include/linux/kobject.h           | 75 ++++++++++++++++++++++++++++++++++++---
 lib/kobject.c                     |  8 +++--
 samples/kobject/kobject-example.c | 24 ++++++-------
 3 files changed, 89 insertions(+), 18 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260525-sysfs-const-attr-kobj-attr-prep-9a5d72913fee

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/4] kobject: Provide macros to initialize 'struct kobj_attribute'
  2026-07-07 16:56 [PATCH v2 0/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
@ 2026-07-07 16:56 ` Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 2/4] samples/kobject: Switch to the new __KOBJ_ATTR() macro Thomas Weißschuh
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-07-07 16:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Andrew Morton
  Cc: Thomas Weißschuh, driver-core, linux-kernel

Currently the generic __ATTR*() macros are used to initialize kobject
attributes. These are not able to handle the upcoming piece-by-piece
constification of the attribute callback handlers.

Add dedicated macros of kobject attributes. For now these are the same
as the generic macros, but they will change soon. Users will be
converted to these macros as part of the constification, but it is
expected that they will be used generally in the future.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 include/linux/kobject.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/include/linux/kobject.h b/include/linux/kobject.h
index bcb5d4e32001..13bd0e87af7b 100644
--- a/include/linux/kobject.h
+++ b/include/linux/kobject.h
@@ -144,6 +144,28 @@ struct kobj_attribute {
 			 const char *buf, size_t count);
 };
 
+#define __KOBJ_ATTR(_name, _mode, _show, _store) {			\
+	.attr	= { .name = __stringify(_name),				\
+		    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },		\
+	.show	= _show,						\
+	.store	= _store,						\
+}
+
+#define __KOBJ_ATTR_RO_MODE(_name, _mode)				\
+	__KOBJ_ATTR(_name, _mode, _name##_show, NULL)
+
+#define __KOBJ_ATTR_RO(_name)						\
+	__KOBJ_ATTR_RO_MODE(_name, 0444)
+
+#define __KOBJ_ATTR_RW_MODE(_name, _mode)				\
+	__KOBJ_ATTR(_name, _mode, _name##_show, _name##_store)
+
+#define __KOBJ_ATTR_WO(_name)						\
+	__KOBJ_ATTR(_name, 0200, NULL, _name##_store)
+
+#define __KOBJ_ATTR_RW(_name)						\
+	__KOBJ_ATTR(_name, 0644, _name##_show, _name##_store)
+
 extern const struct sysfs_ops kobj_sysfs_ops;
 
 struct sock;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/4] samples/kobject: Switch to the new __KOBJ_ATTR() macro
  2026-07-07 16:56 [PATCH v2 0/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 1/4] kobject: Provide macros to initialize 'struct kobj_attribute' Thomas Weißschuh
@ 2026-07-07 16:56 ` Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 3/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-07-07 16:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Andrew Morton
  Cc: Thomas Weißschuh, driver-core, linux-kernel

To demonstrate and test the upcoming 'const struct kobj_attribute'
handling, convert the sample to the new macro.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 samples/kobject/kobject-example.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/samples/kobject/kobject-example.c b/samples/kobject/kobject-example.c
index 36d87ca0bee2..7404f310dc45 100644
--- a/samples/kobject/kobject-example.c
+++ b/samples/kobject/kobject-example.c
@@ -45,7 +45,7 @@ static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
 
 /* Sysfs attributes cannot be world-writable. */
 static struct kobj_attribute foo_attribute =
-	__ATTR(foo, 0664, foo_show, foo_store);
+	__KOBJ_ATTR(foo, 0664, foo_show, foo_store);
 
 /*
  * More complex function where we determine which variable is being accessed by
@@ -80,9 +80,9 @@ static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr,
 }
 
 static struct kobj_attribute baz_attribute =
-	__ATTR(baz, 0664, b_show, b_store);
+	__KOBJ_ATTR(baz, 0664, b_show, b_store);
 static struct kobj_attribute bar_attribute =
-	__ATTR(bar, 0664, b_show, b_store);
+	__KOBJ_ATTR(bar, 0664, b_show, b_store);
 
 
 /*

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/4] kobject: Allow the constification of kobject attributes
  2026-07-07 16:56 [PATCH v2 0/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 1/4] kobject: Provide macros to initialize 'struct kobj_attribute' Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 2/4] samples/kobject: Switch to the new __KOBJ_ATTR() macro Thomas Weißschuh
@ 2026-07-07 16:56 ` Thomas Weißschuh
  2026-07-07 16:56 ` [PATCH v2 4/4] samples/kobject: Constify " Thomas Weißschuh
  2026-07-11 18:47 ` [PATCH v2 0/4] kobject: Allow the constification of " Danilo Krummrich
  4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-07-07 16:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Andrew Morton
  Cc: Thomas Weißschuh, driver-core, linux-kernel

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@weissschuh.net>
---
 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 13bd0e87af7b..55e37a5d405e 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.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 4/4] samples/kobject: Constify kobject attributes
  2026-07-07 16:56 [PATCH v2 0/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
                   ` (2 preceding siblings ...)
  2026-07-07 16:56 ` [PATCH v2 3/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
@ 2026-07-07 16:56 ` Thomas Weißschuh
  2026-07-11 18:47 ` [PATCH v2 0/4] kobject: Allow the constification of " Danilo Krummrich
  4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-07-07 16:56 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Andrew Morton
  Cc: Thomas Weißschuh, driver-core, linux-kernel

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

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 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;

-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/4] kobject: Allow the constification of kobject attributes
  2026-07-07 16:56 [PATCH v2 0/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
                   ` (3 preceding siblings ...)
  2026-07-07 16:56 ` [PATCH v2 4/4] samples/kobject: Constify " Thomas Weißschuh
@ 2026-07-11 18:47 ` Danilo Krummrich
  4 siblings, 0 replies; 6+ messages in thread
From: Danilo Krummrich @ 2026-07-11 18:47 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Danilo Krummrich,
	Andrew Morton, driver-core, linux-kernel

On Tue, 07 Jul 2026 18:56:31 +0200, Thomas Weißschuh wrote:
> [PATCH v2 0/4] kobject: Allow the constification of kobject attributes

Applied, thanks!

  Branch: driver-core-testing
  Tree:   git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/4] kobject: Provide macros to initialize 'struct kobj_attribute'
      commit: 418a74d106ef
[2/4] samples/kobject: Switch to the new __KOBJ_ATTR() macro
      commit: a3a9c7453fd5
[3/4] kobject: Allow the constification of kobject attributes
      commit: ed96337a5bae
[4/4] samples/kobject: Constify kobject attributes
      commit: 584afc86a1d4

The patches will appear in the next linux-next integration (typically within 24
hours on weekdays).

The patches are in the driver-core-testing branch and will be promoted to
driver-core-next after validation.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-07-11 18:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-07 16:56 [PATCH v2 0/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
2026-07-07 16:56 ` [PATCH v2 1/4] kobject: Provide macros to initialize 'struct kobj_attribute' Thomas Weißschuh
2026-07-07 16:56 ` [PATCH v2 2/4] samples/kobject: Switch to the new __KOBJ_ATTR() macro Thomas Weißschuh
2026-07-07 16:56 ` [PATCH v2 3/4] kobject: Allow the constification of kobject attributes Thomas Weißschuh
2026-07-07 16:56 ` [PATCH v2 4/4] samples/kobject: Constify " Thomas Weißschuh
2026-07-11 18:47 ` [PATCH v2 0/4] kobject: Allow the constification of " Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox