[PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition

Sean Christopherson posted 9 patches 2 months, 3 weeks ago
[PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Posted by Sean Christopherson 2 months, 3 weeks ago
Decouple the use of ALTERNATIVE from the encoding of VERW to clear CPU
buffers so that KVM can use ALTERNATIVE_2 to handle "always clear buffers"
and "clear if guest can access host MMIO" in a single statement.

No functional change intended.

Reviewed-by: Brendan Jackman <jackmanb@google.com>
Reviewed-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/include/asm/nospec-branch.h | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 08ed5a2e46a5..8b4885a1b2ef 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -308,24 +308,24 @@
  * CFLAGS.ZF.
  * Note: Only the memory operand variant of VERW clears the CPU buffers.
  */
-.macro __CLEAR_CPU_BUFFERS feature
 #ifdef CONFIG_X86_64
-	ALTERNATIVE "", "verw x86_verw_sel(%rip)", \feature
+#define VERW	verw x86_verw_sel(%rip)
 #else
-	/*
-	 * In 32bit mode, the memory operand must be a %cs reference. The data
-	 * segments may not be usable (vm86 mode), and the stack segment may not
-	 * be flat (ESPFIX32).
-	 */
-	ALTERNATIVE "", "verw %cs:x86_verw_sel", \feature
+/*
+ * In 32bit mode, the memory operand must be a %cs reference. The data segments
+ * may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32).
+ */
+#define VERW	verw %cs:x86_verw_sel
 #endif
-.endm
 
+#define __CLEAR_CPU_BUFFERS	__stringify(VERW)
+
+/* If necessary, emit VERW on exit-to-userspace to clear CPU buffers. */
 #define CLEAR_CPU_BUFFERS \
-	__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF
+	ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF
 
 #define VM_CLEAR_CPU_BUFFERS \
-	__CLEAR_CPU_BUFFERS X86_FEATURE_CLEAR_CPU_BUF_VM
+	ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM
 
 #ifdef CONFIG_X86_64
 .macro CLEAR_BRANCH_HISTORY
-- 
2.52.0.rc1.455.g30608eb744-goog
Re: [PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Posted by Borislav Petkov 2 months, 3 weeks ago
On Thu, Nov 13, 2025 at 03:37:40PM -0800, Sean Christopherson wrote:
> +#define __CLEAR_CPU_BUFFERS	__stringify(VERW)

Let's get rid of one indirection level pls:

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 8b4885a1b2ef..59945cb5e5f9 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -309,23 +309,21 @@
  * Note: Only the memory operand variant of VERW clears the CPU buffers.
  */
 #ifdef CONFIG_X86_64
-#define VERW	verw x86_verw_sel(%rip)
+#define VERW	__stringify(verw x86_verw_sel(%rip))
 #else
 /*
  * In 32bit mode, the memory operand must be a %cs reference. The data segments
  * may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32).
  */
-#define VERW	verw %cs:x86_verw_sel
+#define VERW	__stringify(verw %cs:x86_verw_sel)
 #endif
 
-#define __CLEAR_CPU_BUFFERS	__stringify(VERW)
-
 /* If necessary, emit VERW on exit-to-userspace to clear CPU buffers. */
 #define CLEAR_CPU_BUFFERS \
-	ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF
+	ALTERNATIVE "", VERW, X86_FEATURE_CLEAR_CPU_BUF
 
 #define VM_CLEAR_CPU_BUFFERS \
-	ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM
+	ALTERNATIVE "", VERW, X86_FEATURE_CLEAR_CPU_BUF_VM
 
 #ifdef CONFIG_X86_64
 .macro CLEAR_BRANCH_HISTORY

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Posted by Sean Christopherson 2 months, 3 weeks ago
On Mon, Nov 17, 2025, Borislav Petkov wrote:
> On Thu, Nov 13, 2025 at 03:37:40PM -0800, Sean Christopherson wrote:
> > +#define __CLEAR_CPU_BUFFERS	__stringify(VERW)
> 
> Let's get rid of one indirection level pls:
> 
> diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
> index 8b4885a1b2ef..59945cb5e5f9 100644
> --- a/arch/x86/include/asm/nospec-branch.h
> +++ b/arch/x86/include/asm/nospec-branch.h
> @@ -309,23 +309,21 @@
>   * Note: Only the memory operand variant of VERW clears the CPU buffers.
>   */
>  #ifdef CONFIG_X86_64
> -#define VERW	verw x86_verw_sel(%rip)
> +#define VERW	__stringify(verw x86_verw_sel(%rip))
>  #else
>  /*
>   * In 32bit mode, the memory operand must be a %cs reference. The data segments
>   * may not be usable (vm86 mode), and the stack segment may not be flat (ESPFIX32).
>   */
> -#define VERW	verw %cs:x86_verw_sel
> +#define VERW	__stringify(verw %cs:x86_verw_sel)
>  #endif

Brendan also brought this up in v4[*].  Unless there's a way to coerce ALTERNATIVE_2
into working with multiple strings, the layer of indirection is needed so that KVM
can emit __stringify() for the entire sequence.

  : Heh, I tried that, and AFAICT it simply can't work with the way ALTERNATIVE and
  : friends are implemented, as each paramater needs to be a single unbroken string.
  : 
  : E.g. this 
  : 
  : diff --git a/arch/x86/kvm/vmx/vmenter.S b/arch/x86/kvm/vmx/vmenter.S
  : index 61a809790a58..ffa6bc2345e3 100644
  : --- a/arch/x86/kvm/vmx/vmenter.S
  : +++ b/arch/x86/kvm/vmx/vmenter.S
  : @@ -63,6 +63,8 @@
  :         RET
  :  .endm
  :  
  : +#define CLEAR_CPU_BUFFERS_SEQ_STRING  "verw x86_verw_sel(%rip)"
  : +
  :  .section .noinstr.text, "ax"
  :  
  :  /**
  : @@ -169,9 +171,9 @@ SYM_FUNC_START(__vmx_vcpu_run)
  :  
  :         /* Clobbers EFLAGS.ZF */
  :         ALTERNATIVE_2 "",                                                       \
  : -                     __stringify(jz .Lskip_clear_cpu_buffers;                  \
  : -                                 CLEAR_CPU_BUFFERS_SEQ;                        \
  : -                                 .Lskip_clear_cpu_buffers:),                   \
  : +                     "jz .Lskip_clear_cpu_buffers; "                           \
  : +                     CLEAR_CPU_BUFFERS_SEQ_STRING;                             \
  : +                     ".Lskip_clear_cpu_buffers:",                              \
  :                       X86_FEATURE_CLEAR_CPU_BUF_MMIO,                           \
  :                       __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM
  :  
  : yields wonderfully helpful error messages like so:
  : 
  :   arch/x86/kvm/vmx/vmenter.S: Assembler messages:
  :   arch/x86/kvm/vmx/vmenter.S:173: Error: too many positional arguments
  : 
  : If there's a magic incanation to get things to work, it's unknown to me.

[*] https://lore.kernel.org/all/aQT1JgdgiNae3Ybl@google.com
Re: [PATCH v5 3/9] x86/bugs: Decouple ALTERNATIVE usage from VERW macro definition
Posted by Borislav Petkov 2 months, 3 weeks ago
On Mon, Nov 17, 2025 at 07:33:12AM -0800, Sean Christopherson wrote:
> Brendan also brought this up in v4[*].  Unless there's a way to coerce ALTERNATIVE_2
> into working with multiple strings, the layer of indirection is needed so that KVM
> can emit __stringify() for the entire sequence.

Ah, yah, there was that.

Pls put a small comment above it so that we don't forget.

Thx.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette