kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] KVM: VMX: cleanup VMXON region allocation
@ 2020-03-05 18:37 Vitaly Kuznetsov
  2020-03-05 18:37 ` [PATCH v2 1/2] KVM: VMX: rename 'kvm_area' to 'vmxon_region' Vitaly Kuznetsov
  2020-03-05 18:37 ` [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS Vitaly Kuznetsov
  0 siblings, 2 replies; 5+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-05 18:37 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Jim Mattson, Sean Christopherson, Wanpeng Li, kvm, linux-kernel

Minor cleanup with no functional change (intended):
- Rename 'kvm_area' to 'vmxon_region'
- Simplify setting revision_id for VMXON region when eVMCS is in use

Changes since v1:
- Patches prefix ('KVM: VMX:' now) [Sean Christopherson]
- Add Sean's R-b to PATCH1
- Do not rename alloc_vmcs_cpu() to alloc_vmx_area_cpu() and add a comment
  explaining why we call VMXON region 'VMCS' [Sean Christopherson]

Vitaly Kuznetsov (2):
  KVM: VMX: rename 'kvm_area' to 'vmxon_region'
  KVM: VMX: untangle VMXON revision_id setting when using eVMCS

 arch/x86/kvm/vmx/vmx.c | 42 +++++++++++++++++++-----------------------
 arch/x86/kvm/vmx/vmx.h | 12 +++++++++---
 2 files changed, 28 insertions(+), 26 deletions(-)

-- 
2.24.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] KVM: VMX: rename 'kvm_area' to 'vmxon_region'
  2020-03-05 18:37 [PATCH v2 0/2] KVM: VMX: cleanup VMXON region allocation Vitaly Kuznetsov
@ 2020-03-05 18:37 ` Vitaly Kuznetsov
  2020-03-05 18:37 ` [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS Vitaly Kuznetsov
  1 sibling, 0 replies; 5+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-05 18:37 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Jim Mattson, Sean Christopherson, Wanpeng Li, kvm, linux-kernel

The name 'kvm_area' is misleading (as we have way too many areas which are
KVM related), what alloc_kvm_area()/free_kvm_area() functions really do is
allocate/free VMXON region for all CPUs. Rename accordingly.

No functional change intended.

Reviewed-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 arch/x86/kvm/vmx/vmx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 40b1e6138cd5..dab19e4e5f2b 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2635,7 +2635,7 @@ int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
 	return -ENOMEM;
 }
 
-static void free_kvm_area(void)
+static void free_vmxon_regions(void)
 {
 	int cpu;
 
@@ -2645,7 +2645,7 @@ static void free_kvm_area(void)
 	}
 }
 
-static __init int alloc_kvm_area(void)
+static __init int alloc_vmxon_regions(void)
 {
 	int cpu;
 
@@ -2654,7 +2654,7 @@ static __init int alloc_kvm_area(void)
 
 		vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
 		if (!vmcs) {
-			free_kvm_area();
+			free_vmxon_regions();
 			return -ENOMEM;
 		}
 
@@ -7815,7 +7815,7 @@ static __init int hardware_setup(void)
 			return r;
 	}
 
-	r = alloc_kvm_area();
+	r = alloc_vmxon_regions();
 	if (r)
 		nested_vmx_hardware_unsetup();
 	return r;
@@ -7826,7 +7826,7 @@ static __exit void hardware_unsetup(void)
 	if (nested)
 		nested_vmx_hardware_unsetup();
 
