[PATCH 6.12.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE
From: Paolo Bonzini
Date: Fri Dec 04 2026 - 13:03:13 EST
From: Sean Christopherson <seanjc@xxxxxxxxxx>
[ Upstream commit d31322f7c0003c4b3cba9d44c85fd73aa877c9f9 ]
Queue a TLB flush when eagerly splitting a huge SPTE and adding a child
SPTE to the rmap fails due to hitting the recycle threshold. If adding to
the rmap fails, __rmap_add() zaps the entire list of rmaps, including the
recently-added child SPTE. Eliding the flush in that case is unsafe as the
huge SPTE is still PRESENT, after the TLB flush performed by __rmap_add(),
i.e. can be (re)cached by running vCPUs.
Push the TLB flush out from __rmap_add() to its two callers by returning a
bool to indicate that a TLB flush is needed. Return "TLB flush needed"
instead of success/failuire so that a feature cleanup can simply return the
result of kvm_zap_all_rmap_sptes() (which *should* always be true; if there
isn't at least one rmap entry, then things have really gone sideways).
Fixes: 0378739401cf ("KVM: x86/mmu: Avoid unnecessary flush on eager page split")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Reported-by: Stefan Teodorescu <fane@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261113155010.442816-2-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kvm/mmu/mmu.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index c4f33dc6dabc..0c2bbb9bd301 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1573,7 +1573,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
#define RMAP_RECYCLE_THRESHOLD 1000
-static void __rmap_add(struct kvm *kvm,
+static bool __rmap_add(struct kvm *kvm,
struct kvm_mmu_memory_cache *cache,
const struct kvm_memory_slot *slot,
u64 *spte, gfn_t gfn, unsigned int access)
@@ -1593,8 +1593,9 @@ static void __rmap_add(struct kvm *kvm,
kvm->stat.max_mmu_rmap_size = rmap_count;
if (rmap_count > RMAP_RECYCLE_THRESHOLD) {
kvm_zap_all_rmap_sptes(kvm, rmap_head);
- kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
+ return true;
}
+ return false;
}
static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
@@ -1602,7 +1603,9 @@ static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
{
struct kvm_mmu_memory_cache *cache = &vcpu->arch.mmu_pte_list_desc_cache;
- __rmap_add(vcpu->kvm, cache, slot, spte, gfn, access);
+ if (__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access))
+ kvm_flush_remote_tlbs_gfn(vcpu->kvm, gfn,
+ sptep_to_sp(spte)->role.level);
}
static bool kvm_rmap_age_gfn_range(struct kvm *kvm,
@@ -6828,7 +6831,7 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm,
*
* However, if a given SPTE points to a lower level page table,
* that lower level page table may only be partially populated.
- * Installing such SPTEs would effectively unmap a potion of the
+ * Installing such SPTEs would effectively unmap a portion of the
* huge page. Unmapping guest memory always requires a TLB flush
* since a subsequent operation on the unmapped regions would
* fail to detect the need to flush.
@@ -6840,7 +6843,15 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm,
spte = make_huge_page_split_spte(kvm, huge_spte, sp->role, index);
mmu_spte_set(sptep, spte);
- __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
+
+ /*
+ * Queue a TLB flush if adding the SPTE to the rmap zapped the
+ * child SPTE that was just added. In that case, replacing the
+ * huge SPTE with a shadow page will effectively unmap guest
+ * memory (see above), as the huge SPTE is still PRESENT and
+ * can be (re)cached in the TLB before it is replaced.
+ */
+ flush |= __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
}
__link_shadow_page(kvm, cache, huge_sptep, sp, flush);
--
2.56.1