From patchwork Mon Dec 16 09:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Lin Guay X-Patchwork-Id: 13911630 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3D3CFE7717F for ; Tue, 17 Dec 2024 10:55:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D524810E908; Tue, 17 Dec 2024 10:55:21 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=fb.com header.i=@fb.com header.b="RrP+Yr1L"; dkim-atps=neutral X-Greylist: delayed 340 seconds by postgrey-1.36 at gabe; Mon, 16 Dec 2024 10:01:46 UTC Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4CFE310E184 for ; Mon, 16 Dec 2024 10:01:46 +0000 (UTC) Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4BG6pHO4018658 for ; Mon, 16 Dec 2024 01:56:05 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=facebook; bh=X F5WtkBONmd+8r9yLmklM/sXmsTbxQzH7Qh2X2Sk4Zo=; b=RrP+Yr1LRubVDm2Ud lnNI/6thSJP4K/pTCg4xrH0mQrhxpe83oMQ9n0hNMCJ/cOZ38tgDHaTPli9NdmyB HSOIkRMO/GvhfvQLX0UGL43Ikq9kPYNMWDtJ0LDshc3hELIesDcUjyiA04R87V9X ZDV1BjN6ZOdIR/TQY+fIALxnn8= Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 43jfa28tc0-7 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 16 Dec 2024 01:56:05 -0800 (PST) Received: from twshared60378.16.frc2.facebook.com (2620:10d:c0a8:fe::f072) by mail.thefacebook.com (2620:10d:c0a9:6f::237c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1544.11; Mon, 16 Dec 2024 09:56:01 +0000 Received: by devvm12370.nha0.facebook.com (Postfix, from userid 624418) id D61BA10A1F006; Mon, 16 Dec 2024 01:55:55 -0800 (PST) From: Wei Lin Guay To: , , , CC: , , , , , Wei Lin Guay Subject: [PATCH 2/4] dma-buf: Add dma_buf_try_get() Date: Mon, 16 Dec 2024 01:54:16 -0800 Message-ID: <20241216095429.210792-3-wguay@fb.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241216095429.210792-1-wguay@fb.com> References: <20241216095429.210792-1-wguay@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: uh4obqx7DL5bCo6b-y9L5Yz_RMzwHFvI X-Proofpoint-ORIG-GUID: uh4obqx7DL5bCo6b-y9L5Yz_RMzwHFvI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-05_03,2024-10-04_01,2024-09-30_01 X-Mailman-Approved-At: Tue, 17 Dec 2024 10:55:20 +0000 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Jason Gunthorpe Summary: Used to increment the refcount of the dma buf's struct file, only if the refcount is not zero. Useful to allow the struct file's lifetime to control the lifetime of the dmabuf while still letting the driver to keep track of created dmabufs. Signed-off-by: Jason Gunthorpe Signed-off-by: Wei Lin Guay Reviewed-by: Dag Moxnes Reviewed-by: Keith Busch Reviewed-by: Nic Viljoen --- include/linux/dma-buf.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) -- 2.43.5 diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 36216d28d8bd..9854578afecd 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -614,6 +614,19 @@ int dma_buf_fd(struct dma_buf *dmabuf, int flags); struct dma_buf *dma_buf_get(int fd); void dma_buf_put(struct dma_buf *dmabuf); +/** + * dma_buf_try_get - try to get a reference on a dmabuf + * @dmabuf - the dmabuf to get + * + * Returns true if a reference was successfully obtained. The caller must + * interlock with the dmabuf's release function in some way, such as RCU, to + * ensure that this is not called on freed memory. + */ +static inline bool dma_buf_try_get(struct dma_buf *dmabuf) +{ + return get_file_rcu(&dmabuf->file); +} + struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *, enum dma_data_direction); void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,