Skip to contentSkip to conversation

arm64: sysreg: Generate C compiler warnings on {read,write}_sysreg_s arguments

PATCH Applied James Clark 2 participants 2 messages Aug 15, 2023 – Aug 16, 2023 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: sysreg: Generate C compiler warnings on {read,write}_sysreg_s arguments

Evaluate the register before the asm section so that the C compiler generates warnings when there is an issue with the register argument.

This will prevent possible future issues such as the one seen here [1] where a missing bracket caused the shift and addition operators to be evaluated in the wrong order, but no warning was emitted. The GNU assembler has no warning for when expressions evaluate differently to C due to different operator precedence, but the C compiler has some warnings that may suggest something is wrong. For example in this case the following warning would have been emitted:

error: operator '>>' has lower precedence than '+'; '+' will be evaluated first [-Werror,-Wshift-op-parentheses]

There are currently no existing warnings that need to be fixed.

[1]: https://lore.kernel.org/linux-perf-users/20230728162011.GA22050@willie-the-truck/

---
 arch/arm64/include/asm/sysreg.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm64/include/asm/sysreg.h +6 -0
index 0c07b03d511f..71eb8ea7c795 100644
@@ -777,15 +777,21 @@
 /*
  * For registers without architectural names, or simply unsupported by
  * GAS.
+ *
+ * __check_r forces warnings to be generated by the compiler when
+ * evaluating r which wouldn't normally happen due to being passed to
+ * the assembler via __stringify(r).
  */
 #define read_sysreg_s(r) ({						\
 	u64 __val;							\
+	u32 __maybe_unused __check_r = (u32)(r);			\
 	asm volatile(__mrs_s("%0", r) : "=r" (__val));			\
 	__val;								\
 })
 
 #define write_sysreg_s(v, r) do {					\
 	u64 __val = (u64)(v);						\
+	u32 __maybe_unused __check_r = (u32)(r);			\
 	asm volatile(__msr_s(r, "%x0") : : "rZ" (__val));		\
 } while (0)
 
Signature · 1 line
2.34.1
maintainer Applied
#1 lore raw
Quoted 11 lines · James Clark

Evaluate the register before the asm section so that the C compiler generates warnings when there is an issue with the register argument.

This will prevent possible future issues such as the one seen here [1] where a missing bracket caused the shift and addition operators to be evaluated in the wrong order, but no warning was emitted. The GNU assembler has no warning for when expressions evaluate differently to C due to different operator precedence, but the C compiler has some warnings that may suggest something is wrong. For example in this case the following warning would have been emitted:

Quoted 1 line

[...]

Applied to arm64 (for-next/misc), thanks!

[1/1] arm64: sysreg: Generate C compiler warnings on {read,write}_sysreg_s arguments

      https://git.kernel.org/arm64/c/18b8f57a7f51

Cheers,

Signature · 10 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel