Skip to contentSkip to conversation

Refactor vcpu creation flow of x86 arch

PATCH In review Xiaoyao Li 3 participants 12 messages Oct 15, 2019 – Oct 17, 2019

Subsystems (3)
lore
Share & tools
lore

Xiaoyao Li

author
#0 lore raw

cover Refactor vcpu creation flow of x86 arch

When reading the vcpu creationg flow of vmx, I find it hard to follow since it mixes the data structure allocation and initilization together.

This series tries to make the vcpu creation flow more clear that first allocating data structure and then initializing them. In this way, it helps move FPU allocation to generic x86 code (Patch 4).

This series intends to do no functional change. I just tested it with kvm_unit_tests for vmx since I have no AMD machine at hand.

Xiaoyao Li (4):
KVM: VMX: rename {vmx,nested_vmx}_vcpu_setup functions
KVM: VMX: Setup MSR bitmap only when has msr_bitmap capability
KVM: X86: Refactor kvm_arch_vcpu_create
KVM: X86: Make vcpu's FPU allocation a common function

 arch/x86/include/asm/kvm_host.h |   1 +
 arch/x86/kvm/svm.c              |  81 ++++++---------
 arch/x86/kvm/vmx/nested.c       |   2 +-
 arch/x86/kvm/vmx/nested.h       |   2 +-
 arch/x86/kvm/vmx/vmx.c          | 173 ++++++++++++++------------------
 arch/x86/kvm/x86.c              |  40 ++++++++
 6 files changed, 150 insertions(+), 149 deletions(-)
Signature · 1 line
2.19.1
author
#1 lore raw

PATCH 1/4 KVM: VMX: rename {vmx,nested_vmx}_vcpu_setup functions

Rename {vmx,nested_vmx}_vcpu_setup to {vmx,nested_vmx}_vmcs_setup, to match what they really do.

No functional change.

---
 arch/x86/kvm/vmx/nested.c | 2 +-
 arch/x86/kvm/vmx/nested.h | 2 +-
 arch/x86/kvm/vmx/vmx.c    | 9 +++------
 3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -1
index 5e231da00310..7935422d311f 100644
@@ -5768,7 +5768,7 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
 	return ret;
 }
 
-void nested_vmx_vcpu_setup(void)
+void nested_vmx_vmcs_setup(void)
 {
 	if (enable_shadow_vmcs) {
 		vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
arch/x86/kvm/vmx/nested.h +1 -1
index 187d39bf0bf1..2be1ba7482c9 100644
@@ -11,7 +11,7 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps,
 				bool apicv);
 void nested_vmx_hardware_unsetup(void);
 __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *));
-void nested_vmx_vcpu_setup(void);
+void nested_vmx_vmcs_setup(void);
 void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu);
 int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry);
 bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason);
arch/x86/kvm/vmx/vmx.c +3 -6
index e660e28e9ae0..58b77a882426 100644
@@ -4161,15 +4161,12 @@ static void ept_set_mmio_spte_mask(void)
 
 #define VMX_XSS_EXIT_BITMAP 0
 
-/*
- * Sets up the vmcs for emulated real mode.
- */
-static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
+static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
 {
 	int i;
 
 	if (nested)
-		nested_vmx_vcpu_setup();
+		nested_vmx_vmcs_setup();
 
 	if (cpu_has_vmx_msr_bitmap())
 		vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
@@ -6777,7 +6774,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	cpu = get_cpu();
 	vmx_vcpu_load(&vmx->vcpu, cpu);
 	vmx->vcpu.cpu = cpu;
-	vmx_vcpu_setup(vmx);
+	vmx_vmcs_setup(vmx);
 	vmx_vcpu_put(&vmx->vcpu);
 	put_cpu();
 	if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Signature · 1 line
2.19.1
#2 lore raw

Re: [PATCH 1/4] KVM: VMX: rename {vmx,nested_vmx}_vcpu_setup functions

Quoted 61 lines · Xiaoyao Li

Rename {vmx,nested_vmx}_vcpu_setup to {vmx,nested_vmx}_vmcs_setup, to match what they really do.

No functional change.

---
  arch/x86/kvm/vmx/nested.c | 2 +-
  arch/x86/kvm/vmx/nested.h | 2 +-
  arch/x86/kvm/vmx/vmx.c    | 9 +++------
  3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -1
index 5e231da00310..7935422d311f 100644
@@ -5768,7 +5768,7 @@ static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
  	return ret;
  }
 
