Re: [PATCH 43/61] KVM: x86: Use KVM cpu caps to mark CR4.LA57 as not-reserved

From: Sean Christopherson
Date: Mon Feb 24 2020 - 18:23:10 EST


On Mon, Feb 24, 2020 at 11:08:30PM +0100, Vitaly Kuznetsov wrote:
> Sean Christopherson <sean.j.christopherson@xxxxxxxxx> writes:
>
> > Add accessor(s) for KVM cpu caps and use said accessor to detect
> > hardware support for LA57 instead of manually querying CPUID.
> >
> > No functional change intended.
> >
> > Signed-off-by: Sean Christopherson <sean.j.christopherson@xxxxxxxxx>
> > ---
> > arch/x86/kvm/cpuid.h | 13 +++++++++++++
> > arch/x86/kvm/x86.c | 2 +-
> > 2 files changed, 14 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
> > index 7b71ae0ca05e..5ce4219d465f 100644
> > --- a/arch/x86/kvm/cpuid.h
> > +++ b/arch/x86/kvm/cpuid.h
> > @@ -274,6 +274,19 @@ static __always_inline void kvm_cpu_cap_set(unsigned x86_feature)
> > kvm_cpu_caps[x86_leaf] |= __feature_bit(x86_feature);
> > }
> >
> > +static __always_inline u32 kvm_cpu_cap_get(unsigned x86_feature)
> > +{
> > + unsigned x86_leaf = x86_feature / 32;
> > +
> > + reverse_cpuid_check(x86_leaf);
> > + return kvm_cpu_caps[x86_leaf] & __feature_bit(x86_feature);
> > +}
> > +
> > +static __always_inline bool kvm_cpu_cap_has(unsigned x86_feature)
> > +{
> > + return kvm_cpu_cap_get(x86_feature);
> > +}
>
> I know this works (and I even checked C99 to make sure that it works not
> by accident) but I have to admit that explicit '!!' conversion to bool
> always makes me feel safer :-)

Eh, the flip side of blasting it everywhere is that people then forget why
the pattern exists in the first place and don't understand when it's truly
necessary.

> > +
> > static __always_inline void kvm_cpu_cap_check_and_set(unsigned x86_feature)
> > {
> > if (boot_cpu_has(x86_feature))
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index c5ed199d6cd9..cb40737187a1 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -912,7 +912,7 @@ static u64 kvm_host_cr4_reserved_bits(struct cpuinfo_x86 *c)
> > {
> > u64 reserved_bits = __cr4_reserved_bits(cpu_has, c);
> >
> > - if (cpuid_ecx(0x7) & feature_bit(LA57))
> > + if (kvm_cpu_cap_has(X86_FEATURE_LA57))
> > reserved_bits &= ~X86_CR4_LA57;
> >
> > if (kvm_x86_ops->umip_emulated())
>
> Reviewed-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
>
> --
> Vitaly
>