Re: [PATCH] KVM: VMX: Micro-optimize SPEC_CTRL handling in __vmx_vcpu_run()

From: Uros Bizjak

Date: Thu Nov 06 2025 - 13:29:45 EST


On Thu, Nov 6, 2025 at 2:12 AM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:

> > VMX patch is at [1]. SVM patch is a bit more involved, because new
> > 32-bit code needs to clobber one additional register. The SVM patch is
> > attached to this message, but while I compile tested it, I have no
> > means of testing it with runtime tests. Can you please put it through
> > your torture tests?

[...]

> > -
> > /* Now restore the host value of the MSR if different from the guest's. */
> > - movl PER_CPU_VAR(x86_spec_ctrl_current), %eax
> > - cmp SVM_spec_ctrl(%_ASM_DI), %eax
> > + mov SVM_spec_ctrl(%rdi), %rdx
> > + cmp PER_CPU_VAR(x86_spec_ctrl_current), %rdx
> > je 901b
> > - xor %edx, %edx
> > + movl %edx, %eax
> > + shr $32, %rdx
> > +#else
> > + mov %eax, SVM_spec_ctrl(%edi)
> > + mov %edx, SVM_spec_ctrl + 4(%edi)
> > +998:
> > + /* Now restore the host value of the MSR if different from the guest's. */
> > + mov SVM_spec_ctrl(%edi), %eax
> > + mov PER_CPU_VAR(x86_spec_ctrl_current), %esi
> > + xor %eax, %esi
> > + mov SVM_spec_ctrl + 4(%edi), %edx
> > + mov PER_CPU_VAR(x86_spec_ctrl_current + 4), %edi
> > + xor %edx, %edi
> > + or %edi, %esi
> > + je 901b
>
> This particular flow is backwards, in that it loads the guest value into EDX:EAX
> instead of the host values.

Yeah, sorry about that, I was really not sure which value is where.
Please just swap

SVM_spec_ctrl(%edi) with PER_CPU_VAR(x86_spec_ctrl_current)

references (and their offseted variants) in the above code, and it
will result in the correct and optimal code.

Thanks,
Uros.