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] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations
Date: Thu, 03 Dec 2026 08:07:33 +0000 [thread overview]
Message-ID: <179628525375.378593.18062933948337352088@gmail.com> (raw)
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@vger.kernel.org
Signed-off-by: Krzysztof Woś <krzysztof.wos@gmail.com>
---
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;
reply other threads:[~2026-12-03 8:45 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=179628525375.378593.18062933948337352088@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox