From patchwork Sat Jul 20 07:15:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?WXVuZmVpIERvbmcgKOiRo+S6kemjnik=?= X-Patchwork-Id: 13737670 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 3F1EFC3DA49 for ; Sat, 20 Jul 2024 07:17:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date :Subject:CC:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=91RqwGm8L1GvA0gEqTJqa6Z8jkRudalRKuHiVM28dZU=; b=FayV1BuvkjPmXWsTt+sCzcUI1s rC1WE8rVYR9hYmTSyFuxjS0aqQy5VAJGvOPxRRi79AKQuRUMpv1zQiEC3g6tP97Tk5s0ysenDZknk /id3yThG1P10wLS56EYaVNqTyHiubwjTcox/gj8mPybCGoX43od1575t0Ojubdr18+H1BNk0RaFPB nhewwfinJ9QAKb7JZMi0Z8KgECLXeiuX0qM0oE8WXbfXblS4QJJKHLI0GVEJkSYHDU4PowJTKQW9P VRKQtlrgRBMpa+T0g+8g/fWC+yicefvrOiptAjbBKUx0+O4GkQUh0Yg3t6AeDCcdni6HTsKcCNzez 4kOMK8pg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sV4Lf-00000004cmn-3COX; Sat, 20 Jul 2024 07:17:43 +0000 Received: from mailgw02.mediatek.com ([216.200.240.185]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sV4KM-00000004c03-1LPB; Sat, 20 Jul 2024 07:16:23 +0000 X-UUID: f5d65472466711ef955bff358274dad2-20240720 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=91RqwGm8L1GvA0gEqTJqa6Z8jkRudalRKuHiVM28dZU=; b=rHIE4GgmqeiybFCxUAa0J4XKJ+1eH8iI2gvFMPGE3h3AaJvA6ihMGvxuCfyCc/5dkxaiREKyJlIyQ4n3YyTnGlHU9lsQku/2744vrvmWPYGWia3Hgj7Nz39PfnkutJt3gVlye/fKhEgH3jrwJ/G5s5OdbfWwKGMA8atNcNXQUmw=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.40,REQID:eb0d335b-9c09-4f89-b398-ae0c8c6db7fe,IP:0,U RL:0,TC:0,Content:-25,EDM:-25,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACT ION:release,TS:-50 X-CID-META: VersionHash:ba885a6,CLOUDID:a2c9ad0d-46b0-425a-97d3-4623fe284021,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:1,IP:nil,UR L:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1,S PR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: f5d65472466711ef955bff358274dad2-20240720 Received: from mtkmbs11n1.mediatek.inc [(172.21.101.185)] by mailgw02.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1871271715; Sat, 20 Jul 2024 00:16:19 -0700 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by MTKMBS14N1.mediatek.inc (172.21.101.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Sat, 20 Jul 2024 15:16:16 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Sat, 20 Jul 2024 15:16:14 +0800 From: Yunfei Dong To: Jeffrey Kardatzke , =?utf-8?q?N=C3=ADcolas_F_=2E_?= =?utf-8?q?R_=2E_A_=2E_Prado?= , Nathan Hebert , "Nicolas Dufresne" , Hans Verkuil , AngeloGioacchino Del Regno , Benjamin Gaignard , Sebastian Fricke , Tomasz Figa , "Mauro Carvalho Chehab" , Marek Szyprowski CC: Chen-Yu Tsai , Yong Wu , "Hsin-Yi Wang" , Fritz Koenig , "Daniel Vetter" , Steve Cho , Yunfei Dong , Sumit Semwal , "Brian Starkey" , John Stultz , "T . J . Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Matthias Brugger , , , , , , Subject: [PATCH v7 05/28] dma-heap: Add proper kref handling on dma-buf heaps Date: Sat, 20 Jul 2024 15:15:43 +0800 Message-ID: <20240720071606.27930-6-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240720071606.27930-1-yunfei.dong@mediatek.com> References: <20240720071606.27930-1-yunfei.dong@mediatek.com> MIME-Version: 1.0 X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--5.707100-8.000000 X-TMASE-MatchedRID: uvmf2XsR/xmRKhTVQwgiGPOS+SRxjgFwIaLR+2xKRDJGL0g1nVmkYXB4 4IkzjfYyrUhQzMxACbqtTxRj9EhMYqh+3Rlbev+JEgwM8US/pTFYN1akkye0qOVyoVV0m7wN7fS O5z9h3Mjfbqj275Pjcy33B760297/lwV2iaAfSWcURSScn+QSXt0H8LFZNFG71sULACB0qRI987 fAqpfLD+SVyx8f84bFK4QcjwmrlwJjdxLGTzb+8v8Akuq1Mq2slExlQIQeRG0= X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--5.707100-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: B5C727F956790980F61EEFCB8F9B46EAB3F94B842BBB02CD8E829204F26000262000:8 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240720_001622_404968_78A55784 X-CRM114-Status: GOOD ( 14.40 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org From: John Stultz Add proper reference counting on the dma_heap structure. While existing heaps are built-in, we may eventually have heaps loaded from modules, and we'll need to be able to properly handle the references to the heaps Signed-off-by: John Stultz Signed-off-by: T.J. Mercier Signed-off-by: Yong Wu [Yong: Just add comment for "minor" and "refcount"] Signed-off-by: Yunfei Dong [Yunfei: Change reviewer's comments] --- drivers/dma-buf/dma-heap.c | 29 +++++++++++++++++++++++++++++ include/linux/dma-heap.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 22f6c193db0d..97025ee8500f 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,7 @@ * @heap_devt: heap device node * @list: list head connecting to list of heaps * @heap_cdev: heap char device + * @refcount: reference counter for this heap device * * Represents a heap of memory from which buffers can be made. */ @@ -40,6 +42,7 @@ struct dma_heap { dev_t heap_devt; struct list_head list; struct cdev heap_cdev; + struct kref refcount; }; static LIST_HEAD(heap_list); @@ -240,6 +243,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) if (!heap) return ERR_PTR(-ENOMEM); + kref_init(&heap->refcount); heap->name = exp_info->name; heap->ops = exp_info->ops; heap->priv = exp_info->priv; @@ -304,6 +308,31 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) return err_ret; } +static void dma_heap_release(struct kref *ref) +{ + struct dma_heap *heap = container_of(ref, struct dma_heap, refcount); + unsigned int minor = MINOR(heap->heap_devt); + + mutex_lock(&heap_list_lock); + list_del(&heap->list); + mutex_unlock(&heap_list_lock); + + device_destroy(dma_heap_class, heap->heap_devt); + cdev_del(&heap->heap_cdev); + xa_erase(&dma_heap_minors, minor); + + kfree(heap); +} + +/** + * dma_heap_put - drops a reference to a dmabuf heap, potentially freeing it + * @heap: DMA-Heap whose reference count to decrement + */ +void dma_heap_put(struct dma_heap *heap) +{ + kref_put(&heap->refcount, dma_heap_release); +} + static char *dma_heap_devnode(const struct device *dev, umode_t *mode) { return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev)); diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index fbe86ec889a8..d57593f8a1bc 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -46,4 +46,6 @@ const char *dma_heap_get_name(struct dma_heap *heap); struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); +void dma_heap_put(struct dma_heap *heap); + #endif /* _DMA_HEAPS_H */