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

Sean Christopherson
2025-11-17T15:33:14Z

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