Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Subject: Re: [PATCH v4 06/12] KVM: x86/xen: Retry event channel delivery which races a memslot update
Date: Mon, 19 Oct 2026 16:37:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-177923@kernel.org> (raw)
In-Reply-To: <20261019162445.37219-7-dwmw2@infradead.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] KVM: x86/xen: 3-way deadlock via wait=true in evtchn_set_fn() from PIT worker
--

commit deb1cf394d02a7487c9bd0713d5fed7a4be72540
Author: David Woodhouse <dwmw@amazon.co.uk>

KVM: x86/xen: Retry event channel delivery which races a memslot update

This commit updates Xen event channel delivery callers to handle -EAGAIN
when delivery races with a memslot update, either waiting for the update
to complete or retrying depending on the execution context.

> diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c
> index 8a9d6963fc05a..c0b9ac674255d 100644
> --- a/arch/x86/kvm/xen.c
> +++ b/arch/x86/kvm/xen.c

[ ... ]

> @@ -2153,7 +2165,7 @@ static int evtchn_set_fn(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm
>  	if (!level)
>  		return -EINVAL;
>  
> -	return kvm_xen_set_evtchn(&e->xen_evtchn, kvm);
> +	return kvm_xen_set_evtchn(&e->xen_evtchn, kvm, true);
>  }

[Severity: High]
Can passing wait=true unconditionally in evtchn_set_fn() cause a 3-way
deadlock when GSI 0 is routed to KVM_IRQ_ROUTING_XEN_EVTCHN and the
in-kernel PIT is active?

evtchn_set_fn() is the .set callback invoked by kvm_set_irq(). In addition
to the irqfd workqueue, pit_do_work() in arch/x86/kvm/i8254.c runs on the
pit->worker kthread and fires GSI 0:

arch/x86/kvm/i8254.c:pit_do_work() {
    ...
    kvm_set_irq(kvm, KVM_PIT_IRQ_SOURCE_ID, 0, 1, false);
    kvm_set_irq(kvm, KVM_PIT_IRQ_SOURCE_ID, 0, 0, false);
    ...
}

Because pit->worker is a kthread with current->mm == NULL,
kvm_xen_set_evtchn() borrows kvm->mm via kthread_use_mm() and calls
kvm_gpc_refresh(gpc, PAGE_SIZE, true).

If a memslot move or deletion is concurrently updating the memslot backing
shinfo_cache, kvm_invalidate_memslot() -> kvm_swap_active_memslots() marks
the slot KVM_MEMSLOT_INVALID and blocks in
synchronize_srcu_expedited(&kvm->srcu).

If a vCPU thread inside vcpu_run()'s kvm->srcu read section concurrently
emulates a guest PIO write to PIT ports 0x40-0x43, pit_ioport_write() ->
pit_load_count() calls create_pit_timer() or destroy_pit_timer(), both of
which flush pit->expired while holding kvm->srcu:

arch/x86/kvm/i8254.c:create_pit_timer() {
    ...
    hrtimer_cancel(&ps->timer);
    kthread_flush_work(&pit->expired);
    ...
}

At that point:
1. The memslot update thread waits for the vCPU thread to exit its
   kvm->srcu read section.
2. The vCPU thread holds kvm->srcu and waits in
   kthread_flush_work(&pit->expired) for pit_do_work() to finish.
3. pit_do_work() loops in kvm_gpc_refresh(..., wait=true) on -EAGAIN
   waiting for the memslot update to finish, and because pit->worker is a
   kthread, fatal_signal_pending(current) never triggers.

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261019162445.37219-1-dwmw2@infradead.org?part=6

  reply	other threads:[~2026-10-19 16:37 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 16:24 [PATCH v4 00/12] KVM: x86/xen: Don't drop event deliveries which race memslot updates David Woodhouse
2026-10-19 16:24 ` [PATCH v4 01/12] KVM: x86/xen: Don't block memslot updates while halted in SCHEDOP_poll David Woodhouse
2026-10-19 16:24 ` [PATCH v4 02/12] KVM: pfncache: Return -EAGAIN for a lookup which hits an invalid memslot David Woodhouse
2026-10-19 16:24 ` [PATCH v4 03/12] KVM: pfncache: Add a 'wait' argument to the refresh and activate functions David Woodhouse
2026-10-19 16:24 ` [PATCH v4 04/12] KVM: x86/xen: Wait out memslot updates in the Xen attribute ioctls David Woodhouse
2026-10-19 16:24 ` [PATCH v4 05/12] KVM: x86/xen: Don't lose the timer event when delivery fails transiently David Woodhouse
2026-10-19 16:54   ` sashiko-bot
2026-10-19 16:24 ` [PATCH v4 06/12] KVM: x86/xen: Retry event channel delivery which races a memslot update David Woodhouse
2026-10-19 16:37   ` sashiko-bot [this message]
2026-10-20 13:16     ` David Woodhouse
2026-10-19 16:24 ` [PATCH v4 07/12] KVM: x86/xen: Extract delivery of event to vCPU into a separate helper David Woodhouse
2026-10-19 16:24 ` [PATCH v4 08/12] KVM: x86/xen: Explicitly tag "shared info" page as never being dirty tracked David Woodhouse
2026-10-19 16:24 ` [PATCH v4 09/12] KVM: x86/xen: Don't dirty track "vCPU info" page David Woodhouse
2026-10-19 16:24 ` [PATCH v4 10/12] KVM: x86: Request the guest TLB flush from record_steal_time() David Woodhouse
2026-10-19 16:24 ` [PATCH v4 11/12] KVM: selftests: Add Xen evtchn vs memslot-update race test David Woodhouse
2026-10-19 16:24 ` [PATCH v4 12/12] KVM: selftests: Fix unreachable timer recovery in xen_shinfo_test David Woodhouse
2026-10-19 16:46   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-177923@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox