Skip to contentSkip to conversation

KVM: SVM: Create separate vmcbs for L1 and L2

PATCH In review Cathy Avery 2 participants 5 messages Oct 26, 2020 – Nov 12, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover KVM: SVM: Create separate vmcbs for L1 and L2

svm->vmcb will now point to either a separate vmcb L1 ( not nested ) or L2 vmcb ( nested ).

Changes:
v2 -> v3
- Added vmcb switching helper.
- svm_set_nested_state always forces to L1 before determining state
to set. This is more like vmx and covers any potential L2 to L2 nested state switch.
- Moved svm->asid tracking to pre_svm_run and added ASID set dirty bit checking.

v1 -> v2
- Removed unnecessary update check of L1 save.cr3 during nested_svm_vmexit.
- Moved vmcb01_pa to svm.
- Removed get_host_vmcb() function.
- Updated vmsave/vmload corresponding vmcb state during L2 enter and exit which fixed the L2 load issue.
- Moved asid workaround to a new patch which adds asid to svm.
- Init previously uninitialized L2 vmcb save.gpat and save.cr4

Tested:
kvm-unit-tests
kvm self tests
Loaded fedora nested guest on fedora

Cathy Avery (2):
KVM: SVM: Track asid from vcpu_svm
KVM: SVM: Use a separate vmcb for the nested L2 guest

 arch/x86/kvm/svm/nested.c | 125 ++++++++++++++++++--------------------
 arch/x86/kvm/svm/svm.c    |  58 +++++++++++-------
 arch/x86/kvm/svm/svm.h    |  51 +++++-----------
 3 files changed, 110 insertions(+), 124 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/2 KVM: SVM: Track asid from vcpu_svm

Track asid from svm->asid to allow for vmcb assignment without regard to which level guest is running.

---
 arch/x86/kvm/svm/svm.c | 16 ++++++++++++++--
 arch/x86/kvm/svm/svm.h |  2 ++
 2 files changed, 16 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +14 -2
index d4e18bda19c7..83b4f56883f8 100644
@@ -1101,6 +1101,7 @@ static void init_vmcb(struct vcpu_svm *svm)
 		save->cr4 = 0;
 	}
 	svm->asid_generation = 0;
+	svm->asid = 0;
 
 	svm->nested.vmcb = 0;
 	svm->vcpu.arch.hflags = 0;
@@ -1659,11 +1660,11 @@ static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
 	if (sd->next_asid > sd->max_asid) {
 		++sd->asid_generation;
 		sd->next_asid = sd->min_asid;
-		svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
+		sd->flush_all_asid = true;
 	}
 
 	svm->asid_generation = sd->asid_generation;
-	svm->vmcb->control.asid = sd->next_asid++;
+	svm->asid = sd->next_asid++;
 
 	vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
 }
@@ -3030,6 +3031,17 @@ static void pre_svm_run(struct vcpu_svm *svm)
 	/* FIXME: handle wraparound of asid_generation */
 	if (svm->asid_generation != sd->asid_generation)
 		new_asid(svm, sd);
