]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sysfs: introduce __SYSFS_FUNCTION_ALTERNATIVE()
authorThomas Weißschuh <linux@weissschuh.net>
Wed, 29 Oct 2025 08:12:12 +0000 (09:12 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 26 Nov 2025 14:16:35 +0000 (15:16 +0100)
For the constification phase of 'struct attribute' various callback
struct members will need to exist in both const and non-const variants.
Keeping both members in a union avoids memory and CPU overhead but will
be detected and trapped by Control Flow Integrity (CFI). By deciding
between a struct and a union depending whether CFI is enabled, most
configurations can avoid this overhead. Code using these callbacks will
still need to be updated to handle both members explicitly.
In the union case the compiler will recognize that testing for one union
member is enough and optimize away the code for the other one.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Link: https://patch.msgid.link/20251029-sysfs-const-attr-prep-v5-3-ea7d745acff4@weissschuh.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/sysfs.h

index 92f82cee5f11e98652e5b5fc1e69cd6e9e97833a..9cef5bf24ba71b45f9b8b6f20d3f9b1fa460fe4e 100644 (file)
@@ -58,6 +58,12 @@ do {                                                 \
 #define sysfs_attr_init(attr) do {} while (0)
 #endif
 
+#ifdef CONFIG_CFI
+#define __SYSFS_FUNCTION_ALTERNATIVE(MEMBERS...) struct { MEMBERS }
+#else
+#define __SYSFS_FUNCTION_ALTERNATIVE(MEMBERS...) union { MEMBERS }
+#endif
+
 /**
  * struct attribute_group - data structure used to declare an attribute group.
  * @name:      Optional: Attribute group name