Skip to contentSkip to conversation

Compiler Attributes, s390: Provide and use __uninitialized macro

PATCH In review Heiko Carstens 3 participants 9 messages Feb 5, 2024 – Feb 6, 2024

Subsystems (4)
lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

cover Compiler Attributes, s390: Provide and use __uninitialized macro

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled the kernel will be compiled with -ftrivial-auto-var-init=<...> which causes initialization of stack variables at function entry time.

In order to avoid the performance impact that comes with this users can use the "uninitialized" attribute to prevent such initialization.

In particular code sections in s390 specific kernel code which use floating point or vector registers all come with a 520 byte stack variable to save already in use registers, if required.

If the above named config options are enabled this stack variable will always be initialized on function entry in addition to saving register contents, which contradicts the intend (performance improvement) of such code sections.

Therefore provide a generic __uninitialized macro and an s390 specific DECLARE_KERNEL_FPU_ONSTACK() macro which provides a kernel fpu variable with an __uninitialized attribute, and convert all existing code to use this.

If people are ok which this approach, I'd like to carry this via the s390 tree to avoid potential merge conflicts, since there is a larger fpu code rework pending

Thanks,
Heiko

Heiko Carstens (2):
Compiler Attributes: Add __uninitialized macro
s390/fpu: make use of __uninitialized macro

 arch/s390/crypto/chacha-glue.c      |  2 +-
 arch/s390/crypto/crc32-vx.c         |  2 +-
 arch/s390/include/asm/fpu/types.h   |  3 +++
 arch/s390/kernel/sysinfo.c          |  2 +-
 include/linux/compiler_attributes.h | 12 ++++++++++++
 lib/raid6/s390vx.uc                 |  4 ++--
 6 files changed, 20 insertions(+), 5 deletions(-)
Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 1/2 Compiler Attributes: Add __uninitialized macro

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled the kernel will be compiled with -ftrivial-auto-var-init=<...> which causes initialization of stack variables at function entry time.

In order to avoid the performance impact that comes with this users can use the "uninitialized" attribute to prevent such initialization.

Therefore provide the __uninitialized macro which can be used for cases where INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO is enabled, but only selected variables should not be initialized.

---
 include/linux/compiler_attributes.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/compiler_attributes.h +12 -0
index 28566624f008..f5859b8c68b4 100644
@@ -333,6 +333,18 @@
  */
 #define __section(section)              __attribute__((__section__(section)))
 
+/*
+ * Optional: only supported since gcc >= 12
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#uninitialized
+ */
+#if __has_attribute(__uninitialized__)
+# define __uninitialized		__attribute__((__uninitialized__))
+#else
+# define __uninitialized
+#endif
+
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
Signature · 1 line
2.40.1
author maintainer
#4 lore raw

PATCH 2/2 s390/fpu: make use of __uninitialized macro

In reply to Heiko Carstens

Code sections in s390 specific kernel code which use floating point or vector registers all come with a 520 byte stack variable to save already in use registers, if required.

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled this variable will always be initialized on function entry in addition to saving register contents, which contradicts the intend (performance improvement) of such code sections.

Therefore provide a DECLARE_KERNEL_FPU_ONSTACK() macro which provides struct kernel_fpu variables with an __uninitialized attribute, and convert all existing code to use this.

This way only this specific type of stack variable will not be initialized, regardless of config options.

---
 arch/s390/crypto/chacha-glue.c    | 2 +-
 arch/s390/crypto/crc32-vx.c       | 2 +-
 arch/s390/include/asm/fpu/types.h | 3 +++
 arch/s390/kernel/sysinfo.c        | 2 +-
 lib/raid6/s390vx.uc               | 4 ++--
 5 files changed, 8 insertions(+), 5 deletions(-)
arch/s390/crypto/chacha-glue.c +1 -1
index ed9959e6f714..a823132fc563 100644
@@ -22,7 +22,7 @@ static void chacha20_crypt_s390(u32 *state, u8 *dst, const u8 *src,
 				unsigned int nbytes, const u32 *key,
 				u32 *counter)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 
 	kernel_fpu_begin(&vxstate, KERNEL_VXR);
 	chacha20_vx(dst, src, nbytes, key, counter);
arch/s390/crypto/crc32-vx.c +1 -1
index 017143e9cef7..6ae3e3ff5b0a 100644
@@ -49,8 +49,8 @@ u32 crc32c_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size);
 	static u32 __pure ___fname(u32 crc,				    \
 				unsigned char const *data, size_t datalen)  \
 	{								    \
-		struct kernel_fpu vxstate;				    \
 		unsigned long prealign, aligned, remaining;		    \
+		DECLARE_KERNEL_FPU_ONSTACK(vxstate);			    \
 									    \
 		if (datalen < VX_MIN_LEN + VX_ALIGN_MASK)		    \
 			return ___crc32_sw(crc, data, datalen);		    \
arch/s390/include/asm/fpu/types.h +3 -0
index d889e9436865..b1afa13c07b7 100644
@@ -35,4 +35,7 @@ struct kernel_fpu {
 	};
 };
 
