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

From: Xiaoyao Li
Date: Thu Apr 25 2024 - 10:18:35 EST


On 4/25/2024 4:06 AM, Sean Christopherson wrote:
On Tue, Apr 02, 2024, Sean Christopherson wrote:
On Mon, Apr 01, 2024, Xiaoyao Li wrote:
On 3/16/2024 1:54 AM, Sean Christopherson wrote:
On Fri, Mar 15, 2024, Zhao Liu wrote:
On Fri, Mar 08, 2024 at 05:27:24PM -0800, Sean Christopherson wrote:
Use vmx_misc_preemption_timer_rate() to get the rate in hardware_setup(),
and open code the rate's bitmask in vmx_misc_preemption_timer_rate() so
that the function looks like all the helpers that grab values from
VMX_BASIC and VMX_MISC MSR values.

...

-#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK GENMASK_ULL(4, 0)
#define VMX_MISC_SAVE_EFER_LMA BIT_ULL(5)
#define VMX_MISC_ACTIVITY_HLT BIT_ULL(6)
#define VMX_MISC_ACTIVITY_SHUTDOWN BIT_ULL(7)
@@ -162,7 +161,7 @@ static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic)
static inline int vmx_misc_preemption_timer_rate(u64 vmx_misc)
{
- return vmx_misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
+ return vmx_misc & GENMASK_ULL(4, 0);
}

I feel keeping VMX_MISC_PREEMPTION_TIMER_RATE_MASK is clearer than
GENMASK_ULL(4, 0), and the former improves code readability.

May not need to drop VMX_MISC_PREEMPTION_TIMER_RATE_MASK?

I don't necessarily disagree, but in this case I value consistency over one
individual case. As called out in the changelog, the motivation is to make
vmx_misc_preemption_timer_rate() look like all the surrounding helpers.

_If_ we want to preserve the mask, then we should add #defines for vmx_misc_cr3_count(),
vmx_misc_max_msr(), etc.

I don't have a super strong preference, though I think my vote would be to not
add the masks and go with this patch. These helpers are intended to be the _only_
way to access the fields, i.e. they effectively _are_ the mask macros, just in
function form.


+1.

However, it seems different for vmx_basic_vmcs_mem_type() in patch 5, that I
just recommended to define the MASK.

Because we already have

#define VMX_BASIC_MEM_TYPE_SHIFT 50

and it has been used in vmx/nested.c,

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.

If it goes this way, I beg for a comment above the code to explain. Otherwise, people might send patch to use defined MARCO in the future.

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.