Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: kvmarm@lists.linux.dev, kvm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org
Cc: Steffen Eiden <seiden@linux.ibm.com>,
	Joey Gouly <joey.gouly@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Oliver Upton <oupton@kernel.org>,
	Zenghui Yu <yuzenghui@huawei.com>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	Paolo Bonzini <pbonzini@redhat.com>,
	stable@vger.kernel.org
Subject: [PATCH 6.1.y 5/5] KVM: arm64: vgic: Linearise AP list pruning process
Date: Sat,  5 Dec 2026 10:41:44 +0000	[thread overview]
Message-ID: <20261205104145.1432112-6-maz@kernel.org> (raw)
In-Reply-To: <20261205104145.1432112-1-maz@kernel.org>

commit 504fa43c028d491fd772eb9c264d56bd68a75460 upstream.

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.

[maz: locking/freeing adapted to the older code base]
Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Oliver Upton <oupton@kernel.org>
Cc: stable@vger.kernel.org
---
 arch/arm64/kvm/vgic/vgic.c | 170 +++++++++++++++++++------------------
 1 file changed, 87 insertions(+), 83 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c
index 7c49b113b1f60..a5c63b7f59353 100644
--- a/arch/arm64/kvm/vgic/vgic.c
+++ b/arch/arm64/kvm/vgic/vgic.c
@@ -610,6 +610,26 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)
 	return ret;
 }
 
+/*
+ * This helper checks the target *and* does the laundry, nuking the irq off
+ * the ap_list when necessary, and doing the refcount drop on LPIs. -ENOENT
+ * is returned when LPI release is warranted.
+ */
+static struct kvm_vcpu *vgic_predict_target(struct kvm *kvm, struct vgic_irq *irq)
+{
+	struct kvm_vcpu *target;
+
+	lockdep_assert_held(&irq->irq_lock);
+
+	target = vgic_target_oracle(irq);
+	if (!target) {
+		list_del(&irq->ap_list);
+		irq->vcpu = NULL;
+	}
+
+	return target;
+}
+
 /**
  * vgic_prune_ap_list - Remove non-relevant interrupts from the list
  *
@@ -621,109 +641,93 @@ 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) = {};
+	struct kvm_vcpu *irq_target;
 	struct vgic_irq *irq, *tmp;
+	LIST_HEAD(culling_list);
+	int i;
 
 	DEBUG_SPINLOCK_BUG_ON(!irqs_disabled());
 
-retry:
 	raw_spin_lock(&vgic_cpu->ap_list_lock);
 
 	list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
-		struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
-		bool target_vcpu_needs_kick = false;
-
 		raw_spin_lock(&irq->irq_lock);
-
 		BUG_ON(vcpu != irq->vcpu);
 
-		target_vcpu = vgic_target_oracle(irq);
-
-		if (!target_vcpu) {
-			/*
-			 * We don't need to process this interrupt any
-			 * further, move it off the list.
-			 */
-			list_del(&irq->ap_list);
-			irq->vcpu = NULL;
-			raw_spin_unlock(&irq->irq_lock);
-
-			/*
-			 * This vgic_put_irq call matches the
-			 * vgic_get_irq_kref in vgic_queue_irq_unlock,
-			 * where we added the LPI to the ap_list. As
-			 * we remove the irq from the list, we drop
-			 * also drop the refcount.
-			 */
-			vgic_put_irq(vcpu->kvm, irq);
-			continue;
-		}
-
-		if (target_vcpu == vcpu) {
-			/* We're on the right CPU */
+		/*
+		 * We skip the processing of an irq in a few cases:
+		 * - the irq is on the correct target: nothing to do.
+		 * - the target is NULL: vgic_predict_target() will have
+		 *   culled the irq from the ap_list.
+		 * - we get an error back: the LPI has been deleted.
+		 */
+		irq_target = vgic_predict_target(vcpu->kvm, irq);
+		if (irq_target == vcpu || IS_ERR_OR_NULL(irq_target)) {
 			raw_spin_unlock(&irq->irq_lock);
+			if (!irq_target)
+				vgic_put_irq(vcpu->kvm, irq);
 			continue;
 		}
 
 		/*
-		 * This interrupt looks like it has to be migrated,
-		 * make sure it is kept alive while locks are dropped.
+		 * Snapshot the vcpus to visit (more may be added later),
+		 * and move the IRQ onto the culling list.
 		 */
-		vgic_get_irq_kref(irq);
-
+		__set_bit(irq_target->vcpu_id, pend_map);
+		list_move_tail(&irq->ap_list, &culling_list);
 		raw_spin_unlock(&irq->irq_lock);
-		raw_spin_unlock(&vgic_cpu->ap_list_lock);
-
-		/*
-		 * Ensure locking order by always locking the smallest
-		 * ID first.
-		 */
-		if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
-			vcpuA = vcpu;
-			vcpuB = target_vcpu;
-		} else {
-			vcpuA = target_vcpu;
-			vcpuB = vcpu;
-		}
-
-		raw_spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
-		raw_spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
-				      SINGLE_DEPTH_NESTING);
-		raw_spin_lock(&irq->irq_lock);
-
-		/*
-		 * If the interrupt is still ours and its affinity has
-		 * been preserved, move it around. Otherwise, it means
-		 * things have changed while the interrupt was unlocked
-		 * (it may even have been taken off the list with its
-		 * affinity left untouched), and we need to replay this.
-		 *
-		 * In all cases, we cannot trust the list not to have
-		 * changed, so we restart from the beginning.
-		 */
-		if (irq->vcpu == vcpu && target_vcpu == vgic_target_oracle(irq)) {
-			struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
-
-			list_del(&irq->ap_list);
-			irq->vcpu = target_vcpu;
-			list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
-			target_vcpu_needs_kick = true;
-		}
-
-		raw_spin_unlock(&irq->irq_lock);
-		raw_spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
-		raw_spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
-
-		vgic_put_irq(vcpu->kvm, irq);
-
-		if (target_vcpu_needs_kick) {
-			kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu);
-			kvm_vcpu_kick(target_vcpu);
-		}
-
-		goto retry;
 	}
 
 	raw_spin_unlock(&vgic_cpu->ap_list_lock);
