Re: [RFC PATCH v2 10/34] x86/msr: Convert __rdmsr() uses to native_rdmsrq() uses

Sean Christopherson
2025-04-22T15:09:36Z

On Tue, Apr 22, 2025, Xin Li (Intel) wrote:
> __rdmsr() is the lowest level primitive MSR read API, and its direct
> use is NOT preferred.

Doesn't mean it's wrong.

> Use its wrapper function native_rdmsrq() instead.

...

> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index 1547bfacd40f..e73c1d5ba6c4 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -380,7 +380,7 @@ static __always_inline void vmx_disable_fb_clear(struct vcpu_vmx *vmx)
> if (!vmx->disable_fb_clear)
> return;
>
> - msr = __rdmsr(MSR_IA32_MCU_OPT_CTRL);
> + msr = native_rdmsrq(MSR_IA32_MCU_OPT_CTRL);
> msr |= FB_CLEAR_DIS;
> native_wrmsrq(MSR_IA32_MCU_OPT_CTRL, msr);
> /* Cache the MSR value to avoid reading it later */
> @@ -7307,7 +7307,7 @@ void noinstr vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx,
> return;
>
> if (flags & VMX_RUN_SAVE_SPEC_CTRL)
> - vmx->spec_ctrl = __rdmsr(MSR_IA32_SPEC_CTRL);
> + vmx->spec_ctrl = native_rdmsrq(MSR_IA32_SPEC_CTRL);

And what guarantees that native_rdmsrq() won't have tracing? Ugh, a later patch
renames native_rdmsrq() => native_rdmsrq_no_trace().

I really don't like this. It makes simple and obvious code:

vmx->spec_ctrl = __rdmsr(MSR_IA32_SPEC_CTRL);

so much harder to read:

vmx->spec_ctrl = native_rdmsrq_no_trace(MSR_IA32_SPEC_CTRL);

and does so in a way that is difficult to review, e.g. I have to peek ahead to
understand that this is even ok.

I strongly prefer that we find a way to not require such verbose APIs, especially
if KVM ends up using native variants throughout. Xen PV is supposed to be the
odd one out, yet native code is what suffers. Blech.