Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v2] KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations
@ 2026-12-03 13:51 Krzysztof Woś
  2026-12-03 14:43 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Krzysztof Woś @ 2026-12-03 13:51 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel

Fall back to zapping a collapsible non-leaf TDP MMU SPTE in
recover_huge_pages_range() instead of promoting it in place when an
in-progress mmu_notifier invalidation overlaps the candidate hugepage
range [iter.gfn, iter.gfn + KVM_PAGES_PER_HPAGE(iter.level)).

Under host_pfn_mapping_level()'s documented contract in
arch/x86/kvm/mmu/mmu.c, installing a new mapping from the returned host
mapping level requires holding mmu_lock and ensuring no mmu_notifier
invalidation is in progress for the target range (or already holding a
valid mapping covering the entire range).  By contrast, using the host
mapping level solely to decide whether to zap an entry is safe even
during an invalidation.

Because tdp_mmu_make_huge_spte() synthesizes a huge SPTE as soon as any
single child leaf SPTE in the range is shadow-present, having a present
non-leaf parent SPTE does not guarantee the absence of an active
sub-range invalidation on sibling GFNs within that hugepage.  If
kvm_mmu_notifier_invalidate_range_start() zapped a 4 KiB sub-range and
released mmu_lock while mmu_invalidate_in_progress remains elevated and
before the primary MMU updates or splits the host mapping, in-place
promotion can transiently reinstall a huge SPTE over the invalidated
sub-range.  For non-THP huge mappings where no subsequent full-PMD
invalidation fires from __split_huge_pmd(), that stale mapping is not
re-zapped before invalidate_range_end().

Factor the locked [start, end) invalidation-overlap check out of
mmu_invalidate_retry_gfn() into kvm_mmu_invalidate_range_in_progress()
in include/linux/kvm_host.h.  When an active invalidation overlaps
[iter.gfn, iter.gfn + KVM_PAGES_PER_HPAGE(iter.level)) in
recover_huge_pages_range(), fall back to zapping the collapsible
non-leaf SPTE (SHADOW_NONPRESENT_VALUE) instead of promoting in place so
host_pfn_mapping_level()'s contract is honored without leaving pages
permanently fragmented at 4 KiB after the one-shot dirty-log disable
pass.

Fixes: 13e2e4f62a4b ("KVM: x86/mmu: Recover TDP MMU huge page mappings in-place instead of zapping")
Signed-off-by: Krzysztof Woś <krzysztof.wos@gmail.com>
---
Changes in v2:
- Fall back to zapping the collapsible non-leaf SPTE instead of skipping
  with continue when an invalidation overlaps the hugepage range,
  avoiding leaving unaffected pages in a merged invalidation bounding
  box fragmented at 4 KiB.
- Factor kvm_mmu_invalidate_range_in_progress() into
  include/linux/kvm_host.h and reuse it in mmu_invalidate_retry_gfn().
- Clarify commit message regarding __split_huge_pmd()'s nested full-PMD
  invalidation on THPs versus transient re-installation and non-THP
  sub-range invalidations.
- Link to v1: https://lore.kernel.org/kvm/179628525375.378593.18062933948337352088@gmail.com/

Build-tested on x86_64 (GCC 15.2.0) with W=1, CONFIG_WERROR=y,
CONFIG_KVM_WERROR=y, CONFIG_KVM_INTEL=m, CONFIG_KVM_AMD=m,
CONFIG_KVM_SMM=y, CONFIG_KVM_XEN=y, and CONFIG_PROVE_LOCKING=y.

Base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106

 arch/x86/kvm/mmu/tdp_mmu.c | 18 ++++++++++++++++++
 include/linux/kvm_host.h   | 23 ++++++++++++++++-------
 2 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 44dad106f..af05a91b6 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -1800,6 +1800,24 @@ static void recover_huge_pages_range(struct kvm *kvm,
 		if (max_mapping_level < iter.level)
 			continue;
 
+		/*
+		 * If an in-progress mmu_notifier invalidation overlaps the
+		 * hugepage range, a subset of child SPTEs may have been zapped
+		 * while the host mapping is still huge, making in-place
+		 * promotion unsafe.  Fall back to zapping the collapsible
+		 * non-leaf SPTE, which obeys host_pfn_mapping_level()'s
+		 * contract and lets subsequent faults rebuild a huge SPTE once
+		 * the invalidation completes.
+		 */
+		if (kvm_mmu_invalidate_range_in_progress(kvm, iter.gfn,
+				iter.gfn + KVM_PAGES_PER_HPAGE(iter.level))) {
+			if (tdp_mmu_set_spte_atomic(kvm, &iter, SHADOW_NONPRESENT_VALUE))
+				goto retry;
+
+			flush = true;
+			continue;
+		}
+
 		r = tdp_mmu_make_huge_spte(kvm, &iter, &huge_spte);
 		if (r == -EAGAIN)
 			goto retry;
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index fe300a22a..e41136ae1 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2196,9 +2196,8 @@ static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq)
 	return 0;
 }
 
-static inline int mmu_invalidate_retry_gfn(struct kvm *kvm,
-					   unsigned long mmu_seq,
-					   gfn_t gfn)
+static inline bool kvm_mmu_invalidate_range_in_progress(struct kvm *kvm,
+							gfn_t start, gfn_t end)
 {
 	lockdep_assert_held(&kvm->mmu_lock);
 	/*
@@ -2214,13 +2213,23 @@ static inline int mmu_invalidate_retry_gfn(struct kvm *kvm,
 		 */
 		if (WARN_ON_ONCE(kvm->mmu_invalidate_range_start == INVALID_GPA ||
 				 kvm->mmu_invalidate_range_end == INVALID_GPA))
-			return 1;
+			return true;
 
-		if (gfn >= kvm->mmu_invalidate_range_start &&
-		    gfn < kvm->mmu_invalidate_range_end)
-			return 1;
+		if (start < kvm->mmu_invalidate_range_end &&
+		    end > kvm->mmu_invalidate_range_start)
+			return true;
 	}
 
+	return false;
+}
+
+static inline int mmu_invalidate_retry_gfn(struct kvm *kvm,
+					   unsigned long mmu_seq,
+					   gfn_t gfn)
+{
+	if (kvm_mmu_invalidate_range_in_progress(kvm, gfn, gfn + 1))
+		return 1;
+
 	if (kvm->mmu_invalidate_seq != mmu_seq)
 		return 1;
 	return 0;

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 14:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:51 [PATCH v2] KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations Krzysztof Woś
2026-12-03 14:43 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox