Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2168991rdb; Tue, 20 Feb 2024 21:46:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWAFI2Z1nLlSrjyFztKAjzHJckMLQcL+x7JLmWuQee51o+lcj5ItUSarxWCUlUsJ3bDvqHcXqkQfpMkVcTCiokI5cpuUso1dR4cfkmv9A== X-Google-Smtp-Source: AGHT+IEO8CfQ1IGxQ5C3QIHIZGLQzRbWUQEf9slDBEyHGp6kgmR8AXLmQohl+fKUUeDzqwMPHMmF X-Received: by 2002:a05:622a:514:b0:42d:a750:bd with SMTP id l20-20020a05622a051400b0042da75000bdmr19153268qtx.54.1708494375352; Tue, 20 Feb 2024 21:46:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708494375; cv=pass; d=google.com; s=arc-20160816; b=UHb1d5ynPxH/9yVzwX32x+zy/khmdLNXuVaixbVwWs/UwJDOaZ6x6WSHgqatLGdGAP zTwwAp4AQc2xNiV34uI+FRYCOytgpo+ciHkYnKO9lv+FdB3g5yHfcJ12UDnnAM28buVq ThNQa05CjKEWavCqpJWA5YRKA1POj2ll57svH7GA99oo478NsvscG1cF2zUFz6QoY/rt iZ/jXOE0+huxGbZx84/1LqPapGyE7BUMPKpXBhVop1+tpxHzCc9cpNurHiNl5c8ozj+h uHKTuxA3YyVSHj+wuKMHcKZh6Hq1Iow4iU2fjyvf3zZn807L/p9eh87KMQdhxUCu1tjl mB5A== 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74107-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d9-20020ac85d89000000b0042c260dff43si10857532qtx.637.2024.02.20.21.46.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 21:46:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-74107-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-74107-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 211D71C23330 for ; Wed, 21 Feb 2024 05:46:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E3FB03D547; 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="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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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); -- 2.44.0.rc0.258.g7320e95886-goog