Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Paolo Bonzini" <pbonzini@redhat.com>, kvm@vger.kernel.org
Cc: kvmarm@lists.linux.dev, "Oliver Upton" <oupton@kernel.org>,
	"Marc Zyngier" <maz@kernel.org>
Subject: Re: [PATCH 4/4] KVM: arm64: vgic: Linearise AP list pruning process
Date: Fri, 04 Dec 2026 18:21:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-250407@kernel.org> (raw)
In-Reply-To: <20261204175740.2153746-5-pbonzini@redhat.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] KVM: arm64: vgic: O(N * V) culling_list scan in
  vgic_prune_ap_list()
- [High] KVM: arm64: vgic: unbounded retry livelock in
  vgic_prune_ap_list()
- [Medium] KVM: arm64: vgic: unconditional bitmap scans per exit in
  vgic_prune_ap_list()
- [Medium] KVM: arm64: vgic: O(N) kvm_get_vcpu_by_id() lookup in
  vgic_prune_ap_list()
- [Low] KVM: arm64: vgic: missing Fixes trailer for vgic_prune_ap_list()
  DoS fix
--

commit b2eeef66270a88825d1c1ca4b6594220f852f430
Author: Marc Zyngier <maz@kernel.org>

> KVM: arm64: vgic: Linearise AP list pruning process
>
> The algorithm used to prune IRQs sitting in the AP list has a quadratic
> complexity, as any concurrent change in the list results in a restart of
> the processing. The locking is also rather complicated, as we have to
> take the locks for both source and target vcpus, plus the per-irq lock.
>
> While this has served us well so far, people are finding new and amusing
> ways to abuse the locking and perform a DoS on the host. Not fun.
>
> Instead, switch to a linear approach where potentially moved IRQs are
> taken off the AP list and then dispatched onto their new targets. This
> simplifies the locking (only one vcpu lock at any given time), and only
> the interrupts that have been changed during that process are evaluated
> again. This allows the pruning process to converge much quicker.
>
> Signed-off-by: Marc Zyngier <maz@kernel.org>
> Reviewed-by: Oliver Upton <oupton@kernel.org>
> Cc: stable@vger.kernel.org

[Severity: Low]
This fixes a host DoS in vgic_prune_ap_list() and is tagged for stable.
Should this include:

  Fixes: 0919e84c0fc1 ("KVM: arm/arm64: vgic-new: Add IRQ sync/flush framework")

> diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c
> index bea848c93a990..a7e3e1a8a64f1 100644
> --- a/arch/arm64/kvm/vgic/vgic.c
> +++ b/arch/arm64/kvm/vgic/vgic.c

[ ... ]

