Skip to contentSkip to conversation

KVM: x86: Kill off pdptrs_changed()

PATCH In review Sean Christopherson 2 participants 6 messages Apr 23, 2021

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86: Kill off pdptrs_changed()

Remove pdptrs_changed(), which is mostly dead anyways, and the few bits that are still thrashing are useless.

This conflicts with Maxim's work to migrate PDPTRs out-of-band, but I think it will conflict in a good way as the "skip load_pdptrs()" logic for the out-of-band case won't have to juggle this legacy crud.

Sean Christopherson (4):
KVM: nVMX: Drop obsolete (and pointless) pdptrs_changed() check
KVM: nSVM: Drop pointless pdptrs_changed() check on nested transition
KVM: x86: Always load PDPTRs on CR3 load for SVM w/o NPT and a PAE
guest
KVM: x86: Unexport kvm_read_guest_page_mmu()

 arch/x86/include/asm/kvm_host.h |  4 ----
 arch/x86/kvm/svm/nested.c       |  6 ++---
 arch/x86/kvm/vmx/nested.c       |  8 +++----
 arch/x86/kvm/x86.c              | 41 ++++-----------------------------
 4 files changed, 10 insertions(+), 49 deletions(-)
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
author maintainer
#1 lore raw

PATCH 1/4 KVM: nVMX: Drop obsolete (and pointless) pdptrs_changed() check

Remove the pdptrs_changed() check when loading L2's CR3. The set of available registers is always reset when switching VMCSes (see commit e5d03de5937e, "KVM: nVMX: Reset register cache (available and dirty masks) on VMCS switch"), thus the "are PDPTRs available" check will always fail. And even if it didn't fail, reading guest memory to check the PDPTRs is just as expensive as reading guest memory to load 'em.

---
 arch/x86/kvm/vmx/nested.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/nested.c +3 -5
index 00339d624c92..eece7fff0441 100644
@@ -1118,11 +1118,9 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool ne
 	 * must not be dereferenced.
 	 */
 	if (!nested_ept && is_pae_paging(vcpu) &&
-	    (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
-		if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
-			*entry_failure_code = ENTRY_FAIL_PDPTE;
-			return -EINVAL;
-		}
+	    CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) {
+		*entry_failure_code = ENTRY_FAIL_PDPTE;
+		return -EINVAL;
 	}
 
 	/*
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
author maintainer
#2 lore raw

PATCH 2/4 KVM: nSVM: Drop pointless pdptrs_changed() check on nested transition

In reply to Sean Christopherson

Remove the "PDPTRs unchanged" check to skip PDPTR loading during nested SVM transitions as it's not at all an optimization. Reading guest memory to get the PDPTRs isn't magically cheaper by doing it in pdptrs_changed(), and if the PDPTRs did change, KVM will end up doing the read twice.

---
 arch/x86/kvm/svm/nested.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +2 -4
index 540d43ba2cf4..9cc95895866a 100644
@@ -391,10 +391,8 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,
 		return -EINVAL;
 
 	if (!nested_npt && is_pae_paging(vcpu) &&
-	    (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) {
-		if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
-			return -EINVAL;
-	}
+	    CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)))
+		return -EINVAL;
 
 	/*
 	 * TODO: optimize unconditional TLB flush/MMU sync here and in
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
author maintainer
#3 lore raw

PATCH 3/4 KVM: x86: Always load PDPTRs on CR3 load for SVM w/o NPT and a PAE guest

In reply to Sean Christopherson

Kill off pdptrs_changed() and instead go through the full kvm_set_cr3() for PAE guest, even if the new CR3 is the same as the current CR3. For VMX, and SVM with NPT enabled, the PDPTRs are unconditionally marked as unavailable after VM-Exit, i.e. the optimization is dead code except for SVM without NPT.

In the unlikely scenario that anyone cares about SVM without NPT _and_ a PAE guest, they've got bigger problems if their guest is loading the same CR3 so frequently that the performance of kvm_set_cr3() is notable, especially since KVM's fast PGD switching means reloading the same CR3 does not require a full rebuild. Given that PAE and PCID are mutually exclusive, i.e. a sync and flush are guaranteed in any case, the actual benefits of the pdptrs_changed() optimization are marginal at best.

---
 arch/x86/include/asm/kvm_host.h |  1 -
 arch/x86/kvm/x86.c              | 34 ++-------------------------------
 2 files changed, 2 insertions(+), 33 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
index 3e5fc80a35c8..30e95c52769c 100644
@@ -1475,7 +1475,6 @@ unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm);
 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages);
 
 int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
-bool pdptrs_changed(struct kvm_vcpu *vcpu);
 
 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
 			  const void *val, int bytes);
arch/x86/kvm/x86.c +2 -32
index 3bf52ba5f2bb..d099d6e54a6f 100644
@@ -751,13 +751,6 @@ int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 }
 EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
 
-static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
-			       void *data, int offset, int len, u32 access)
-{
-	return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
-				       data, offset, len, access);
-}
-
 static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
 {
 	return vcpu->arch.reserved_gpa_bits | rsvd_bits(5, 8) | rsvd_bits(1, 2);
@@ -799,30 +792,6 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
 }
 EXPORT_SYMBOL_GPL(load_pdptrs);
 
-bool pdptrs_changed(struct kvm_vcpu *vcpu)
-{
-	u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
-	int offset;
-	gfn_t gfn;
-	int r;
-
-	if (!is_pae_paging(vcpu))
-		return false;
-
-	if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
-		return true;
-
-	gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
-	offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
-	r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
-				       PFERR_USER_MASK | PFERR_WRITE_MASK);
-	if (r < 0)
-		return true;
-
-	return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
-}
-EXPORT_SYMBOL_GPL(pdptrs_changed);
-
 void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0)
 {
 	unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
@@ -1069,7 +1038,8 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
 	}
 #endif
 
-	if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
+	/* PDPTRs are always reloaded for PAE paging. */
+	if (cr3 == kvm_read_cr3(vcpu) && !is_pae_paging(vcpu)) {
 		if (!skip_tlb_flush) {
 			kvm_mmu_sync_roots(vcpu);
 			kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
author maintainer
#4 lore raw

PATCH 4/4 KVM: x86: Unexport kvm_read_guest_page_mmu()

In reply to Sean Christopherson

Unexport kvm_read_guest_page_mmu(), its only current user is for loading PDPTRs, and with luck, KVM will not have to support similar insanity in the future.

---
 arch/x86/include/asm/kvm_host.h | 3 ---
 arch/x86/kvm/x86.c              | 7 +++----
 2 files changed, 3 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -3
index 30e95c52769c..be271fdf584e 100644
@@ -1614,9 +1614,6 @@ void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
 bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
 				    struct x86_exception *fault);
-int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
-			    gfn_t gfn, void *data, int offset, int len,
-			    u32 access);
 bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
 bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr);
 