+#define DECLARE_KERNEL_FPU_ONSTACK(name)	\
+	struct kernel_fpu name __uninitialized
+
 #endif /* _ASM_S390_FPU_TYPES_H */
arch/s390/kernel/sysinfo.c +1 -1
index f6f8f498c9be..b439f17516eb 100644
@@ -426,9 +426,9 @@ subsys_initcall(create_proc_service_level);
  */
 void s390_adjust_jiffies(void)
 {
+	DECLARE_KERNEL_FPU_ONSTACK(fpu);
 	struct sysinfo_1_2_2 *info;
 	unsigned long capability;
-	struct kernel_fpu fpu;
 
 	info = (void *) get_zeroed_page(GFP_KERNEL);
 	if (!info)
lib/raid6/s390vx.uc +2 -2
index cd0b9e95f499..7b0b355e1a26 100644
@@ -81,7 +81,7 @@ static inline void COPY_VEC(int x, int y)
 
 static void raid6_s390vx$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 	u8 **dptr, *p, *q;
 	int d, z, z0;
 
@@ -114,7 +114,7 @@ static void raid6_s390vx$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
 static void raid6_s390vx$#_xor_syndrome(int disks, int start, int stop,
 					size_t bytes, void **ptrs)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 	u8 **dptr, *p, *q;
 	int d, z, z0;
 
Signature · 1 line
2.40.1
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] Compiler Attributes: Add __uninitialized macro

In reply to Heiko Carstens
Quoted 4 lines · Heiko Carstens

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled the kernel will be compiled with -ftrivial-auto-var-init=<...> which causes initialization of stack variables at function entry time.

Quoted 8 lines

In order to avoid the performance impact that comes with this users can use the "uninitialized" attribute to prevent such initialization.

Therefore provide the __uninitialized macro which can be used for cases where INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO is enabled, but only selected variables should not be initialized.

Thanks! We had something similar a while back with syscall entry:
efa90c11f62e ("stack: Constrain and fix stack offset randomization with Clang builds")

Signature · 1 line
Kees Cook
#5 lore raw

Re: [PATCH 2/2] s390/fpu: make use of __uninitialized macro

In reply to Heiko Carstens
Quoted 57 lines · Heiko Carstens

Code sections in s390 specific kernel code which use floating point or vector registers all come with a 520 byte stack variable to save already in use registers, if required.

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled this variable will always be initialized on function entry in addition to saving register contents, which contradicts the intend (performance improvement) of such code sections.

Therefore provide a DECLARE_KERNEL_FPU_ONSTACK() macro which provides struct kernel_fpu variables with an __uninitialized attribute, and convert all existing code to use this.

This way only this specific type of stack variable will not be initialized, regardless of config options.

---
 arch/s390/crypto/chacha-glue.c    | 2 +-
 arch/s390/crypto/crc32-vx.c       | 2 +-
 arch/s390/include/asm/fpu/types.h | 3 +++
 arch/s390/kernel/sysinfo.c        | 2 +-
 lib/raid6/s390vx.uc               | 4 ++--
 5 files changed, 8 insertions(+), 5 deletions(-)
arch/s390/crypto/chacha-glue.c +1 -1
index ed9959e6f714..a823132fc563 100644
@@ -22,7 +22,7 @@ static void chacha20_crypt_s390(u32 *state, u8 *dst, const u8 *src,
 				unsigned int nbytes, const u32 *key,
 				u32 *counter)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 
 	kernel_fpu_begin(&vxstate, KERNEL_VXR);
 	chacha20_vx(dst, src, nbytes, key, counter);