> @@ -765,109 +788,83 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
>  static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
>  {
>  	struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
> +	DECLARE_BITMAP(pend_map, VGIC_V3_MAX_CPUS) = {};
> +	DECLARE_BITMAP(kick_map, VGIC_V3_MAX_CPUS) = {};

[Severity: Medium]
Does this add unnecessary work to every VM exit when no IRQs migrate?

kvm_vgic_sync_hwstate() calls vgic_prune_ap_list() on every non-nested
GICv2/GICv3 VM exit with IRQs disabled.  Zeroing both 512-bit bitmaps on
the stack and unconditionally running bitmap_empty() and both
for_each_set_bit() loops executes 128 bytes of stack zeroing and three
out-of-line 512-bit scans on every exit even when culling_list is empty.

Guarding the bitmap initialization and dispatch/kick loops with
!list_empty(&culling_list) would keep this off the common exit path.

[ ... ]

> +	for_each_set_bit(i, pend_map, VGIC_V3_MAX_CPUS) {
> +		struct kvm_vcpu *target = kvm_get_vcpu_by_id(vcpu->kvm, i);

[Severity: Medium]
Can kvm_get_vcpu_by_id() fall back to a linear O(N) xarray walk here (and
again in the kick_map loop) with IRQs disabled when userspace creates vCPUs
with sparse or out-of-order vcpu_ids?

Since pend_map and kick_map are local to vgic_prune_ap_list(), indexing
them by dense vcpu->vcpu_idx would allow O(1) kvm_get_vcpu() lookups and
bounding the bitmap scans by atomic_read(&vcpu->kvm->online_vcpus) instead
of VGIC_V3_MAX_CPUS.

[ ... ]

> +		guard(raw_spinlock)(&target->arch.vgic_cpu.ap_list_lock);
>  
> -		raw_spin_lock(&irq->irq_lock);
> -		raw_spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
> -		raw_spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
> +		list_for_each_entry_safe(irq, tmp, &culling_list, ap_list) {
> +			guard(raw_spinlock)(&irq->irq_lock);

[Severity: High]
Can scanning the entire remaining culling_list for each set bit in pend_map
still perform O(N * V) work with hard IRQs disabled and stall remote vCPUs
on ap_list_lock?

An unprivileged guest with vITS can queue up to 57,344 pending LPIs
(INTERRUPT_ID_BITS_ITS is 16) on one vCPU, then retarget one IRQ to each of
vCPUs 0..V-2 and the rest to vCPU V-1 (up to 512 vCPUs).  Nearly all N IRQs
stay on culling_list across every lower-ID iteration, causing ~29M raw
spinlock acquisitions with hard IRQs disabled and risking host hard lockups
or RCU stalls while holding each target's ap_list_lock across unrelated
IRQs.

Even with M evenly distributed migrating IRQs, this regresses baseline's
O(M) migration cost to O(M * V).  In addition, the outer loop has no early
list_empty(&culling_list) break, so if earlier iterations cull the
remaining IRQs it still looks up and locks ap_list_lock for every remaining
stale bit in pend_map.

[ ... ]

> +			/* Catch a potential affinity change */
> +			if (irq_target != target) {
> +				__set_bit(irq_target->vcpu_id, pend_map);
> +				continue;
> +			}

[ ... ]

> +		__clear_bit(i, pend_map);
>  	}
>  
> +	/* Leftovers? Same player shoot again! */
> +	if (unlikely(!list_empty(&culling_list)))
> +		goto retry;

[Severity: High]
Can a multi-vCPU guest livelock the host CPU here indefinitely with hard
IRQs disabled?

Because irq->irq_lock is dropped between target passes and
vgic_mmio_write_irouter() only takes irq->irq_lock to update
irq->target_vcpu, another vCPU can keep flipping an IRQ's target ahead of
the current pass:

CPU0                              CPU1
kvm_vgic_sync_hwstate()
  vgic_prune_ap_list()
    target = vcpu0 (pend_map: 0)
                                  vgic_mmio_write_irouter()
                                    lock(&irq->irq_lock)
                                    irq->target_vcpu = vcpu1
                                    unlock(&irq->irq_lock)
    lock(&irq->irq_lock)
    irq_target != vcpu0
      __set_bit(1, pend_map)
    unlock(&irq->irq_lock)
    __clear_bit(0, pend_map)
    target = vcpu1 (pend_map: 1)
                                  vgic_mmio_write_irouter()
                                    lock(&irq->irq_lock)
                                    irq->target_vcpu = vcpu0
                                    unlock(&irq->irq_lock)
    lock(&irq->irq_lock)
    irq_target != vcpu1
      __set_bit(0, pend_map)
    unlock(&irq->irq_lock)
    __clear_bit(1, pend_map)
  goto retry;                     <-- livelocks with IRQs disabled

Since kvm_arch_vcpu_ioctl_run() calls kvm_vgic_sync_hwstate() with hard
IRQs disabled and vcpu->mode still IN_GUEST_MODE, this blocks host
interrupts and cross-CPU IPIs (such as kvm_flush_remote_tlbs()) and can
trigger hard lockups or RCU stalls.  Bounding the retries and falling back
(for example, returning stubborn IRQs to the source vCPU's ap_list) would
prevent the livelock.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204175740.2153746-1-pbonzini@redhat.com?part=4

      reply	other threads:[~2026-12-04 18:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 17:57 [PATCH 0/4] Fixes for various kvm-arm vulnerabilities Paolo Bonzini
2026-12-04 17:57 ` [PATCH 1/4] KVM: arm64: vgic-its: Bound PINTID during ITE table restore Paolo Bonzini
2026-12-04 18:14   ` sashiko-bot
2026-12-04 17:57 ` [PATCH 2/4] irqchip/gic-v4: Contain VMAPTI vINTID to the expected boundaries Paolo Bonzini
2026-12-04 17:57 ` [PATCH 3/4] KVM: arm64: vgic-its: Add signal checks in scan_its_table() Paolo Bonzini
2026-12-04 18:23   ` sashiko-bot
2026-12-04 17:57 ` [PATCH 4/4] KVM: arm64: vgic: Linearise AP list pruning process Paolo Bonzini
2026-12-04 18:21   ` sashiko-bot [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=sashiko-outbox-250407@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --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