Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1293956rdb; Fri, 16 Feb 2024 10:54:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV23hu3nOfkzBOjqj+8fH2vHU6uAbY5TwQW+0KtTftWT0oUzpjOBgbWdGgMNZxoNxjqV69+2UoqftPkuR8CSNKVSTQNpe/rdfXlGGPfXw== X-Google-Smtp-Source: AGHT+IFCltPnHvSzoKiG3TndmVMcOBnCzzEfKMMOjUwHKazKbaxGqh2zAUgAkIJRb1JBPTaOJd7b X-Received: by 2002:a62:d40b:0:b0:6e1:399b:fac3 with SMTP id a11-20020a62d40b000000b006e1399bfac3mr2686254pfh.25.1708109660421; Fri, 16 Feb 2024 10:54:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708109660; cv=pass; d=google.com; s=arc-20160816; b=jh/fRsH8iLosmFZALcmE+x9zvRkWCznhkmQg3Lci9rFRWzr+U9t61PL5Mi6qO9fjzC GrBjUgRhS1X846XZVtY5c+zAOyU23+JmQ8qG82QMG5HzkIvYHhOop2HKAHepDJxX12x3 nt0MrCwWDTQ7xdb4jqTxON8m9P7xf30qSkL32C7ze0usm7uEaKnNt06+0AdgTWBJBxm9 M43cBy05Ox9Ju1ieUP6KNmO8SCAOclAZblQr2VpTMztplje45HdLB+bi0ejGdfIx56cx qGmMta6J8/jMOowhKEY5AcfrXihdDFnM+0I5arK9kcGZylPcjvaVz3ZGDkYZw/ruyNBh RUgw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cQF8jmMEEUM2DwWlHNkDBl2apuHpFelpH7G2Kk29lho=; fh=OGIcGdyoke6ohfCedcQQ3UJr3JDRxZcQDHVG7x10Yg0=; b=U70J8HCKOTiy5VGGic7I31kk/dT6JObFeBjKwi4mtTtdQTjL4D5Ba07guSCcIjRwfN A5kX+FM0ikVNMOvWdq4azhlnPfGC7knU99LH6ZGnG98V0TVGZx/td4IOEF7zv7JKYwdT EbCzDQDrH+6wcaul11IQuic7W3HVDKL+IKOLgl+MAeyo0j4jsDhTB+g//CXeKW6VJpLM AovDlPI/1hU+MivRda0mZQhMMn8+yF25Eku0dtjF45tnO7uwspNXiBtwrAfcYRdamUV/ ZoIb2f0pysxRBtNjaSjcEY0vwfQ/6X2DnAHgCPhStyjr+tydy+H9CYJNZMOoGBzaTn+t NMuw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oTBnQU5q; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-69189-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69189-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id y27-20020a637d1b000000b005cef24b3015si290144pgc.219.2024.02.16.10.54.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 10:54:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69189-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oTBnQU5q; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-69189-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69189-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id E22CAB22C79 for ; Fri, 16 Feb 2024 18:44:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E2501482E3; Fri, 16 Feb 2024 18:42:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="oTBnQU5q" Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88A5814690E for ; Fri, 16 Feb 2024 18:42:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708108944; cv=none; b=rdsO/7QQQqWPXrwfihC+eVdikwxLlpQRc4QEqH5lwSi0u2XfPJPFBC3diZqJracuR6E+SRzhTzkBCIKIVgkJ4nJOfMhJf56AxXFj2YFIJ7PmGtRLPj9NWDPky8wA4vHtp58c2JAZL3a9/CMt3KtJhkhwZyS6HsIU9v0EVkmK2kw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708108944; c=relaxed/simple; bh=SeNrU4ebThHySloZ/TR+kB1GC+oWuzUM+5Ajy3ISOQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uIJJA2tSUsE4yRnAmUnCWGJgET08+hqZe6UJLJjm26yUErajbW3EDt4AeuBpeknwi6Fhx6wt0ayZnP4LWpBQtCJKSzkMG316ZKKRf0T5VebTUgSHg2FqjXzR19CYt8SUfhnKpanF1vDXiPIftRNYzlSYa2sLk3zMW6fu/neLYEs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=oTBnQU5q; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1708108941; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cQF8jmMEEUM2DwWlHNkDBl2apuHpFelpH7G2Kk29lho=; b=oTBnQU5qPGkYHCizl8J+qjyAehCVu38cmLrtpEEygEROkm0VoerNZhquN0IIRIcY5RnvjN 6ANsbJ6rbKZCeF8F1yno7ZklnqkDwLYGHWg/qWlVXBtXCfQno+URKfRUqvNpbp9Afxhyqc 7TOCUUKhqw1uKfu2420fNRxQiMnKuvc= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Suzuki K Poulose , Zenghui Yu , linux-kernel@vger.kernel.org, Oliver Upton Subject: [PATCH v3 08/10] KVM: arm64: vgic: Rely on RCU protection in vgic_get_lpi() Date: Fri, 16 Feb 2024 18:41:51 +0000 Message-ID: <20240216184153.2714504-9-oliver.upton@linux.dev> In-Reply-To: <20240216184153.2714504-1-oliver.upton@linux.dev> References: <20240216184153.2714504-1-oliver.upton@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Stop acquiring the lpi_list_lock in favor of RCU for protecting the read-side critical section in vgic_get_lpi(). In order for this to be safe, we also need to be careful not to take a reference on an irq with a refcount of 0, as it is about to be freed. Signed-off-by: Oliver Upton --- arch/arm64/kvm/vgic/vgic.c | 9 ++++----- arch/arm64/kvm/vgic/vgic.h | 14 +++++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c index be3ed4c5e1fa..128ae53a0a55 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -62,15 +62,14 @@ static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid) { struct vgic_dist *dist = &kvm->arch.vgic; struct vgic_irq *irq = NULL; - unsigned long flags; - raw_spin_lock_irqsave(&dist->lpi_list_lock, flags); + rcu_read_lock(); irq = xa_load(&dist->lpi_xa, intid); - if (irq) - vgic_get_irq_kref(irq); + if (!vgic_try_get_irq_kref(irq)) + irq = NULL; - raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags); + rcu_read_unlock(); return irq; } diff --git a/arch/arm64/kvm/vgic/vgic.h b/arch/arm64/kvm/vgic/vgic.h index 8d134569d0a1..f874b9932c5a 100644 --- a/arch/arm64/kvm/vgic/vgic.h +++ b/arch/arm64/kvm/vgic/vgic.h @@ -220,12 +220,20 @@ void vgic_v2_vmcr_sync(struct kvm_vcpu *vcpu); void vgic_v2_save_state(struct kvm_vcpu *vcpu); void vgic_v2_restore_state(struct kvm_vcpu *vcpu); -static inline void vgic_get_irq_kref(struct vgic_irq *irq) +static inline bool vgic_try_get_irq_kref(struct vgic_irq *irq) { + if (!irq) + return false; + if (irq->intid < VGIC_MIN_LPI) - return; + return true; - kref_get(&irq->refcount); + return kref_get_unless_zero(&irq->refcount); +} + +static inline void vgic_get_irq_kref(struct vgic_irq *irq) +{ + WARN_ON_ONCE(!vgic_try_get_irq_kref(irq)); } void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu); -- 2.44.0.rc0.258.g7320e95886-goog