x86/altcall: Fix crash caused by code generation in alternative_callN()
PATCH 1/1 x86/altcall: Fix crash caused by code generation in alternative_callN()
The "g" constraint is not actually used by the asm() block. It's there to communicate to the compiler that func() is referenced.
Unfortuantely, "g" is permitted to be interpreted by the compiler as "irm", and both "r" and "m" are explicitly unsafe for certain altcalls.
James reports that GCC 13 from Ubuntu 24.04 emitted the following code:
do_memory_op (inlined xsm_memory_adjust_reservation, common/memory.c:1577)
mov 0x2210c3(%rip),%rdx # <xsm_ops+0xe0>
...
call *0x2210a9(%rip) # <xsm_ops+0xe0> // Patched to direct call at bootWith CONFIG_XSM=y and CONFIG_LIVEPATCH=n, the xsm_ops structure is __initdata, causing the MOV to fault.
Remove the constraint entirely. Because [addr] "i" (&(func)) is a pointer capture, it ought to convey the same "func is referenced" information to the compiler.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
Tracing the second fixes tag also identifies why this showed up in 4.22. The bad code generation was latent before __alt_call_maybe_initdata.
I don't have a compiler handy which generates the problem seqeunce for xsm_memory_adjust_reservation(), but the patch does have a reasonable change generally. For the conditional altcalls, it changes from:
mov 0xf951(%rip),%rdx # <hvm_funcs+0x228> test %rdx,%rdx je ... call *0xf946(%rip) # <hvm_funcs+0x228>
to the slightly shorter:
cmpq $0x0,0x108d0(%rip) # <hvm_funcs+0x228> je ... call *0x108c8(%rip) # <hvm_funcs+0x228>
and with a knock-on effect with register schedulling.
--- xen/arch/x86/include/asm/alternative-call.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/include/asm/alternative-call.h +1 -1
@@ -94,7 +94,7 @@ struct alt_call { ".popsection" \ : ALT_CALL ## n ## _OUT, "=a" (ret_), \ "=r" (r10_), "=r" (r11_) ASM_CALL_CONSTRAINT \ - : [addr] "i" (&(func)), "g" (func) \ + : [addr] "i" (&(func)) \ : "memory" ); \ ret_; \ })
base-commit: a1bb3efd35c2ea04c861f53df602795abb52038a
Signature · 1 line
2.39.5