-	free_kvm_area();
+	free_vmxon_regions();
 }
 
 static bool vmx_check_apicv_inhibit_reasons(ulong bit)
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS
  2020-03-05 18:37 [PATCH v2 0/2] KVM: VMX: cleanup VMXON region allocation Vitaly Kuznetsov
  2020-03-05 18:37 ` [PATCH v2 1/2] KVM: VMX: rename 'kvm_area' to 'vmxon_region' Vitaly Kuznetsov
@ 2020-03-05 18:37 ` Vitaly Kuznetsov
  2020-03-05 20:10   ` Sean Christopherson
  1 sibling, 1 reply; 5+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-05 18:37 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Jim Mattson, Sean Christopherson, Wanpeng Li, kvm, linux-kernel

As stated in alloc_vmxon_regions(), VMXON region needs to be tagged with
revision id from MSR_IA32_VMX_BASIC even in case of eVMCS. The logic to
do so is not very straightforward: first, we set
hdr.revision_id = KVM_EVMCS_VERSION in alloc_vmcs_cpu() just to reset it
back to vmcs_config.revision_id in alloc_vmxon_regions(). Simplify this by
introducing 'enum vmcs_type' parameter to alloc_vmcs_cpu().

No functional change intended.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
 arch/x86/kvm/vmx/vmx.c | 32 ++++++++++++++------------------
 arch/x86/kvm/vmx/vmx.h | 12 +++++++++---
 2 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index dab19e4e5f2b..697be8823576 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2554,7 +2554,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
 	return 0;
 }
 
-struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
+struct vmcs *alloc_vmcs_cpu(enum vmcs_type type, int cpu, gfp_t flags)
 {
 	int node = cpu_to_node(cpu);
 	struct page *pages;
@@ -2566,13 +2566,21 @@ struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
 	vmcs = page_address(pages);
 	memset(vmcs, 0, vmcs_config.size);
 
-	/* KVM supports Enlightened VMCS v1 only */
-	if (static_branch_unlikely(&enable_evmcs))
+	/*
+	 * When eVMCS is enabled, vmcs->revision_id needs to be set to the
+	 * supported eVMCS version (KVM_EVMCS_VERSION) instead of revision_id
+	 * reported by MSR_IA32_VMX_BASIC.
+	 *
+	 * However, even though not explicitly documented by TLFS, VMXArea
+	 * passed as VMXON argument should still be marked with revision_id
+	 * reported by physical CPU.
+	 */
+	if (type != VMXON_REGION && static_branch_unlikely(&enable_evmcs))
 		vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
 	else
 		vmcs->hdr.revision_id = vmcs_config.revision_id;
 
-	if (shadow)
+	if (type == SHADOW_VMCS_REGION)
 		vmcs->hdr.shadow_vmcs = 1;
 	return vmcs;
 }
@@ -2652,25 +2660,13 @@ static __init int alloc_vmxon_regions(void)
 	for_each_possible_cpu(cpu) {
 		struct vmcs *vmcs;
 
-		vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
+		/* The VMXON region is really just a special type of VMCS. */
+		vmcs = alloc_vmcs_cpu(VMXON_REGION, cpu, GFP_KERNEL);
 		if (!vmcs) {
 			free_vmxon_regions();
 			return -ENOMEM;
 		}
 
-		/*
-		 * When eVMCS is enabled, alloc_vmcs_cpu() sets
-		 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
-		 * revision_id reported by MSR_IA32_VMX_BASIC.
-		 *
-		 * However, even though not explicitly documented by
-		 * TLFS, VMXArea passed as VMXON argument should
-		 * still be marked with revision_id reported by
-		 * physical CPU.
-		 */
-		if (static_branch_unlikely(&enable_evmcs))
-			vmcs->hdr.revision_id = vmcs_config.revision_id;
-
 		per_cpu(vmxarea, cpu) = vmcs;
 	}
 	return 0;
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index e64da06c7009..4c327030bb9c 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -489,7 +489,13 @@ static inline struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
 	return &(to_vmx(vcpu)->pi_desc);
 }
 
-struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
+enum vmcs_type {
+	VMXON_REGION,
+	VMCS_REGION,
+	SHADOW_VMCS_REGION,
+};
+
+struct vmcs *alloc_vmcs_cpu(enum vmcs_type type, int cpu, gfp_t flags);
 void free_vmcs(struct vmcs *vmcs);
 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
 void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
@@ -498,8 +504,8 @@ void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
 
 static inline struct vmcs *alloc_vmcs(bool shadow)
 {
-	return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
-			      GFP_KERNEL_ACCOUNT);
+	return alloc_vmcs_cpu(shadow ? SHADOW_VMCS_REGION : VMCS_REGION,
+			      raw_smp_processor_id(), GFP_KERNEL_ACCOUNT);
 }
 
 u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS
  2020-03-05 18:37 ` [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS Vitaly Kuznetsov
@ 2020-03-05 20:10   ` Sean Christopherson
  2020-03-06 10:06     ` Vitaly Kuznetsov
  0 siblings, 1 reply; 5+ messages in thread
From: Sean Christopherson @ 2020-03-05 20:10 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: Paolo Bonzini, Jim Mattson, Wanpeng Li, kvm, linux-kernel

On Thu, Mar 05, 2020 at 07:37:25PM +0100, Vitaly Kuznetsov wrote:
> As stated in alloc_vmxon_regions(), VMXON region needs to be tagged with
> revision id from MSR_IA32_VMX_BASIC even in case of eVMCS. The logic to
> do so is not very straightforward: first, we set
> hdr.revision_id = KVM_EVMCS_VERSION in alloc_vmcs_cpu() just to reset it
> back to vmcs_config.revision_id in alloc_vmxon_regions(). Simplify this by
> introducing 'enum vmcs_type' parameter to alloc_vmcs_cpu().
> 
> No functional change intended.
> 
> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
> ---

...

> +	 * However, even though not explicitly documented by TLFS, VMXArea
> +	 * passed as VMXON argument should still be marked with revision_id
> +	 * reported by physical CPU.

LOL, nice.


> +	 */
> +	if (type != VMXON_REGION && static_branch_unlikely(&enable_evmcs))
>  		vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
>  	else
>  		vmcs->hdr.revision_id = vmcs_config.revision_id;
>  
> -	if (shadow)
> +	if (type == SHADOW_VMCS_REGION)
>  		vmcs->hdr.shadow_vmcs = 1;
>  	return vmcs;
>  }