+
+	if (sd->flush_all_asid) {
+		svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
+		sd->flush_all_asid = false;
+		vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
+	}
+
+	if (unlikely(svm->asid != svm->vmcb->control.asid))
+		vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
+
+	svm->vmcb->control.asid = svm->asid;
 }
 
 static void svm_inject_nmi(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +2 -0
index a798e1731709..22832362bced 100644
@@ -104,6 +104,7 @@ struct vcpu_svm {
 	struct vmcb *vmcb;
 	unsigned long vmcb_pa;
 	struct svm_cpu_data *svm_data;
+	u32 asid;
 	uint64_t asid_generation;
 	uint64_t sysenter_esp;
 	uint64_t sysenter_eip;
@@ -164,6 +165,7 @@ struct svm_cpu_data {
 	int cpu;
 
 	u64 asid_generation;
+	bool flush_all_asid;
 	u32 max_asid;
 	u32 next_asid;
 	u32 min_asid;
Signature · 1 line
2.20.1
#2 lore raw

PATCH v3 2/2 KVM: SVM: Use a separate vmcb for the nested L2 guest

In reply to Cathy Avery

svm->vmcb will now point to either a separate vmcb L1 ( not nested ) or L2 vmcb ( nested ).

Issues:

1) There is some wholesale copying of vmcb.save and vmcb.contol areas which will need to be refined.

Tested:
kvm-unit-tests
kvm self tests
Loaded fedora nested guest on fedora

---
 arch/x86/kvm/svm/nested.c | 125 ++++++++++++++++++--------------------
 arch/x86/kvm/svm/svm.c    |  42 +++++++------
 arch/x86/kvm/svm/svm.h    |  49 ++++-----------
 3 files changed, 94 insertions(+), 122 deletions(-)
arch/x86/kvm/svm/nested.c +58 -67
index e90bc436f584..3acab2bf99a5 100644
@@ -75,12 +75,12 @@ static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
 static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-	struct vmcb *hsave = svm->nested.hsave;
 
 	WARN_ON(mmu_is_nested(vcpu));
 
 	vcpu->arch.mmu = &vcpu->arch.guest_mmu;
-	kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, hsave->save.cr4, hsave->save.efer,
+	kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01->save.cr4,
+				svm->vmcb01->save.efer,
 				svm->nested.ctl.nested_cr3);
 	vcpu->arch.mmu->get_guest_pgd     = nested_svm_get_tdp_cr3;
 	vcpu->arch.mmu->get_pdptr         = nested_svm_get_tdp_pdptr;
@@ -105,7 +105,7 @@ void recalc_intercepts(struct vcpu_svm *svm)
 		return;
 
 	c = &svm->vmcb->control;
-	h = &svm->nested.hsave->control;
+	h = &svm->vmcb01->control;
 	g = &svm->nested.ctl;
 
 	svm->nested.host_intercept_exceptions = h->intercept_exceptions;
@@ -403,7 +403,7 @@ static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
 
 	svm->vmcb->control.int_ctl             =
 		(svm->nested.ctl.int_ctl & ~mask) |
-		(svm->nested.hsave->control.int_ctl & mask);
+		(svm->vmcb01->control.int_ctl & mask);
 
 	svm->vmcb->control.virt_ext            = svm->nested.ctl.virt_ext;
 	svm->vmcb->control.int_vector          = svm->nested.ctl.int_vector;
@@ -426,12 +426,29 @@ static void nested_prepare_vmcb_control(struct vcpu_svm *svm)
 	vmcb_mark_all_dirty(svm->vmcb);
 }
 