arch/s390/crypto/crc32-vx.c +1 -1
index 017143e9cef7..6ae3e3ff5b0a 100644
@@ -49,8 +49,8 @@ u32 crc32c_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size);
 	static u32 __pure ___fname(u32 crc,				    \
 				unsigned char const *data, size_t datalen)  \
 	{								    \
-		struct kernel_fpu vxstate;				    \
 		unsigned long prealign, aligned, remaining;		    \
+		DECLARE_KERNEL_FPU_ONSTACK(vxstate);			    \
 									    \
 		if (datalen < VX_MIN_LEN + VX_ALIGN_MASK)		    \
 			return ___crc32_sw(crc, data, datalen);		    \
arch/s390/include/asm/fpu/types.h +0 -0
index d889e9436865..b1afa13c07b7 100644

arch/s390/include/asm/fpu/types.h struct kernel_fpu { · line 39

Quoted 5 lines
arch/s390/include/asm/fpu/types.h +2 -0
@@ -35,4 +35,7 @@ struct kernel_fpu {
 	};
 };
 
+#define DECLARE_KERNEL_FPU_ONSTACK(name)	\
+	struct kernel_fpu name __uninitialized

Are there cases when struct kernel_fpu should be initialized? e.g. should the attribute just be added to the struct definition instead of marking each use?

-Kees

Quoted 42 lines
arch/s390/include/asm/fpu/types.h +1 -0
+
 #endif /* _ASM_S390_FPU_TYPES_H */
arch/s390/kernel/sysinfo.c +1 -1
index f6f8f498c9be..b439f17516eb 100644
@@ -426,9 +426,9 @@ subsys_initcall(create_proc_service_level);
  */
 void s390_adjust_jiffies(void)
 {
+	DECLARE_KERNEL_FPU_ONSTACK(fpu);
 	struct sysinfo_1_2_2 *info;
 	unsigned long capability;
-	struct kernel_fpu fpu;
 
 	info = (void *) get_zeroed_page(GFP_KERNEL);
 	if (!info)
lib/raid6/s390vx.uc +2 -2
index cd0b9e95f499..7b0b355e1a26 100644
@@ -81,7 +81,7 @@ static inline void COPY_VEC(int x, int y)
 
 static void raid6_s390vx$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 	u8 **dptr, *p, *q;
 	int d, z, z0;
 
@@ -114,7 +114,7 @@ static void raid6_s390vx$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
 static void raid6_s390vx$#_xor_syndrome(int disks, int start, int stop,
 					size_t bytes, void **ptrs)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 	u8 **dptr, *p, *q;
 	int d, z, z0;
 
Signature · 1 line
2.40.1
Signature · 1 line
Kees Cook
author maintainer
#6 lore raw

Re: [PATCH 2/2] s390/fpu: make use of __uninitialized macro

Quoted 6 lines · Kees Cook

arch/s390/include/asm/fpu/types.h struct kernel_fpu { · line 39

Quoted 2 lines
arch/s390/include/asm/fpu/types.h +2 -0
+#define DECLARE_KERNEL_FPU_ONSTACK(name)	\
+	struct kernel_fpu name __uninitialized

Are there cases when struct kernel_fpu should be initialized? e.g. should the attribute just be added to the struct definition instead of marking each use?

I tried that, but failed:

./arch/s390/include/asm/fpu/types.h:36:3: warning: '__uninitialized__' attribute only applies to local variables [-Wignored-attributes]

   36 | } __uninitialized;
      |   ^

./include/linux/compiler_attributes.h:343:42: note: expanded from macro '__uninitialized'
343 | # define __uninitialized __attribute__((__uninitialized__))
| ^

That's why I came up with this macro. I'd prefer to have this added only to the struct definition, but looks like this is not possible (or I just can't figure out who to do that).

Reviewed-by
#7 lore raw

Re: [PATCH 2/2] s390/fpu: make use of __uninitialized macro

Quoted 8 lines · Heiko Carstens
Quoted 6 lines · Kees Cook

arch/s390/include/asm/fpu/types.h struct kernel_fpu { · line 39

Quoted 2 lines
arch/s390/include/asm/fpu/types.h +2 -0
+#define DECLARE_KERNEL_FPU_ONSTACK(name)	\
+	struct kernel_fpu name __uninitialized

Are there cases when struct kernel_fpu should be initialized? e.g. should the attribute just be added to the struct definition instead of marking each use?

Quoted 5 lines

I tried that, but failed:

./arch/s390/include/asm/fpu/types.h:36:3: warning: '__uninitialized__' attribute only applies to local variables [-Wignored-attributes]

   36 | } __uninitialized;
      |   ^

Oh. That's extremely disappointing. I think we may want to consider opening bug reports with GCC and Clang for this. Not that it'll help us now, since it needs to actually work today. Bummer!

In that case:

Signature · 1 line
Kees Cook
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] Compiler Attributes: Add __uninitialized macro

In reply to Heiko Carstens
Quoted 4 lines · Heiko Carstens

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled the kernel will be compiled with -ftrivial-auto-var-init=<...> which causes initialization of stack variables at function entry time.

Quoted 8 lines

In order to avoid the performance impact that comes with this users can use the "uninitialized" attribute to prevent such initialization.

Therefore provide the __uninitialized macro which can be used for cases where INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO is enabled, but only selected variables should not be initialized.

Quoted 30 lines
---
 include/linux/compiler_attributes.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/compiler_attributes.h +12 -0
index 28566624f008..f5859b8c68b4 100644
@@ -333,6 +333,18 @@
  */
 #define __section(section)              __attribute__((__section__(section)))
 
+/*
+ * Optional: only supported since gcc >= 12
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-uninitialized-variable-attribute
+ * clang: https://clang.llvm.org/docs/AttributeReference.html#uninitialized
+ */
+#if __has_attribute(__uninitialized__)
+# define __uninitialized		__attribute__((__uninitialized__))
+#else
+# define __uninitialized
+#endif
+
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
Signature · 1 line
2.40.1
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] s390/fpu: make use of __uninitialized macro

