Re: [PATCH v1 2/4] x86/fred: Write to FRED MSRs with wrmsrns()
From: H. Peter Anvin
Date: Wed Jul 03 2024 - 12:08:11 EST
On July 3, 2024 9:00:53 AM PDT, Andrew Cooper <andrew.cooper3@xxxxxxxxxx> wrote:
>On 03/07/2024 4:54 pm, Borislav Petkov wrote:
>> Dave Hansen <dave.hansen@xxxxxxxxx> wrote:
>>> On 7/3/24 01:54, Xin Li (Intel) wrote:
>>> > Do FRED MSR writes with wrmsrns() rather than wrmsrl().
>>>
>>> A longer changelog would be appreciated here. The wrmsrns() is
>>> presumably to avoid the WRMSR serialization overhead and the CR4 write
>>> provides all of the serialization that we need.
>> Also, all those wrmsrns() writes better be behind a CPUID check.
>
>They're not, in Linux.
>
>For the $N'th time, here is the primitive that Linux wants to stea^w
>borrow for this to be sane.
>
>/* Non-serialising WRMSR, when available. Falls back to a serialising
>WRMSR. */
>static inline void wrmsrns(uint32_t msr, uint32_t lo, uint32_t hi)
>{
> /*
> * WRMSR is 2 bytes. WRMSRNS is 3 bytes. Pad WRMSR with a redundant CS
> * prefix to avoid a trailing NOP.
> */
> alternative_input(".byte 0x2e; wrmsr",
> ".byte 0x0f,0x01,0xc6", X86_FEATURE_WRMSRNS,
> "c" (msr), "a" (lo), "d" (hi));
>}
>
>~Andrew
I believe tglx declared to use them unconditionally since FRED depends on WRMSRNS (and the kernel enforces that.)
Using an alternative would make wrmsrns() a more useful construct in general, though.