From patchwork Tue Feb 13 09:32:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 200292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp424666dyb; Tue, 13 Feb 2024 01:37:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6UIfIOgQrHFmajjNduNM4Ji6262hPo/Q+qU/rYsQQK37VtbgPpZOM5jXrYXIy5GM2z5LR X-Received: by 2002:aa7:d951:0:b0:561:aa3:f9e6 with SMTP id l17-20020aa7d951000000b005610aa3f9e6mr6238030eds.15.1707817039392; Tue, 13 Feb 2024 01:37:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707817039; cv=pass; d=google.com; s=arc-20160816; b=cbk4WNkI8Rr4ZJiJrsUUzTBngi7yy+h3fgVdX53e4tVzm1AsDWinE/2GgKcDXvB89E zvoznLM9OidZjEvmXMQ71F35fZzYjW7PigjcA/h92LpiLGxR1Hr7+j84tdBbbVcG3Eqc Pu54KRJSZfUWs3Vg4Kdod28OeKo2Is6O02W9jQu8/caZXU9oA7kv1vKPmi+TFi1cSwrV dx3gr3dpEz9+PAjMjXcNViWwF5EmyUV+AzrAtEgXD9QfToHf0VxR2KJ1O/BYcmSZVHoo nYMP3tQ+APo2NSJvDGIzrJj9LyAIaiecXTxa+cY/Jq/F8BiU1SgWiNnyI1lUPml6ioJ5 k1KQ== 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=Tw7Zntu0i50/45BH/WcBGp2LvuWwU+qxBI8D9y8HogY=; fh=v12+Dl3EATS/WNwhqufzzBMByhnp9nvJItkZ6yyZR2c=; b=yj92+Y92T8vS0TNLRZMwHCskujTvCPYkLc5UmcgKmRy2CZU0OaQHJ6NW22eFW8w1GH i2jMjr2UDBEuYH1aVvE0kZ4sVNKoBTpwCl3CDo3vfQ7f5IiVCI+dy/LsDr/ILz7v6y7C 1o+dEtWakPNB0b0W8/JzAUJTitatrIw8ruQhhM4+1o54eRlHXDroHk32pw7saggp4N8d 54+kMyG9stcLy9pd8I6FQ7YyTLtaiKG+7ce00o36EqGqTjMLOnN/QpUExLFd9NVMTmLs cYPyuK6L+MvgDkJEMaozmQhgieTBcqQB8lHbN02DW/QETD9r7yBFS4G2KU0D3CNQ7P2Y 6SCg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=jazLFqqx; 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-63215-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63215-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev X-Forwarded-Encrypted: i=2; AJvYcCURCN3UpGegK7T+LpGN79StNHQQlsj4VI1ND75lnhmoUuzBJUJMPflPfJCWTKfLbLqnDl0l5uFdjVFDMOL57klsBwwNiw== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r6-20020a05640251c600b00561d07b6c4csi1226706edd.657.2024.02.13.01.37.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 01:37:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63215-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=jazLFqqx; 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-63215-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63215-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 08D781F2340A for ; Tue, 13 Feb 2024 09:37:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 64EC7224E7; Tue, 13 Feb 2024 09:33:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="jazLFqqx" Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.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 1BD1B3FB39 for ; Tue, 13 Feb 2024 09:33:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707816813; cv=none; b=tvPZmk/k9Geyn6b159DR7bIZDLFtibP/T2xZNZlHTfNf0JSBzkK26UN9lg0U5YDyYWSgveLh6pcC/QecdG5GWNpzzEtRFnQ5GfHZ/5c4pBCbWPbz1NWwUcaYRTkTwUtvyE/HIYmeA4Gf26ofQ3GpIvSPWYC1/YCftxgobqpfFXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707816813; c=relaxed/simple; bh=vk23VxPinLQAfnq8uTzaCqEThe7jwZl09foYlDKigqw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YCu0ARBnyL/Wes+Jz1XEEWihW9gk7cTJDoebUqL2EubS5Tq0QLVgr/qFr3QdU6aUDjNYgBObtEqdNvHfwqGWYOaS2BE8XWOI8DejU5vYc+olL+/4uc9DuGxo5EigonCPB2MWoEEX7cgy8PSBlF8mxnokHXt38RBfYpw1dXTijl8= 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=jazLFqqx; arc=none smtp.client-ip=95.215.58.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=1707816810; 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=Tw7Zntu0i50/45BH/WcBGp2LvuWwU+qxBI8D9y8HogY=; b=jazLFqqxgtjzkcxj1dc01Z4YBDKx8CvNw9zqpJpY6FjkPxtrbnhpejwlDxidZ7Kq8tXaXb 91T712fapp4jgvLZo/42Dhp1S3DBz/2cpv3OwOdQj0XnYmVZ0aaLyrJYTAA/3WZmPadSn2 l5iY+4M2h6Rg4VWGdqFUqZVxtbdv30g= 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 10/23] KVM: arm64: vgic: Ensure the irq refcount is nonzero when taking a ref Date: Tue, 13 Feb 2024 09:32:47 +0000 Message-ID: <20240213093250.3960069-11-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 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790775960357556837 X-GMAIL-MSGID: 1790775960357556837 It will soon be possible for get() and put() calls to happen in parallel, which means in most cases we must ensure the refcount is nonzero when taking a new reference. Switch to using vgic_try_get_irq_kref() where necessary, and document the few conditions where an IRQ's refcount is guaranteed to be nonzero. Signed-off-by: Oliver Upton --- arch/arm64/kvm/vgic/vgic-its.c | 18 ++++++++---------- arch/arm64/kvm/vgic/vgic.c | 3 ++- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic-its.c b/arch/arm64/kvm/vgic/vgic-its.c index 048226812974..6b9634cec77f 100644 --- a/arch/arm64/kvm/vgic/vgic-its.c +++ b/arch/arm64/kvm/vgic/vgic-its.c @@ -75,18 +75,11 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, * check that we don't add a second list entry with the same LPI. */ oldirq = xa_load(&dist->lpi_xa, intid); - if (oldirq) { + if (vgic_try_get_irq_kref(oldirq)) { /* Someone was faster with adding this LPI, lets use that. */ kfree(irq); irq = oldirq; - /* - * This increases the refcount, the caller is expected to - * call vgic_put_irq() on the returned pointer once it's - * finished with the IRQ. - */ - vgic_get_irq_kref(irq); - goto out_unlock; } @@ -610,8 +603,8 @@ static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db, raw_spin_lock_irqsave(&dist->lpi_list_lock, flags); irq = __vgic_its_check_cache(dist, db, devid, eventid); - 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); @@ -660,6 +653,11 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, __vgic_put_lpi_locked(kvm, cte->irq); } + /* + * The irq refcount is guaranteed to be nonzero while holding the + * its_lock, as the ITE (and the reference it holds) cannot be freed. + */ + lockdep_assert_held(&its->its_lock); vgic_get_irq_kref(irq); cte->db = db; diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c index 128ae53a0a55..2a288d6c0be7 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -394,7 +394,8 @@ bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq, /* * Grab a reference to the irq to reflect the fact that it is - * now in the ap_list. + * now in the ap_list. This is safe as the caller must already hold a + * reference on the irq. */ vgic_get_irq_kref(irq); list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);