Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp245219lqo; Thu, 16 May 2024 05:21:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVpDJMm+MIo5UHFzFC4N49S7DCPKHl8pECoiasVOST7+D0jnLSC3DKaqREK6UiIPhrGpKWzh2gkvR6lb9P7pSNGdoA1IwgBmdZJeyt+kQ== X-Google-Smtp-Source: AGHT+IHMss0/EpStTCRtl+smogD1SkhySfL/rlQxTu8ngqc0w2KygePx5w50IrtvtHpDDCWl0pcK X-Received: by 2002:a05:622a:314:b0:43a:7c0d:8921 with SMTP id d75a77b69052e-43dfdcd6ccamr184300261cf.53.1715862100571; Thu, 16 May 2024 05:21:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715862100; cv=pass; d=google.com; s=arc-20160816; b=BcDKJRYYP1J8SlmFESA2ULM+ykRFU0zVCiF/9ZKSlrOcucW/x//FG5Ln+u4XWdULzA h3kjFfq/YuV4Cwdjv4d8cnrn/o1LAAmrErfJ4/J9LcnyPa5aL/4oXhRr61uKZsn08Cjq UF6Jkt2L1s9iO8l097WUuAVI1eB7CurnjaivqtYW8FV/Ubuzq4KD5QSYHcnCOefU0yYK LFUyBn3WziH+LxoRmuLa0B978OdctMvrvGZ60ykXaA267Ya8At3BGs0P+L7UfzpmI30m V82NGtHkpl8hnOSmefZ+eGsE+lxl24EbPUNxj67PRa5hQhJi3YCy42bhAE8jvj09ivzp tZ4w== 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=0Rn0saqDmTQjD0WNVdp2F7VABGLgyNnBXqUojrGtCpU=; fh=biKe3ab6vu9B2YV/BY0rSGdgJ9M/b05dGcvzyA7ivrs=; b=Bb1nNUQPNpCjftImCbub6HrRd4wqyFhOye76vVFtcDaWrEXkc2YlARDvUmSE4mdRk4 NSBrfMtRbZxN0RdYLt2kGm3KBxFFA/SGDro/up69I4+ijtTLg7+c+ZwQG/qkD5UtWfgt WeupNvi9I3mGcBj/igi69GgvHDSe1oPXezydAh5BaG0w2+YOuou/LkM5qg6iO7SFWZHT UEMQj7v/pVnM94yTSx4x7vK3xhNyJzGqZZHI86Kf8dD3Kqnfuk8qYl8D30ajpgIVPaIn MNMDGgQ+jGSBXtX2DFvI4Q/SqXXhzQ7ZCqNYgd5+SVK4RO00SpYUt/4IgtDaBTHiBRQ0 mEuQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=JwEg9fDq; 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-180993-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-180993-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43df54a128fsi160821701cf.133.2024.05.16.05.21.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 05:21:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-180993-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=JwEg9fDq; 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-180993-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-180993-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 3E4111C21B02 for ; Thu, 16 May 2024 12:21:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9E4281465B8; Thu, 16 May 2024 12:21:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="JwEg9fDq" Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) (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 B29D8145FFE; Thu, 16 May 2024 12:21:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=60.244.123.138 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715862082; cv=none; b=lphcaZP7l1s8i8amVS6T89sLjo4mzajIrBZTG6N76tBz70Vso5embLRgJ1TdD3guJB1KRZW1hkyWbnHgFNHxwLT9vSqC/egHSYlOp62CMQcDOFL4RDZFdKn5YlNeFVbiYfRaAcpzqlmRSJbn8sWvqUoIKeNPSeA5MeGQNxXudc4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715862082; c=relaxed/simple; bh=oMrLGI58Ejb/YaA+hQ6MBTjzysB33TkK6JOUSEkfhvg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SpkmR5TFrO+tets0s1fqHJYSQV/r21MnyI4KW8GXAQKuTkjhNYvvOP0cRnxc8P/ddRmOuUOzwiFraq0erJ55h2exW/14c/No8+g4ppR2vjK2cvPnNv0aU62QnwxMx0Hn8Sq+xPI/rRFa+ZsYcq8BjEhkWlpOYOTztpAYzg66QaI= 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=JwEg9fDq; arc=none smtp.client-ip=60.244.123.138 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: cb9e307e137e11efb92737409a0e9459-20240516 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=0Rn0saqDmTQjD0WNVdp2F7VABGLgyNnBXqUojrGtCpU=; b=JwEg9fDqk297Jh36QlaiW01UJ+c9pWiJaoeCt0uZNhnGyxca9nai7oer6pVEKBFju/sZcuAcD+7mKuTpNmMgbXk/NqI7AwP+BGldlLWk0UXknmMG+T8x7VYzCl8RCaVQiEa2BTS1eunk/vS//Zx6sBRlty7ZokOQZSmCIFwmf5U=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.38,REQID:ae10827c-e45f-4cca-a3c1-4e99d644af2b,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:82c5f88,CLOUDID:88537087-8d4f-477b-89d2-1e3bdbef96d1,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: cb9e307e137e11efb92737409a0e9459-20240516 Received: from mtkmbs14n1.mediatek.inc [(172.21.101.75)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1065563800; Thu, 16 May 2024 20:21:17 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Thu, 16 May 2024 20:21:16 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Thu, 16 May 2024 20:21:14 +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 v6,06/24] dma-heap: Add proper kref handling on dma-buf heaps Date: Thu, 16 May 2024 20:20:44 +0800 Message-ID: <20240516122102.16379-7-yunfei.dong@mediatek.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240516122102.16379-1-yunfei.dong@mediatek.com> References: <20240516122102.16379-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-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.25.1