Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1294038rdb; Fri, 16 Feb 2024 10:54:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUruIUzpauHcp7PVDinItUFIrUitQjIZ6KagJNW90c5b6ZlSYGPue3SUrNW27DjIk73FtHg6uZ9ady/HSp96FpGKdCpf+sQ/fhNjcwwmQ== X-Google-Smtp-Source: AGHT+IG/ugoaNdzHnkZ45OPTrb2HqsVHsFJYYjRG06aKdI+j2+hi1YYh7aAz0NhgFhYW1KkhtVx2 X-Received: by 2002:a05:6a21:3183:b0:19e:a9e6:c05 with SMTP id za3-20020a056a21318300b0019ea9e60c05mr6486611pzb.43.1708109668058; Fri, 16 Feb 2024 10:54:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708109668; cv=pass; d=google.com; s=arc-20160816; b=WStirAiMbOW3Ubyy0HIr+tx7MCLlnk0+3PIny3YDYJSrxoxwyKmJ7vD33Tfn3KzPmU aWibn4Q2QwpELUNjwakYsXLHuLoOJfJ8Ft/pHumYVjfMSubtIBrC7pW184cG4EbnZTVv ljHLSMtoRY6KyEAvtveU4KJJPgGFc1+yGGQJ9Hrf/azM4iF1DpessZmUmnsYfq3FVrnN JemQ5ynvW8h95r+8sYSryQtHNMjcVQNIu5vz56NLGfH15FpMY6WDgIurlMXKeqVUPlrB CQTY7JBtWCPMnX4KjoIAMLR7gb3Ns5z41Kd6X3En2cPN0jKD5n7tQU971+4SwV75DlSA bFJQ== 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=9exRx46WJAI7PPjinT5FzyCoHPJk8OWrIMcDrv8MJtA=; fh=OGIcGdyoke6ohfCedcQQ3UJr3JDRxZcQDHVG7x10Yg0=; b=SbybaRWdm0rXO8PMlaw+HybVXEbkuOYMUTq4sbd7+FsebtcBR6cZAKJ8KXLLOoiZG0 J/sr/ouwe4dKxDtEhQc+WU4XbXDLG8pxJc/AEM5eOxyFEuGtPu06DJIc/5HUG+Ft3/La Rco0GT43KulkLdj4yZwCMl0TgMDT6k1UlZmz30izuXLREgi94P0mJWz77tXeSymPV90F 6zLqkV2rLWJYFvsJBJo3cd+vfFRpY0Z823BzhO2uHcnovxJFcBm9vx+X1Ttlk3tE/W4q aRu9OR62ogVKyLXsjJBqExTHTusM4gu3AWWhgPGlji4kT+eIOPMFwFyGT+z4IGRz1fgp 7z3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=LlwyUY4z; 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-69190-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69190-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id k12-20020a17090a9d8c00b00296a42c0f14si363231pjp.73.2024.02.16.10.54.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 10:54:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69190-linux.lists.archive=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=LlwyUY4z; 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-69190-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69190-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id CC37AB22E12 for ; Fri, 16 Feb 2024 18:45:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A8EA3148305; Fri, 16 Feb 2024 18:42:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="LlwyUY4z" 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 5522E1474BC for ; Fri, 16 Feb 2024 18:42:24 +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=1708108945; cv=none; b=hDX2qg7TPf0bPRiJ+CYokg0a/hE6fFqtEk9Hd+Gdjba5aUIpsdv+bhZDvXviHVFhT4SZ4JkRXIIZ+eM2KsN7KdqFkoZnAcf2oIjBa9G/DOB0A+40+YxlXPp6nW4iDVCOzfFBzTFYg6KIJMIdDj5R0DiPAJaKJNsWHcFVlD7O4EA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708108945; c=relaxed/simple; bh=0+9hvpIMVuumxZZRLzdSlgvQCBBmw4uMTPz59W0JCTQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lln0B7zZhDCi9fYdPA0q+CAgTD/qmQZijvdy533INI2JWzohUye0xgutWhQWkmEi7PC7tCA3c3Zjlkj3Bq2VY9I45sEO10PBqhq4fA1Qy6vcEmDD/9E4j7oxHSnVe34IDi0BuvbHdyvRTLeC0qNjpwUrwNHj1D/6Hxwj+GJWNhs= 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=LlwyUY4z; 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=1708108942; 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=9exRx46WJAI7PPjinT5FzyCoHPJk8OWrIMcDrv8MJtA=; b=LlwyUY4zVt9oeDsGjl2cESfGbvcBUqxmU9X0jYGPlk4hY4xItlonwv/uzIe7LzX3lnVt68 vFjohrhpxUXrXI6UrVnZG+79PnUpoPLOvYkHiYPA0HGpUNmt0TaFN81eGP5B8MddedUgPs NG/hqEjrnCXoUTbmcxfw3U94Vul/1e4= 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 v3 09/10] KVM: arm64: vgic: Ensure the irq refcount is nonzero when taking a ref Date: Fri, 16 Feb 2024 18:41:52 +0000 Message-ID: <20240216184153.2714504-10-oliver.upton@linux.dev> In-Reply-To: <20240216184153.2714504-1-oliver.upton@linux.dev> References: <20240216184153.2714504-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 27a7451ad8b9..c3d1bca0b458 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; } @@ -609,8 +602,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); @@ -656,6 +649,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 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); -- 2.44.0.rc0.258.g7320e95886-goog