From patchwork Mon Nov 6 12:04:08 2023 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: 161977 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2607805vqu; Mon, 6 Nov 2023 04:07:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGO4VOd+kPalMZlCUraNYEZIbaPFeCOhAY9g11uIoLJ9hRm3i1ZqNv0DxGfxMH6fvU3JVg1 X-Received: by 2002:a17:902:db0e:b0:1cc:476c:896c with SMTP id m14-20020a170902db0e00b001cc476c896cmr20341697plx.60.1699272436295; Mon, 06 Nov 2023 04:07:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699272436; cv=none; d=google.com; s=arc-20160816; b=KPCgyjATLVr6Du3hO33UgimhxU4Y6kz/eTLVkgVYVqGa8K360AxgYlL4XhkmjTqcXi P3NBnP7YApwZH5An6bMmmuEMWtzMdoYQqv1L/qfyMg94a80h2U5W0lQjQBOv/8moyhJE k6FDv+ZJNSJ6EYerfAA/yiqpJ1gk5umckONGS2XGGo8ixs267z727cyASc/E45Exjdgb ZXudPYsgHHXeI+uH7EB67D2gqKBOGw77tBzfx1zT4oLDaO6bIZZUTLcAJWBVDpjOpmRh Vw+SVG1MuZkzE2qFjMjLxxmfUN9jp62o5inrTHH5Adu6Py1F9qLP/+HxZEQQCdOUPqf4 ILFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=VBRSF4YwZCz5T0Zk1nE7YVf8B1quLvMs45dUf37h/JQ=; fh=HMoU6Z0fAVEi/36PnBTb4UQLM/fqzPQmIZjxf5hFbO0=; b=Fh7ptDOHf/i2/jk7/rmRs4NmB6D8lku2xdk9Fv7JAz47tQJTpRU1nT341uruPrt6v0 Nl4OEy+dtIIqZRHdyvOEto2Gi8HJ6H2N3xVQcutbEQv5jPKjSakpI1RTyGdinH6c3MFK ohZPk//HcSc47Oge6lx1Lc/LONfj51bzfedqiZ4IS6lPr3FJTcRA1JW77bGs7Tejbpfg 0AU1h3xJ2PaC3bXzuE9nyitrcfOoRdWAOpxddCGbZVTg2172tILteZkyLdjPOalwmiQH zAj3lpUdLcX0ggcOC6vJJa5cd/UE2t0sCwUV4hLualMm8elR+G9EoFpxqR1ACQpVFd6a 0VFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=Asrt4H8z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id a16-20020a170902ecd000b001c413905d87si8542374plh.49.2023.11.06.04.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 04:07:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=Asrt4H8z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D5A188070334; Mon, 6 Nov 2023 04:06:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231767AbjKFMFJ (ORCPT + 36 others); Mon, 6 Nov 2023 07:05:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231590AbjKFMEn (ORCPT ); Mon, 6 Nov 2023 07:04:43 -0500 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03594FA; Mon, 6 Nov 2023 04:04:39 -0800 (PST) X-UUID: a5e598287c9c11eea33bb35ae8d461a2-20231106 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=Asrt4H8znuog+xVotnxsrm/eDQIj6y+p3f+7QtKMMiJ3BN+7ObQv2iTisFXghVpRsoXRMsRS7ZV67Nj2eD0egeC9Yb7yoidr5FxwQkRDDYiZcgUjtqtrsIbZUyBZxmM/TZ8DhQOfVPiI15Wf/eVpcL7378DU3+J+aRnnBa+ZWCQ=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.33,REQID:ccbb2cd7-f535-4868-8cf0-20ab0f584cb0,IP:0,U RL:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-5 X-CID-META: VersionHash:364b77b,CLOUDID:2fe51195-10ce-4e4b-85c2-c9b5229ff92b,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File: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: a5e598287c9c11eea33bb35ae8d461a2-20231106 Received: from mtkmbs14n2.mediatek.inc [(172.21.101.76)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1024095665; Mon, 06 Nov 2023 20:04:33 +0800 Received: from mtkmbs11n1.mediatek.inc (172.21.101.185) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 6 Nov 2023 20:04:32 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs11n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 6 Nov 2023 20:04:31 +0800 From: Yunfei Dong To: Jeffrey Kardatzke , "T . J . Mercier" , John Stultz , Yong Wu , =?utf-8?q?N=C3=ADcolas_F_=2E_R_=2E_A_=2E_Pr?= =?utf-8?q?ado?= , Nicolas Dufresne , Hans Verkuil , AngeloGioacchino Del Regno , Benjamin Gaignard , Nathan Hebert CC: Chen-Yu Tsai , Hsin-Yi Wang , Fritz Koenig , Daniel Vetter , "Steve Cho" , Yunfei Dong , , , , , , Subject: [PATCH v2,06/21] dma-heap: Add proper kref handling on dma-buf heaps Date: Mon, 6 Nov 2023 20:04:08 +0800 Message-ID: <20231106120423.23364-7-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231106120423.23364-1-yunfei.dong@mediatek.com> References: <20231106120423.23364-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.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: 81C59CCFD5557B78C34A3A681D9B384C675A99966F72903600CF31B01A234F362000:8 X-MTK: N X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 06 Nov 2023 04:06:12 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781816294156813257 X-GMAIL-MSGID: 1781816294156813257 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 */