> -struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
> +enum vmcs_type {
> +	VMXON_REGION,
> +	VMCS_REGION,
> +	SHADOW_VMCS_REGION,
> +};
> +
> +struct vmcs *alloc_vmcs_cpu(enum vmcs_type type, int cpu, gfp_t flags);
>  void free_vmcs(struct vmcs *vmcs);
>  int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
>  void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
> @@ -498,8 +504,8 @@ void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
>  
>  static inline struct vmcs *alloc_vmcs(bool shadow)

I think it'd be cleaner overall to take "enum vmcs_type" in alloc_vmcs().
Then the ternary operator goes away and the callers (all two of 'em) are
self-documenting.  E.g.

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 79c7764c77b1..2c8a0a1386b1 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -4566,7 +4566,7 @@ static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
        WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);

        if (!loaded_vmcs->shadow_vmcs) {
-               loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
+               loaded_vmcs->shadow_vmcs = alloc_vmcs(SHADOW_VMCS_REGION);
                if (loaded_vmcs->shadow_vmcs)
                        vmcs_clear(loaded_vmcs->shadow_vmcs);
        }
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 5bdf6919de83..4634f6d7d55a 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2605,7 +2605,7 @@ void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)

 int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
 {
-       loaded_vmcs->vmcs = alloc_vmcs(false);
+       loaded_vmcs->vmcs = alloc_vmcs(VMCS_REGION);
        if (!loaded_vmcs->vmcs)
                return -ENOMEM;

diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 4c327030bb9c..a5eb92638ac2 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -502,10 +502,10 @@ void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
 void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs);
 void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);

-static inline struct vmcs *alloc_vmcs(bool shadow)
+static inline struct vmcs *alloc_vmcs(enum vmcs_type type)
 {
-       return alloc_vmcs_cpu(shadow ? SHADOW_VMCS_REGION : VMCS_REGION,
-                             raw_smp_processor_id(), GFP_KERNEL_ACCOUNT);
+       return alloc_vmcs_cpu(type, raw_smp_processor_id(),
+                             GFP_KERNEL_ACCOUNT);
 }

 u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);



