Re: [PATCH 2/2] KVM: arm64: gic-v5: Don't move the vPE doorbell from kvm_sched_out()
From: sashiko-bot
Date: Mon Nov 16 2026 - 03:07:22 EST
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: arm64: gic-v5: vgic_v5_load() in WFI disarms doorbells
and loses wakeups
--
commit 36844cdbda4df2b177b6c013c2074287d75ff5f3
Author: Jia He <justin.he@xxxxxxx>
KVM: arm64: gic-v5: Don't move the vPE doorbell from kvm_sched_out()
This commit skips calling irq_set_affinity() in vgic_v5_put() when
vcpu->scheduled_out is set, avoiding acquiring irq_desc and pi_lock under the
runqueue lock during kvm_sched_out().
> diff --git a/arch/arm64/kvm/vgic/vgic-v5.c b/arch/arm64/kvm/vgic/vgic-v5.c
> index 91810fbd99afa..5824b00bb50df 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));
[Severity: Medium]
Should vgic_v5_load() bail out when IN_WFI is set instead of only skipping
irq_set_affinity() here?
Because vgic_v5_load() does not check vcpu_get_flag(vcpu, IN_WFI), any
kvm_sched_in() while the vCPU is halting in kvm_vcpu_halt() calls
__vgic_v5_make_resident(), setting gicv5_vpe.resident = true and clearing
ICH_CONTEXTR_EL2_DB in hardware.
If the vPE doorbell fires while blocked in kvm_vcpu_block(), db_handler()
sets gicv5_vpe.db_fired = true and wakes the vCPU thread. When the thread
schedules in, vgic_v5_load() makes the vPE resident before schedule() returns
to the loop in kvm_vcpu_block(). If the vCPU thread is preempted again before
kvm_vcpu_check_block() consumes db_fired, kvm_sched_out() -> vgic_v5_put()
still calls __vgic_v5_make_non_resident():
arch/arm64/kvm/hyp/vgic-v5-sr.c:__vgic_v5_make_non_resident() {
...
WRITE_ONCE(cpu_if->gicv5_vpe.db_fired, false);
cpu_if->gicv5_vpe.resident = false;
}
When the thread schedules back in, kvm_vcpu_check_block() ->
kvm_vgic_vcpu_pending_irq() sees db_fired == false and goes back to sleep in
schedule(), losing the wakeup.
Making the vPE resident on schedule-in during halt-polling also disarms the
hardware doorbell for the rest of the poll window, so newly pending SPIs and
LPIs do not set db_fired, and a subsequent kvm_sched_out() still runs the EL2
residency hypercalls and vgic_v5_sync_ppi_priorities() under the runqueue
lock. Returning early from vgic_v5_load() when IN_WFI is set keeps
gicv5_vpe.resident false throughout kvm_vcpu_halt(), so kvm_sched_out()
returns immediately in vgic_v5_put().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261116071304.2037895-1-justin.he@xxxxxxx?part=2