-void nested_vmx_vcpu_setup(void)
+void nested_vmx_vmcs_setup(void)
  {
  	if (enable_shadow_vmcs) {
  		vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
arch/x86/kvm/vmx/nested.h +1 -1
index 187d39bf0bf1..2be1ba7482c9 100644
@@ -11,7 +11,7 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps,
  				bool apicv);
  void nested_vmx_hardware_unsetup(void);
  __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *));
-void nested_vmx_vcpu_setup(void);
+void nested_vmx_vmcs_setup(void);
  void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu);
  int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry);
  bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason);
arch/x86/kvm/vmx/vmx.c +2 -5
index e660e28e9ae0..58b77a882426 100644
@@ -4161,15 +4161,12 @@ static void ept_set_mmio_spte_mask(void)
 
  #define VMX_XSS_EXIT_BITMAP 0
 
-/*
- * Sets up the vmcs for emulated real mode.
- */
-static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
+static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
  {
  	int i;
 
  	if (nested)
-		nested_vmx_vcpu_setup();
+		nested_vmx_vmcs_setup();
 
  	if (cpu_has_vmx_msr_bitmap())
  		vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));

arch/x86/kvm/vmx/vmx.c static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) · line 6780

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -6777,7 +6774,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  	cpu = get_cpu();
  	vmx_vcpu_load(&vmx->vcpu, cpu);
  	vmx->vcpu.cpu = cpu;
-	vmx_vcpu_setup(vmx);
+	vmx_vmcs_setup(vmx);
  	vmx_vcpu_put(&vmx->vcpu);
  	put_cpu();
  	if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {

May be we should rename vmx_vcpu_reset() to vmx_vmcs_reset()  as well  ?

author
#3 lore raw

Re: [PATCH 1/4] KVM: VMX: rename {vmx,nested_vmx}_vcpu_setup functions

Quoted 64 lines · Krish Sadhukhan
Quoted 61 lines · Xiaoyao Li

Rename {vmx,nested_vmx}_vcpu_setup to {vmx,nested_vmx}_vmcs_setup, to match what they really do.

No functional change.

---
  arch/x86/kvm/vmx/nested.c | 2 +-
  arch/x86/kvm/vmx/nested.h | 2 +-
  arch/x86/kvm/vmx/vmx.c    | 9 +++------

  3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -1
index 5e231da00310..7935422d311f 100644
@@ -5768,7 +5768,7 @@ static int vmx_set_nested_state(struct kvm_vcpu 
 *vcpu,
       return ret;
   }
-void nested_vmx_vcpu_setup(void)
+void nested_vmx_vmcs_setup(void)
   {
       if (enable_shadow_vmcs) {
           vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
arch/x86/kvm/vmx/nested.h +1 -1
index 187d39bf0bf1..2be1ba7482c9 100644
@@ -11,7 +11,7 @@ void nested_vmx_setup_ctls_msrs(struct 
 nested_vmx_msrs *msrs, u32 ept_caps,
                   bool apicv);
   void nested_vmx_hardware_unsetup(void);
   __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *));
-void nested_vmx_vcpu_setup(void);
+void nested_vmx_vmcs_setup(void);
   void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu);
   int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry);

  bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason);

arch/x86/kvm/vmx/vmx.c +2 -5
index e660e28e9ae0..58b77a882426 100644
@@ -4161,15 +4161,12 @@ static void ept_set_mmio_spte_mask(void)
   #define VMX_XSS_EXIT_BITMAP 0
-/*
- * Sets up the vmcs for emulated real mode.
- */
-static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
+static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
   {
       int i;
       if (nested)
-        nested_vmx_vcpu_setup();
+        nested_vmx_vmcs_setup();
       if (cpu_has_vmx_msr_bitmap())
           vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
@@ -6777,7 +6774,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct 
 kvm *kvm, unsigned int id)
       cpu = get_cpu();
       vmx_vcpu_load(&vmx->vcpu, cpu);
Quoted 8 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1 -1
       vmx->vcpu.cpu = cpu;
