mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Justin He <Justin.He@arm.com>
To: "sashiko-reviews@lists.linux.dev" <sashiko-reviews@lists.linux.dev>
Cc: "kvmarm@lists.linux.dev" <kvmarm@lists.linux.dev>,
	"linux-rt-devel@lists.linux.dev" <linux-rt-devel@lists.linux.dev>,
	Oliver Upton <oupton@kernel.org>, Marc Zyngier <maz@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH 2/2] KVM: arm64: gic-v5: Don't move the vPE doorbell from kvm_sched_out()
Date: Mon, 16 Nov 2026 09:37:14 +0000	[thread overview]
Message-ID: <AS2PR08MB97868A5CE01B18481E9549E0F77C2@AS2PR08MB9786.eurprd08.prod.outlook.com> (raw)
In-Reply-To: <sashiko-outbox-216506@kernel.org>



> -----Original Message-----
> From: sashiko-bot@kernel.org <sashiko-bot@kernel.org>
> Sent: Monday, November 16, 2026 4:07 PM
> To: Justin He <Justin.He@arm.com>
> Cc: kvmarm@lists.linux.dev; linux-rt-devel@lists.linux.dev; Oliver Upton
> <oupton@kernel.org>; Marc Zyngier <maz@kernel.org>; linux-
> kernel@vger.kernel.org
> 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@arm.com>
> 
> 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)



      reply	other threads:[~2026-11-16  9:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  7:13 [PATCH 0/2] KVM: arm64: gic-v5: Fix irq_desc locks taken under the runqueue lock Jia He
2026-11-16  7:13 ` [PATCH 1/2] KVM: arm64: Give the host timer interrupts a separate lockdep class Jia He
2026-11-16  7:13 ` [PATCH 2/2] KVM: arm64: gic-v5: Don't move the vPE doorbell from kvm_sched_out() Jia He
2026-11-16  8:07   ` sashiko-bot
2026-11-16  9:37     ` Justin He [this message]

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=AS2PR08MB97868A5CE01B18481E9549E0F77C2@AS2PR08MB9786.eurprd08.prod.outlook.com \
    --to=justin.he@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@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

all inboxes | Powered by JetHome®