[PATCH 6.1.y 17/24] KVM: WARN if there are in-flight memslot updates when a VM's process exits
From: Paolo Bonzini
Date: Fri Dec 04 2026 - 13:13:37 EST
From: Sean Christopherson <seanjc@xxxxxxxxxx>
[ Upstream commit fd00a80aa395b803832ebaf1c494d721e9483dbb ]
Move the sanity check on there being no outstanding memslot updates from VM
destruction to kvm_mmu_notifier_release(), i.e. WARN if there are
outstanding memslot updates when the VM is dying *or* the owning process is
exiting. KVM subtly already guarantees (and relies on!) memslot updates to
be mutually exclusive with the mm dying.
If KVM unregistered its mmu_notifier between .invalidate_range_start() and
.invalidate_range_end(), then the invalidate count would be left elevated
until the VM is destroyed, but if there were an outstanding memslot update,
it would be impossible for the VM to be destroyed.
Opportunistically change the WARNs to WARN_ON_ONCE() so that a KVM bug
doesn't unintentionally DoS the host due to spamming dmesg (hanging a task
is bad enough).
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261120195742.55768-4-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
virt/kvm/kvm_main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 7ffe1900ffaf..9845e6a734f3 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -894,6 +894,14 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
struct kvm *kvm = mmu_notifier_to_kvm(mn);
int idx;
+ /*
+ * No threads can be waiting in kvm_swap_active_memslots() as KVM only
+ * allows KVM_SET_USER_MEMORY_REGION{,2} in process context, and only
+ * from the process that created the VM, i.e. to @mm. If @mm is dying,
+ * then there can't possibly be outstanding memslot updates.
+ */
+ WARN_ON_ONCE(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
+
idx = srcu_read_lock(&kvm->srcu);
kvm_arch_flush_shadow_all(kvm);
kvm_arch_guest_memory_reclaimed(kvm);
@@ -1317,7 +1325,6 @@ static void kvm_destroy_vm(struct kvm *kvm)
* notifier between a start() and end(), then there shouldn't be any
* in-progress invalidations.
*/
- WARN_ON(rcuwait_active(&kvm->mn_memslots_update_rcuwait));
if (kvm->mn_active_invalidate_count)
kvm->mn_active_invalidate_count = 0;
else
--
2.56.1