Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407080; b=SQlZPxU205EQNK2DERbfcgVLgKD+j4BRmqa6/EV1rDCjeOTrkry8qH5LrQswkGtjMH+a fLmZpgiq08szwwPvScZkDzSv9STMjz5iRms94fodCgAebGyCFZv9Kkm84eqHRimR9eo7p aZOt29YZPNZoYq9ziTURt0IbazudxFTOprLu4DHBy9rLFDneldw33zN9LbDrpcOHlPnui /cZfBB4TFRM8H8ff+lHqKUpVoQzgSjPJ38t3unOIj3MhaDndK5KjdqIm+CMmEQz/TEBpR YBF9e77m6UA1HvEsIgP8SBS5UzOjfxdVaSbFREfNX5lPfeSlMJiYpkCrEhqM1NgabHA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407080; h=DMARC-Filter:DKIM-Signature:X-MC-Rep:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version:X-Scanned-By: X-Mimecast-MFC-PROC-ID:X-Mimecast-Originator: Content-Transfer-Encoding:content-type; bh=tpCDLXD/O3EwEXqDFVdsklW4pz1SJs2KbPATwGGrUOM=; b=HfraqtcRkbZmmDkvGKo3xDm22g37KRNKiVStHF38Gd16pe68nyvIsgl9k44ssD/8xHiR NCrvBH1YrKiqJNRU4sIokTWd7cPAc1vJdOJhRUssn1WH8D9ddhZ+JPwlhZgbShJmK5hyD +KB1b30ZF6JRLzgcVnnxNvNORgPGpcWFSEYiGR/PiSWzmZXUhqHL8tx5C/xsotYTRBu5S QHC1f+BfNSA25pWskhrzi+QO2nHKjXu6ydld5ROzkrJoR0wGsMaW35l3GIUuvPNQviNNo qicCPnRf8CVpF3knbmR0h1ZKPkwM2DHv7E+2vPBK/VomBuJDRGuLvAy2Ix95Jzrr31Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XFx4L4v1; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 B562F606A33E for ; Fri, 4 Dec 2026 17:57:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B562F606A33E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XFx4L4v1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tpCDLXD/O3EwEXqDFVdsklW4pz1SJs2KbPATwGGrUOM=; b=XFx4L4v1Qx0VgouQ6F8ZSFN4N+yNVhxyH3XZktSkkBEnUYx2lFJFnxt5KU8EoX51K7KHBj cYqOzxMS85YieRJAkYafCCss+nNoX8RgQvHI1/3KlhPDxK07zv4o4MWOS5ZiRvGJ4+tHBU HfU6oEoR7Vzs8+OXeV8zhiT2on6KGYc= X-MC-Rep: binary re single Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-22-Zpoov0mSPuqlkOA0wdtciA-1; Fri, 4 Dec 2026 17:57:54 +0000 X-MC-Unique: Zpoov0mSPuqlkOA0wdtciA-1 X-Mimecast-MFC-AGG-ID: Zpoov0mSPuqlkOA0wdtciA_1796407073 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6726D18AE6E7; Fri, 4 Dec 2026 17:57:53 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8F1F718019CB; Fri, 4 Dec 2026 17:57:51 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: maz@kernel.org, oupton@kernel.org, kvmarm@lists.linux.dev, stable@vger.kernel.org Subject: [PATCH 4/4] KVM: arm64: vgic: Linearise AP list pruning process Date: Fri, 4 Dec 2026 18:57:40 +0100 Message-ID: <20261204175740.2153746-5-pbonzini@redhat.com> In-Reply-To: <20261204175740.2153746-1-pbonzini@redhat.com> References: <20261204175740.2153746-1-pbonzini@redhat.com> X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-MFC-PROC-ID: WYhTY8IWvLTITKzAzvbck8QAmB_55paC6F_-i_Kzjg4_1796407073 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true From: Marc Zyngier 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 Reviewed-by: Oliver Upton Cc: stable@vger.kernel.org --- arch/arm64/kvm/vgic/vgic.c | 175 ++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 88 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c index bea848c93a99..a7e3e1a8a64f 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -754,6 +754,29 @@ 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; + + if (vgic_put_irq_norelease(kvm, irq)) + target = ERR_PTR(-ENOENT); + } + + return target; +} + /** * vgic_prune_ap_list - Remove non-relevant interrupts from the list * @@ -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) = {}; + struct kvm_vcpu *irq_target; struct vgic_irq *irq, *tmp; bool deleted_lpis = false; + 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); - + guard(raw_spinlock)(&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_ref 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. - */ - deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq); - continue; - } - - if (target_vcpu == vcpu) { - /* We're on the right CPU */ - raw_spin_unlock(&irq->irq_lock); + /* + * 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)) { + deleted_lpis |= IS_ERR(irq_target); 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_ref(irq); - - 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); - - deleted_lpis |= vgic_put_irq_norelease(vcpu->kvm, irq); - - if (target_vcpu_needs_kick) { - kvm_make_request(KVM_REQ_IRQ_PENDING, target_vcpu); - kvm_vcpu_kick(target_vcpu); - } - - goto retry; + __set_bit(irq_target->vcpu_id, pend_map); + list_move_tail(&irq->ap_list, &culling_list); } + 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) { + guard(raw_spinlock)(&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)) { + deleted_lpis |= IS_ERR(irq_target); + continue; + } + /* Catch a potential affinity change */ + if (irq_target != target) { + __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); + } + + __clear_bit(i, pend_map); + } + + /* Leftovers? Same player shoot again! */ + if (unlikely(!list_empty(&culling_list))) + goto retry; + /* * Fix the last_lr_irq refcount which was obtained while * populating the LRs. This can also result in the LPI being @@ -879,10 +876,12 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) *host_data_ptr(last_lr_irq) = NULL; } - raw_spin_unlock(&vgic_cpu->ap_list_lock); - if (unlikely(deleted_lpis)) vgic_release_deleted_lpis(vcpu->kvm); + + for_each_set_bit(i, kick_map, VGIC_V3_MAX_CPUS) + kvm_make_request_and_kick(KVM_REQ_IRQ_PENDING, + kvm_get_vcpu_by_id(vcpu->kvm, i)); } static void vgic_fold_state(struct kvm_vcpu *vcpu) -- 2.56.1