>  {
> -	return alloc_vmcs_cpu(shadow, raw_smp_processor_id(),
> -			      GFP_KERNEL_ACCOUNT);
> +	return alloc_vmcs_cpu(shadow ? SHADOW_VMCS_REGION : VMCS_REGION,
> +			      raw_smp_processor_id(), GFP_KERNEL_ACCOUNT);
>  }
>  
>  u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
> -- 
> 2.24.1
> 

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS
  2020-03-05 20:10   ` Sean Christopherson
@ 2020-03-06 10:06     ` Vitaly Kuznetsov
  0 siblings, 0 replies; 5+ messages in thread
From: Vitaly Kuznetsov @ 2020-03-06 10:06 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Jim Mattson, Wanpeng Li, kvm, linux-kernel

Sean Christopherson <sean.j.christopherson@intel.com> writes:

> On Thu, Mar 05, 2020 at 07:37:25PM +0100, Vitaly Kuznetsov wrote:
>> As stated in alloc_vmxon_regions(), VMXON region needs to be tagged with
>> revision id from MSR_IA32_VMX_BASIC even in case of eVMCS. The logic to
>> do so is not very straightforward: first, we set
>> hdr.revision_id = KVM_EVMCS_VERSION in alloc_vmcs_cpu() just to reset it
>> back to vmcs_config.revision_id in alloc_vmxon_regions(). Simplify this by
>> introducing 'enum vmcs_type' parameter to alloc_vmcs_cpu().
>> 
>> No functional change intended.
>> 
>> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
>> ---
>
> ...
>
>> +	 * However, even though not explicitly documented by TLFS, VMXArea
>> +	 * passed as VMXON argument should still be marked with revision_id
>> +	 * reported by physical CPU.
>
> LOL, nice.
>
>
>> +	 */
>> +	if (type != VMXON_REGION && static_branch_unlikely(&enable_evmcs))
>>  		vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
>>  	else
>>  		vmcs->hdr.revision_id = vmcs_config.revision_id;
>>  
>> -	if (shadow)
>> +	if (type == SHADOW_VMCS_REGION)
>>  		vmcs->hdr.shadow_vmcs = 1;
>>  	return vmcs;
>>  }
>
>> -struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags);
>> +enum vmcs_type {
>> +	VMXON_REGION,
>> +	VMCS_REGION,
>> +	SHADOW_VMCS_REGION,
>> +};
>> +
>> +struct vmcs *alloc_vmcs_cpu(enum vmcs_type type, int cpu, gfp_t flags);
>>  void free_vmcs(struct vmcs *vmcs);
>>  int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
>>  void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs);
>> @@ -498,8 +504,8 @@ void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs);
>>  
>>  static inline struct vmcs *alloc_vmcs(bool shadow)
>
> I think it'd be cleaner overall to take "enum vmcs_type" in alloc_vmcs().
> Then the ternary operator goes away and the callers (all two of 'em) are
> self-documenting.

Ya, it didn't seem to be needed with my initial suggestion to rename
alloc_vmcs_cpu() to alloc_vmx_area_cpu() because in case we think of
VMXON region as something different from VMCS we have only two options:
normal VMCS or shadow VMCS and bool flag works perfectly. 

v3 is on the way, stay tuned!

-- 
Vitaly


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-03-06 10:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-05 18:37 [PATCH v2 0/2] KVM: VMX: cleanup VMXON region allocation Vitaly Kuznetsov
2020-03-05 18:37 ` [PATCH v2 1/2] KVM: VMX: rename 'kvm_area' to 'vmxon_region' Vitaly Kuznetsov
2020-03-05 18:37 ` [PATCH v2 2/2] KVM: VMX: untangle VMXON revision_id setting when using eVMCS Vitaly Kuznetsov
2020-03-05 20:10   ` Sean Christopherson
2020-03-06 10:06     ` Vitaly Kuznetsov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).