[PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations

From: Krzysztof Woś

Date: Thu Dec 03 2026 - 03:45:32 EST


Skip promoting a non-leaf TDP MMU SPTE in recover_huge_pages_range() if
an in-progress mmu_notifier invalidation overlaps the candidate hugepage
GFN range [iter.gfn, iter.gfn + KVM_PAGES_PER_HPAGE(iter.level)).

host_pfn_mapping_level() requires callers installing new mappings under
mmu_lock to ensure no mmu_notifier invalidation is in progress for the
target HVA range (either via mmu_invalidate_retry_gfn() or by already
holding a valid mapping covering the entire HVA range). Unlike
kvm_mmu_zap_collapsible_spte(), which only zaps 4 KiB SPTEs,
recover_huge_pages_range() promotes a non-leaf SPTE to a present 2 MiB
or 1 GiB leaf SPTE in place via tdp_mmu_make_huge_spte() and
tdp_mmu_set_spte_atomic().

Because tdp_mmu_make_huge_spte() succeeds as soon as any single child
leaf SPTE in the range is shadow-present, having a shadow-present
non-leaf parent SPTE does not guarantee the absence of an active
sub-range invalidation on another GFN within that hugepage. If the
primary MMU runs kvm_mmu_notifier_invalidate_range_start() on a 4 KiB
sub-range of a 2 MiB THP (zapping that 4 KiB SPTE and releasing mmu_lock
while mmu_invalidate_in_progress remains non-zero and before the host
PMD is split or cleared) concurrently with disabling dirty logging,
recover_huge_pages_range() can still observe PG_LEVEL_2M from
host_pfn_mapping_level(), synthesize a 2 MiB leaf SPTE from a surviving
sibling 4 KiB SPTE, and install a stale present mapping covering the
invalidated sub-range that persists past invalidate_range_end().

Checking the full hugepage GFN span against mmu_invalidate_in_progress
and mmu_invalidate_range_start..end while holding mmu_lock for read
prevents installing huge SPTEs over actively invalidated sub-ranges, as
starting or expanding an invalidation range requires holding mmu_lock
for write. When tdp_mmu_iter_cond_resched() drops and re-acquires
mmu_lock (or tdp_mmu_make_huge_spte() / tdp_mmu_set_spte_atomic()
retries), control loops back to retry: and re-evaluates the overlap
check under the newly acquired lock.

Found via TLA+ concurrency model checking and LLM-assisted static review
of TDP MMU invalidation paths on commit d06ce6f1eb5e, and manually
verified against the mmu_notifier locking protocol. Build-tested on
x86_64 (GCC 15.2.0) with KVM_INTEL=m, KVM_AMD=m, KVM_WERROR=y,
KVM_SMM=y, KVM_XEN=y, and PROVE_LOCKING=y; no runtime reproducer was
executed for the concurrent dirty-log teardown and sub-range THP
invalidation race.

Fixes: 13e2e4f62a4b ("KVM: x86/mmu: Recover TDP MMU huge page mappings in-place instead of zapping")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Krzysztof Woś <krzysztof.wos@xxxxxxxxx>
---
Base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106

arch/x86/kvm/mmu/tdp_mmu.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 44dad106f..ce496e3d3 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -1750,6 +1750,22 @@ static int tdp_mmu_make_huge_spte(struct kvm *kvm,
return -ENOENT;
}

+static bool tdp_mmu_invalidate_in_progress(struct kvm *kvm,
+ gfn_t start, gfn_t end)
+{
+ lockdep_assert_held(&kvm->mmu_lock);
+
+ if (likely(!kvm->mmu_invalidate_in_progress))
+ return false;
+
+ if (WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA ||
+ kvm->mmu_invalidate_range_end == INVALID_GPA))
+ return true;
+
+ return start < kvm->mmu_invalidate_range_end &&
+ end > kvm->mmu_invalidate_range_start;
+}
+
static void recover_huge_pages_range(struct kvm *kvm,
struct kvm_mmu_page *root,
const struct kvm_memory_slot *slot)
@@ -1796,6 +1812,16 @@ static void recover_huge_pages_range(struct kvm *kvm,
if (iter.gfn < start || iter.gfn >= end)
continue;

+ /*
+ * Do not promote to a huge SPTE if an in-progress mmu_notifier
+ * invalidation overlaps the hugepage range, as only a subset of
+ * child SPTEs may have been zapped while the host mapping is
+ * still huge, violating host_pfn_mapping_level()'s contract.
+ */
+ if (tdp_mmu_invalidate_in_progress(kvm, iter.gfn,
+ iter.gfn + KVM_PAGES_PER_HPAGE(iter.level)))
+ continue;
+
max_mapping_level = kvm_mmu_max_mapping_level(kvm, NULL, slot, iter.gfn);
if (max_mapping_level < iter.level)
continue;