[PATCH 6.12.y 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released

From: Paolo Bonzini

Date: Fri Dec 04 2026 - 13:04:16 EST


From: Sean Christopherson <seanjc@xxxxxxxxxx>

[ Upstream commit fca5e4804655f39e40cf302ca37f6bd35445881b ]

Set mn_active_invalidate_count permanently (until the VM is destroyed) when
the VM's mmu_notifier release() hook is called, i.e. when the VM and/or its
process is dying. This will allow using gfn_to_pfn_cache_invalidate_start()
more or less verbatim to invalidate all GPCs on .release(), without having
to play games with the sequence count. As pointed out by Sashiko, a racing
hva_to_pfn_retry() could set its GPC valid after re-acquiring gpc->lock if
it's not forced to retry.

Isolate the mn_active_invalidate_count change on the off chance it causes
problems. Elevating mn_active_invalidate_count should be a-ok because it's
already possible for kvm_mmu_notifier_release() to run while the count is
elevated *and* cause it to remain elevated, as .release() can be called
between .invalidate_range_start() and .invalidate_range_end(). If that
happens, the matching end() will never occur, but kvm_destroy_vm() will
squash the count before freeing memslots. Note, there can't be any
in-flight memslot updates when .release() runs, as KVM only does memslot
updates from process context (except for the kvm_destroy_vm() case).

Adjust the sanity check on in-progress invalidations to account for an
invalidate count of '1' now being the effective baseline when the VM is
being destroyed.

Opportunistically tweak the comment to call out that unregistering the MMU
notifier between start() and end() doesn't *guarantee* KVM should observe
an in-progress invalidation, because KVM tracks those invalidations if and
only if there is a relevant memslot.

Opportunistically add a TODO to call out that KVM can stop squashing the
invalidate count once x86 stops abusing internal memslot updates to unmap
VMAs for KVM-internal memslots[*].

Link: https://lore.kernel.org/all/20261030212112.2598640-7-seanjc@xxxxxxxxxx [*]
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261120195742.55768-5-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
virt/kvm/kvm_main.c | 42 ++++++++++++++++++++++++++++--------------
1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 02290261e64b..29c723a06f68 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -919,6 +919,16 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
*/
WARN_ON_ONCE(rcuwait_active(&kvm->mn_memslots_update_rcuwait));

+ /*
+ * Elevate the invalidate count to prevent concurrent walks of the user
+ * page tables from fully succeeding if they race with teardown of the
+ * page tables. The elevated count will force lookups to always retry,
+ * until eventually the lookup fails outright after teardown completes.
+ */
+ spin_lock(&kvm->mn_invalidate_lock);
+ kvm->mn_active_invalidate_count++;
+ spin_unlock(&kvm->mn_invalidate_lock);
+
idx = srcu_read_lock(&kvm->srcu);
kvm_arch_flush_shadow_all(kvm);
kvm_arch_guest_memory_reclaimed(kvm);
@@ -1341,22 +1351,26 @@ static void kvm_destroy_vm(struct kvm *kvm)
kvm_coalesced_mmio_free(kvm);
#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER
mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
+
/*
- * At this point, pending calls to invalidate_range_start()
- * have completed but no more MMU notifiers will run, so
- * mn_active_invalidate_count may remain unbalanced.
- * No threads can be waiting in kvm_swap_active_memslots() as the
- * last reference on KVM has been dropped, but freeing
- * memslots would deadlock without this manual intervention.
- *
- * If the count isn't unbalanced, i.e. KVM did NOT unregister its MMU
- * notifier between a start() and end(), then there shouldn't be any
- * in-progress invalidations.
+ * Sanity check KVM's tracking and internal use of start()+end(), e.g.
+ * to detect a unpaired start() or end(). KVM increments the count in
+ * kvm_mmu_notifier_release(), so the count must be at least 1; and
+ * if kvm->mmu_invalidate_in_progress is set, then KVM unregistered
+ * its MMU notifier between kvm_mmu_invalidate_range_start() and
+ * kvm_mmu_invalidate_range_end(), so kvm->mn_active_invalidate_count must
+ * have been elevated further by kvm_mmu_notifier_invalidate_range_start().
*/
- if (kvm->mn_active_invalidate_count)
- kvm->mn_active_invalidate_count = 0;
- else
- WARN_ON(kvm->mmu_invalidate_in_progress);
+ WARN_ON_ONCE(kvm->mn_active_invalidate_count < 1 + !!kvm->mmu_invalidate_in_progress);
+
+ /*
+ * Squash the invalidate count so that arch code can update memslots
+ * as part of VM destruction.
+ *
+ * TODO: Drop this once KVM x86 no longer relies on setting internal
+ * memslots to munmap() the corresponding VMAs.
+ */
+ kvm->mn_active_invalidate_count = 0;
#else
kvm_flush_shadow_all(kvm);
#endif
--
2.56.1