[PATCH 2/2] KVM: arm64: gic-v5: Don't move the vPE doorbell from kvm_sched_out()

From: Jia He

Date: Mon Nov 16 2026 - 02:13:39 EST


vgic_v5_put() makes the vPE doorbell affine to the current CPU when the
vcpu enters WFI. This is meant for the explicit put from kvm_vcpu_wfi(),
but the same code also runs from kvm_sched_out() whenever the vPE is
resident with IN_WFI set.

That happens when the vcpu thread is scheduled back in while halting:
unlike vgic_v4_load(), vgic_v5_load() does not bail out on IN_WFI and
makes the vPE resident again, so the next sched-out, from
kvm_vcpu_block() or from a preemption during halt polling, goes through
vgic_v5_put() with IN_WFI still set. If the thread moved to another
CPU in between, irq_set_affinity() is called with the runqueue lock
held:

irq_set_affinity+0x38/0x88
vgic_v5_put+0xf0/0x2b8
kvm_vgic_put+0x64/0xb0
vhe_vcpu_put+0x40/0xa0
kvm_arch_vcpu_put+0x28/0x98
kvm_sched_out+0x4c/0x70
prepare_task_switch+0x1b0/0x388
__schedule+0x234/0x958
schedule+0x58/0x138
kvm_vcpu_block+0x88/0x130
kvm_vcpu_halt+0x120/0x468
kvm_vcpu_wfi+0x80/0x190

irq_set_affinity() takes the doorbell's irq_desc lock and, when the
doorbell is force-threaded (threadirqs, PREEMPT_RT), wakes its irq
thread:

&rq->__lock --> &irq_desc_lock_class --> &p->pi_lock --> &rq->__lock

Moving the doorbell is only an optimisation: the doorbell handler kicks
the vcpu from any CPU, and the next WFI entry moves it again. Skip it
when vcpu->scheduled_out is set, which kvm_vcpu_put_hw_mmu() also uses
to tell a sched-out from an explicit put.

Fixes: 2111261ac1c8 ("KVM: arm64: gic-v5: Request doorbells when VPEs enter WFI")
Signed-off-by: Jia He <justin.he@xxxxxxx>
---
arch/arm64/kvm/vgic/vgic-v5.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/vgic/vgic-v5.c b/arch/arm64/kvm/vgic/vgic-v5.c
index 91810fbd99af..5824b00bb50d 100644
--- a/arch/arm64/kvm/vgic/vgic-v5.c
+++ b/arch/arm64/kvm/vgic/vgic-v5.c
@@ -1248,8 +1248,14 @@ void vgic_v5_put(struct kvm_vcpu *vcpu)
* Make the doorbell affine to this CPU, if it isn't
* already. Actively check the cpumask first as it is
* cheaper than changing the affinity every time.
+ *
+ * Don't do it from kvm_sched_out(), which holds the
+ * runqueue lock: irq_set_affinity() takes the
+ * doorbell's irq_desc lock and may wake its irq
+ * thread. The doorbell works from any CPU, and the
+ * next WFI entry moves it.
*/
- if (!cpumask_test_cpu(cpu, aff)) {
+ if (!vcpu->scheduled_out && !cpumask_test_cpu(cpu, aff)) {
int ret;

ret = irq_set_affinity(db_irq, cpumask_of(cpu));
--
2.34.1