Skip to contentSkip to conversation

KVM: x86/mmu: Clean up hugepage split error handling

PATCH Applied Sean Christopherson 2 participants 4 messages Jul 12, 2024 – Jul 16, 2024

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86/mmu: Clean up hugepage split error handling

"Fix" an impossible scenario where KVM would install a '0' SPTE instead of using SHADOW_NONPRESENT_VALUE. In quotes because (a) there's not truly anything to fix (the code should never be hit), and (b) bugging the VM doesn't guarantee KVM won't get confused (though it's still better than installing an empty SPTE).

The main motivation for this is to eliminate installing a '0' SPTE so that future audits of the MMU don't complain about not using SHADOW_NONPRESENT_VALUE.

Sean Christopherson (2):
KVM: x86/mmu: Bug the VM if KVM tries to split a !hugepage SPTE
KVM: x86/mmu: Clean up make_huge_page_split_spte() definition and
intro

 arch/x86/kvm/mmu/spte.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

base-commit: 771df9ffadb8204e61d3e98f36c5067102aab78f

Signature · 1 line
2.45.2.993.g49e7a77208-goog
author maintainer
#1 lore raw

PATCH 1/2 KVM: x86/mmu: Bug the VM if KVM tries to split a !hugepage SPTE

Bug the VM instead of simply warning if KVM tries to split a SPTE that is non-present or not-huge. KVM is guaranteed to end up in a broken state as the callers fully expect a valid SPTE, e.g. the shadow MMU will add an rmap entry, and all MMUs will account the expected small page. Returning '0' is also technically wrong now that SHADOW_NONPRESENT_VALUE exists, i.e. would cause KVM to create a potential #VE SPTE.

While it would be possible to have the callers gracefully handle failure, doing so would provide no practical value as the scenario really should be impossible, while the error handling would add a non-trivial amount of noise.

Fixes a3fe5dbda0a4 ("KVM: x86/mmu: Split huge pages mapped by the TDP MMU when dirty logging is enabled")
Cc David Matlack <[email protected]>
---
 arch/x86/kvm/mmu/spte.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/x86/kvm/mmu/spte.c +1 -5
index c8fe13217ff7..bc55e3b26045 100644
@@ -296,11 +296,7 @@ u64 make_huge_page_split_spte(struct kvm *kvm, u64 huge_spte, union kvm_mmu_page
 {
 	u64 child_spte;
 
-	if (WARN_ON_ONCE(!is_shadow_present_pte(huge_spte)))
-		return 0;
-
-	if (WARN_ON_ONCE(!is_large_pte(huge_spte)))
-		return 0;
+	KVM_BUG_ON(!is_shadow_present_pte(huge_spte) || !is_large_pte(huge_spte), kvm);
 
 	child_spte = huge_spte;
 
Signature · 1 line
2.45.2.993.g49e7a77208-goog
author maintainer
#2 lore raw

PATCH 2/2 KVM: x86/mmu: Clean up make_huge_page_split_spte() definition and intro

In reply to Sean Christopherson

Tweak the definition of make_huge_page_split_spte() to eliminate an unnecessarily long line, and opportunistically initialize child_spte to make it more obvious that the child is directly derived from the huge parent.

No functional change intended.

---
 arch/x86/kvm/mmu/spte.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/spte.c +3 -5
index bc55e3b26045..10390eecd85d 100644
@@ -291,15 +291,13 @@ static u64 make_spte_executable(u64 spte)
  * This is used during huge page splitting to build the SPTEs that make up the
  * new page table.
  */
-u64 make_huge_page_split_spte(struct kvm *kvm, u64 huge_spte, union kvm_mmu_page_role role,
-			      int index)
+u64 make_huge_page_split_spte(struct kvm *kvm, u64 huge_spte,
+			      union kvm_mmu_page_role role, int index)
 {
-	u64 child_spte;
+	u64 child_spte = huge_spte;
 
 	KVM_BUG_ON(!is_shadow_present_pte(huge_spte) || !is_large_pte(huge_spte), kvm);
 
-	child_spte = huge_spte;
-
 	/*
 	 * The child_spte already has the base address of the huge page being
 	 * split. So we just have to OR in the offset to the page at the next
Signature · 1 line
2.45.2.993.g49e7a77208-goog