[PATCH] x86/retpoline: Optimize inline assembler for vmexit_fill_RSB

From: Andi Kleen
Date: Wed Jan 17 2018 - 17:53:58 EST


From: Andi Kleen <ak@xxxxxxxxxxxxxxx>

I was looking at the generated assembler for the C fill RSB
inline asm operations, and noticed several issues:

- The C code sets up the loop register, which
is then immediately overwritten in __FILL_RETURN_BUFFER
with the same value again.

- The C code also passes in the iteration count
in another register, which is not used at all.

Remove these two unnecessary operations. Just rely on
the single constant passed to the macro for the iterations.

This eliminates several instructions and avoids unnecessarily
clobbering a register.

Signed-off-by: Andi Kleen <ak@xxxxxxxxxxxxxxx>
---
arch/x86/include/asm/nospec-branch.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 1e170fd3dc51..fed8703a28b9 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -204,15 +204,15 @@ enum spectre_v2_mitigation {
static inline void vmexit_fill_RSB(void)
{
#ifdef CONFIG_RETPOLINE
- unsigned long loops = RSB_CLEAR_LOOPS / 2;
+ unsigned long loops;

asm volatile (ANNOTATE_NOSPEC_ALTERNATIVE
ALTERNATIVE("jmp 910f",
__stringify(__FILL_RETURN_BUFFER(%0, RSB_CLEAR_LOOPS, %1)),
X86_FEATURE_RETPOLINE)
"910:"
- : "=&r" (loops), ASM_CALL_CONSTRAINT
- : "r" (loops) : "memory" );
+ : "=r" (loops), ASM_CALL_CONSTRAINT
+ : : "memory" );
#endif
}

--
2.14.3