In reply to Heiko Carstens
Quoted 9 lines · Heiko Carstens

Code sections in s390 specific kernel code which use floating point or vector registers all come with a 520 byte stack variable to save already in use registers, if required.

With INIT_STACK_ALL_PATTERN or INIT_STACK_ALL_ZERO enabled this variable will always be initialized on function entry in addition to saving register contents, which contradicts the intend (performance improvement) of such code sections.

Quoted 8 lines

Therefore provide a DECLARE_KERNEL_FPU_ONSTACK() macro which provides struct kernel_fpu variables with an __uninitialized attribute, and convert all existing code to use this.

This way only this specific type of stack variable will not be initialized, regardless of config options.

Quoted 87 lines
---
 arch/s390/crypto/chacha-glue.c    | 2 +-
 arch/s390/crypto/crc32-vx.c       | 2 +-
 arch/s390/include/asm/fpu/types.h | 3 +++
 arch/s390/kernel/sysinfo.c        | 2 +-
 lib/raid6/s390vx.uc               | 4 ++--
 5 files changed, 8 insertions(+), 5 deletions(-)
arch/s390/crypto/chacha-glue.c +1 -1
index ed9959e6f714..a823132fc563 100644
@@ -22,7 +22,7 @@ static void chacha20_crypt_s390(u32 *state, u8 *dst, const u8 *src,
 				unsigned int nbytes, const u32 *key,
 				u32 *counter)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 
 	kernel_fpu_begin(&vxstate, KERNEL_VXR);
 	chacha20_vx(dst, src, nbytes, key, counter);
arch/s390/crypto/crc32-vx.c +1 -1
index 017143e9cef7..6ae3e3ff5b0a 100644
@@ -49,8 +49,8 @@ u32 crc32c_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size);
 	static u32 __pure ___fname(u32 crc,				    \
 				unsigned char const *data, size_t datalen)  \
 	{								    \
-		struct kernel_fpu vxstate;				    \
 		unsigned long prealign, aligned, remaining;		    \
+		DECLARE_KERNEL_FPU_ONSTACK(vxstate);			    \
 									    \
 		if (datalen < VX_MIN_LEN + VX_ALIGN_MASK)		    \
 			return ___crc32_sw(crc, data, datalen);		    \
arch/s390/include/asm/fpu/types.h +3 -0
index d889e9436865..b1afa13c07b7 100644
@@ -35,4 +35,7 @@ struct kernel_fpu {
 	};
 };
 
+#define DECLARE_KERNEL_FPU_ONSTACK(name)	\
+	struct kernel_fpu name __uninitialized
+
 #endif /* _ASM_S390_FPU_TYPES_H */
arch/s390/kernel/sysinfo.c +1 -1
index f6f8f498c9be..b439f17516eb 100644
@@ -426,9 +426,9 @@ subsys_initcall(create_proc_service_level);
  */
 void s390_adjust_jiffies(void)
 {
+	DECLARE_KERNEL_FPU_ONSTACK(fpu);
 	struct sysinfo_1_2_2 *info;
 	unsigned long capability;
-	struct kernel_fpu fpu;
 
 	info = (void *) get_zeroed_page(GFP_KERNEL);
 	if (!info)
lib/raid6/s390vx.uc +2 -2
index cd0b9e95f499..7b0b355e1a26 100644
@@ -81,7 +81,7 @@ static inline void COPY_VEC(int x, int y)
 
 static void raid6_s390vx$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 	u8 **dptr, *p, *q;
 	int d, z, z0;
 
@@ -114,7 +114,7 @@ static void raid6_s390vx$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
 static void raid6_s390vx$#_xor_syndrome(int disks, int start, int stop,
 					size_t bytes, void **ptrs)
 {
-	struct kernel_fpu vxstate;
+	DECLARE_KERNEL_FPU_ONSTACK(vxstate);
 	u8 **dptr, *p, *q;
 	int d, z, z0;
 
Signature · 1 line
2.40.1