KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations
PATCH v2 1/1 KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations
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.
---
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/[email protected]/
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(-)
arch/x86/kvm/mmu/tdp_mmu.c +18 -0
@@ -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;
include/linux/kvm_host.h +16 -7
@@ -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 ;
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: x86/mmu: recover_huge_pages_range() can zap TDX mirror non-leaf SPTEs --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: x86/mmu: recover_huge_pages_range() can zap TDX mirror non-leaf SPTEs
--
commit 383a8a9cb0faf2691412211c4c44174397818320
Author: Krzysztof Woś <[email protected]>
KVM: x86/mmu: Zap collapsible SPTEs on ranges with active invalidations
This commit factors out kvm_mmu_invalidate_range_in_progress() and uses it in recover_huge_pages_range() to zap collapsible non-leaf SPTEs instead of promoting them in place when an mmu_notifier invalidation overlaps the hugepage range.
Quoted 19 lines
arch/x86/kvm/mmu/tdp_mmu.c +11
-0@@ -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))) {
arch/x86/kvm/mmu/tdp_mmu.c static void recover_huge_pages_range(struct kvm *kvm, · line 1823
Quoted 10 lines
arch/x86/kvm/mmu/tdp_mmu.c +7
-0+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;
[Severity: High]
Can this zap a lingering non-leaf SPTE in an active TDX mirror root?
kvm_tdp_mmu_recover_huge_pages() walks KVM_VALID_ROOTS, which includes mirror roots. When a KVM_MEM_GUEST_MEMFD memslot is deleted in a TDX VM, its empty mirror non-leaf SPTEs remain linked until TD teardown.
If userspace then clears KVM_MEMORY_ATTRIBUTE_PRIVATE on that range, adds a regular hugepage-backed memslot with KVM_MEM_LOG_DIRTY_PAGES, and disables dirty logging during an overlapping mmu_notifier invalidation, kvm_mmu_max_mapping_level() returns PG_LEVEL_2M via host_pfn_mapping_level().
Before this patch, tdp_mmu_make_huge_spte() returned -ENOENT on the empty mirror child page table and skipped the update. Now this fallback runs first and zaps the mirror non-leaf SPTE, calling handle_removed_pt() -> tdx_sept_free_private_spt().
Because the TD HKID is still assigned, tdx_sept_free_private_spt() hits KVM_BUG_ON(is_hkid_assigned(to_kvm_tdx(kvm)), kvm), bugs the VM, and leaks the encrypted S-EPT page in sp->external_spt. Should kvm_tdp_mmu_recover_huge_pages() walk only direct roots?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1