Skip to contentSkip to conversation

KVM: nSVM: Additions to optimizing L12 to L2 vmcb.save copies

PATCH In review Cathy Avery 2 participants 2 messages Mar 17, 2021 – Mar 23, 2021

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

PATCH 1/1 KVM: nSVM: Additions to optimizing L12 to L2 vmcb.save copies

Extend using the vmcb12 control clean field to determine which vmcb12.save registers were marked dirty in order to minimize register copies by including the CR bit.

This patch also fixes the init of last_vmcb12_gpa by using an invalid physical address instead of 0.

Tested:
kvm-unit-tests
kvm selftests
Fedora L1 L2

---
 arch/x86/kvm/svm/nested.c | 9 ++++++---
 arch/x86/kvm/svm/svm.c    | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +6 -3
index 8523f60adb92..6f9a40e002bc 100644
@@ -449,9 +449,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
 	}
 
 	kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
-	svm_set_efer(&svm->vcpu, vmcb12->save.efer);
-	svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
-	svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
+
+	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_CR))) {
+		svm_set_efer(&svm->vcpu, vmcb12->save.efer);
+		svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
+		svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
+	}
 
 	svm->vcpu.arch.cr2 = vmcb12->save.cr2;
 
arch/x86/kvm/svm/svm.c +1 -1
index 271196400495..41f5cd1009ca 100644
@@ -1234,7 +1234,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 	svm->asid = 0;
 
 	svm->nested.vmcb12_gpa = 0;
-	svm->nested.last_vmcb12_gpa = 0;
+	svm->nested.last_vmcb12_gpa = -1;
 	vcpu->arch.hflags = 0;
 
 	if (!kvm_pause_in_guest(vcpu->kvm)) {
Signature · 1 line
2.26.2
#1 lore raw
Quoted 24 lines · Cathy Avery

Extend using the vmcb12 control clean field to determine which vmcb12.save registers were marked dirty in order to minimize register copies by including the CR bit.

This patch also fixes the init of last_vmcb12_gpa by using an invalid physical address instead of 0.

Tested:
kvm-unit-tests
kvm selftests
Fedora L1 L2

---
 arch/x86/kvm/svm/nested.c | 9 ++++++---
 arch/x86/kvm/svm/svm.c    | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +0 -0
index 8523f60adb92..6f9a40e002bc 100644
@@ -449,9 +449,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
 	}

arch/x86/kvm/svm/nested.c static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12 · line 456

Quoted 10 lines
arch/x86/kvm/svm/nested.c +5 -3
 
 	kvm_set_rflags(&svm->vcpu, vmcb12->save.rflags | X86_EFLAGS_FIXED);
-	svm_set_efer(&svm->vcpu, vmcb12->save.efer);
-	svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
-	svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);
+
+	if (unlikely(new_vmcb12 || vmcb_is_dirty(vmcb12, VMCB_CR))) {
+		svm_set_efer(&svm->vcpu, vmcb12->save.efer);
+		svm_set_cr0(&svm->vcpu, vmcb12->save.cr0);
+		svm_set_cr4(&svm->vcpu, vmcb12->save.cr4);

This doesn't seem correct. Regardless of when vmcb12 was last touched, KVM still needs to set L2's state in vcpu->arch and set the correct MMU context.

Quoted 9 lines
arch/x86/kvm/svm/nested.c +1 -0
+	}
 
 	svm->vcpu.arch.cr2 = vmcb12->save.cr2;
 
arch/x86/kvm/svm/svm.c +0 -0
index 271196400495..41f5cd1009ca 100644

arch/x86/kvm/svm/svm.c static void init_vmcb(struct kvm_vcpu *vcpu) · line 1237

Quoted 5 lines
arch/x86/kvm/svm/svm.c +1 -1
@@ -1234,7 +1234,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 	svm->asid = 0;
 
 	svm->nested.vmcb12_gpa = 0;
-	svm->nested.last_vmcb12_gpa = 0;
+	svm->nested.last_vmcb12_gpa = -1;

INVALID_PAGE would be even better.

Quoted 6 lines
arch/x86/kvm/svm/svm.c +0 -0
 	vcpu->arch.hflags = 0;
 
 	if (!kvm_pause_in_guest(vcpu->kvm)) {
Signature · 1 line
2.26.2