[PATCH 3/4] KVM: x86/mmu: Make sure pfn is not changed for spurious fault
From: Yan Zhao
Date: Thu Feb 06 2025 - 22:11:01 EST
Make sure pfn is not changed for a spurious fault by warning in the TDP
MMU. For shadow path, only treat a prefetch fault as spurious when pfn is
not changed, since the rmap removal and add are required when pfn is
changed.
Cc: Sean Christopherson <seanjc@xxxxxxxxxx>
Signed-off-by: Yan Zhao <yan.y.zhao@xxxxxxxxx>
---
arch/x86/kvm/mmu/mmu.c | 3 ++-
arch/x86/kvm/mmu/tdp_mmu.c | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 3d74e680006f..47fd3712afe6 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -2846,7 +2846,8 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot,
}
if (is_shadow_present_pte(*sptep)) {
- if (prefetch && is_last_spte(*sptep, level))
+ if (prefetch && is_last_spte(*sptep, level) &&
+ pfn == spte_to_pfn(*sptep))
return RET_PF_SPURIOUS;
/*
diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 5f9e7374220e..8b37e4f542f3 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -1139,7 +1139,8 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
if (is_shadow_present_pte(iter->old_spte) &&
is_access_allowed(fault, iter->old_spte) &&
- is_last_spte(iter->old_spte, iter->level))
+ is_last_spte(iter->old_spte, iter->level) &&
+ !WARN_ON_ONCE(fault->pfn != spte_to_pfn(iter->old_spte)))
return RET_PF_SPURIOUS;
if (unlikely(!fault->slot))
--
2.43.2