Re: [PATCH v6 8/9] KVM: VMX: Open code VMX preemption timer rate mask in its accessor

Sean Christopherson
2024-04-25T14:42:06Z

On Thu, Apr 25, 2024, Kai Huang wrote:
> On Wed, 2024-04-24 at 13:06 -0700, Sean Christopherson wrote:
> > > > static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic)
> > > > {
> > > > return (vmx_basic & GENMASK_ULL(53, 50)) >>
> > > > VMX_BASIC_MEM_TYPE_SHIFT;
> > > > }
> > > >
> > > > looks not intuitive than original patch.
> > >
> > > Yeah, agreed, that's taking the worst of both worlds. I'll update patch 5 to drop
> > > VMX_BASIC_MEM_TYPE_SHIFT when effectively "moving" it into vmx_basic_vmcs_mem_type().
> >
> > Drat. Finally getting back to this, dropping VMX_BASIC_MEM_TYPE_SHIFT doesn't
> > work because it's used by nested_vmx_setup_basic(), as is VMX_BASIC_VMCS_SIZE_SHIFT,
> > which is presumably why past me kept them around.
> >
> > I'm leaning towards keeping things as proposed in this series. I don't see us
> > gaining a third copy, or even a third user, i.e. I don't think we are creating a
> > future problem by open coding the shift in vmx_basic_vmcs_mem_type(). And IMO
> > code like this
> >
> > return (vmx_basic & VMX_BASIC_MEM_TYPE_MASK) >>
> > VMX_BASIC_MEM_TYPE_SHIFT;
> >
> > is an unnecessary obfuscation when there is literally one user (the accessor).
> >
> > Another idea would be to delete VMX_BASIC_MEM_TYPE_SHIFT and VMX_BASIC_VMCS_SIZE_SHIFT,
> > and either open code the values or use local const variables, but that also seems
> > like a net negative, e.g. splits the effective definitions over too many locations.
>
> Alternatively, we can add macros like below to <asm/vmx.h> close to
> vmx_basic_vmcs_size() etc, so it's straightforward to see.
>
> +#define VMX_BSAIC_VMCS12_SIZE ((u64)VMCS12_SIZE << 32)
> +#define VMX_BASIC_MEM_TYPE_WB (MEM_TYPE_WB << 50)

Hmm, it's a bit hard to see it's specifically VMCS12 size, and given that prior
to this series, VMX_BASIC_MEM_TYPE_WB = 6, I'm hesitant to re-introduce/redefine
that macro with a different value.

What if we add a helper in vmx.h to encode the VMCS info? Then the #defines for
the shifts can go away because the open coded shifts are colocated and more
obviously related. E.g.

static inline u64 vmx_basic_encode_vmcs_info(u32 revision, u16 size, u8 memtype)
{
return revision | ((u64)size << 32) | ((u64)memtype << 50);
}

and

static void nested_vmx_setup_basic(struct nested_vmx_msrs *msrs)
{
/*
* This MSR reports some information about VMX support. We
* should return information about the VMX we emulate for the
* guest, and the VMCS structure we give it - not about the
* VMX support of the underlying hardware.
*/
msrs->basic = vmx_basic_encode_vmcs_info(VMCS12_REVISION, VMCS12_SIZE,
X86_MEMTYPE_WB);

msrs->basic |= VMX_BASIC_TRUE_CTLS
if (cpu_has_vmx_basic_inout())
msrs->basic |= VMX_BASIC_INOUT;
}