-    vmx_vcpu_setup(vmx);
+    vmx_vmcs_setup(vmx);
       vmx_vcpu_put(&vmx->vcpu);
       put_cpu();

      if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {

May be we should rename vmx_vcpu_reset() to vmx_vmcs_reset()  as well  ?

Not really. vmx_vcpu_reset() not only resets vmcs but also the emulated field of vmx vcpu.

Reviewed-by
#4 lore raw

Re: [PATCH 1/4] KVM: VMX: rename {vmx,nested_vmx}_vcpu_setup functions

Quoted 74 lines · Xiaoyao Li
Quoted 64 lines · Krish Sadhukhan
Quoted 61 lines · Xiaoyao Li

Rename {vmx,nested_vmx}_vcpu_setup to {vmx,nested_vmx}_vmcs_setup, to match what they really do.

No functional change.

---
  arch/x86/kvm/vmx/nested.c | 2 +-
  arch/x86/kvm/vmx/nested.h | 2 +-
  arch/x86/kvm/vmx/vmx.c    | 9 +++------

  3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -1
index 5e231da00310..7935422d311f 100644
@@ -5768,7 +5768,7 @@ static int vmx_set_nested_state(struct 
 kvm_vcpu *vcpu,
       return ret;
   }
-void nested_vmx_vcpu_setup(void)
+void nested_vmx_vmcs_setup(void)
   {
       if (enable_shadow_vmcs) {
           vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
arch/x86/kvm/vmx/nested.h +1 -1
index 187d39bf0bf1..2be1ba7482c9 100644
@@ -11,7 +11,7 @@ void nested_vmx_setup_ctls_msrs(struct 
 nested_vmx_msrs *msrs, u32 ept_caps,
                   bool apicv);
   void nested_vmx_hardware_unsetup(void);
   __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *));
-void nested_vmx_vcpu_setup(void);
+void nested_vmx_vmcs_setup(void);
   void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu);
   int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry);

  bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason);

arch/x86/kvm/vmx/vmx.c +2 -5
index e660e28e9ae0..58b77a882426 100644
@@ -4161,15 +4161,12 @@ static void ept_set_mmio_spte_mask(void)
   #define VMX_XSS_EXIT_BITMAP 0
-/*
- * Sets up the vmcs for emulated real mode.
- */
-static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
+static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
   {
       int i;
       if (nested)
-        nested_vmx_vcpu_setup();
+        nested_vmx_vmcs_setup();
       if (cpu_has_vmx_msr_bitmap())
           vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
@@ -6777,7 +6774,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct 
 kvm *kvm, unsigned int id)
       cpu = get_cpu();
       vmx_vcpu_load(&vmx->vcpu, cpu);
Quoted 8 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1 -1
       vmx->vcpu.cpu = cpu;
-    vmx_vcpu_setup(vmx);
+    vmx_vmcs_setup(vmx);
       vmx_vcpu_put(&vmx->vcpu);
       put_cpu();

      if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {

May be we should rename vmx_vcpu_reset() to vmx_vmcs_reset()  as well  ?

Quoted 1 line

Not really. vmx_vcpu_reset() not only resets vmcs but also the emulated field of vmx vcpu.

It would be a better organization of the code if the resetting of the VMCS fields were placed in a separate function.

author
#5 lore raw

Re: [PATCH 1/4] KVM: VMX: rename {vmx,nested_vmx}_vcpu_setup functions

Quoted 76 lines · Krish Sadhukhan
Quoted 74 lines · Xiaoyao Li
Quoted 72 lines · Krish Sadhukhan
Quoted 66 lines · Xiaoyao Li

Rename {vmx,nested_vmx}_vcpu_setup to {vmx,nested_vmx}_vmcs_setup, to match what they really do.

No functional change.

---
  arch/x86/kvm/vmx/nested.c | 2 +-
  arch/x86/kvm/vmx/nested.h | 2 +-
  arch/x86/kvm/vmx/vmx.c    | 9 +++------

  3 files changed, 5 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -1
index 5e231da00310..7935422d311f 100644
@@ -5768,7 +5768,7 @@ static int vmx_set_nested_state(struct 
 kvm_vcpu *vcpu,
       return ret;
   }
-void nested_vmx_vcpu_setup(void)
+void nested_vmx_vmcs_setup(void)
   {
       if (enable_shadow_vmcs) {
           vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
arch/x86/kvm/vmx/nested.h +1 -1
index 187d39bf0bf1..2be1ba7482c9 100644
@@ -11,7 +11,7 @@ void nested_vmx_setup_ctls_msrs(struct 
 nested_vmx_msrs *msrs, u32 ept_caps,
                   bool apicv);
   void nested_vmx_hardware_unsetup(void);
   __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *));
-void nested_vmx_vcpu_setup(void);
+void nested_vmx_vmcs_setup(void);
   void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu);
   int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry);

  bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason);

