Re: [PATCH 1/1] KVM: VMX: Cleanup VMX basic information defines and usages

From: Sean Christopherson
Date: Thu Oct 19 2023 - 12:15:20 EST


On Thu, Oct 19, 2023, Xin Li wrote:
> On 10/18/2023 2:08 PM, Sean Christopherson wrote:
>
> > > Add IA32_VMX_BASIC MSR bitfield shift macros and use them to define VMX
> > > basic information bitfields.
> >
> > Why? Unless something actually uses the shift independently, just define the
> > BIT_ULL(...) straightaway.
>
> Well, reading "BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |" is hard.

I wasn't suggesting that, I was suggesting:

#define VMX_BASIC_INOUT BIT_ULL(54)

instead of

#define VMX_BASIC_INOUT BIT_ULL(VMX_BASIC_INOUT_SHIFT)

Defining a shift adds a pointless layer of indirection (if the shift isn't used
directly). It's especially problematic when there are a series of definitions.
E.g. if I want to know which bit a flag corresponds to, this:

#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(48)
#define VMX_BASIC_DUAL_MONITOR_TREATMENT BIT_ULL(49)
#define VMX_BASIC_MEM_TYPE(x) (((x) & GENMASK_ULL(53, 50)) >> 50)
#define VMX_BASIC_INOUT BIT_ULL(54)
#define VMX_BASIC_TRUE_CTLS BIT_ULL(55)

is much easier for me to process than this

#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT 48
#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT)
#define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT 49
#define VMX_BASIC_DUAL_MONITOR_TREATMENT BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT)
#define VMX_BASIC_MEM_TYPE_SHIFT 50
#define VMX_BASIC_INOUT_SHIFT 54
#define VMX_BASIC_INOUT BIT_ULL(VMX_BASIC_INOUT_SHIFT)
#define VMX_BASIC_TRUE_CTLS_SHIFT 55
#define VMX_BASIC_TRUE_CTLS BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT)

and the former also tends to work better for IDEs that support peeking at macro
definitions.

> > > ---
> > > arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------
> > > arch/x86/kvm/vmx/nested.c | 10 +++------
> > > arch/x86/kvm/vmx/vmx.c | 2 +-
> > > tools/arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------
> >
> > Please drop the tools/ update, copying kernel headers into tools is a perf tools
> > thing that I want no part of.
> >
> > https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%2Fv@xxxxxxxxxx
>
> why can't we simply remove tools/arch/x86/include/asm/msr-index.h?

That's a question for the tools/perf folks, though I believe the answer is partly
that the perf tooling relies on *exactly* matching kernel-internal structures, and
so tools/perf doesn't want to rely on installed headers.

> > > +#define VMX_BASIC_RESERVED_BITS \
> > > + (VMX_BASIC_ALWAYS_0 | \
> > > + VMX_BASIC_RESERVED_RANGE_1 | \
> > > + VMX_BASIC_RESERVED_RANGE_2)
> >
> > I don't see any value in defining VMX_BASIC_RESERVED_RANGE_1 and
> > VMX_BASIC_RESERVED_RANGE_2 separately. Or VMX_BASIC_ALWAYS_0 for the matter.
> > And I don't think these macros need to go in msr-index.h, e.g. just define them
> > above vmx_restore_vmx_basic() as that's likely going to be the only user, ever.
>
> hmm, I'm overusing macros, better do:
> #define VMX_BASIC_RESERVED_BITS \
> (BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56))

Please define from high=>low, x86 is little-endian. I.e.

(GENMASK_ULL(63, 56) | GENMASK_ULL(47, 45) | BIT_ULL(31))

> Probably should also move VMX MSR field defs from msr-index.h to
> a vmx header file.

Why bother putting them in a header? As above, it's extremely unlikely anything
besides vmx_restore_vmx_basic() will ever care about exactly which bits are
reserved.