+static void svm_switch_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb, unsigned long vmcb_pa)
+{
+	if (svm->vmcb == vmcb)
+		return;
+
+	svm->vmcb = vmcb;
+	svm->vmcb_pa = vmcb_pa;
+}
+
 int enter_svm_guest_mode(struct vcpu_svm *svm, u64 vmcb_gpa,
 			  struct vmcb *nested_vmcb)
 {
 	int ret;
 
 	svm->nested.vmcb = vmcb_gpa;
+
+	WARN_ON(svm->vmcb == svm->nested.vmcb02);
+
+	svm->nested.vmcb02->control = svm->vmcb01->control;
+	svm->nested.vmcb02->save.cr4 = svm->vmcb01->save.cr4;
+
+	nested_svm_vmloadsave(svm->vmcb01, svm->nested.vmcb02);
+	svm_switch_vmcb(svm, svm->nested.vmcb02, svm->nested.vmcb02_pa);
 	load_nested_vmcb_control(svm, &nested_vmcb->control);
 	nested_prepare_vmcb_save(svm, nested_vmcb);
 	nested_prepare_vmcb_control(svm);
@@ -450,8 +467,6 @@ int nested_svm_vmrun(struct vcpu_svm *svm)
 {
 	int ret;
 	struct vmcb *nested_vmcb;
-	struct vmcb *hsave = svm->nested.hsave;
-	struct vmcb *vmcb = svm->vmcb;
 	struct kvm_host_map map;
 	u64 vmcb_gpa;
 
@@ -496,29 +511,14 @@ int nested_svm_vmrun(struct vcpu_svm *svm)
 	kvm_clear_exception_queue(&svm->vcpu);
 	kvm_clear_interrupt_queue(&svm->vcpu);
 
-	/*
-	 * Save the old vmcb, so we don't need to pick what we save, but can
-	 * restore everything when a VMEXIT occurs
-	 */
-	hsave->save.es     = vmcb->save.es;
-	hsave->save.cs     = vmcb->save.cs;
-	hsave->save.ss     = vmcb->save.ss;
-	hsave->save.ds     = vmcb->save.ds;
-	hsave->save.gdtr   = vmcb->save.gdtr;
-	hsave->save.idtr   = vmcb->save.idtr;
-	hsave->save.efer   = svm->vcpu.arch.efer;
-	hsave->save.cr0    = kvm_read_cr0(&svm->vcpu);
-	hsave->save.cr4    = svm->vcpu.arch.cr4;
-	hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
-	hsave->save.rip    = kvm_rip_read(&svm->vcpu);
-	hsave->save.rsp    = vmcb->save.rsp;
-	hsave->save.rax    = vmcb->save.rax;
-	if (npt_enabled)
-		hsave->save.cr3    = vmcb->save.cr3;
-	else
-		hsave->save.cr3    = kvm_read_cr3(&svm->vcpu);
-
-	copy_vmcb_control_area(&hsave->control, &vmcb->control);
+	svm->vmcb01->save.efer   = svm->vcpu.arch.efer;
+	svm->vmcb01->save.cr0    = kvm_read_cr0(&svm->vcpu);
+	svm->vmcb01->save.cr4    = svm->vcpu.arch.cr4;
+	svm->vmcb01->save.rflags = kvm_get_rflags(&svm->vcpu);
+	svm->vmcb01->save.rip    = kvm_rip_read(&svm->vcpu);
+
+	if (!npt_enabled)
+		svm->vmcb01->save.cr3 = kvm_read_cr3(&svm->vcpu);
 
 	svm->nested.nested_run_pending = 1;
 
@@ -564,7 +564,6 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 {
 	int rc;
 	struct vmcb *nested_vmcb;
-	struct vmcb *hsave = svm->nested.hsave;
 	struct vmcb *vmcb = svm->vmcb;
 	struct kvm_host_map map;
 
@@ -628,8 +627,8 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 	nested_vmcb->control.pause_filter_thresh =
 		svm->vmcb->control.pause_filter_thresh;
 
-	/* Restore the original control entries */
-	copy_vmcb_control_area(&vmcb->control, &hsave->control);
+	nested_svm_vmloadsave(svm->nested.vmcb02, svm->vmcb01);
+	svm_switch_vmcb(svm, svm->vmcb01, svm->vmcb01_pa);
 
 	/* On vmexit the  GIF is set to false */
 	svm_set_gif(svm, false);
@@ -640,19 +639,13 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 	svm->nested.ctl.nested_cr3 = 0;
 
 	/* Restore selected save entries */
-	svm->vmcb->save.es = hsave->save.es;
-	svm->vmcb->save.cs = hsave->save.cs;
-	svm->vmcb->save.ss = hsave->save.ss;
-	svm->vmcb->save.ds = hsave->save.ds;
-	svm->vmcb->save.gdtr = hsave->save.gdtr;
-	svm->vmcb->save.idtr = hsave->save.idtr;
-	kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
-	svm_set_efer(&svm->vcpu, hsave->save.efer);
-	svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
-	svm_set_cr4(&svm->vcpu, hsave->save.cr4);
-	kvm_rax_write(&svm->vcpu, hsave->save.rax);
-	kvm_rsp_write(&svm->vcpu, hsave->save.rsp);
-	kvm_rip_write(&svm->vcpu, hsave->save.rip);
+	kvm_set_rflags(&svm->vcpu, svm->vmcb->save.rflags);
+	svm_set_efer(&svm->vcpu, svm->vmcb->save.efer);
+	svm_set_cr0(&svm->vcpu, svm->vmcb->save.cr0 | X86_CR0_PE);
+	svm_set_cr4(&svm->vcpu, svm->vmcb->save.cr4);
+	kvm_rax_write(&svm->vcpu, svm->vmcb->save.rax);
+	kvm_rsp_write(&svm->vcpu, svm->vmcb->save.rsp);
+	kvm_rip_write(&svm->vcpu, svm->vmcb->save.rip);
 	svm->vmcb->save.dr7 = 0;
 	svm->vmcb->save.cpl = 0;
 	svm->vmcb->control.exit_int_info = 0;
@@ -670,13 +663,10 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 
 	nested_svm_uninit_mmu_context(&svm->vcpu);
 
-	rc = nested_svm_load_cr3(&svm->vcpu, hsave->save.cr3, false);
+	rc = nested_svm_load_cr3(&svm->vcpu, svm->vmcb->save.cr3, false);
 	if (rc)
 		return 1;
 
-	if (npt_enabled)
-		svm->vmcb->save.cr3 = hsave->save.cr3;
-
 	/*
 	 * Drop what we picked up for L2 via svm_complete_interrupts() so it
 	 * doesn't end up in L1.
@@ -694,12 +684,9 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 void svm_leave_nested(struct vcpu_svm *svm)
 {
 	if (is_guest_mode(&svm->vcpu)) {
-		struct vmcb *hsave = svm->nested.hsave;
-		struct vmcb *vmcb = svm->vmcb;
-
 		svm->nested.nested_run_pending = 0;
 		leave_guest_mode(&svm->vcpu);
-		copy_vmcb_control_area(&vmcb->control, &hsave->control);
+		svm_switch_vmcb(svm, svm->vmcb01, svm->vmcb01_pa);
 		nested_svm_uninit_mmu_context(&svm->vcpu);
 	}
 }
@@ -982,7 +969,7 @@ int nested_svm_exit_special(struct vcpu_svm *svm)
 	case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
 		u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
 
-		if (get_host_vmcb(svm)->control.intercept_exceptions & excp_bits)
+		if (svm->vmcb01->control.intercept_exceptions & excp_bits)
 			return NESTED_EXIT_HOST;
 		else if (exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR &&
 			 svm->vcpu.arch.apf.host_apf_flags)
@@ -1046,10 +1033,9 @@ static int svm_get_nested_state(struct kvm_vcpu *vcpu,
 	if (copy_to_user(&user_vmcb->control, &svm->nested.ctl,
 			 sizeof(user_vmcb->control)))
 		return -EFAULT;
-	if (copy_to_user(&user_vmcb->save, &svm->nested.hsave->save,
+	if (copy_to_user(&user_vmcb->save, &svm->vmcb01->save,
 			 sizeof(user_vmcb->save)))
 		return -EFAULT;
-
 out:
 	return kvm_state.size;
 }
@@ -1059,7 +1045,6 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 				struct kvm_nested_state *kvm_state)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-	struct vmcb *hsave = svm->nested.hsave;
 	struct vmcb __user *user_vmcb = (struct vmcb __user *)
 		&user_kvm_nested_state->data.svm[0];
 	struct vmcb_control_area ctl;
@@ -1088,10 +1073,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 	if (is_smm(vcpu) && (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
 		return -EINVAL;
 
-	if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) {
-		svm_leave_nested(svm);
+	/* If in L2 force to L1 */
+
+	svm_leave_nested(svm);
+
+	if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
 		goto out_set_gif;
-	}
 
 	if (!page_address_valid(vcpu, kvm_state->hdr.svm.vmcb_pa))
 		return -EINVAL;
@@ -1121,16 +1108,20 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 	if (!(save.cr0 & X86_CR0_PG))
 		return -EINVAL;
 
+	svm->nested.vmcb02->control = svm->vmcb01->control;
+	svm->nested.vmcb02->save = svm->vmcb01->save;
+	svm->vmcb01->save = save;
+
+	svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa;
+	svm_switch_vmcb(svm, svm->nested.vmcb02, svm->nested.vmcb02_pa);
+
 	/*
-	 * All checks done, we can enter guest mode.  L1 control fields
-	 * come from the nested save state.  Guest state is already
-	 * in the registers, the save area of the nested state instead
-	 * contains saved L1 state.
+	 * All checks done, we can enter guest mode. L2 control fields will
+	 * be the result of a combination of L1 and userspace indicated
+	 * L12.control. The save area of L1 vmcb now contains the userspace
+	 * indicated L1.save.
 	 */
-	copy_vmcb_control_area(&hsave->control, &svm->vmcb->control);
-	hsave->save = save;
 
-	svm->nested.vmcb = kvm_state->hdr.svm.vmcb_pa;
 	load_nested_vmcb_control(svm, &ctl);
 	nested_prepare_vmcb_control(svm);
 
arch/x86/kvm/svm/svm.c +23 -19

117 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +13 -36

115 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw
In reply to Cathy Avery

Hi Cathy,
I was going to test these patches. But it did not apply on my tree. Tried on kvm(https://git.kernel.org/pub/scm/virt/kvm/kvm.git) and Mainline
(https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git). What is your base tree?
thanks
Babu

Quoted 45 lines

-----Original Message-----
From: Cathy Avery <[email protected]>
Sent: Monday, October 26, 2020 12:42 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Huang2, Wei <[email protected]>; [email protected]; [email protected]
Subject: [PATCH v3 0/2] KVM: SVM: Create separate vmcbs for L1 and L2

svm->vmcb will now point to either a separate vmcb L1 ( not nested ) or L2 vmcb ( nested ).

Changes:
v2 -> v3
- Added vmcb switching helper.
- svm_set_nested_state always forces to L1 before determining state to set. This is more like vmx and covers any potential L2 to L2 nested state
switch.
- Moved svm->asid tracking to pre_svm_run and added ASID set dirty bit checking.

v1 -> v2
- Removed unnecessary update check of L1 save.cr3 during nested_svm_vmexit.
- Moved vmcb01_pa to svm.
- Removed get_host_vmcb() function.
- Updated vmsave/vmload corresponding vmcb state during L2 enter and exit which fixed the L2 load issue.
- Moved asid workaround to a new patch which adds asid to svm.
- Init previously uninitialized L2 vmcb save.gpat and save.cr4

Tested:
kvm-unit-tests
kvm self tests
Loaded fedora nested guest on fedora

Cathy Avery (2):
KVM: SVM: Track asid from vcpu_svm
KVM: SVM: Use a separate vmcb for the nested L2 guest

 arch/x86/kvm/svm/nested.c | 125 ++++++++++++++++++--------------------
 arch/x86/kvm/svm/svm.c    |  58 +++++++++++-------
 arch/x86/kvm/svm/svm.h    |  51 +++++-----------
 3 files changed, 110 insertions(+), 124 deletions(-)

--
2.20.1

#4 lore raw

That would be the master branch of git://git.kernel.org/pub/scm/virt/kvm/kvm.git where the last commit was 969df928fee43b4219646a57c7beaccccf2c0635

I was originally working off of the queue branch but there were issues with the prior commits passing the various tests.

Cathy

Quoted 54 lines · Babu Moger

Hi Cathy,
I was going to test these patches. But it did not apply on my tree. Tried on kvm(https://git.kernel.org/pub/scm/virt/kvm/kvm.git) and Mainline
(https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git). What is your base tree?
thanks
Babu

Quoted 45 lines

-----Original Message-----
From: Cathy Avery <[email protected]>
Sent: Monday, October 26, 2020 12:42 PM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Huang2, Wei <[email protected]>; [email protected]; [email protected]
Subject: [PATCH v3 0/2] KVM: SVM: Create separate vmcbs for L1 and L2

svm->vmcb will now point to either a separate vmcb L1 ( not nested ) or L2 vmcb ( nested ).

Changes:
v2 -> v3
- Added vmcb switching helper.
- svm_set_nested_state always forces to L1 before determining state to set. This is more like vmx and covers any potential L2 to L2 nested state
switch.
- Moved svm->asid tracking to pre_svm_run and added ASID set dirty bit checking.

v1 -> v2
- Removed unnecessary update check of L1 save.cr3 during nested_svm_vmexit.
- Moved vmcb01_pa to svm.
- Removed get_host_vmcb() function.
- Updated vmsave/vmload corresponding vmcb state during L2 enter and exit which fixed the L2 load issue.
- Moved asid workaround to a new patch which adds asid to svm.
- Init previously uninitialized L2 vmcb save.gpat and save.cr4

Tested:
kvm-unit-tests
kvm self tests
Loaded fedora nested guest on fedora

Cathy Avery (2):
KVM: SVM: Track asid from vcpu_svm
KVM: SVM: Use a separate vmcb for the nested L2 guest

  arch/x86/kvm/svm/nested.c | 125 ++++++++++++++++++--------------------
  arch/x86/kvm/svm/svm.c    |  58 +++++++++++-------
  arch/x86/kvm/svm/svm.h    |  51 +++++-----------
  3 files changed, 110 insertions(+), 124 deletions(-)

--
2.20.1