[PATCH 09/17] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel

From: Paolo Bonzini

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


From: Sean Christopherson <seanjc@xxxxxxxxxx>

When setting a Xen event channel from a kthread, get a reference to the
VM's mm prior to borrowing the mm, and bail with -EFAULT if the mm is dead
or dying. The VM's irqfds are deactivated only when the VM itself is dying,
i.e. the worker can trigger event channel writes if the VM's process exits
before the VM itself is destroyed.

Note, there's no need to use mmput_async(), as kvm_xen_set_evtchn() borrows
an mm if and only if it's already running in a workqueue.

Fixes: 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and event channel delivery")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Sashiko Bot <sashiko-bot@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261120195742.55768-2-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kvm/xen.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
index 6aa00f746cba..0e8ca3e4681c 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -1984,6 +1984,9 @@ static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)
if (WARN_ON_ONCE(current->mm))
return -EINVAL;

+ if (!mmget_not_zero(kvm->mm))
+ return -EFAULT;
+
kthread_use_mm(kvm->mm);
mm_borrowed = true;
}
@@ -2015,8 +2018,10 @@ static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)
srcu_read_unlock(&kvm->srcu, idx);
} while(!rc);

- if (mm_borrowed)
+ if (mm_borrowed) {
kthread_unuse_mm(kvm->mm);
+ mmput(kvm->mm);
+ }

return rc;
}
--
2.56.1