Re: [PATCH 06/20] x86/msr: Standardize on 'u32' MSR indices in

Xin Li
2025-04-10T17:45:50Z

On 4/10/2025 12:48 AM, Peter Zijlstra wrote:
> I have vague memories of locations where we*have* to use native_{rd,wr}msr()
> because the paravirt indirections cause problems.
>
> I don't clearly recall the reasons, but it could be instrumentation
> related. The paravirt muck has tracepoints and other crap included.

Hey, I was discussing this with hpa a few days ago ;)

Here is the existing code:

#define native_wrmsr(msr, low, high) \
__wrmsr(msr, low, high)

...

static inline void notrace native_write_msr(unsigned int msr, u32 low,
u32 high)
{
__wrmsr(msr, low, high);

if (tracepoint_enabled(write_msr))
do_trace_write_msr(msr, ((u64)high << 32 | low), 0);
}

So native_write_msr() is just native_wrmsr() + do_trace().

native_wrmsr() is used only once in arch/x86/coco/sev/core.c, and Boris
explained that in commit:

https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=f980f9c31a923e9040dee0bc679a5f5b09e61f40.

[ bp: Use the non-tracing rd/wrmsr variants because:
vmlinux.o: warning: objtool: __sev_es_nmi_complete()+0x11f: \
call to do_trace_write_msr() leaves .noinstr.text section
as __sev_es_nmi_complete() is noinstr due to being called from the
NMI handler exc_nmi(). ]

It's confusing from the function names when to use native_write_msr()
or native_wrmsr() after removing the paravirt MSR APIs. And I want to
rename native_wrmsr() to native_wrmsr_no_trace(), and then
native_write_msr() to native_wrmsr() to make their difference explicit.

The same to native_wrmsrl(), which is used in VMX noinstr functions.

There is only native_rdmsr(), but no native_rdmsrl(). And
native_rdmsr() is used in microcode code only.

BTW, using the native_*() functions on Xen shouldn't be a problem,
because they cause #GP and then Xen could:

1) emulate the MSR instruction, and advance guest IP.

2) inject #GP back into the guest, and the guest MSR instruction is
skipped in guest #GP handler, with MSR access *_safe() APIs an error is
returned to the MSR APIs.

Thanks!
Xin