[PATCH 1/2] clang: work around asm output constraint problems

Eric Dumazet posted 2 patches 1 month, 3 weeks ago
[PATCH 1/2] clang: work around asm output constraint problems
Posted by Eric Dumazet 1 month, 3 weeks ago
Work around clang problems with "=rm" asm constraint.

clang seems to always chose the memory output, while it is almost
always the worst choice.

Add ASM_OUTPUT_RM so that we can replace "=rm" constraint
where it maters for clang, while not penalizing gcc.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Suggested-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
---
 include/linux/compiler-clang.h | 7 +++++++
 include/linux/compiler_types.h | 6 +++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/include/linux/compiler-clang.h b/include/linux/compiler-clang.h
index 107ce05bd16eb4a2ecb0f8cbf5c9efeab1845a1c..a82db8ab2812b9aa8851bf51daed3f81d2a41979 100644
--- a/include/linux/compiler-clang.h
+++ b/include/linux/compiler-clang.h
@@ -146,6 +146,13 @@
 #define ASM_INPUT_G "ir"
 #define ASM_INPUT_RM "r"
 
+/*
+ * clang has non optimal behavior with "=rm" constraints for asm outputs.
+ * It favors memory operand, forcing additional logic in tail functions
+ * when CONFIG_STACKPROTECTOR_STRONG=y.
+ */
+#define ASM_OUTPUT_RM "=r"
+
 /*
  * Declare compiler support for __typeof_unqual__() operator.
  *
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 1280693766b9dd844ec7ca83053381799be12456..b66aa58dacd595546b17582df03a762dd23cb15c 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -548,13 +548,17 @@ struct ftrace_likely_data {
 
 /*
  * Clang has trouble with constraints with multiple
- * alternative behaviors (mainly "g" and "rm").
+ * alternative behaviors ("g" , "rm" and "=rm").
  */
 #ifndef ASM_INPUT_G
   #define ASM_INPUT_G "g"
   #define ASM_INPUT_RM "rm"
 #endif
 
+#ifndef ASM_OUTPUT_RM
+  #define ASM_OUTPUT_RM "=rm"
+#endif
+
 #ifdef CONFIG_CC_HAS_ASM_INLINE
 #define asm_inline asm __inline
 #else
-- 
2.52.0.322.g1dd061c0dc-goog
Re: [PATCH 1/2] clang: work around asm output constraint problems
Posted by Linus Torvalds 1 month, 2 weeks ago
On Fri, 19 Dec 2025 at 03:20, Eric Dumazet <edumazet@google.com> wrote:
>
> Add ASM_OUTPUT_RM so that we can replace "=rm" constraint
> where it maters for clang, while not penalizing gcc.

I'll apply this - and the use case patch in 2/2 - directly, but I'll
just group them all together the same wary the input side (RM/G) is
already just done right next to each other.

              Linus
Re: [PATCH 1/2] clang: work around asm output constraint problems
Posted by Eric Dumazet 1 month, 2 weeks ago
On Sat, Dec 20, 2025 at 11:33 PM Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> On Fri, 19 Dec 2025 at 03:20, Eric Dumazet <edumazet@google.com> wrote:
> >
> > Add ASM_OUTPUT_RM so that we can replace "=rm" constraint
> > where it maters for clang, while not penalizing gcc.
>
> I'll apply this - and the use case patch in 2/2 - directly, but I'll
> just group them all together the same wary the input side (RM/G) is
> already just done right next to each other.

SGTM, thanks Linus.