arch/x86/kvm/vmx/vmx.c +3 -6
index e660e28e9ae0..58b77a882426 100644
@@ -4161,15 +4161,12 @@ static void ept_set_mmio_spte_mask(void)
   #define VMX_XSS_EXIT_BITMAP 0
-/*
- * Sets up the vmcs for emulated real mode.
- */
-static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
+static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
   {
       int i;
       if (nested)
-        nested_vmx_vcpu_setup();
+        nested_vmx_vmcs_setup();
       if (cpu_has_vmx_msr_bitmap())
           vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
@@ -6777,7 +6774,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct 
 kvm *kvm, unsigned int id)
       cpu = get_cpu();
       vmx_vcpu_load(&vmx->vcpu, cpu);
       vmx->vcpu.cpu = cpu;
-    vmx_vcpu_setup(vmx);
+    vmx_vmcs_setup(vmx);
       vmx_vcpu_put(&vmx->vcpu);
       put_cpu();
Quoted 1 line

      if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {

May be we should rename vmx_vcpu_reset() to vmx_vmcs_reset()  as well  ?

Quoted 4 lines
Quoted 1 line

Not really. vmx_vcpu_reset() not only resets vmcs but also the emulated field of vmx vcpu.

It would be a better organization of the code if the resetting of the VMCS fields were placed in a separate function.

OK, will do it in next version.

Quoted 2 lines
author
#6 lore raw

PATCH 2/4 KVM: VMX: Setup MSR bitmap only when has msr_bitmap capability

In reply to Xiaoyao Li

Move the MSR bitmap setup codes to vmx_vmcs_setup() and only setup them when hardware has msr_bitmap capability.

---
 arch/x86/kvm/vmx/vmx.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
arch/x86/kvm/vmx/vmx.c +20 -19
index 58b77a882426..7051511c27c2 100644
@@ -4164,12 +4164,30 @@ static void ept_set_mmio_spte_mask(void)
 static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
 {
 	int i;
+	unsigned long *msr_bitmap;
 
 	if (nested)
 		nested_vmx_vmcs_setup();
 
-	if (cpu_has_vmx_msr_bitmap())
-		vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
+	if (cpu_has_vmx_msr_bitmap()) {
+		msr_bitmap = vmx->vmcs01.msr_bitmap;
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
+		if (kvm_cstate_in_guest(vmx->vcpu.kvm)) {
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
+		}
+
+		vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
+	}
+	vmx->msr_bitmap_mode = 0;
 
 	vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
 
@@ -6697,7 +6715,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 {
 	int err;
 	struct vcpu_vmx *vmx;
-	unsigned long *msr_bitmap;
 	int cpu;
 
 	BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
@@ -6754,22 +6771,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	if (err < 0)
 		goto free_msrs;
 
-	msr_bitmap = vmx->vmcs01.msr_bitmap;
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
-	if (kvm_cstate_in_guest(kvm)) {
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
-	}
-	vmx->msr_bitmap_mode = 0;
-
 	vmx->loaded_vmcs = &vmx->vmcs01;
 	cpu = get_cpu();
 	vmx_vcpu_load(&vmx->vcpu, cpu);
Signature · 1 line
2.19.1
#7 lore raw

Re: [PATCH 2/4] KVM: VMX: Setup MSR bitmap only when has msr_bitmap capability

Quoted 16 lines · Xiaoyao Li

Move the MSR bitmap setup codes to vmx_vmcs_setup() and only setup them when hardware has msr_bitmap capability.

---
  arch/x86/kvm/vmx/vmx.c | 39 ++++++++++++++++++++-------------------
  1 file changed, 20 insertions(+), 19 deletions(-)
arch/x86/kvm/vmx/vmx.c +0 -0
index 58b77a882426..7051511c27c2 100644
@@ -4164,12 +4164,30 @@ static void ept_set_mmio_spte_mask(void)
  static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
  {
  	int i;

arch/x86/kvm/vmx/vmx.c static void ept_set_mmio_spte_mask(void) · line 4174

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +4 -2
+	unsigned long *msr_bitmap;
 
  	if (nested)
  		nested_vmx_vmcs_setup();
 
-	if (cpu_has_vmx_msr_bitmap())
-		vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
+	if (cpu_has_vmx_msr_bitmap()) {
+		msr_bitmap = vmx->vmcs01.msr_bitmap;
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);

vmx_disable_intercept_for_msr() also calls cpu_has_vmx_msr_bitmap(), which means we are repeating the check. A cleaner approach is to remove the call to cpu_has_vmx_msr_bitmap()  from vmx_disable_intercept_for_msr()  and let its callers do the check just like you are doing here.

Quoted 50 lines
arch/x86/kvm/vmx/vmx.c +16 -17
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
+		vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
+		if (kvm_cstate_in_guest(vmx->vcpu.kvm)) {
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
+			vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
+		}
+
+		vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
+	}
+	vmx->msr_bitmap_mode = 0;
 
  	vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
 
@@ -6697,7 +6715,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  {
  	int err;
  	struct vcpu_vmx *vmx;
-	unsigned long *msr_bitmap;
  	int cpu;
 
  	BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
@@ -6754,22 +6771,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  	if (err < 0)
  		goto free_msrs;
 
-	msr_bitmap = vmx->vmcs01.msr_bitmap;
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
-	vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
-	if (kvm_cstate_in_guest(kvm)) {
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
-		vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
-	}
-	vmx->msr_bitmap_mode = 0;
-
  	vmx->loaded_vmcs = &vmx->vmcs01;
  	cpu = get_cpu();
  	vmx_vcpu_load(&vmx->vcpu, cpu);
author
#8 lore raw

Re: [PATCH 2/4] KVM: VMX: Setup MSR bitmap only when has msr_bitmap capability

Quoted 22 lines · Krish Sadhukhan
Quoted 19 lines · Xiaoyao Li

Move the MSR bitmap setup codes to vmx_vmcs_setup() and only setup them when hardware has msr_bitmap capability.

---
  arch/x86/kvm/vmx/vmx.c | 39 ++++++++++++++++++++-------------------

  1 file changed, 20 insertions(+), 19 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -0
index 58b77a882426..7051511c27c2 100644
@@ -4164,12 +4164,30 @@ static void ept_set_mmio_spte_mask(void)
   static void vmx_vmcs_setup(struct vcpu_vmx *vmx)
   {
       int i;
+    unsigned long *msr_bitmap;
       if (nested)
           nested_vmx_vmcs_setup();
Quoted 9 lines

arch/x86/kvm/vmx/vmx.c static void ept_set_mmio_spte_mask(void)

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3 -2
-    if (cpu_has_vmx_msr_bitmap())
-        vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
+    if (cpu_has_vmx_msr_bitmap()) {
+        msr_bitmap = vmx->vmcs01.msr_bitmap;
+        vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, 
 MSR_TYPE_R);

vmx_disable_intercept_for_msr() also calls cpu_has_vmx_msr_bitmap(), which means we are repeating the check. A cleaner approach is to remove the call to cpu_has_vmx_msr_bitmap()  from vmx_disable_intercept_for_msr()  and let its callers do the check just like you are doing here.

Right.
I'll improve it. Thanks!

Quoted 67 lines
Quoted 66 lines
arch/x86/kvm/vmx/vmx.c +5 -0
+        vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, 
 MSR_TYPE_RW);
+        vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, 
 MSR_TYPE_RW);
+        vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, 
 MSR_TYPE_RW);
+        vmx_disable_intercept_for_msr(msr_bitmap, 
 MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
+        vmx_disable_intercept_for_msr(msr_bitmap, 

MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
+        vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
+        if (kvm_cstate_in_guest(vmx->vcpu.kvm)) { +            vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
+            vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
+            vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
+            vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
+        }
+
+        vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); +    }
+    vmx->msr_bitmap_mode = 0; vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */

arch/x86/kvm/vmx/vmx.c +0 -9
@@ -6697,7 +6715,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct 
 kvm *kvm, unsigned int id)
   {
       int err;
       struct vcpu_vmx *vmx;
-    unsigned long *msr_bitmap;
       int cpu;
       BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
@@ -6754,22 +6771,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct 
 kvm *kvm, unsigned int id)
       if (err < 0)
           goto free_msrs;
-    msr_bitmap = vmx->vmcs01.msr_bitmap;
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, 
 MSR_TYPE_RW);
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, 
 MSR_TYPE_RW);
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, 
 MSR_TYPE_RW);