arch/x86/kvm/x86.c +3 -4
index d099d6e54a6f..06bc59c3abb9 100644
@@ -732,9 +732,9 @@ EXPORT_SYMBOL_GPL(kvm_require_dr);
  * running guest. The difference to kvm_vcpu_read_guest_page is that this function
  * can read from guest physical or from the guest's guest physical memory.
  */
-int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
-			    gfn_t ngfn, void *data, int offset, int len,
-			    u32 access)
+static int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
+				   gfn_t ngfn, void *data, int offset, int len,
+				   u32 access)
 {
 	struct x86_exception exception;
 	gfn_t real_gfn;
@@ -749,7 +749,6 @@ int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 
 	return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
 }
-EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
 
 static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
 {
Signature · 1 line
2.31.1.498.g6c1eba8ee3d-goog
maintainer
#5 lore raw
In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson

Remove pdptrs_changed(), which is mostly dead anyways, and the few bits that are still thrashing are useless.

This conflicts with Maxim's work to migrate PDPTRs out-of-band, but I think it will conflict in a good way as the "skip load_pdptrs()" logic for the out-of-band case won't have to juggle this legacy crud.

Maxim, can you integrate these patches in your series yourself?

Thanks,

Paolo

Quoted 13 lines

Sean Christopherson (4):
KVM: nVMX: Drop obsolete (and pointless) pdptrs_changed() check
KVM: nSVM: Drop pointless pdptrs_changed() check on nested transition
KVM: x86: Always load PDPTRs on CR3 load for SVM w/o NPT and a PAE
guest
KVM: x86: Unexport kvm_read_guest_page_mmu()

  arch/x86/include/asm/kvm_host.h |  4 ----
  arch/x86/kvm/svm/nested.c       |  6 ++---
  arch/x86/kvm/vmx/nested.c       |  8 +++----
  arch/x86/kvm/x86.c              | 41 ++++-----------------------------
  4 files changed, 10 insertions(+), 49 deletions(-)