[RFC PATCH 2/3] KVM: x86: Generate set of VMX feature MSRs using first/last definitions

Sean Christopherson posted 3 patches 3 years, 8 months ago
There is a newer version of this series
[RFC PATCH 2/3] KVM: x86: Generate set of VMX feature MSRs using first/last definitions
Posted by Sean Christopherson 3 years, 8 months ago
Add VMX MSRs to the runtime list of feature MSRs by iterating over the
range of emulated MSRs instead of manually defining each MSR in the "all"
list.  Using the range definition reduces the cost of emulating a new VMX
MSR, e.g. prevents forgetting to add an MSR to the list.

Extracting the VMX MSRs from the "all" list, which is a compile-time
constant, also shrinks the list to the point where the compiler can
heavily optimize code that iterates over the list.

No functional change intended.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/x86.c | 53 +++++++++++++++++++---------------------------
 1 file changed, 22 insertions(+), 31 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 33560bfa0cac..a1c65b77fb16 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1526,36 +1526,19 @@ static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
 static unsigned num_emulated_msrs;
 
 /*
- * List of msr numbers which are used to expose MSR-based features that
- * can be used by a hypervisor to validate requested CPU features.
+ * List of MSRs that control the existence of MSR-based features, i.e. MSRs
+ * that are effectively CPUID leafs.  VMX MSRs are also included in the set of
+ * feature MSRs, but are handled separately to allow expedited lookups.
  */
-static const u32 msr_based_features_all[] = {
-	MSR_IA32_VMX_BASIC,
-	MSR_IA32_VMX_TRUE_PINBASED_CTLS,
-	MSR_IA32_VMX_PINBASED_CTLS,
-	MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
-	MSR_IA32_VMX_PROCBASED_CTLS,
-	MSR_IA32_VMX_TRUE_EXIT_CTLS,
-	MSR_IA32_VMX_EXIT_CTLS,
-	MSR_IA32_VMX_TRUE_ENTRY_CTLS,
-	MSR_IA32_VMX_ENTRY_CTLS,
-	MSR_IA32_VMX_MISC,
-	MSR_IA32_VMX_CR0_FIXED0,
-	MSR_IA32_VMX_CR0_FIXED1,
-	MSR_IA32_VMX_CR4_FIXED0,
-	MSR_IA32_VMX_CR4_FIXED1,
-	MSR_IA32_VMX_VMCS_ENUM,
-	MSR_IA32_VMX_PROCBASED_CTLS2,
-	MSR_IA32_VMX_EPT_VPID_CAP,
-	MSR_IA32_VMX_VMFUNC,
-
+static const u32 msr_based_features_all_except_vmx[] = {
 	MSR_F10H_DECFG,
 	MSR_IA32_UCODE_REV,
 	MSR_IA32_ARCH_CAPABILITIES,
 	MSR_IA32_PERF_CAPABILITIES,
 };
 
-static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
+static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all_except_vmx) +
+			      (KVM_LAST_EMULATED_VMX_MSR - KVM_FIRST_EMULATED_VMX_MSR + 1)];
 static unsigned int num_msr_based_features;
 
 static u64 kvm_get_arch_capabilities(void)
@@ -6868,6 +6851,18 @@ long kvm_arch_vm_ioctl(struct file *filp,
 	return r;
 }
 
+static void kvm_proble_feature_msr(u32 msr_index)
+{
+	struct kvm_msr_entry msr = {
+		.index = msr_index,
+	};
+
+	if (kvm_get_msr_feature(&msr))
+		return;
+
+	msr_based_features[num_msr_based_features++] = msr_index;
+}
+
 static void kvm_init_msr_list(void)
 {
 	u32 dummy[2];
@@ -6954,15 +6949,11 @@ static void kvm_init_msr_list(void)
 		emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
 	}
 
-	for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
-		struct kvm_msr_entry msr;
+	for (i = KVM_FIRST_EMULATED_VMX_MSR; i <= KVM_LAST_EMULATED_VMX_MSR; i++)
+		kvm_proble_feature_msr(i);
 
-		msr.index = msr_based_features_all[i];
-		if (kvm_get_msr_feature(&msr))
-			continue;
-
-		msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
-	}
+	for (i = 0; i < ARRAY_SIZE(msr_based_features_all_except_vmx); i++)
+		kvm_proble_feature_msr(msr_based_features_all_except_vmx[i]);
 }
 
 static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