-    vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, 

MSR_TYPE_RW);
-    if (kvm_cstate_in_guest(kvm)) {
-        vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
-        vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
-        vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
-        vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
-    }
-    vmx->msr_bitmap_mode = 0;
-
      vmx->loaded_vmcs = &vmx->vmcs01;
      cpu = get_cpu();
      vmx_vcpu_load(&vmx->vcpu, cpu);

author
#9 lore raw

PATCH 3/4 KVM: X86: Refactor kvm_arch_vcpu_create

In reply to Xiaoyao Li

Current x86 arch vcpu creation flow is a little bit messed. Specifically, vcpu's data structure allocation and vcpu initialization are mixed up, which is unfriendly to read.

Seperating the vcpu_create and vcpu_init just like what ARM does, that it first calls vcpu_create related functions for vcpu's data structure allocation and then calls vcpu_init related functions to initialize the vcpu.

---
 arch/x86/include/asm/kvm_host.h |   1 +
 arch/x86/kvm/svm.c              |  63 +++++++++---------
 arch/x86/kvm/vmx/vmx.c          | 109 ++++++++++++++++----------------
 arch/x86/kvm/x86.c              |  16 +++++
 4 files changed, 102 insertions(+), 87 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 5d8056ff7390..d574c2391145 100644
@@ -1015,6 +1015,7 @@ struct kvm_x86_ops {
 
 	/* Create, but do not attach this VCPU */
 	struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
+	int (*vcpu_init)(struct kvm_vcpu *vcpu);
 	void (*vcpu_free)(struct kvm_vcpu *vcpu);
 	void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
 
arch/x86/kvm/svm.c +30 -33

127 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +55 -54

177 lines not on this page · raw message

arch/x86/kvm/x86.c +16 -0
index f26f8be4e621..fd9f1a1f0f01 100644
@@ -9016,6 +9016,7 @@ void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
 
 	kvmclock_reset(vcpu);
 
+	kvm_vcpu_uninit(vcpu);
 	kvm_x86_ops->vcpu_free(vcpu);
 	free_cpumask_var(wbinvd_dirty_mask);
 }
