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

From: Paolo Bonzini

Date: Fri Dec 04 2026 - 13:06:03 EST


From: Sean Christopherson <seanjc@xxxxxxxxxx>

[ Upstream commit d6d94b952ac457d3c34aad172cacf00ef01eef8c ]

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>
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 335e046c6836..97cb4261d781 100644
--- a/arch/x86/kvm/xen.c
+++ b/arch/x86/kvm/xen.c
@@ -1685,6 +1685,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;
}
@@ -1720,8 +1723,10 @@ static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)

mutex_unlock(&kvm->arch.xen.xen_lock);

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

return rc;
}
--
2.56.1