Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp27801lql; Fri, 12 Apr 2024 02:33:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV7+ycPjCLoNh01UIhAKYo2X3x8AJG2DwA3bl7v5MfwT+6FdoNyUnlbV2WjX343zlOgMBc3wBKVY8WfAkIRCOZK6/hsdr6Im9deky3O3A== X-Google-Smtp-Source: AGHT+IHFbxmcmgFIrm5ZvUvmzi6D5moM0DelDO7705mE2upO4Z5lWAS0bqA+0zS8YyEcZdNG9BC7 X-Received: by 2002:a05:6830:19ec:b0:6ea:181a:a539 with SMTP id p44-20020a05683019ec00b006ea181aa539mr2278823otp.3.1712914390645; Fri, 12 Apr 2024 02:33:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712914390; cv=pass; d=google.com; s=arc-20160816; b=ZisavJOrpsUTQJUpb4CzH43T5oSAx3MxHjnGhSAfr0n2VGisif2MK3FMCx0RTL81py iPtZKoOtPyfAjQkwD5EmHi81k+bBBZ/OdTy7dj8VWEYqzhxzvAvXtjePTm4RcZp/b5FN L7d8zvKpHZ5sG2qtNoC0CD8pVQEnHBkLuWmobfmEPsu+T4dPZym+zHR9JnaoQHJ0BQ6z LxOyv7v/YcW3hfdDCwFWwC+vIB+ErnA+oojwGF59k9QHhCr//e0Hm57elZ8fNEaBOYNs pNsaPEQSywGrAV1YpKlFyX4LKVLntmgfHyQeHlj36vG2q9P5vlJhQVA48DXfXnSqQDKg TZ+A== 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=VBRSF4YwZCz5T0Zk1nE7YVf8B1quLvMs45dUf37h/JQ=; fh=+3keNzaSOvsaq+BXFM2IqKmKMiZV6QsxTCb//k4Cpek=; b=ULM1xWGaJNn1BIYFse3wPNkBhGdfv/xPEoNYDmZ00VS0pANcyHEsMVIZ72SR2WKdzO X87SEoSbvCgdDtVM4PMzLyIL9MTr8Qu5zgwM+Rpb6biHLaULQLUwq/S0e6ONBKZR23mf JZcrmuKmQoPk2/CevRvwHoWdvDjr6Y6mQ2Z7FxX8IbeGZP2klQ6lCslPqQZhLNMpLQM1 bVjv7c3+BI/Z9PUCn0t45xk3LYM6i/uwSg62j7ZgFGdxn2gxKHcAd24f/KQSTqR3pQX5 EdFAZPrgEFtqUsyc3qU3if17gOzl8RNdhTtqUvF72Zvly6PKaYGTLwv5h8Sij7yQXohJ PtNw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=HMxsf5MR; arc=pass (i=1 spf=pass spfdomain=mediatek.com dkim=pass dkdomain=mediatek.com dmarc=pass fromdomain=mediatek.com); spf=pass (google.com: domain of linux-kernel+bounces-142346-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142346-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id n16-20020a63e050000000b005f0be94d560si2849484pgj.161.2024.04.12.02.33.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 02:33:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142346-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=@mediatek.com header.s=dk header.b=HMxsf5MR; arc=pass (i=1 spf=pass spfdomain=mediatek.com dkim=pass dkdomain=mediatek.com dmarc=pass fromdomain=mediatek.com); spf=pass (google.com: domain of linux-kernel+bounces-142346-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142346-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com 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 8D5DEB2364D for ; Fri, 12 Apr 2024 09:10:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9FB9F548FD; Fri, 12 Apr 2024 09:09:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="HMxsf5MR" Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) (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 B899D537FB; Fri, 12 Apr 2024 09:09:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.61.82.184 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712912949; cv=none; b=kdvebhot3lObCNrhUsL/p/XIwyLrL7TrDxFRpS53K1f//FzKkvYAwnAGIgUiETP4yNMTubaStVEF6sg2fjlotRcwUpnFcENTI7L7Zysu5cP2WqGMgzOHHDwyODCvaCmpMBWH7A/2s1PP5VoZVjzTQBejybDe0cDp24elvQ6FUSU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712912949; c=relaxed/simple; bh=YmGSlmvsC6y8Pg4I3syRhSxSRjKdGNAUmRqX5TOnbAs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TwmS7ojZ75I8LzNDp9m/TwyaGP/9vqYDst1ykbdn2pGhp2SjkHz2YtN7SpDevPrkGDTgeVa2ehwAECtX4KNBv5AHWjdrgmasBFD8/IttfK+ugXa0ZLaj5bkqQmYLkbs9kM/IzQIG7Z5rWuRbbDjEJ+RdhY/Nnis3uIR3rLggReM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b=HMxsf5MR; arc=none smtp.client-ip=210.61.82.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com X-UUID: 4ff170b8f8ac11ee935d6952f98a51a9-20240412 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=VBRSF4YwZCz5T0Zk1nE7YVf8B1quLvMs45dUf37h/JQ=; b=HMxsf5MRiyu44E7gUg+bxVEj2ngL7eQVZPGs8LgQQF9HJ6wYRfxQc7jJ2EIvJmoK1mILxxMA/2/eHCo11Vi0+V0mP1lBJ7dGHHKGvYvRDmkiA0CgpdiwZKabp+Aq/uaemyts6fCHLDt6jJLO3jUq5JlXSLgMyxZ7uJ3faZbGQKU=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.37,REQID:0887f156-bdc3-464e-a1ae-78efd250bfca,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:6f543d0,CLOUDID:0bf09882-4f93-4875-95e7-8c66ea833d57,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES:1, SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-UUID: 4ff170b8f8ac11ee935d6952f98a51a9-20240412 Received: from mtkmbs13n1.mediatek.inc [(172.21.101.193)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 316032842; Fri, 12 Apr 2024 17:09:05 +0800 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; Fri, 12 Apr 2024 17:09:03 +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; Fri, 12 Apr 2024 17:09:02 +0800 From: Yunfei Dong To: Jeffrey Kardatzke , =?UTF-8?q?N=C3=ADcolas=20F=20=2E=20R=20=2E=20A=20=2E=20Prado?= , 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=20K=C3=B6nig?= , Matthias Brugger , , , , , , Subject: [PATCH v5,06/23] dma-heap: Add proper kref handling on dma-buf heaps Date: Fri, 12 Apr 2024 17:08:34 +0800 Message-ID: <20240412090851.24999-7-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240412090851.24999-1-yunfei.dong@mediatek.com> References: <20240412090851.24999-1-yunfei.dong@mediatek.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TM-AS-Product-Ver: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-AS-Result: No-10--5.336200-8.000000 X-TMASE-MatchedRID: wDTc/wNgEcORKhTVQwgiGPOS+SRxjgFwsuIso71Vk6Is/uUAk6xP7Nn7 b/+Y15IvCPnYMmIYXkdgHnELoaUKtjWBtSWZ+bE64RtSDjG+z7Cb/LTS0T1K1su87Zav+1yuHtb BWB6gIuZsCaY7uWwO5cTKrSvo7uRnHxPMjOKY7A8LbigRnpKlKWxlRJiH4397F2RJEFmPCGx8h4 tq/svqyZUjfijuYodQ9rpjadQF0gx0pT5AvbYy8A== X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--5.336200-8.000000 X-TMASE-Version: SMEX-14.0.0.3152-9.1.1006-23728.005 X-TM-SNTS-SMTP: 441B48343834E676656E862813A88FA05EBC1DA09580E8C9895063313FA1E6622000:8 X-MTK: N From: John Stultz Add proper refcounting 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 --- 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 */ -- 2.18.0