Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2937646rdb; Tue, 13 Feb 2024 01:36:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUPDSHw2LaAhntDlf37rZrxh7TiO3w0VkcZ61wLtoqSyoJhqA10gzDfYlBztY3qZiKpFeFux592xKCyJ6a4wm98uN4kWM05UC34gplV9w== X-Google-Smtp-Source: AGHT+IG+6pkYMuNaJpNXC+F1uJnOU1JdiflyVTTHbpS2OVbMC9jCmcL4SaOjytETWlPe93Yc1FZR X-Received: by 2002:a17:902:ecc1:b0:1d7:b1f:be9e with SMTP id a1-20020a170902ecc100b001d70b1fbe9emr10680036plh.66.1707817001620; Tue, 13 Feb 2024 01:36:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707817001; cv=pass; d=google.com; s=arc-20160816; b=L515ECezHKzReP+GggUTaH5U6z9b2BvfBYAZ5stm7C0JNnXXg3Gb53V3CsJFzqBRRp UGu9ApicdgqtrctFQSB11CpUtGVP32s9Dvo0p3yRGQD/dqNf3Xxu3sxzVsQQySC5hBr3 0zMTk9oiROXKz7yrCHHICjqY9dsrEowm8FmX3dQ9UxHogr1jKG5jN6lufsHhrq3039HM ava8Qm+rp2ZvBGhR4jH+nsw0WYJgo1Jf9k4GnnxmOxkPgZO5ELp60w9O2/wG/CtT4JPs jPgbGMfW8A6o8UrLayhm9j3Lq5suw8zLmnPHE3IBq5WWpz9AxL2etoOTMZcq6ZVvwE+B YR3Q== 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=+sE7IWb8cyeSXhM2SoQx6feQUCncZFMKiDi8i8kQuog=; fh=56ZRa+m+jt7duS/nxKi8+78kR3lN6bTpe8j5bZg9ZFI=; b=OjeoVXLvFYOpnlFCX7LhqHRL6z/m/mzanFS9NS5j7bXnFI91HtuLYAZH0RJJcLJpfe 07T5damCvuKs7L6I1DzFJ3e+mOC1lB/fmPZSIRAeEQpyTo02UCWu6M1fXhwq6kV5TzTA cqJOQZArcey+NkkW2JimUFXhUg06O1vfpj94MnkDS63vAeaDWFgXzwiU+bN64cCjWkCb kAvg0DQjqo/zSGByRN4lNBuptA/NkYXGXNUEIBjjFHBoCI42lPJr7MKuJvOw7uQy5QJa +GMTPfGTLDKgasuNysupskIb7QpUupAy0miX72x1qRpteDb+3+BVzGO1jFkgi+eBYUiD +7HA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=fIbqeW1Z; 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-63214-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63214-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev X-Forwarded-Encrypted: i=2; AJvYcCXfweqLatuz9hXDNm4hbfxuLOmUj5IrFBpCEjoy/qRpZS1D3ZexuAWWFZJrur8XtvHbod3WUyG4cpfbwrD/ar0b42HhLTqoFmyXMsNrLg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id m1-20020a170902db0100b001d936b75f10si1737910plx.267.2024.02.13.01.36.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 01:36:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63214-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=fIbqeW1Z; 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-63214-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63214-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D4A0A2832D3 for ; Tue, 13 Feb 2024 09:36:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 862A442066; Tue, 13 Feb 2024 09:33:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="fIbqeW1Z" Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (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 38FB939AF0 for ; Tue, 13 Feb 2024 09:33:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707816811; cv=none; b=AUfY8G2jaoI/zZAwVL9nVQ09pnVee5P9vmMDGGcOeosEBKBN+kYCLpcIT5ICPdrmlpU7dZH+qhzPJwVplDBGVX/BkF5JGOR136CRn9R1/hZ04jouH35o/++sHc1JEqg8GPBz0sPD62LvdrW5UqNHdlS+wEc5NL55TaGBFF3XvCs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707816811; c=relaxed/simple; bh=0T2ru6MMuxoS0lW+UfmGEtA3Xi3gFhDd6hkRlTBsx60=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Cdf+tNhNKjKiBvX5k0IxNJPymGQ1swQOEpdpPKQFS0SU6MKfeHr27S7OgF+Ouozyd2cTOwpAxgV8RFi+5weaxxK1ztpr8WHxMhIUTPMk7994vh0e2K6n+/muLDOf0qpaD/wj79lrs6XUHuYV8YdlOvOdVp0QZZxjb8+Kp4TAJyI= 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=fIbqeW1Z; arc=none smtp.client-ip=95.215.58.187 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=1707816808; 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=+sE7IWb8cyeSXhM2SoQx6feQUCncZFMKiDi8i8kQuog=; b=fIbqeW1ZtUZ68xNmBl0T2oKjxtonR+jlZ0zOZVCkvVUyb9G8iY3hNpGjdaSwnBzT25HlJu aTq0LUmN+pDqwuA4EwzAsaCusVYF+CmVzZ7Cy6egs0n+XFNiTk5KKapqAkptc3AoQoEGOs MFPfZbxRvSMakWrZbH07OLbdPfSuMRY= 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 v2 09/23] KVM: arm64: vgic: Rely on RCU protection in vgic_get_lpi() Date: Tue, 13 Feb 2024 09:32:46 +0000 Message-ID: <20240213093250.3960069-10-oliver.upton@linux.dev> In-Reply-To: <20240213093250.3960069-1-oliver.upton@linux.dev> References: <20240213093250.3960069-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.43.0.687.g38aa6559b0-goog