+
+retry:
+	BUG_ON(bitmap_empty(pend_map, VGIC_V3_MAX_CPUS) != list_empty(&culling_list));
+
+	for_each_set_bit(i, pend_map, VGIC_V3_MAX_CPUS) {
+		struct kvm_vcpu *target = kvm_get_vcpu_by_id(vcpu->kvm, i);
+
+		guard(raw_spinlock)(&target->arch.vgic_cpu.ap_list_lock);
+
+		list_for_each_entry_safe(irq, tmp, &culling_list, ap_list) {
+			raw_spin_lock(&irq->irq_lock);
+
+			/*
+			 * Check again, now that we hold all the locks.  If
+			 * the irq has been moved again, leave it for later.
+			 */
+			irq_target = vgic_predict_target(vcpu->kvm, irq);
+			if (IS_ERR_OR_NULL(irq_target)) {
+				raw_spin_unlock(&irq->irq_lock);
+				vgic_put_irq(vcpu->kvm, irq);
+				continue;
+			}
+			/* Catch a potential affinity change */
+			if (irq_target != target) {
+				raw_spin_unlock(&irq->irq_lock);
+				__set_bit(irq_target->vcpu_id, pend_map);
+				continue;
+			}
+
+			irq->vcpu = target;
+			list_move_tail(&irq->ap_list, &target->arch.vgic_cpu.ap_list_head);
+			__set_bit(target->vcpu_id, kick_map);
+			raw_spin_unlock(&irq->irq_lock);
+		}
+
+		__clear_bit(i, pend_map);
+	}
+
+	/* Leftovers? Same player shoot again! */
+	if (unlikely(!list_empty(&culling_list)))
+		goto retry;
+
+	for_each_set_bit(i, kick_map, VGIC_V3_MAX_CPUS) {
+		irq_target = kvm_get_vcpu_by_id(vcpu->kvm, i);
+		kvm_make_request(KVM_REQ_IRQ_PENDING, irq_target);
+		kvm_vcpu_kick(irq_target);
+	}
 }
 
 static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
-- 
2.47.3


      parent reply	other threads:[~2026-12-05 10:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 10:41 [PATCH 6.1.y 0/5] KVM/arm64 stable fixes Marc Zyngier
2026-12-05 10:41 ` [PATCH 6.1.y 1/5] KVM: arm64: vgic-its: Stop the VM when handling MOVALL Marc Zyngier
2026-12-05 10:41 ` [PATCH 6.1.y 2/5] KVM: arm64: vgic-its: Bound PINTID during ITE table restore Marc Zyngier
2026-12-05 10:41 ` [PATCH 6.1.y 3/5] irqchip/gic-v4: Contain VMAPTI vINTID to the expected boundaries Marc Zyngier
2026-12-05 10:41 ` [PATCH 6.1.y 4/5] KVM: arm64: vgic-its: Add signal checks in scan_its_table() Marc Zyngier
2026-12-05 10:41 ` Marc Zyngier [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=20261205104145.1432112-6-maz@kernel.org \
    --to=maz@kernel.org \
    --cc=fuad.tabba@linux.dev \
    --cc=joey.gouly@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seiden@linux.ibm.com \
    --cc=stable@vger.kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=yuzenghui@huawei.com \
    /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