-- 
2.37.1.559.g78731f0fdb-goog
Re: [RFC PATCH 2/3] KVM: x86: Generate set of VMX feature MSRs using first/last definitions
Posted by Paolo Bonzini 3 years, 8 months ago
On 8/5/22 19:29, Sean Christopherson wrote:
>   
> +static void kvm_proble_feature_msr(u32 msr_index)
> +{
> +	struct kvm_msr_entry msr = {
> +		.index = msr_index,
> +	};
> +
> +	if (kvm_get_msr_feature(&msr))
> +		return;
> +
> +	msr_based_features[num_msr_based_features++] = msr_index;
> +}
> +
>   static void kvm_init_msr_list(void)
>   {
>   	u32 dummy[2];
> @@ -6954,15 +6949,11 @@ static void kvm_init_msr_list(void)
>   		emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
>   	}
>   
> -	for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
> -		struct kvm_msr_entry msr;
> +	for (i = KVM_FIRST_EMULATED_VMX_MSR; i <= KVM_LAST_EMULATED_VMX_MSR; i++)
> +		kvm_proble_feature_msr(i);
>   
> -		msr.index = msr_based_features_all[i];
> -		if (kvm_get_msr_feature(&msr))
> -			continue;
> -
> -		msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
> -	}
> +	for (i = 0; i < ARRAY_SIZE(msr_based_features_all_except_vmx); i++)
> +		kvm_proble_feature_msr(msr_based_features_all_except_vmx[i]);

I'd rather move all the code to a new function 
kvm_init_feature_msr_list() instead, and call it from 
kvm_arch_hardware_setup().

Thanks,

paolo
Re: [RFC PATCH 2/3] KVM: x86: Generate set of VMX feature MSRs using first/last definitions
Posted by Sean Christopherson 3 years, 8 months ago
On Wed, Aug 10, 2022, Paolo Bonzini wrote:
> On 8/5/22 19:29, Sean Christopherson wrote:
> > +static void kvm_proble_feature_msr(u32 msr_index)
> > +{
> > +	struct kvm_msr_entry msr = {
> > +		.index = msr_index,
> > +	};
> > +
> > +	if (kvm_get_msr_feature(&msr))
> > +		return;
> > +
> > +	msr_based_features[num_msr_based_features++] = msr_index;
> > +}
> > +
> >   static void kvm_init_msr_list(void)
> >   {
> >   	u32 dummy[2];
> > @@ -6954,15 +6949,11 @@ static void kvm_init_msr_list(void)
> >   		emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
> >   	}
> > -	for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
> > -		struct kvm_msr_entry msr;
> > +	for (i = KVM_FIRST_EMULATED_VMX_MSR; i <= KVM_LAST_EMULATED_VMX_MSR; i++)
> > +		kvm_proble_feature_msr(i);
> > -		msr.index = msr_based_features_all[i];
> > -		if (kvm_get_msr_feature(&msr))
> > -			continue;
> > -
> > -		msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
> > -	}
> > +	for (i = 0; i < ARRAY_SIZE(msr_based_features_all_except_vmx); i++)
> > +		kvm_proble_feature_msr(msr_based_features_all_except_vmx[i]);
> 
> I'd rather move all the code to a new function kvm_init_feature_msr_list()
> instead, and call it from kvm_arch_hardware_setup().

Would it make sense to also split out kvm_init_emulated_msr_list()?  Hmm, and
rename this to kvm_init_virtualized_msr_list()?  I can't tell if that would be
helpful or confusing.
Re: [RFC PATCH 2/3] KVM: x86: Generate set of VMX feature MSRs using first/last definitions
Posted by Paolo Bonzini 3 years, 8 months ago
On 8/10/22 16:40, Sean Christopherson wrote:
>> I'd rather move all the code to a new function kvm_init_feature_msr_list()
>> instead, and call it from kvm_arch_hardware_setup().
> 
> Would it make sense to also split out kvm_init_emulated_msr_list()?  Hmm, and
> rename this to kvm_init_virtualized_msr_list()?  I can't tell if that would be
> helpful or confusing.

I thought of feature MSRs because it's a different ioctl altogether, but 
this is not an objection; whatever seems less confusing to you.

Paolo