Skip to contentSkip to conversation

x86/altcall: further refine clang workaround

PATCH Superseded Roger Pau Monne 3 participants 7 messages Jul 29, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/altcall: further refine clang workaround

The current code in ALT_CALL_ARG() won't successfully workaround the clang code-generation issue if the arg parameter has a size that's not a power of 2. While there are no such sized parameters at the moment, improve the workaround to also be effective when such sizes are used.

Instead of using a union with a long use an unsigned long that's first initialized to 0 and afterwards set to the argument value.

---
 xen/arch/x86/include/asm/alternative.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/include/asm/alternative.h +7 -7
index e63b45927643..12590504d465 100644
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \
+         tmp;                                                            \
+     })
 #else
 #define ALT_CALL_ARG(arg, n) \
     register typeof(arg) a ## n ## _ asm ( ALT_CALL_arg ## n ) = \
Signature · 1 line
2.45.2
#1 lore raw
Quoted 8 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +0 -2
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \

xen/arch/x86/include/asm/alternative.h extern void alternative_branches(void); · line 190

Quoted 10 lines
xen/arch/x86/include/asm/alternative.h +5 -5
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \

With this, even more so than before, I think the type of tmp would better be void * (or the BUILD_BUG_ON() be made use unsigned long, yet I consider that less desirable). As a nit, I also don't think the backslashes need moving out by one position. Finally I'm afraid you're leaving stale the comment ahead of this construct.

Jan

Reviewed-by
#2 lore raw
Quoted 26 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +0 -2
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \

xen/arch/x86/include/asm/alternative.h extern void alternative_branches(void); · line 190

Quoted 10 lines
xen/arch/x86/include/asm/alternative.h +5 -5
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \

With this, even more so than before, I think the type of tmp would better be void * (or the BUILD_BUG_ON() be made use unsigned long, yet I consider that less desirable). As a nit, I also don't think the backslashes need moving out by one position. Finally I'm afraid you're leaving stale the comment ahead of this construct.

Quoted 1 line

Jan

I wouldn't be thrilled to create a temp variable of yet another type that is potentially neither "typeof(arg)" nor "unsigned long". No need to create a 3 body problem, where 2 is enough. Adjusting BUILD_BUG_ON() to use the same temp type seems sensible, but I don't mind very much.

With the stale comment adjusted:

Cheers,
Alejandro

#3 lore raw
In reply to Jan Beulich
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +1 -7
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
Quoted 8 lines

xen/arch/x86/include/asm/alternative.h extern void alternative_branches(void); · line 190

Quoted 4 lines
xen/arch/x86/include/asm/alternative.h +4 -0
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \

With this, even more so than before, I think the type of tmp would better be void * (or the BUILD_BUG_ON() be made use unsigned long, yet I consider that less desirable).

Won't using void * be uglier, as we then need to cast the last tmp statement to unsigned long?

Quoted 3 lines

As a nit, I also don't think the backslashes need
moving out by one position. Finally I'm afraid you're leaving stale the comment ahead of this construct.

Yes, thanks for noticing. Will attempt to adjust the comment.

Roger.

#4 lore raw
Quoted 25 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +1 -7
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
Quoted 8 lines

xen/arch/x86/include/asm/alternative.h extern void alternative_branches(void); · line 190

Quoted 4 lines
xen/arch/x86/include/asm/alternative.h +4 -0
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \

With this, even more so than before, I think the type of tmp would better be void * (or the BUILD_BUG_ON() be made use unsigned long, yet I consider that less desirable).

Quoted 2 lines

Won't using void * be uglier, as we then need to cast the last tmp statement to unsigned long?

Only if we stick to using unsigned long for a ## n ## _. Afaics there's nothing wrong with making that void *, too.

Jan

#5 lore raw
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +0 -2
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \

xen/arch/x86/include/asm/alternative.h extern void alternative_branches(void); · line 190

Quoted 10 lines
xen/arch/x86/include/asm/alternative.h +5 -5
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \

With this, even more so than before, I think the type of tmp would better be void * (or the BUILD_BUG_ON() be made use unsigned long, yet I consider that less desirable).

Quoted 5 lines
Quoted 2 lines

Won't using void * be uglier, as we then need to cast the last tmp statement to unsigned long?

Only if we stick to using unsigned long for a ## n ## _. Afaics there's nothing wrong with making that void *, too.

Right, but then for consistency I would also like to make r{10,11}_ void *, and ALT_CALL_NO_ARG(), which might be too much.

My preference is likely to keep it at unsigned long, and adjust the BUILD_BUG_ON(), unless you have a strong opinion to change it to void
* (and possibly the rest of the register variables).

Thanks, Roger.

#6 lore raw
Quoted 33 lines · Roger Pau Monné
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +0 -2
@@ -183,13 +183,13 @@ extern void alternative_branches(void);
  * https://github.com/llvm/llvm-project/issues/12579
  * https://github.com/llvm/llvm-project/issues/82598
  */
-#define ALT_CALL_ARG(arg, n)                                            \
-    register union {                                                    \

xen/arch/x86/include/asm/alternative.h extern void alternative_branches(void); · line 190

Quoted 10 lines
xen/arch/x86/include/asm/alternative.h +5 -5
-        typeof(arg) e[sizeof(long) / sizeof(arg)];                      \
-        unsigned long r;                                                \
-    } a ## n ## _ asm ( ALT_CALL_arg ## n ) = {                         \
-        .e[0] = ({ BUILD_BUG_ON(sizeof(arg) > sizeof(void *)); (arg); })\
-    }
+#define ALT_CALL_ARG(arg, n)                                             \
+     register unsigned long a ## n ## _ asm ( ALT_CALL_arg ## n ) = ({   \
+         unsigned long tmp = 0;                                          \
+         *(typeof(arg) *)&tmp = (arg);                                   \
+         BUILD_BUG_ON(sizeof(arg) > sizeof(void *));                     \

With this, even more so than before, I think the type of tmp would better be void * (or the BUILD_BUG_ON() be made use unsigned long, yet I consider that less desirable).

Quoted 5 lines
Quoted 2 lines

Won't using void * be uglier, as we then need to cast the last tmp statement to unsigned long?

Only if we stick to using unsigned long for a ## n ## _. Afaics there's nothing wrong with making that void *, too.

Quoted 6 lines

Right, but then for consistency I would also like to make r{10,11}_ void *, and ALT_CALL_NO_ARG(), which might be too much.

My preference is likely to keep it at unsigned long, and adjust the BUILD_BUG_ON(), unless you have a strong opinion to change it to void
* (and possibly the rest of the register variables).

That's okay if you prefer it that way; I said "less desirable" and I really don't mean any more than that.

Jan