x86/retpoline: Optimize inline assembler for vmexit_fill_RSB
Subsystems (2)
PATCH 1/1 x86/retpoline: Optimize inline assembler for vmexit_fill_RSB
From: Andi Kleen <[email protected]>
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.
--- arch/x86/include/asm/nospec-branch.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/nospec-branch.h +3 -3
@@ -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 }
Signature · 1 line
2.14.3
Quoted 12 lines · Andi Kleen
From: Andi Kleen <[email protected]>
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_BUFFERwith the same value again.- The C code also passes in the iteration count
in another register, which is not used at all.
Quoted 7 lines
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 <[email protected]>
We still clobber the register, but you're right it's now filled in the __FILL_RETURN_BUFFER macro itself. It was a previous iteration which had the loop count passed in.
tip-bot for Andi Kleen
merge botx86/retpoline: Optimize inline assembler for vmexit_fill_RSB
Commit-ID: 3f7d875566d8e79c5e0b2c9a413e91b2c29e0854 Gitweb: https://git.kernel.org/tip/3f7d875566d8e79c5e0b2c9a413e91b2c29e0854 Author: Andi Kleen <[email protected]> AuthorDate: Wed, 17 Jan 2018 14:53:28 -0800 Committer: Thomas Gleixner <[email protected]> CommitDate: Fri, 19 Jan 2018 16:31:30 +0100
x86/retpoline: Optimize inline assembler for vmexit_fill_RSB
The generated assembler for the C fill RSB inline asm operations has 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.
--- arch/x86/include/asm/nospec-branch.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/include/asm/nospec-branch.h +4 -3
@@ -206,16 +206,17 @@ extern char __indirect_thunk_end[];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 } +#endif /* __ASSEMBLY__ */ #endif /* __NOSPEC_BRANCH_H__ */