From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87C08606A322 for ; Sat, 5 Dec 2026 10:42:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=zAPTsSfT; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XG4Mi1Cp DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=GJ7dMXXFf39JhSpbv3/5ZWaXuGp4HtkBOGY3+mjo+so=; b=zAPTsSfTqjWMRx WOOnGWhfPUTeKxtHhEMVhF6nAK0K/qAaifBpYBHfW1ZL9x4cQlX6Ho06R7CYhXuMZXbg9yv2cLJJB GSEv6oCp2WDm8YnCY4U8D4sWKbH+tvLTIQ1y42+8jkCw1D3S0yOjlWD6cLX8MF5176D4RbCQTa/dd uJE/g7aDplFAIaLJs66/o4B+YNE+2GveuObuvp8VSzlD19N5nmJbjNJraFgVmPLUl7EeyeIxWi+A9 CED02qUEUE3XR5QS+z4ToRf5FYe7/Uo6MOESEX7CAorrVHVqF9CrN8b23v82HV/vC63vJITxkFRtR y1FHctumabZeQRvrALRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZnDe-00000002IKD-0jvj; Sat, 05 Dec 2026 10:42:18 +0000 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZnD3-00000002Huz-19u9 for linux-arm-kernel@lists.infradead.org; Sat, 05 Dec 2026 10:41:44 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id B171A6192B; Sat, 5 Dec 2026 10:41:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DC231F00A25; Sat, 5 Dec 2026 10:41:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796467300; bh=GJ7dMXXFf39JhSpbv3/5ZWaXuGp4HtkBOGY3+mjo+so=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XG4Mi1Cpp54vlydVQzuAfSol2dLPt37uekX9NeuwhjbVWkOnYkTQ9GCzlIGgZQjZ4 /a99O2hCH/jsRZyjcu/5H9W4R1EmqFA+Uoe23FXjZaFoboYKDsaRTBnYkshNCK0F4m Pn+GVr1YJBsYrwaLQZecy91AS1v1OcOMph9OHNutw3fNRdtYhvyJN/psMMF8gpxxpY Yk0QOYQBF9UJ2zHmpahj1fV7VmpmtdwCMR56z2nQ1NGiFtju7lrERFDkFKJI7TauXV pQcqDwVA4znNAdE/BUZfIf9XYjdBC5rl/Sr8FJ+ubi+EG4cFTFDzW10SZCnuZ/KPtb L+2D0Ktn695Zg== Received: from sofa.misterjones.org ([185.219.108.64] helo=valley-girl.lan) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xZnCz-00000007UT8-3xkp; Sat, 05 Dec 2026 10:41:38 +0000 From: Marc Zyngier To: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Steffen Eiden , Joey Gouly , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Fuad Tabba , Paolo Bonzini , stable@vger.kernel.org Subject: [PATCH 6.12.y 5/5] KVM: arm64: vgic: Linearise AP list pruning process Date: Sat, 5 Dec 2026 10:41:31 +0000 Message-ID: <20261205104132.1432014-6-maz@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205104132.1432014-1-maz@kernel.org> References: <20261205104132.1432014-1-maz@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, seiden@linux.ibm.com, joey.gouly@arm.com, suzuki.poulose@arm.com, oupton@kernel.org, yuzenghui@huawei.com, fuad.tabba@linux.dev, pbonzini@redhat.com, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 Reviewed-by: Oliver Upton 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 967d8111642cf..16818433dc112 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -608,6 +608,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 * @@ -619,109 +639,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