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.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/include/asm/nospec-branch.h | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 08ed5a2e46a5..923ae21cbef1 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -308,24 +308,23 @@
* 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 CLEAR_CPU_BUFFERS_SEQ 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 CLEAR_CPU_BUFFERS_SEQ verw %cs:x86_verw_sel
#endif
-.endm
+
+#define __CLEAR_CPU_BUFFERS __stringify(CLEAR_CPU_BUFFERS_SEQ)
#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.51.1.930.gacf6e81ea2-goog
On Thu, Oct 30, 2025 at 05:30:34PM -0700, Sean Christopherson wrote: > 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. > > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > arch/x86/include/asm/nospec-branch.h | 21 ++++++++++----------- > 1 file changed, 10 insertions(+), 11 deletions(-) > > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > index 08ed5a2e46a5..923ae21cbef1 100644 > --- a/arch/x86/include/asm/nospec-branch.h > +++ b/arch/x86/include/asm/nospec-branch.h > @@ -308,24 +308,23 @@ > * 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 CLEAR_CPU_BUFFERS_SEQ 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 CLEAR_CPU_BUFFERS_SEQ verw %cs:x86_verw_sel > #endif > -.endm > + > +#define __CLEAR_CPU_BUFFERS __stringify(CLEAR_CPU_BUFFERS_SEQ) > > #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 Sorry nitpicking, we have too many "CLEAR_CPU_BUF" in these macros, can we avoid adding CLEAR_CPU_BUFFERS_SEQ? Or better yet, can we name the actual instruction define to VERW_SEQ, so as to differentiate it from the ALTERNATIVE defines? --- diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h index 4cf347732ec1..16b957382224 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 CLEAR_CPU_BUFFERS_SEQ verw x86_verw_sel(%rip) +#define VERW_SEQ 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 CLEAR_CPU_BUFFERS_SEQ verw %cs:x86_verw_sel +#define VERW_SEQ verw %cs:x86_verw_sel #endif -#define __CLEAR_CPU_BUFFERS __stringify(CLEAR_CPU_BUFFERS_SEQ) - /* Primarily used in exit-to-userspace path */ #define CLEAR_CPU_BUFFERS \ - ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF + ALTERNATIVE "", __stringify(VERW_SEQ), X86_FEATURE_CLEAR_CPU_BUF #define VM_CLEAR_CPU_BUFFERS \ - ALTERNATIVE "", __CLEAR_CPU_BUFFERS, X86_FEATURE_CLEAR_CPU_BUF_VM + ALTERNATIVE "", __stringify(VERW_SEQ), X86_FEATURE_CLEAR_CPU_BUF_VM #ifdef CONFIG_X86_64 .macro CLEAR_BRANCH_HISTORY
On Fri, Oct 31, 2025, Pawan Gupta wrote: > On Thu, Oct 30, 2025 at 05:30:34PM -0700, Sean Christopherson wrote: > > 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. > > > > Signed-off-by: Sean Christopherson <seanjc@google.com> > > --- > > arch/x86/include/asm/nospec-branch.h | 21 ++++++++++----------- > > 1 file changed, 10 insertions(+), 11 deletions(-) > > > > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > > index 08ed5a2e46a5..923ae21cbef1 100644 > > --- a/arch/x86/include/asm/nospec-branch.h > > +++ b/arch/x86/include/asm/nospec-branch.h > > @@ -308,24 +308,23 @@ > > * 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 CLEAR_CPU_BUFFERS_SEQ 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 CLEAR_CPU_BUFFERS_SEQ verw %cs:x86_verw_sel > > #endif > > -.endm > > + > > +#define __CLEAR_CPU_BUFFERS __stringify(CLEAR_CPU_BUFFERS_SEQ) > > > > #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 > > Sorry nitpicking, we have too many "CLEAR_CPU_BUF" in these macros, can we > avoid adding CLEAR_CPU_BUFFERS_SEQ? AFAICT, there's no sane way to avoid defining a macro for the raw instruction. :-/ > Or better yet, can we name the actual instruction define to VERW_SEQ, Works for me.
On Mon, Nov 03, 2025 at 09:00:48AM -0800, Sean Christopherson wrote:
> > Or better yet, can we name the actual instruction define to VERW_SEQ,
>
> Works for me.
Just call it VERW. If a separate x86-insn-like macro wants to appear, we can
disambiguate then.
Thx.
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
On Mon, Nov 03, 2025 at 09:00:48AM -0800, Sean Christopherson wrote: > On Fri, Oct 31, 2025, Pawan Gupta wrote: > > On Thu, Oct 30, 2025 at 05:30:34PM -0700, Sean Christopherson wrote: > > > 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. > > > > > > Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
On Fri Oct 31, 2025 at 12:30 AM UTC, Sean Christopherson wrote: > 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. > > Signed-off-by: Sean Christopherson <seanjc@google.com> > --- > arch/x86/include/asm/nospec-branch.h | 21 ++++++++++----------- > 1 file changed, 10 insertions(+), 11 deletions(-) > > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h > index 08ed5a2e46a5..923ae21cbef1 100644 > --- a/arch/x86/include/asm/nospec-branch.h > +++ b/arch/x86/include/asm/nospec-branch.h > @@ -308,24 +308,23 @@ > * 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 CLEAR_CPU_BUFFERS_SEQ 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 CLEAR_CPU_BUFFERS_SEQ verw %cs:x86_verw_sel > #endif > -.endm > + > +#define __CLEAR_CPU_BUFFERS __stringify(CLEAR_CPU_BUFFERS_SEQ) Maybe CLEAR_CPU_BUFFERS_SEQ should just be defined as a string in the first place? But meh, that's a very bikeshed comment. I can see the aeshetic appeal of the separate __stringify(). Reviewed-by: Brendan Jackman <jackmanb@google.com> > > #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
On Fri, Oct 31, 2025, Brendan Jackman wrote:
> On Fri Oct 31, 2025 at 12:30 AM UTC, Sean Christopherson wrote:
> > 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.
> >
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> > arch/x86/include/asm/nospec-branch.h | 21 ++++++++++-----------
> > 1 file changed, 10 insertions(+), 11 deletions(-)
> >
> > diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
> > index 08ed5a2e46a5..923ae21cbef1 100644
> > --- a/arch/x86/include/asm/nospec-branch.h
> > +++ b/arch/x86/include/asm/nospec-branch.h
> > @@ -308,24 +308,23 @@
> > * 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 CLEAR_CPU_BUFFERS_SEQ 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 CLEAR_CPU_BUFFERS_SEQ verw %cs:x86_verw_sel
> > #endif
> > -.endm
> > +
> > +#define __CLEAR_CPU_BUFFERS __stringify(CLEAR_CPU_BUFFERS_SEQ)
>
> Maybe CLEAR_CPU_BUFFERS_SEQ should just be defined as a string in the
> first place?
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.
© 2016 - 2026 Red Hat, Inc.