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

From: Justin He

Date: Mon Nov 16 2026 - 04:38:02 EST




> -----Original Message-----
> From: sashiko-bot@xxxxxxxxxx <sashiko-bot@xxxxxxxxxx>
> Sent: Monday, November 16, 2026 4:07 PM
> To: Justin He <Justin.He@xxxxxxx>
> Cc: kvmarm@xxxxxxxxxxxxxxx; linux-rt-devel@xxxxxxxxxxxxxxx; Oliver Upton
> <oupton@xxxxxxxxxx>; Marc Zyngier <maz@xxxxxxxxxx>; linux-
> kernel@xxxxxxxxxxxxxxx
> Subject: Re: [PATCH 2/2] KVM: arm64: gic-v5: Don't move the vPE doorbell
> from kvm_sched_out()
>
> 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().
But IIUC, the wakeup isn't lost. The second put requests the doorbell
again. The doorbell can be generated for an interrupt that is already
pending when the vPE becomes non-resident, not only for a new pending
interrupt (IKZXCY in IHI 111701). The guest hasn't run in between, so
the interrupt is still pending and the priority mask hasn't changed.
db_fired is cleared before requesting the doorbell again, so the new
doorbell isn't missed. Am I missing sth else?

For the halt-polling case, I agree there is an issue. After a sched-in,
nothing requests the doorbell until the next sched-out. Making
vgic_v5_load() return early on IN_WFI, like v4 does, should fix this.
But since this is an optimisation, I prefer to leave it for a follow-up,
unless the maintainers want it in this patch.
___ 
Best Regards,
Jia He (Justin)