* [PATCH] KVM: Send reschedule IPIs instead of SMP function calls for non-waiting requests
@ 2026-11-17 20:54 Sean Christopherson
2026-11-17 21:18 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Sean Christopherson @ 2026-11-17 20:54 UTC (permalink / raw)
To: Paolo Bonzini; +Cc: Sean Christopherson, kvm, linux-kernel, Satish Kharat
When kicking multiple vCPUs for a non-waiting request, send a reschedule
IPI to each vCPU instead of batching IPIs via a single SMP function call so
that KVM can make non-waiting requests with IRQs disabled. I.e. handle
multi-vCPU kicks the same way KVM handles single-vCPU kicks, for the exact
same reason.
Avoiding SMP function calls for non-waiting requests fixes a potential
deadlock if KVM VMX triggers KVM_REQ_UNBLOCK to all vCPUs when starting
device assignmennt:
WARNING: kernel/smp.c:943 at smp_call_function_many_cond
kvm_make_all_cpus_request [kvm]
kvm_arch_irq_bypass_add_producer [kvm]
__connect [irqbypass]
irq_bypass_register_producer [irqbypass]
vfio_msi_set_vector_signal [vfio_pci_core]
Send reschedule IPIs immediately to avoid having to re-walk the set of CPUs,
even though it makes the code slightly convoluted. It's also generally
advantageous to kick vCPUs as soon as possible, e.g. so that the target
vCPU(s) can react to pending events/actions, but that's purely nice-to-have
as there are very few paths in KVM that kick multiple vCPUs for non-waiting
requests.
Fixes: 77e1b8332d1d ("KVM: x86: Decouple device assignment from IRQ bypass")
Cc: stable@vger.kernel.org
Reported-by: Satish Kharat <satishkh@cisco.com>
Closes: https://lore.kernel.org/all/20261116-b4-kvm-irq-bypass-irqs-20261116-v1-1-6cc721a062fd@cisco.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
virt/kvm/kvm_main.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index cf4cb586f140..0b733746884c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -218,7 +218,10 @@ static inline bool kvm_kick_many_cpus(struct cpumask *cpus, bool wait)
if (cpumask_empty(cpus))
return false;
- smp_call_function_many(cpus, ack_kick, NULL, wait);
+ /* See kvm_vcpu_kick() and kvm_make_vcpu_request() for details. */
+ if (wait)
+ smp_call_function_many(cpus, ack_kick, NULL, wait);
+
return true;
}
@@ -241,12 +244,20 @@ static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
* satisfied if the vCPU migrates. Entering READING_SHADOW_PAGE_TABLES
* after this point is also OK, as the requirement is only that KVM wait
* for vCPUs that were reading SPTEs _before_ any changes were
- * finalized. See kvm_vcpu_kick() for more details on handling requests.
+ * finalized.
+ *
+ * See kvm_vcpu_kick() for more details on handling requests, including
+ * the use of a reschedule IPI for non-waiting requests. If waiting is
+ * required, batch the SMP function call until after all to-be-kicked
+ * CPUs are gathered, to avoid waiting multiple times.
*/
if (kvm_request_needs_ipi(vcpu, req)) {
cpu = READ_ONCE(vcpu->cpu);
- if (cpu != -1 && cpu != current_cpu)
+ if (cpu != -1 && cpu != current_cpu) {
+ if (!(req & KVM_REQUEST_WAIT))
+ smp_send_reschedule(cpu);
__cpumask_set_cpu(cpu, tmp);
+ }
}
}
base-commit: d21eef2084a9af87ac2a783bc7c2b175ba114e4e
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] KVM: Send reschedule IPIs instead of SMP function calls for non-waiting requests
2026-11-17 20:54 [PATCH] KVM: Send reschedule IPIs instead of SMP function calls for non-waiting requests Sean Christopherson
@ 2026-11-17 21:18 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-17 21:18 UTC (permalink / raw)
To: Sean Christopherson; +Cc: kvm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] KVM: per-CPU cpu_kick_mask corruption in
kvm_make_all_cpus_request()
- [High] KVM: s390 build failure in kvm_make_vcpu_request() with
CONFIG_KVM=m
--
commit 38c2c2306d98411374ab3430ab319733689457e7
Author: Sean Christopherson <seanjc@google.com>
KVM: Send reschedule IPIs instead of SMP function calls for non-waiting requests
This commit sends a reschedule IPI to each target vCPU instead of batching
IPIs via smp_call_function_many() when making non-waiting multi-vCPU
requests, allowing non-waiting requests to be made with interrupts disabled.
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index cf4cb586f1407..0b733746884ce 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
[ ... ]
> @@ -241,12 +244,20 @@ static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
> * satisfied if the vCPU migrates. Entering READING_SHADOW_PAGE_TABLES
> * after this point is also OK, as the requirement is only that KVM wait
> * for vCPUs that were reading SPTEs _before_ any changes were
> - * finalized. See kvm_vcpu_kick() for more details on handling requests.
> + * finalized.
> + *
> + * See kvm_vcpu_kick() for more details on handling requests, including
> + * the use of a reschedule IPI for non-waiting requests. If waiting is
> + * required, batch the SMP function call until after all to-be-kicked
> + * CPUs are gathered, to avoid waiting multiple times.
> */
> if (kvm_request_needs_ipi(vcpu, req)) {
> cpu = READ_ONCE(vcpu->cpu);
> - if (cpu != -1 && cpu != current_cpu)
> + if (cpu != -1 && cpu != current_cpu) {
> + if (!(req & KVM_REQUEST_WAIT))
> + smp_send_reschedule(cpu);
[Severity: High]
Does calling smp_send_reschedule() here break the build on s390 when
CONFIG_KVM=m?
On s390, smp_send_reschedule() expands to arch_smp_send_reschedule(),
which is not exported to modules. __kvm_vcpu_kick() is guarded by
#ifndef CONFIG_S390 for that reason (see 0266c894b51c ("KVM: put back
#ifndef CONFIG_S390 around kvm_vcpu_kick")), whereas
kvm_make_vcpu_request() is built into kvm.ko on s390.
> __cpumask_set_cpu(cpu, tmp);
> + }
> }
> }
[Severity: High]
Can a non-waiting request in interrupt context clobber the per-CPU
cpu_kick_mask of an in-progress waiting request on the same CPU?
kvm_make_all_cpus_request() and kvm_make_vcpus_request_mask() only disable
preemption via get_cpu() while building cpu_kick_mask, leaving interrupts
enabled for waiting requests, and still clear and populate cpu_kick_mask
even when KVM_REQUEST_WAIT is not set:
virt/kvm/kvm_main.c:kvm_make_all_cpus_request() {
me = get_cpu();
cpus = this_cpu_cpumask_var_ptr(cpu_kick_mask);
cpumask_clear(cpus);
kvm_for_each_vcpu(i, vcpu, kvm)
kvm_make_vcpu_request(vcpu, req, cpus, me);
called = kvm_kick_many_cpus(cpus, !!(req & KVM_REQUEST_WAIT));
put_cpu();
...
}
If an interrupt fires mid-loop and makes a non-waiting multi-vCPU request
(for example on arm64 via irqfd_wakeup() -> kvm_arch_set_irq_inatomic() ->
... -> vgic_queue_irq_unlock()):
kvm_flush_remote_tlbs()
kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH)
cpumask_clear(cpus)
kvm_make_vcpu_request(vcpu0, ...)
kvm_vcpu_exiting_guest_mode(vcpu0)
cmpxchg(&vcpu0->mode, IN_GUEST_MODE, EXITING_GUEST_MODE)
__cpumask_set_cpu(vcpu0->cpu, cpus)
<hardirq>
irqfd_wakeup()
...
kvm_make_all_cpus_request(kvm, KVM_REQ_IRQ_PENDING)
cpumask_clear(cpus) <-- wipes vcpu0->cpu from cpu_kick_mask
kvm_make_vcpu_request(vcpu0, ...)
kvm_request_needs_ipi(vcpu0, ...) <-- false (EXITING_GUEST_MODE)
kvm_kick_many_cpus(cpus, true) <-- misses vcpu0->cpu
Because the outer request already transitioned vcpu0->mode from
IN_GUEST_MODE to EXITING_GUEST_MODE, the nested non-waiting request sees
mode != IN_GUEST_MODE and neither sends a reschedule IPI nor re-adds
vcpu0->cpu to cpu_kick_mask. When the interrupt returns,
kvm_kick_many_cpus() skips kicking and waiting for vcpu0, so the waiting
request (such as a remote TLB flush) can complete while vcpu0 is still in
guest mode or reading shadow page tables.
Only touching cpu_kick_mask when KVM_REQUEST_WAIT is set (and tracking
whether any CPU was kicked with a local boolean for the return value)
would avoid this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117205428.3717-1-seanjc@google.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-17 21:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 20:54 [PATCH] KVM: Send reschedule IPIs instead of SMP function calls for non-waiting requests Sean Christopherson
2026-11-17 21:18 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox