From: "Krzysztof Woś" <krzysztof.wos@gmail.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations
Date: Thu, 03 Dec 2026 13:51:59 +0000 [thread overview]
Message-ID: <179630591906.459399.8619305795048760319@gmail.com> (raw)
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;
next reply other threads:[~2026-12-03 14:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 13:51 Krzysztof Woś [this message]
2026-12-03 14:43 ` [PATCH v2] KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179630591906.459399.8619305795048760319@gmail.com \
--to=krzysztof.wos@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.