From patchwork Wed Feb 21 05:42:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 203938 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp850257dyc; Tue, 20 Feb 2024 21:46:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW/N0mQYaJLjkomDJ3ME0gbg958ZKWKuO/rfMPVijK+imevEOuKY2hsLw8CntrxaZ+F2hyh0AcYUxBrKU+UjAyHyJwOZw== X-Google-Smtp-Source: AGHT+IEpk2hyxlpliuaJm6w9onGMmZWY7sa4o+c2XHIGGrT9DSIje7fB4jlO/84Du1oNPzNptTxA X-Received: by 2002:a17:902:e74d:b0:1db:e600:4585 with SMTP id p13-20020a170902e74d00b001dbe6004585mr10887594plf.19.1708494406781; Tue, 20 Feb 2024 21:46:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708494406; cv=pass; d=google.com; s=arc-20160816; b=I7aG8deu8kt7LE252uSAgRQLDEaiDkFDoZarZtJBVyZ8PBCvgFjW1ZdWbBEzywgWYd qfFdXt9D5ddKo8gOVwzcvHs965COK5NDebBtuAtEQDGEYNeKjRDuyDFAEsK6xw2G3HXH MgDZ+JpIS2tY3YlYgLOIiIQL9bcN4NdjzmSIvqm/wOeeNaUjHHl7gy2x0jBBJq0RH9hm pCasAp719d+n5g28Xo+cTyR7L0CpuWNaoB7AvklN0U3l3PFLITTVE+f0I61qfAJAoaY6 0iVvR7Bn5ehiySGvWyGG2HllOuRx3Wj+ViArzxShQNj3wgGkrbTeh/IbLvtuYEmpl60l ZnLw== 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=BJ4oP9M3Q0IijgdCm1rTjm/2Zq88g2zw/bm3B6utCQw=; fh=kCIhDCwfhVa+1moYcBlLNuY7yX/BfELZCoRk1wSf3jY=; b=g7P9fgKzlSt5xt9zuw7TuE7GKgba4UqubFwxBfyCspecfl3pHAEWC1k74R51IQsFCh BfxPKLGRLISoQuAxUL6KXsSUDzphtqopnx//DSZ85vaTD773tPWRflz2peCe3hRUbFFS QKG4difqFcvsFzV4zFmTC+go2GsREkh8yZlV/IazzvZGxNU43NhvWhhZgY1fkol5upwi Ctbjp0RGY9hBFwuan9IEQe07oq7JaedAh4SfMZdDATbE7wPNC7KmDEw6ji71Au6CYjsF F5EJsqU8fSuwqnN17mF/PveWYdAIAEVEkCyotB6ss3Q2J7KpCyqmRP8L5jP85xxEcBxU R1LQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=UhQFNMtJ; 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-74106-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74106-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id a2-20020a170902ecc200b001da22d9e810si7658948plh.219.2024.02.20.21.46.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 21:46:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74106-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=UhQFNMtJ; 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-74106-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74106-ouuuleilei=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 44D91281214 for ; Wed, 21 Feb 2024 05:46:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8CCCB3D3BA; Wed, 21 Feb 2024 05:43:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="UhQFNMtJ" Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) (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 58A7C3CF6C for ; Wed, 21 Feb 2024 05:43:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708494217; cv=none; b=KPk0fnRD6QW2lI94H2bLGjgGhe793t4vI3/wCUotTyVqW/e+Hb+ILHRkQs6ifd3143Mhw9ub5RTlvlwmYiKc60uZHzOsHLmEpKwDlQ3/LIB5T/bWJiptRnqczq6ozqocWb5ZRoMGY3NyYlawfJ2V+qjYqRld0dpDDnCPV2vRCm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708494217; c=relaxed/simple; bh=w2mKAUpjDefBH8ZRTYVpf6wUaoHL7tLPKvgNGeP/7Pc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZTsEGKeBol5R8edRpHguD+Dyn314/eV+OKyEho6cmk/OBc8s2Lcrdgp4DZ8PXBFe9r5+v6dIpxho1mT0Xsrh/nU9JFKIag/N0GLWViu9Xhwv1lORw7hbjaQR9zQvvsoXZjvh5DOkUqfL0LLpOMITJpahYhF4APrynYq5XU3jA2w= 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=UhQFNMtJ; arc=none smtp.client-ip=91.218.175.174 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=1708494214; 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=BJ4oP9M3Q0IijgdCm1rTjm/2Zq88g2zw/bm3B6utCQw=; b=UhQFNMtJMJzvESlIIp0h2831VVZci4irnCeQlhWnqIopdwDfPiffDEsl1FdQD/dUj82J4M ai73hWEqta6WhHvDHXn6FLl1i57BCdfuXEx6luFE3soK+eVjBFfYjSCh9j3BoY4wgq0fDC cziW4XfD063WKUDH4KIEB9TmWsv5rVM= 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, Eric Auger , Oliver Upton Subject: [PATCH v4 08/10] KVM: arm64: vgic: Rely on RCU protection in vgic_get_lpi() Date: Wed, 21 Feb 2024 05:42:51 +0000 Message-ID: <20240221054253.3848076-9-oliver.upton@linux.dev> In-Reply-To: <20240221054253.3848076-1-oliver.upton@linux.dev> References: <20240221054253.3848076-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 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791486231186140456 X-GMAIL-MSGID: 1791486231186140456 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 3fedc58e663a..76abf3d946fe 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -63,15 +63,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);