* [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations
@ 2026-12-03 8:07 Krzysztof Woś
2026-12-03 9:22 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Krzysztof Woś @ 2026-12-03 8:07 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel
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;
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations
2026-12-03 8:07 [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations Krzysztof Woś
@ 2026-12-03 9:22 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 9:22 UTC (permalink / raw)
To: Krzysztof Woś; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] KVM: x86/mmu: skipping hugepage recovery leaves pages
permanently fragmented
- [Low] KVM: x86/mmu: Commit message overstates 2 MiB THP stale SPTE
persistence
- [Low] KVM: x86/mmu: tdp_mmu_invalidate_in_progress() duplicates core
range check
--
commit 108097c7b14815cdf087cf02d4ef5a7591086b62
Author: Krzysztof Woś <krzysztof.wos@gmail.com>
KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations
This commit adds a check in recover_huge_pages_range() to skip promoting
non-leaf TDP MMU SPTEs whose hugepage GFN range overlaps an in-progress
mmu_notifier invalidation.
> 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().
[Severity: Low]
Is the claim that a 4 KiB THP sub-range invalidation leaves a stale 2 MiB
SPTE persisting past invalidate_range_end() accurate?
Before splitting a 2 MiB THP for a sub-range operation (e.g. zap_pmd_range()),
__split_huge_pmd() issues a nested mmu_notifier_invalidate_range_start() for
the full 2 MiB range, which zaps any 2 MiB SPTE installed in the interim
before the PMD is split and before the outer invalidate_range_end() finishes.
The code change still avoids transiently violating host_pfn_mapping_level()'s
contract, so rewording this paragraph would avoid overstating the issue as a
persistent stale mapping.
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index 44dad106fad19..ce496e3d31e37 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;
> +}
[Severity: Low]
Can this range-overlap check be factored into a generic helper in
include/linux/kvm_host.h instead of duplicating it here?
tdp_mmu_invalidate_in_progress() has no x86 or TDP-MMU specifics and
duplicates the range and INVALID_GPA checks in mmu_invalidate_retry_gfn().
No other arch/ code reads mmu_invalidate_range_start/end directly, so sharing
a core helper keeps those invariants from drifting.
[ ... ]
> @@ -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;
[Severity: Medium]
Does skipping with continue here leave unaffected hugepages permanently
fragmented as 4 KiB SPTEs?
recover_huge_pages_range() is a one-shot pass called only when disabling
dirty logging (kvm_mmu_slot_apply_flags() -> kvm_mmu_recover_huge_pages()),
so skipped non-leaf SPTEs are never revisited.
Because kvm_mmu_invalidate_range_add() merges concurrent invalidations into a
single min(start)..max(end) bounding box until all of them finish, two
distant invalidations during a recovery walk can skip large regions of
unaffected memory, leaving them fragmented until another dirty-log cycle.
When tdp_mmu_invalidate_in_progress() is true, falling back to zapping
collapsible non-leaf SPTEs, as before 13e2e4f62a4b ("KVM: x86/mmu: Recover
TDP MMU huge page mappings in-place instead of zapping"), obeys
host_pfn_mapping_level()'s contract and lets later faults rebuild hugepages.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179628525375.378593.18062933948337352088@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 9:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 8:07 [PATCH] KVM: x86/mmu: Skip hugepage recovery on ranges with active invalidations Krzysztof Woś
2026-12-03 9:22 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox