Skip to contentSkip to conversation

x86/retpoline: Optimize inline assembler for vmexit_fill_RSB

PATCH In review Andi Kleen 3 participants 3 messages Jan 17, 2018 – Jan 19, 2018

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

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
index 1e170fd3dc51..fed8703a28b9 100644
@@ -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
Acked-by on v1 1/1
#1 lore raw
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_BUFFER with 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.

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.

#2 lore raw
In reply to Andi Kleen
x86/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
index 19ba5ad..4ad4108 100644
@@ -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__ */