[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] 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 
boot

With 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.

Fixes: 67d01cdb5518 ("x86: infrastructure to allow converting certain indirect 
calls to direct ones")
Fixes: d6385ea15f70 ("xsm: Switch xsm_ops to __alt_call_maybe_initdata")
Reported-by: James Dingwall <james-xen@xxxxxxxxxxxxxx>
Debugged-by: Teddy Astie <teddy.astie@xxxxxxxxxx>
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
Tested-by: James Dingwall <james-xen@xxxxxxxxxxxxxx>
---
CC: Jan Beulich <jbeulich@xxxxxxxx>
CC: Roger Pau Monné <roger@xxxxxxxxxxxxxx>
CC: Teddy Astie <teddy.astie@xxxxxxxxxx>

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(-)

diff --git a/xen/arch/x86/include/asm/alternative-call.h 
b/xen/arch/x86/include/asm/alternative-call.h
index 809023bc4d87..c877998f8363 100644
--- a/xen/arch/x86/include/asm/alternative-call.h
+++ b/xen/arch/x86/include/asm/alternative-call.h
@@ -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
-- 
2.39.5




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.