@@ -9024,6 +9025,7 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
 						unsigned int id)
 {
 	struct kvm_vcpu *vcpu;
+	int err;
 
 	if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
 		printk_once(KERN_WARNING
@@ -9031,6 +9033,14 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
 		"guest TSC will not be reliable\n");
 
 	vcpu = kvm_x86_ops->vcpu_create(kvm, id);
+	if (IS_ERR(vcpu))
+		return vcpu;
+
+	err = kvm_vcpu_init(vcpu, kvm, id);
+	if (err) {
+		kvm_x86_ops->vcpu_free(vcpu);
+		return ERR_PTR(err);
+	}
 
 	return vcpu;
 }
@@ -9083,6 +9093,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 	kvm_mmu_unload(vcpu);
 	vcpu_put(vcpu);
 
+	kvm_vcpu_uninit(vcpu);
 	kvm_x86_ops->vcpu_free(vcpu);
 }
 
@@ -9379,8 +9390,13 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 
 	kvm_hv_vcpu_init(vcpu);
 
+	if (kvm_x86_ops->vcpu_init(vcpu))
+		goto fail_free_wbinvd_dirty_mask;
+
 	return 0;
 
+fail_free_wbinvd_dirty_mask:
+	free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
 fail_free_mce_banks:
 	kfree(vcpu->arch.mce_banks);
 fail_free_lapic:
Signature · 1 line
2.19.1
#10 lore raw

Re: [PATCH 3/4] KVM: X86: Refactor kvm_arch_vcpu_create

Quoted 8 lines · Xiaoyao Li

Current x86 arch vcpu creation flow is a little bit messed. Specifically, vcpu's data structure allocation and vcpu initialization are mixed up, which is unfriendly to read.

Seperating the vcpu_create and vcpu_init just like what ARM does, that it first calls vcpu_create related functions for vcpu's data structure allocation and then calls vcpu_init related functions to initialize the vcpu.

My vote is to take advantage of the requirement that @vcpu must reside at offset 0 in vmx_vcpu and svm_vcpu, and allocate the vcpu in x86 code. That would allow kvm_arch_vcpu_create() to invoke kvm_vcpu_init() directly instead of bouncing through the vendor code.

