From patchwork Wed Feb 21 05:42:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 203940 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp850515dyc; Tue, 20 Feb 2024 21:47:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWMDDQXk84/vdUwczIUUQehdIlNjeNRORm+V39XuW/Cpnk9xaP1mZQe6KdbRZwfGW72CkZBBhj2VMyN2w8z+oTGYH126A== X-Google-Smtp-Source: AGHT+IExwuFnynA913JIj0AElII8dwLneqfDrwapGdQqenzsamkqgZDVGp+T+MlF3nsfsa5OqSV4 X-Received: by 2002:a05:6a21:9101:b0:19e:ccb2:fd80 with SMTP id tn1-20020a056a21910100b0019eccb2fd80mr16356825pzb.8.1708494472176; Tue, 20 Feb 2024 21:47:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708494472; cv=pass; d=google.com; s=arc-20160816; b=Kb+QowBqbpzw6fLwoytEoaBesNao3K5U+tYOVI2uSJ2PUBBND07XUnusSmYxzZZAvU +3Kr+kvgzP9wMsOD2rvpKGIYkw78clYLKhV3qjbri3lCGpPATeZayBNJJufK5Ozpa0F/ 4mb1QHJA0JLH7JjiqcHQFqkhRVyXBctv1G3ZMVF2g+Vjcyf4LCJFsUqzxMHkzUkzyoUG xQ4UmyoYij03peHSxGg//I5poB4rFfnVMk0bPe6eyrC9UrD4x/mVdGym06dJZpPH6JUk QyCyRbWgWiCVfphi/zuByYnV4QYEpAuhXOFj2mP2XhNV8xNJiHiVrdNJNwVGqjxSaDhe R31w== 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=QeMz6ixCWY/1TUllIvS9OwAED3X7tPlGIFKged/im1A=; fh=kCIhDCwfhVa+1moYcBlLNuY7yX/BfELZCoRk1wSf3jY=; b=TFUGIowZNhYs3UIsT6aZVatK1WBJLe9UiJKrIGC4gCosKwLj1/k9ZnsaF5hVdmLJl0 qhmQ2sncMl8jHhoxJ/tjLr85MCeWtlUf+ZmihhTRmFJRC/thG3946OYHZIjo1SAwaqfl E2BpWmrhIX4vczAr0RGgrLj3f7FO0b3xlwS+h3Qpf2TYsN76bagGVydoAXW+yl1jJoPF gIqOEEGFQ0/FjYU/s2AJmAJOT6UcD9Ok78h3yma1MkGg9Bmi63sLvAxrgkRk3Hjloh9A wYSLaxsgdOOyovOMAR53Mor0xMdifOocqB8AaMl6HSU2+z6R4N82Gb7FzT0CbU3D5FmH R8nQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oJYMuUDa; 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-74107-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74107-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id np6-20020a17090b4c4600b0029a1aaefe30si362159pjb.188.2024.02.20.21.47.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 21:47:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74107-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oJYMuUDa; 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-74107-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74107-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id D14ACB23303 for ; Wed, 21 Feb 2024 05:46:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C90533D578; Wed, 21 Feb 2024 05:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="oJYMuUDa" Received: from out-176.mta0.migadu.com (out-176.mta0.migadu.com [91.218.175.176]) (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 5F2E23D0C6 for ; Wed, 21 Feb 2024 05:43:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708494220; cv=none; b=nts3WQmI7Hj10QJ6wCKaxaQ+hjeZ6bfINOQG8WNOHH3GYfGC6aFZNE47RoQpsucsRgnlbabHlFgRLGxOmTR+yvRBHbbs72Q2kEwIXwxQaE7u0rlMR2E4iSrE8LNfBGtiUD4oVcv5oaIjW3qyC8Bs/PWiLoiw6TFoaAG5vZkZjGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708494220; c=relaxed/simple; bh=ohZ5HNh3+AbhZtUPEYzsTEwYsRO0b4zg7BkWRCA1M6o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LX6dUhjfgPEjgsnJRgOhiwDVvh7Nmfr3pJVG5uhLSZSdNx3nFjOdUtCyz6FQMF5dixsjDMtElFmrZl+plircMN6wEXUDHTGaPVyd2ET8bvWRU/rlX8DSUDIfo7pwQU1YVu/Lwr+BCIOyOShSluQQ1a0XwtgLj9V61S6WmAOmevU= 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=oJYMuUDa; arc=none smtp.client-ip=91.218.175.176 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=1708494216; 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=QeMz6ixCWY/1TUllIvS9OwAED3X7tPlGIFKged/im1A=; b=oJYMuUDa4lorjzjLyP4W/y7ThUhnzjPTg1eB4MUsxt75hClMvcOy7W5XhtgPpwI/Z4vK9f Q2cvCYDT3VIRIO3EH6bfRd2UeplzRCqfbqNE5BAQq8jYAUnSfYsZMCdvCrM4hYZDZyHB3Q QI7HCZ7WCRhcFu35cRe8lW/4RIxoxpY= 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 09/10] KVM: arm64: vgic: Ensure the irq refcount is nonzero when taking a ref Date: Wed, 21 Feb 2024 05:42:52 +0000 Message-ID: <20240221054253.3848076-10-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: 1791486299573923559 X-GMAIL-MSGID: 1791486299573923559 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 0be3c33676c3..dad6f0ee7c49 100644 --- a/arch/arm64/kvm/vgic/vgic-its.c +++ b/arch/arm64/kvm/vgic/vgic-its.c @@ -74,18 +74,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; } @@ -611,8 +604,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); @@ -658,6 +651,11 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its, if (cte->irq) __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 76abf3d946fe..df9e1aa1956c 100644 --- a/arch/arm64/kvm/vgic/vgic.c +++ b/arch/arm64/kvm/vgic/vgic.c @@ -395,7 +395,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);