And if we're extra lucky and the other architectures can use a similar pattern, kvm_vm_ioctl_create_vcpu() could be refactored to something like:

	vcpu = kvm_arch_vcpu_alloc(kvm, id);
	if (IS_ERR(vcpu)) {
		r = PTR_ERR(vcpu);
		goto vcpu_decrement;
	}

	r = kvm_arch_vcpu_init(vcpu);
	if (r)
		goto vcpu_destroy;
author
#11 lore raw

PATCH 4/4 KVM: X86: Make vcpu's FPU allocation a common function

In reply to Xiaoyao Li

They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions and delay it a little bit later after .create_vcpu.

No functional change intended.

---
 arch/x86/kvm/svm.c     | 18 ------------------
 arch/x86/kvm/vmx/vmx.c | 18 ------------------
 arch/x86/kvm/x86.c     | 24 ++++++++++++++++++++++++
 3 files changed, 24 insertions(+), 36 deletions(-)
arch/x86/kvm/svm.c +0 -18
index d35ef5456462..cbb5f5b9a9e7 100644
@@ -2181,20 +2181,6 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
 	if (!svm)
 		goto out;
 
-	svm->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
-						     GFP_KERNEL_ACCOUNT);
-	if (!svm->vcpu.arch.user_fpu) {
-		printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
-		goto free_partial_svm;
-	}
-
-	svm->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
-						     GFP_KERNEL_ACCOUNT);
-	if (!svm->vcpu.arch.guest_fpu) {
-		printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
-		goto free_user_fpu;
-	}
-
 	page = alloc_page(GFP_KERNEL_ACCOUNT);
 	if (!page)
 		goto free_svm;
@@ -2228,10 +2214,6 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
 free_page1:
 	__free_page(page);
 free_svm:
-	kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.guest_fpu);
-free_user_fpu:
-	kmem_cache_free(x86_fpu_cache, svm->vcpu.arch.user_fpu);
-free_partial_svm:
 	kmem_cache_free(kvm_vcpu_cache, svm);
 out:
 	return ERR_PTR(err);
arch/x86/kvm/vmx/vmx.c +0 -18
index 2f54a3bcb6a5..183112604f04 100644
@@ -6773,20 +6773,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	if (!vmx)
 		goto out;
 
-	vmx->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
-			GFP_KERNEL_ACCOUNT);
-	if (!vmx->vcpu.arch.user_fpu) {
-		printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
-		goto free_partial_vcpu;
-	}
-
-	vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
-			GFP_KERNEL_ACCOUNT);
-	if (!vmx->vcpu.arch.guest_fpu) {
-		printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
-		goto free_user_fpu;
-	}
-
 	vmx->vpid = allocate_vpid();
 
 	/*
@@ -6822,10 +6808,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	vmx_destroy_pml_buffer(vmx);
 free_vcpu:
 	free_vpid(vmx->vpid);
-	kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
-free_user_fpu:
-	kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
-free_partial_vcpu:
 	kmem_cache_free(kvm_vcpu_cache, vmx);
 out:
 	return ERR_PTR(err);
arch/x86/kvm/x86.c +24 -0
index fd9f1a1f0f01..c4b477a9c7f6 100644
@@ -9021,6 +9021,26 @@ void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
 	free_cpumask_var(wbinvd_dirty_mask);
 }
 
+static int kvm_vcpu_create_fpu(struct kvm_vcpu *vcpu)
+{
+	vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
+			GFP_KERNEL_ACCOUNT);
+	if (vcpu->arch.user_fpu) {
+		printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
+		return -ENOMEM;
+	}
+
+	vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
+			GFP_KERNEL_ACCOUNT);
+	if (vcpu->arch.guest_fpu) {
+		printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
+		kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
+		return -ENOMEM;
+	}
+
+	return 0;
+}
+
 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
 						unsigned int id)
 {
@@ -9036,6 +9056,10 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
 	if (IS_ERR(vcpu))
 		return vcpu;
 
+	err = kvm_vcpu_create_fpu(vcpu);
+	if (err)
+		return ERR_PTR(err);
+
 	err = kvm_vcpu_init(vcpu, kvm, id);
 	if (err) {
 		kvm_x86_ops->vcpu_free(vcpu);
Signature · 1 line
2.19.1