From nobody Thu Apr 9 16:36:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 DA694346A1C for ; Tue, 3 Mar 2026 12:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541266; cv=none; b=u6W5t5tjGynoMaNEZzktcxJNI5FmtMG2UENn/bHFurjuS6qjfRxIQ50SjkAD7dNB7BcEAI4rNIPfh+ONXWx3Zu27fy92M7XNjuetlxdsVJ6FCeY/TMMFhLh79iRI61n5U48O6OenAexf9Ar+Nx1iE6USh3sXBrFStn2yqASzavg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541266; c=relaxed/simple; bh=kloNlAqHPnkos0GJMbM9m3JwAUQYH04vJyIdMd2b3o0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hnDKfzrtsbld2yNfU/6Xx2zQNfdZeAn5Fa89QlgfQ1/YRgaipTSFiVfRFA8OtVqOdCooOGY+7yQA2afFgLetCastdXQyluqYz0LnCfbHhI8KsLbXxEgGXIgi7/3/XVDuLbNrTBZosiF31r0AvY9qLWyFM0Fb7wFLAlLaoROGtyE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=P7MV3yPX; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="P7MV3yPX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772541262; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YW5SxBIIgkWx2jwEXG8osOzGtmkFwVM71Xjy+8ygUKo=; b=P7MV3yPX3ESmaxpxtdxXa/zTzboEslZ1xva8zXUUkSu3ViXgmeNtDzt5UVLrpQ7AUzzpmn Aeo30GcMfMwkZ5BlWQly5NHfmvMqwpoqwrVc1CsCRFU9B15YzRNLpPjc8PFtk0yVVe3CF6 elh1zA1XNgFrvkv/OcIqIhzL2XZ9uAQ= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-384-t6iJc4AkPQu8r09LjPv5Jw-1; Tue, 03 Mar 2026 07:34:19 -0500 X-MC-Unique: t6iJc4AkPQu8r09LjPv5Jw-1 X-Mimecast-MFC-AGG-ID: t6iJc4AkPQu8r09LjPv5Jw_1772541257 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 439B3180025B; Tue, 3 Mar 2026 12:34:17 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 26F2830001B9; Tue, 3 Mar 2026 12:34:10 +0000 (UTC) From: Albert Esteve Date: Tue, 03 Mar 2026 13:33:44 +0100 Subject: [PATCH v2 1/6] dma-buf: dma-heap: Keep track of the heap device struct Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-b4-dmabuf-heap-coherent-rmem-v2-1-65a4653b3378@redhat.com> References: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> In-Reply-To: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> To: Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Marek Szyprowski , Robin Murphy , Rob Herring , Saravana Kannan Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Albert Esteve , echanude@redhat.com, mripard@redhat.com, John Stultz X-Developer-Signature: v=1; a=ed25519-sha256; t=1772541243; l=3358; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=QjNRn783TryVaP32s1fqdde0JJl5VlRoYl0aey6l8JI=; b=ZTgmWxaTm/nzxm/hJzIpamvvLX7d8KyH9pT9SgKvu5Ga0Z6Na1Isl5iBXBfJT8GRq6iQDFm90 qFRX/fnBUg+DuPrGY0zNdK7au0KM6M72mRUXBFqDgF5U5p4HmiO3Qb2 X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 From: John Stultz Keep track of the heap device struct. This will be useful for special DMA allocations and actions. Signed-off-by: John Stultz Signed-off-by: Albert Esteve Reviewed-by: Maxime Ripard --- drivers/dma-buf/dma-heap.c | 34 ++++++++++++++++++++++++++-------- include/linux/dma-heap.h | 2 ++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index ac5f8685a6494..1124d63eb1398 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -31,6 +31,7 @@ * @heap_devt: heap device node * @list: list head connecting to list of heaps * @heap_cdev: heap char device + * @heap_dev: heap device * * Represents a heap of memory from which buffers can be made. */ @@ -41,6 +42,7 @@ struct dma_heap { dev_t heap_devt; struct list_head list; struct cdev heap_cdev; + struct device *heap_dev; }; =20 static LIST_HEAD(heap_list); @@ -223,6 +225,19 @@ const char *dma_heap_get_name(struct dma_heap *heap) } EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, "DMA_BUF_HEAP"); =20 +/** + * dma_heap_get_dev() - get device struct for the heap + * @heap: DMA-Heap to retrieve device struct from + * + * Returns: + * The device struct for the heap. + */ +struct device *dma_heap_get_dev(struct dma_heap *heap) +{ + return heap->heap_dev; +} +EXPORT_SYMBOL_NS_GPL(dma_heap_get_dev, "DMA_BUF_HEAP"); + /** * dma_heap_add - adds a heap to dmabuf heaps * @exp_info: information needed to register this heap @@ -230,7 +245,6 @@ EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, "DMA_BUF_HEAP"); struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) { struct dma_heap *heap, *h, *err_ret; - struct device *dev_ret; unsigned int minor; int ret; =20 @@ -272,14 +286,14 @@ struct dma_heap *dma_heap_add(const struct dma_heap_e= xport_info *exp_info) goto err1; } =20 - dev_ret =3D device_create(dma_heap_class, - NULL, - heap->heap_devt, - NULL, - heap->name); - if (IS_ERR(dev_ret)) { + heap->heap_dev =3D device_create(dma_heap_class, + NULL, + heap->heap_devt, + NULL, + heap->name); + if (IS_ERR(heap->heap_dev)) { pr_err("dma_heap: Unable to create device\n"); - err_ret =3D ERR_CAST(dev_ret); + err_ret =3D ERR_CAST(heap->heap_dev); goto err2; } =20 @@ -295,6 +309,10 @@ struct dma_heap *dma_heap_add(const struct dma_heap_ex= port_info *exp_info) } } =20 + /* Make sure it doesn't disappear on us */ + heap->heap_dev =3D get_device(heap->heap_dev); + + /* Add heap to the list */ list_add(&heap->list, &heap_list); mutex_unlock(&heap_list_lock); diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index 648328a64b27e..493085e69b70e 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -12,6 +12,7 @@ #include =20 struct dma_heap; +struct device; =20 /** * struct dma_heap_ops - ops to operate on a given heap @@ -43,6 +44,7 @@ struct dma_heap_export_info { void *dma_heap_get_drvdata(struct dma_heap *heap); =20 const char *dma_heap_get_name(struct dma_heap *heap); +struct device *dma_heap_get_dev(struct dma_heap *heap); =20 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); =20 --=20 2.52.0 From nobody Thu Apr 9 16:36:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 939E83446BC for ; Tue, 3 Mar 2026 12:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541274; cv=none; b=tzq4yhnYNACrE4MRsIHEvu7lrj/Lo2+tHy1aXPBLWyc1jTH2JIza9wz1T348gFoNgMssZ8gESX1h2qx4KPmu8Dq5NQP8X6i7UW7H55JDmQhTJctz3GjDsk72osajXdtzjK+IThHT7N1JAynpmJfbXNkVPT7vKUYVvMBRP94hQj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541274; c=relaxed/simple; bh=rT4eCrjoQu7EvTeC9Zi2EQ7EHm1V9wN2D3i4WLgOBL8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tAdoyo0uDk3ztT9FsOlGxQHBQniaJWAytKfB4YWb7K1foaxw7ohvH0lzGV5wT6XjxhmO9aAPGqzaJPySU1LKnwYgsNjD8GZYsemCY6HACMEAGy+zqUyjHJ02UA20qez3+8GZKTYdKFSNCUkU8Rwpbn+mr8EQIMEdV7o/UWDS3pQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=IVZmYruz; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="IVZmYruz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772541270; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L2WQTNBlzMDcavmEhhF5QEatl1tMG2I69r3hsa4655g=; b=IVZmYruzpc44bGYcvryyD3WHwsAJUQrhEYjR/KzkA9MTOPiWbO3Y0b/D7YPlas+YS0HHai Tn6XN0tmxbDqFBwJXSYw5iGZBYmljEqIUjR4AEYCX6zkWdLoHeIhlAKwtyL+O5GdOQy5K2 PY9ReYYAQjn/DvQShOR8CCQr8JZiliI= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-N7OrLEwtNuGjw09bCTBezQ-1; Tue, 03 Mar 2026 07:34:26 -0500 X-MC-Unique: N7OrLEwtNuGjw09bCTBezQ-1 X-Mimecast-MFC-AGG-ID: N7OrLEwtNuGjw09bCTBezQ_1772541264 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 15B2C1800464; Tue, 3 Mar 2026 12:34:24 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BBEC730001BF; Tue, 3 Mar 2026 12:34:17 +0000 (UTC) From: Albert Esteve Date: Tue, 03 Mar 2026 13:33:45 +0100 Subject: [PATCH v2 2/6] dma-buf: dma-heap: split dma_heap_add Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-b4-dmabuf-heap-coherent-rmem-v2-2-65a4653b3378@redhat.com> References: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> In-Reply-To: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> To: Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Marek Szyprowski , Robin Murphy , Rob Herring , Saravana Kannan Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Albert Esteve , echanude@redhat.com, mripard@redhat.com X-Developer-Signature: v=1; a=ed25519-sha256; t=1772541243; l=6736; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=rT4eCrjoQu7EvTeC9Zi2EQ7EHm1V9wN2D3i4WLgOBL8=; b=WyYJMMZjafvKEln5EMzK35BIFWIKyBwZjgKRJeKr/K5sa3XEXqCBPwD2HOUyxnOKeDhqy32W0 LDiUoTUFgtwD0jNccSayx+CrlCFk5prI74Zg2anYbmnK0Ya+AYBc6Ry X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Split dma_heap_add() into creation and registration phases while preserving the ordering between cdev_add() and device_add(), and ensuring all device fields are initialised. This will allow to access the heap_dev before it is registered and becomes available to userspace, making error handling easier. Signed-off-by: Albert Esteve --- drivers/dma-buf/dma-heap.c | 126 +++++++++++++++++++++++++++++++++++------= ---- include/linux/dma-heap.h | 3 ++ 2 files changed, 103 insertions(+), 26 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 1124d63eb1398..88189d4e48561 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -238,15 +238,30 @@ struct device *dma_heap_get_dev(struct dma_heap *heap) } EXPORT_SYMBOL_NS_GPL(dma_heap_get_dev, "DMA_BUF_HEAP"); =20 +static void dma_heap_dev_release(struct device *dev) +{ + struct dma_heap *heap; + + pr_debug("heap device: '%s': %s\n", dev_name(dev), __func__); + heap =3D dev_get_drvdata(dev); + kfree(heap->name); + kfree(heap); + kfree(dev); +} + /** - * dma_heap_add - adds a heap to dmabuf heaps - * @exp_info: information needed to register this heap + * dma_heap_create() - allocate and initialize a heap object + * @exp_info: information needed to create a heap + * + * Creates a heap instance but does not register it or create device nodes. + * Use dma_heap_register() to make it visible to userspace, or + * dma_heap_destroy() to release it. + * + * Returns a heap on success or ERR_PTR(-errno) on failure. */ -struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) +struct dma_heap *dma_heap_create(const struct dma_heap_export_info *exp_in= fo) { - struct dma_heap *heap, *h, *err_ret; - unsigned int minor; - int ret; + struct dma_heap *heap; =20 if (!exp_info->name || !strcmp(exp_info->name, "")) { pr_err("dma_heap: Cannot add heap without a name\n"); @@ -265,13 +280,41 @@ struct dma_heap *dma_heap_add(const struct dma_heap_e= xport_info *exp_info) heap->name =3D exp_info->name; heap->ops =3D exp_info->ops; heap->priv =3D exp_info->priv; + heap->heap_dev =3D kzalloc_obj(*heap->heap_dev); + if (!heap->heap_dev) { + kfree(heap); + return ERR_PTR(-ENOMEM); + } + + device_initialize(heap->heap_dev); + dev_set_drvdata(heap->heap_dev, heap); + + dev_set_name(heap->heap_dev, heap->name); + heap->heap_dev->class =3D dma_heap_class; + heap->heap_dev->release =3D dma_heap_dev_release; + + return heap; +} +EXPORT_SYMBOL_NS_GPL(dma_heap_create, "DMA_BUF_HEAP"); + +/** + * dma_heap_register() - register a heap with the dma-heap framework + * @heap: heap instance created with dma_heap_create() + * + * Registers the heap, creating its device node and adding it to the heap + * list. Returns 0 on success or a negative error code on failure. + */ +int dma_heap_register(struct dma_heap *heap) +{ + struct dma_heap *h; + unsigned int minor; + int ret; =20 /* Find unused minor number */ ret =3D xa_alloc(&dma_heap_minors, &minor, heap, XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL); if (ret < 0) { pr_err("dma_heap: Unable to get minor number for heap\n"); - err_ret =3D ERR_PTR(ret); goto err0; } =20 @@ -282,42 +325,34 @@ struct dma_heap *dma_heap_add(const struct dma_heap_e= xport_info *exp_info) ret =3D cdev_add(&heap->heap_cdev, heap->heap_devt, 1); if (ret < 0) { pr_err("dma_heap: Unable to add char device\n"); - err_ret =3D ERR_PTR(ret); goto err1; } =20 - heap->heap_dev =3D device_create(dma_heap_class, - NULL, - heap->heap_devt, - NULL, - heap->name); - if (IS_ERR(heap->heap_dev)) { - pr_err("dma_heap: Unable to create device\n"); - err_ret =3D ERR_CAST(heap->heap_dev); + heap->heap_dev->devt =3D heap->heap_devt; + + ret =3D device_add(heap->heap_dev); + if (ret) { + pr_err("dma_heap: Unable to add device\n"); goto err2; } =20 mutex_lock(&heap_list_lock); /* check the name is unique */ list_for_each_entry(h, &heap_list, list) { - if (!strcmp(h->name, exp_info->name)) { + if (!strcmp(h->name, heap->name)) { mutex_unlock(&heap_list_lock); pr_err("dma_heap: Already registered heap named %s\n", - exp_info->name); - err_ret =3D ERR_PTR(-EINVAL); + heap->name); + ret =3D -EINVAL; goto err3; } } =20 - /* Make sure it doesn't disappear on us */ - heap->heap_dev =3D get_device(heap->heap_dev); - - /* Add heap to the list */ list_add(&heap->list, &heap_list); mutex_unlock(&heap_list_lock); =20 - return heap; + return 0; =20 err3: device_destroy(dma_heap_class, heap->heap_devt); @@ -326,8 +361,47 @@ struct dma_heap *dma_heap_add(const struct dma_heap_ex= port_info *exp_info) err1: xa_erase(&dma_heap_minors, minor); err0: - kfree(heap); - return err_ret; + dma_heap_destroy(heap); + return ret; +} +EXPORT_SYMBOL_NS_GPL(dma_heap_register, "DMA_BUF_HEAP"); + +/** + * dma_heap_destroy() - release a heap created by dma_heap_create() + * @heap: heap instance to release + * + * Drops the heap device reference; the heap and its device are freed in t= he + * device release path when the last reference is gone. + */ +void dma_heap_destroy(struct dma_heap *heap) +{ + put_device(heap->heap_dev); +} +EXPORT_SYMBOL_NS_GPL(dma_heap_destroy, "DMA_BUF_HEAP"); + +/** + * dma_heap_add - adds a heap to dmabuf heaps + * @exp_info: information needed to register this heap + */ +struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) +{ + struct dma_heap *heap; + int ret; + + heap =3D dma_heap_create(exp_info); + if (IS_ERR(heap)) { + pr_err("dma_heap: failed to create heap (%d)\n", PTR_ERR(heap)); + return PTR_ERR(heap); + } + + ret =3D dma_heap_register(heap); + if (ret) { + pr_err("dma_heap: failed to register heap (%d)\n", ret); + dma_heap_destroy(heap); + return ERR_PTR(ret); + } + + return heap; } EXPORT_SYMBOL_NS_GPL(dma_heap_add, "DMA_BUF_HEAP"); =20 diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index 493085e69b70e..1b0ea43ba66c3 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -46,6 +46,9 @@ void *dma_heap_get_drvdata(struct dma_heap *heap); const char *dma_heap_get_name(struct dma_heap *heap); struct device *dma_heap_get_dev(struct dma_heap *heap); =20 +struct dma_heap *dma_heap_create(const struct dma_heap_export_info *exp_in= fo); +int dma_heap_register(struct dma_heap *heap); +void dma_heap_destroy(struct dma_heap *heap); struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); =20 extern bool mem_accounting; --=20 2.52.0 From nobody Thu Apr 9 16:36:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 AEEEC1A8F97 for ; Tue, 3 Mar 2026 12:34:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541278; cv=none; b=ca7nsMZnFVo4C7m1K3o4PO/9YxG5A+cYt844SwsF65buBuIdEUiWECkgRDAulr4H4dfGRHE0x0bpTupwrfZNK1z932MCSoGJ4uPu4SRlev43+zniBtGA4uuQyb95HbhFa4XnbIDBye/hr0KqBeS/uglwjv2+P8icVc5DT4H2IoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541278; c=relaxed/simple; bh=Oiyav+nKn59PGwnJ/kuaw2S3AHX/9+uyerzkejMZoD4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eh/aV9nrBJgecrMC8XO3bZudvCQba1PTOj3P0mgDg1Q2WPk4Rj4Ozebh0NyfAXXKgbLD6eWhaUD9oYTbK5Zp6k1iLBqNXkNexLmSCSBMtXOdZbO8TfS1wAsUBJ/gmEMxTxh09gRRoZxeOipqSsUFn+gQzYnZWD9U3xg+0fqeRIM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=WYUhZfhW; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WYUhZfhW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772541275; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2Eja0IKLWM7TXMINx19h4PVbisMkWsGrNkIdZK4d0xk=; b=WYUhZfhWczy8xq/J0uYh0tzwWx4+UfPEu0fP/y/GWo6eLcuuolEsUIAs092Mg/M5N+cuq1 ZDeI3jerWBMJ46FwnjvpxVIqh/57hLXfaLQ7K9XjakErFCahpJgJ4PkPG5idYfI5kebJpV 8zs3bn1NPrcNvKIu037huNSA4mXAbQs= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-P4LtvZmcMl2bsCgGydgWWQ-1; Tue, 03 Mar 2026 07:34:32 -0500 X-MC-Unique: P4LtvZmcMl2bsCgGydgWWQ-1 X-Mimecast-MFC-AGG-ID: P4LtvZmcMl2bsCgGydgWWQ_1772541270 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 847961800365; Tue, 3 Mar 2026 12:34:30 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A0A7330001B9; Tue, 3 Mar 2026 12:34:24 +0000 (UTC) From: Albert Esteve Date: Tue, 03 Mar 2026 13:33:46 +0100 Subject: [PATCH v2 3/6] of_reserved_mem: add a helper for rmem device_init op Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-b4-dmabuf-heap-coherent-rmem-v2-3-65a4653b3378@redhat.com> References: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> In-Reply-To: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> To: Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Marek Szyprowski , Robin Murphy , Rob Herring , Saravana Kannan Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Albert Esteve , echanude@redhat.com, mripard@redhat.com X-Developer-Signature: v=1; a=ed25519-sha256; t=1772541243; l=3269; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=Oiyav+nKn59PGwnJ/kuaw2S3AHX/9+uyerzkejMZoD4=; b=i3wbq5E4k9VfhS9W3iaMgYMRMa0WY56H+E6aqqEhiNhouFoq+Q7bGPoRNT8RSld0ItGC3n+Mo VBn/S9AJdEHB91d8iZ3E61e216OnDtU3RjkvsWWVtnZ0J+Hin44U7YH X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Add a helper function wrapping internal reserved memory device_init call and expose it externally. Use the new helper function within of_reserved_mem_device_init_by_idx(). Signed-off-by: Albert Esteve --- drivers/of/of_reserved_mem.c | 27 +++++++++++++++++++++++---- include/linux/of_reserved_mem.h | 8 ++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 1fd28f8056108..3a350bef8f11e 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -605,6 +605,28 @@ struct rmem_assigned_device { static LIST_HEAD(of_rmem_assigned_device_list); static DEFINE_MUTEX(of_rmem_assigned_device_mutex); =20 +/** + * of_reserved_mem_device_init_with_mem() - assign reserved memory region = to + * given device + * @dev: Pointer to the device to configure + * @rmem: Reserved memory region to assign + * + * This function assigns respective DMA-mapping operations based on the + * reserved memory region already provided in @rmem to the @dev device, + * without walking DT nodes. + * + * Returns error code or zero on success. + */ +int of_reserved_mem_device_init_with_mem(struct device *dev, + struct reserved_mem *rmem) +{ + if (!dev || !rmem || !rmem->ops || !rmem->ops->device_init) + return -EINVAL; + + return rmem->ops->device_init(rmem, dev); +} +EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_with_mem); + /** * of_reserved_mem_device_init_by_idx() - assign reserved memory region to * given device @@ -643,14 +665,11 @@ int of_reserved_mem_device_init_by_idx(struct device = *dev, rmem =3D of_reserved_mem_lookup(target); of_node_put(target); =20 - if (!rmem || !rmem->ops || !rmem->ops->device_init) - return -EINVAL; - rd =3D kmalloc_obj(struct rmem_assigned_device); if (!rd) return -ENOMEM; =20 - ret =3D rmem->ops->device_init(rmem, dev); + ret =3D of_reserved_mem_device_init_with_mem(dev, rmem); if (ret =3D=3D 0) { rd->dev =3D dev; rd->rmem =3D rmem; diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_me= m.h index f573423359f48..12f7ddb7ee61f 100644 --- a/include/linux/of_reserved_mem.h +++ b/include/linux/of_reserved_mem.h @@ -32,6 +32,8 @@ typedef int (*reservedmem_of_init_fn)(struct reserved_mem= *rmem); #define RESERVEDMEM_OF_DECLARE(name, compat, init) \ _OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn) =20 +int of_reserved_mem_device_init_with_mem(struct device *dev, + struct reserved_mem *rmem); int of_reserved_mem_device_init_by_idx(struct device *dev, struct device_node *np, int idx); int of_reserved_mem_device_init_by_name(struct device *dev, @@ -51,6 +53,12 @@ int of_reserved_mem_region_count(const struct device_nod= e *np); #define RESERVEDMEM_OF_DECLARE(name, compat, init) \ _OF_DECLARE_STUB(reservedmem, name, compat, init, reservedmem_of_init_fn) =20 +static inline int of_reserved_mem_device_init_with_mem(struct device *dev, + struct reserved_mem *rmem) +{ + return -EOPNOTSUPP; +} + static inline int of_reserved_mem_device_init_by_idx(struct device *dev, struct device_node *np, int idx) { --=20 2.52.0 From nobody Thu Apr 9 16:36:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 5962F33F5AD for ; Tue, 3 Mar 2026 12:34:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541286; cv=none; b=ZpZ7Dim5i4l2vZcQZGp63sh19+IG/TNbNH5FjAxdArtTynkMma4AKY5yBZmYjtqE/qo733O8aqPsWQidZvqpkfoElBFuDLZRc3CggKe7wi+45iCnXsuAuiSE2tWnQe2J8mNx1K7SIajSgAeMew5Dbwbn25lvN7sGUkIrREtN040= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541286; c=relaxed/simple; bh=18y4x/nLAqgzJYKxeW2JDPFLaF5fjF4DUyDLeQZpv+4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oyWkRf0/ntH8LbGQ1/pC895pYEBNOxNKZqXncL9XxnSUT2h0iXnuxtrmO0EOj7UynNr88ikHw2NMJiDmRApNfnP5IawezMzOh5n+fNNc5oriSNvXJr4IOtSO/kYAClnPmieFRODUMfNcA4WXoFX78sDpXFb2TGt9tNoJwT+D0qY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ER74BQjM; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ER74BQjM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772541283; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mjxxnI+eqW3NrszpVBs1Ajj0xf1dVGpAUt1pVC1eJHg=; b=ER74BQjMmkXeCKnAcOTiUqCDdyXXuvfwFn40/JJptqYG1asJ6X589UWTiHkqUvwc6vE0wg V1TaiGgMmSJBknieY3z9Z+pDe+Dn9IIKTghB+kkccOjHTjUIAUKI191Y3GPcPDqCJkuYWq vRQitFFAuDDEDVPcNMTwxMjZBgfQGFk= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-457-1jncD2fROhS_FLNijQCAog-1; Tue, 03 Mar 2026 07:34:39 -0500 X-MC-Unique: 1jncD2fROhS_FLNijQCAog-1 X-Mimecast-MFC-AGG-ID: 1jncD2fROhS_FLNijQCAog_1772541277 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2260A1956051; Tue, 3 Mar 2026 12:34:37 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0BAD930001B9; Tue, 3 Mar 2026 12:34:30 +0000 (UTC) From: Albert Esteve Date: Tue, 03 Mar 2026 13:33:47 +0100 Subject: [PATCH v2 4/6] dma-buf: heaps: Add Coherent heap to dmabuf heaps Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-b4-dmabuf-heap-coherent-rmem-v2-4-65a4653b3378@redhat.com> References: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> In-Reply-To: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> To: Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Marek Szyprowski , Robin Murphy , Rob Herring , Saravana Kannan Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Albert Esteve , echanude@redhat.com, mripard@redhat.com X-Developer-Signature: v=1; a=ed25519-sha256; t=1772541243; l=15787; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=18y4x/nLAqgzJYKxeW2JDPFLaF5fjF4DUyDLeQZpv+4=; b=Ag+cUxlOfd+bLmQ01YcjKbckD1ff3JcNKZtnNG0aEMhEqPQOitYS91lYUT4uOS87eHg3u8w2b GMImy3dGNWUD+vbNL8Sy3kJaM5HwrbruSuJuiTNjXC5QAIRI7w6dbOs X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Add a dma-buf heap for DT coherent reserved-memory (i.e., 'shared-dma-pool' without 'reusable' property), exposing one heap per region for userspace buffers. The heap binds the heap device to each memory region so coherent allocations use the correct dev->dma_mem, and it defers registration until module_init when normal allocators are available. Signed-off-by: Albert Esteve --- drivers/dma-buf/dma-heap.c | 4 +- drivers/dma-buf/heaps/Kconfig | 9 + drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/coherent_heap.c | 426 ++++++++++++++++++++++++++++++= ++++ include/linux/dma-heap.h | 11 + include/linux/dma-map-ops.h | 7 + 6 files changed, 456 insertions(+), 2 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 88189d4e48561..ba87e5ac16ae2 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -390,8 +390,8 @@ struct dma_heap *dma_heap_add(const struct dma_heap_exp= ort_info *exp_info) =20 heap =3D dma_heap_create(exp_info); if (IS_ERR(heap)) { - pr_err("dma_heap: failed to create heap (%d)\n", PTR_ERR(heap)); - return PTR_ERR(heap); + pr_err("dma_heap: failed to create heap (%ld)\n", PTR_ERR(heap)); + return ERR_CAST(heap); } =20 ret =3D dma_heap_register(heap); diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig index a5eef06c42264..aeb475e585048 100644 --- a/drivers/dma-buf/heaps/Kconfig +++ b/drivers/dma-buf/heaps/Kconfig @@ -12,3 +12,12 @@ config DMABUF_HEAPS_CMA Choose this option to enable dma-buf CMA heap. This heap is backed by the Contiguous Memory Allocator (CMA). If your system has these regions, you should say Y here. + +config DMABUF_HEAPS_COHERENT + bool "DMA-BUF Coherent Reserved-Memory Heap" + depends on DMABUF_HEAPS && OF_RESERVED_MEM && DMA_DECLARE_COHERENT + help + Choose this option to enable coherent reserved-memory dma-buf heaps. + This heap is backed by non-reusable DT "shared-dma-pool" regions. + If your system defines coherent reserved-memory regions, you should + say Y here. diff --git a/drivers/dma-buf/heaps/Makefile b/drivers/dma-buf/heaps/Makefile index 974467791032f..96bda7a65f041 100644 --- a/drivers/dma-buf/heaps/Makefile +++ b/drivers/dma-buf/heaps/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) +=3D system_heap.o obj-$(CONFIG_DMABUF_HEAPS_CMA) +=3D cma_heap.o +obj-$(CONFIG_DMABUF_HEAPS_COHERENT) +=3D coherent_heap.o diff --git a/drivers/dma-buf/heaps/coherent_heap.c b/drivers/dma-buf/heaps/= coherent_heap.c new file mode 100644 index 0000000000000..d033d737bb9df --- /dev/null +++ b/drivers/dma-buf/heaps/coherent_heap.c @@ -0,0 +1,426 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * DMABUF heap for coherent reserved-memory regions + * + * Copyright (C) 2026 Red Hat, Inc. + * Author: Albert Esteve + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct coherent_heap { + struct dma_heap *heap; + struct reserved_mem *rmem; + char *name; +}; + +struct coherent_heap_buffer { + struct coherent_heap *heap; + struct list_head attachments; + struct mutex lock; + unsigned long len; + dma_addr_t dma_addr; + void *alloc_vaddr; + struct page **pages; + pgoff_t pagecount; + int vmap_cnt; + void *vaddr; +}; + +struct dma_heap_attachment { + struct device *dev; + struct sg_table table; + struct list_head list; + bool mapped; +}; + +static int coherent_heap_attach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + struct dma_heap_attachment *a; + int ret; + + a =3D kzalloc_obj(*a); + if (!a) + return -ENOMEM; + + ret =3D sg_alloc_table_from_pages(&a->table, buffer->pages, + buffer->pagecount, 0, + buffer->pagecount << PAGE_SHIFT, + GFP_KERNEL); + if (ret) { + kfree(a); + return ret; + } + + a->dev =3D attachment->dev; + INIT_LIST_HEAD(&a->list); + a->mapped =3D false; + + attachment->priv =3D a; + + mutex_lock(&buffer->lock); + list_add(&a->list, &buffer->attachments); + mutex_unlock(&buffer->lock); + + return 0; +} + +static void coherent_heap_detach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + struct dma_heap_attachment *a =3D attachment->priv; + + mutex_lock(&buffer->lock); + list_del(&a->list); + mutex_unlock(&buffer->lock); + + sg_free_table(&a->table); + kfree(a); +} + +static struct sg_table *coherent_heap_map_dma_buf(struct dma_buf_attachmen= t *attachment, + enum dma_data_direction direction) +{ + struct dma_heap_attachment *a =3D attachment->priv; + struct sg_table *table =3D &a->table; + int ret; + + ret =3D dma_map_sgtable(attachment->dev, table, direction, 0); + if (ret) + return ERR_PTR(-ENOMEM); + a->mapped =3D true; + + return table; +} + +static void coherent_heap_unmap_dma_buf(struct dma_buf_attachment *attachm= ent, + struct sg_table *table, + enum dma_data_direction direction) +{ + struct dma_heap_attachment *a =3D attachment->priv; + + a->mapped =3D false; + dma_unmap_sgtable(attachment->dev, table, direction, 0); +} + +static int coherent_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, + enum dma_data_direction direction) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + struct dma_heap_attachment *a; + + mutex_lock(&buffer->lock); + if (buffer->vmap_cnt) + invalidate_kernel_vmap_range(buffer->vaddr, buffer->len); + + list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; + dma_sync_sgtable_for_cpu(a->dev, &a->table, direction); + } + mutex_unlock(&buffer->lock); + + return 0; +} + +static int coherent_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf, + enum dma_data_direction direction) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + struct dma_heap_attachment *a; + + mutex_lock(&buffer->lock); + if (buffer->vmap_cnt) + flush_kernel_vmap_range(buffer->vaddr, buffer->len); + + list_for_each_entry(a, &buffer->attachments, list) { + if (!a->mapped) + continue; + dma_sync_sgtable_for_device(a->dev, &a->table, direction); + } + mutex_unlock(&buffer->lock); + + return 0; +} + +static int coherent_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struc= t *vma) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + struct coherent_heap *coh_heap =3D buffer->heap; + struct device *heap_dev =3D dma_heap_get_dev(coh_heap->heap); + + return dma_mmap_coherent(heap_dev, vma, buffer->alloc_vaddr, + buffer->dma_addr, buffer->len); +} + +static void *coherent_heap_do_vmap(struct coherent_heap_buffer *buffer) +{ + void *vaddr; + + vaddr =3D vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL); + if (!vaddr) + return ERR_PTR(-ENOMEM); + + return vaddr; +} + +static int coherent_heap_vmap(struct dma_buf *dmabuf, struct iosys_map *ma= p) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + void *vaddr; + int ret =3D 0; + + mutex_lock(&buffer->lock); + if (buffer->vmap_cnt) { + buffer->vmap_cnt++; + iosys_map_set_vaddr(map, buffer->vaddr); + goto out; + } + + vaddr =3D coherent_heap_do_vmap(buffer); + if (IS_ERR(vaddr)) { + ret =3D PTR_ERR(vaddr); + goto out; + } + + buffer->vaddr =3D vaddr; + buffer->vmap_cnt++; + iosys_map_set_vaddr(map, buffer->vaddr); +out: + mutex_unlock(&buffer->lock); + + return ret; +} + +static void coherent_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map = *map) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + + mutex_lock(&buffer->lock); + if (!--buffer->vmap_cnt) { + vunmap(buffer->vaddr); + buffer->vaddr =3D NULL; + } + mutex_unlock(&buffer->lock); + iosys_map_clear(map); +} + +static void coherent_heap_dma_buf_release(struct dma_buf *dmabuf) +{ + struct coherent_heap_buffer *buffer =3D dmabuf->priv; + struct coherent_heap *coh_heap =3D buffer->heap; + struct device *heap_dev =3D dma_heap_get_dev(coh_heap->heap); + + if (buffer->vmap_cnt > 0) { + WARN(1, "%s: buffer still mapped in the kernel\n", __func__); + vunmap(buffer->vaddr); + buffer->vaddr =3D NULL; + buffer->vmap_cnt =3D 0; + } + + if (buffer->alloc_vaddr) + dma_free_coherent(heap_dev, buffer->len, buffer->alloc_vaddr, + buffer->dma_addr); + kfree(buffer->pages); + kfree(buffer); +} + +static const struct dma_buf_ops coherent_heap_buf_ops =3D { + .attach =3D coherent_heap_attach, + .detach =3D coherent_heap_detach, + .map_dma_buf =3D coherent_heap_map_dma_buf, + .unmap_dma_buf =3D coherent_heap_unmap_dma_buf, + .begin_cpu_access =3D coherent_heap_dma_buf_begin_cpu_access, + .end_cpu_access =3D coherent_heap_dma_buf_end_cpu_access, + .mmap =3D coherent_heap_mmap, + .vmap =3D coherent_heap_vmap, + .vunmap =3D coherent_heap_vunmap, + .release =3D coherent_heap_dma_buf_release, +}; + +static struct dma_buf *coherent_heap_allocate(struct dma_heap *heap, + unsigned long len, + u32 fd_flags, + u64 heap_flags) +{ + struct coherent_heap *coh_heap; + struct coherent_heap_buffer *buffer; + struct device *heap_dev; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + size_t size =3D PAGE_ALIGN(len); + pgoff_t pagecount =3D size >> PAGE_SHIFT; + struct dma_buf *dmabuf; + int ret =3D -ENOMEM; + pgoff_t pg; + + coh_heap =3D dma_heap_get_drvdata(heap); + if (!coh_heap) + return ERR_PTR(-EINVAL); + + heap_dev =3D dma_heap_get_dev(coh_heap->heap); + if (!heap_dev) + return ERR_PTR(-ENODEV); + + buffer =3D kzalloc_obj(*buffer); + if (!buffer) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&buffer->attachments); + mutex_init(&buffer->lock); + buffer->len =3D size; + buffer->heap =3D coh_heap; + buffer->pagecount =3D pagecount; + + buffer->alloc_vaddr =3D dma_alloc_coherent(heap_dev, buffer->len, + &buffer->dma_addr, GFP_KERNEL); + if (!buffer->alloc_vaddr) { + ret =3D -ENOMEM; + goto free_buffer; + } + + buffer->pages =3D kmalloc_array(pagecount, sizeof(*buffer->pages), + GFP_KERNEL); + if (!buffer->pages) { + ret =3D -ENOMEM; + goto free_dma; + } + + for (pg =3D 0; pg < pagecount; pg++) + buffer->pages[pg] =3D virt_to_page((char *)buffer->alloc_vaddr + + (pg * PAGE_SIZE)); + + /* create the dmabuf */ + exp_info.exp_name =3D dma_heap_get_name(heap); + exp_info.ops =3D &coherent_heap_buf_ops; + exp_info.size =3D buffer->len; + exp_info.flags =3D fd_flags; + exp_info.priv =3D buffer; + dmabuf =3D dma_buf_export(&exp_info); + if (IS_ERR(dmabuf)) { + ret =3D PTR_ERR(dmabuf); + goto free_pages; + } + return dmabuf; + +free_pages: + kfree(buffer->pages); +free_dma: + dma_free_coherent(heap_dev, buffer->len, buffer->alloc_vaddr, + buffer->dma_addr); +free_buffer: + kfree(buffer); + return ERR_PTR(ret); +} + +static const struct dma_heap_ops coherent_heap_ops =3D { + .allocate =3D coherent_heap_allocate, +}; + +static int coherent_heap_init_dma_mask(struct device *dev) +{ + int ret; + + ret =3D dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(64)); + if (!ret) + return 0; + + /* Fallback to 32-bit DMA mask */ + return dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); +} + +static int __coherent_heap_register(struct reserved_mem *rmem) +{ + struct dma_heap_export_info exp_info; + struct coherent_heap *coh_heap; + struct device *heap_dev; + int ret; + + if (!rmem || !rmem->name) + return -EINVAL; + + coh_heap =3D kzalloc_obj(*coh_heap); + if (!coh_heap) + return -ENOMEM; + + coh_heap->rmem =3D rmem; + coh_heap->name =3D kstrdup(rmem->name, GFP_KERNEL); + if (!coh_heap->name) { + ret =3D -ENOMEM; + goto free_coherent_heap; + } + + exp_info.name =3D coh_heap->name; + exp_info.ops =3D &coherent_heap_ops; + exp_info.priv =3D coh_heap; + + coh_heap->heap =3D dma_heap_create(&exp_info); + if (IS_ERR(coh_heap->heap)) { + ret =3D PTR_ERR(coh_heap->heap); + goto free_name; + } + + heap_dev =3D dma_heap_get_dev(coh_heap->heap); + ret =3D coherent_heap_init_dma_mask(heap_dev); + if (ret) { + pr_err("coherent_heap: failed to set DMA mask (%d)\n", ret); + goto destroy_heap; + } + + ret =3D of_reserved_mem_device_init_with_mem(heap_dev, rmem); + if (ret) { + pr_err("coherent_heap: failed to initialize memory (%d)\n", ret); + goto destroy_heap; + } + + ret =3D dma_heap_register(coh_heap->heap); + if (ret) { + pr_err("coherent_heap: failed to register heap (%d)\n", ret); + goto destroy_heap; + } + + return 0; + +destroy_heap: + dma_heap_destroy(coh_heap->heap); + coh_heap->heap =3D NULL; +free_name: + kfree(coh_heap->name); +free_coherent_heap: + kfree(coh_heap); + + return ret; +} + +static int __init coherent_heap_register(void) +{ + struct reserved_mem *rmem; + unsigned int i; + int ret; + + for (i =3D 0; (rmem =3D dma_coherent_get_reserved_region(i)) !=3D NULL; i= ++) { + ret =3D __coherent_heap_register(rmem); + if (ret) { + pr_warn("Failed to add coherent heap %s", + rmem->name ? rmem->name : "unknown"); + continue; + } + } + + return 0; +} +module_init(coherent_heap_register); +MODULE_DESCRIPTION("DMA-BUF heap for coherent reserved-memory regions"); diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h index 1b0ea43ba66c3..77e6cb66ffce1 100644 --- a/include/linux/dma-heap.h +++ b/include/linux/dma-heap.h @@ -9,10 +9,12 @@ #ifndef _DMA_HEAPS_H #define _DMA_HEAPS_H =20 +#include #include =20 struct dma_heap; struct device; +struct reserved_mem; =20 /** * struct dma_heap_ops - ops to operate on a given heap @@ -53,4 +55,13 @@ struct dma_heap *dma_heap_add(const struct dma_heap_expo= rt_info *exp_info); =20 extern bool mem_accounting; =20 +#if IS_ENABLED(CONFIG_DMABUF_HEAPS_COHERENT) +int dma_heap_coherent_register(struct reserved_mem *rmem); +#else +static inline int dma_heap_coherent_register(struct reserved_mem *rmem) +{ + return -EOPNOTSUPP; +} +#endif + #endif /* _DMA_HEAPS_H */ diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 60b63756df821..c87e5e44e5383 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -12,6 +12,7 @@ =20 struct cma; struct iommu_ops; +struct reserved_mem; =20 struct dma_map_ops { void *(*alloc)(struct device *dev, size_t size, @@ -161,6 +162,7 @@ int dma_alloc_from_dev_coherent(struct device *dev, ssi= ze_t size, int dma_release_from_dev_coherent(struct device *dev, int order, void *vad= dr); int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *= vma, void *cpu_addr, size_t size, int *ret); +struct reserved_mem *dma_coherent_get_reserved_region(unsigned int idx); #else static inline int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, dma_addr_t device_addr, size_t size) @@ -172,6 +174,11 @@ static inline int dma_declare_coherent_memory(struct d= evice *dev, #define dma_release_from_dev_coherent(dev, order, vaddr) (0) #define dma_mmap_from_dev_coherent(dev, vma, vaddr, order, ret) (0) static inline void dma_release_coherent_memory(struct device *dev) { } +static inline +struct reserved_mem *dma_coherent_get_reserved_region(unsigned int idx) +{ + return NULL; +} #endif /* CONFIG_DMA_DECLARE_COHERENT */ =20 #ifdef CONFIG_DMA_GLOBAL_POOL --=20 2.52.0 From nobody Thu Apr 9 16:36:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 5F65B347FF4 for ; Tue, 3 Mar 2026 12:34:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541290; cv=none; b=Rfh2msrEZc6WNgd37jAEyCCZPGPHiXDFKZ3KYsci6j2Q3uZcu78gS40MaGXaE49l119uVfVzIibr3z++TeWh8qa6OdF4GUa0+3Sd1tZmPKwXTRFwnYLUrRzl3YeSu6Fix7c7z5ynxsDJXYlWNYTQeoJHdCkpGDY5RYfgmsKRh8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541290; c=relaxed/simple; bh=lrvc9MWslkCTI2B+p97908kyghT5uZlrlNqzW9O5SpY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WRlgG21lZRrk1PzpMQIc9dE/7eI94u0Ja3Y1nDr+eXhbSaE77US4LCENlHiFVweey8d17r1IKisFs/1kPEiScncYBS9Zv5eqAqVd72I73/jRvalsMB2F6LBa00dP8PcLjFIlAh1sBIcCJrXpWH2TwCZyWI+O+ciieygy4f2DhIA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eCkwmSVV; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eCkwmSVV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772541288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R1UE53XZ4lch6jevT017UAHnSaeVjSTyhkHggOjem0k=; b=eCkwmSVVNJr4vIXurGH9CI3TxFlFdOnywDWNsVDHkE2KTB7MxN8kw6MdwDQokbW3VShqEL QB/LbYdOHx/Uc77NRXX47dZ8oaQ2BOWxE7+KcLLhrEJlMampGC8DhhxnFfHr0qNR/MMPoh qEi7+QgxmLqt3W7raAWNggQMZMlEl64= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-277-U_kfqHQZPh6gdPfelQmprw-1; Tue, 03 Mar 2026 07:34:45 -0500 X-MC-Unique: U_kfqHQZPh6gdPfelQmprw-1 X-Mimecast-MFC-AGG-ID: U_kfqHQZPh6gdPfelQmprw_1772541283 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 542EA1956051; Tue, 3 Mar 2026 12:34:43 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 975DE30001B9; Tue, 3 Mar 2026 12:34:37 +0000 (UTC) From: Albert Esteve Date: Tue, 03 Mar 2026 13:33:48 +0100 Subject: [PATCH v2 5/6] dma: coherent: register to coherent heap Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-b4-dmabuf-heap-coherent-rmem-v2-5-65a4653b3378@redhat.com> References: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> In-Reply-To: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> To: Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Marek Szyprowski , Robin Murphy , Rob Herring , Saravana Kannan Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Albert Esteve , echanude@redhat.com, mripard@redhat.com X-Developer-Signature: v=1; a=ed25519-sha256; t=1772541243; l=2169; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=lrvc9MWslkCTI2B+p97908kyghT5uZlrlNqzW9O5SpY=; b=jLQqlRlnj7PRlCbO13o5sKaHNOI4ilEetAb8IzQq+Dinq87XdXnqlAWW/1/+f0nHz6U9Xht/K eb4/cQpBROmA/RFPjeX810ZnIgdDeGY5Lqjt+uKXdScoH64Zwa45gpV X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Add dma_heap_coherent_register() call within reserved memory DMA setup logic for non-reusable DT nodes. Signed-off-by: Albert Esteve --- kernel/dma/coherent.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/kernel/dma/coherent.c b/kernel/dma/coherent.c index 1147497bc512c..d0d0979ffb153 100644 --- a/kernel/dma/coherent.c +++ b/kernel/dma/coherent.c @@ -9,6 +9,7 @@ #include #include #include +#include =20 struct dma_coherent_mem { void *virt_base; @@ -334,6 +335,31 @@ static phys_addr_t dma_reserved_default_memory_base __= initdata; static phys_addr_t dma_reserved_default_memory_size __initdata; #endif =20 +#define MAX_COHERENT_REGIONS 64 + +static struct reserved_mem *rmem_coherent_areas[MAX_COHERENT_REGIONS]; +static unsigned int rmem_coherent_areas_num; + +static int rmem_coherent_insert_area(struct reserved_mem *rmem) +{ + if (rmem_coherent_areas_num >=3D MAX_COHERENT_REGIONS) { + pr_warn("Deferred heap areas list full, dropping %s\n", + rmem->name ? rmem->name : "unknown"); + return -EINVAL; + } + rmem_coherent_areas[rmem_coherent_areas_num++] =3D rmem; + return 0; +} + +struct reserved_mem *dma_coherent_get_reserved_region(unsigned int idx) +{ + if (idx >=3D rmem_coherent_areas_num) + return NULL; + + return rmem_coherent_areas[idx]; +} +EXPORT_SYMBOL_GPL(dma_coherent_get_reserved_region); + static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *= dev) { struct dma_coherent_mem *mem =3D rmem->priv; @@ -393,6 +419,14 @@ static int __init rmem_dma_setup(struct reserved_mem *= rmem) rmem->ops =3D &rmem_dma_ops; pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n", &rmem->base, (unsigned long)rmem->size / SZ_1M); + + if (IS_ENABLED(CONFIG_DMABUF_HEAPS_COHERENT)) { + int ret =3D rmem_coherent_insert_area(rmem); + + if (ret) + pr_warn("Reserved memory: failed to store coherent area for %s (%d)\n", + rmem->name ? rmem->name : "unknown", ret); + } return 0; } =20 --=20 2.52.0 From nobody Thu Apr 9 16:36:15 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 C633B34A791 for ; Tue, 3 Mar 2026 12:34:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541295; cv=none; b=jdZ3AjMFYZMduVeSmN4BLLg40GBJQEIHB0VhapVKA64jG9QZjk3mEckoLMRwmyDQ5DmbWff8BEa+PCOLxdafnnNIlywoJXwbKLNAZ1ngJUu2hQmkHLi8AYCvGh5pjAN8NxlF7SPmiNWI9OXQhb1HQonpHbbsZzPlF+1YCzUSdxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772541295; c=relaxed/simple; bh=VpcxdAAdPOpdXHrvDAVNa63WDfXimLnTVF72oWUsomU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tGlcmeqbIGEUyZQ5qpuKCBnnmtH4Px+q9uPUzc6eos9SCeFbrPaQ/RGQpbmFlrLACotd3asagHLDQrx4knztrzRONYBTIoPKx8m31l9hAt+UzvXMt+UAEbmnVF01UaIguXHY2LZEEl+MlHs3eVHLRoX4+YqQlyyWWLQcekahSRQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=CO10ouIL; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="CO10ouIL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772541293; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tlBrU2TSGeDaYB6/j/AShAP1tI2ewUQw53Z0nN5L9EY=; b=CO10ouILaOn2MvfBIfI1yRFLvC/ZPTKZZMMjZ8ZLrRllUKg2xOqe5z9GyBkkx0aE+Va1wf aiHbGggqB3mxNz80fzWwd97fbIZ4wXRc2DQML5SedqQ+q+Z7zfRGe8qsqqcYieqQVGyUv7 ZjWmi+Gs7K7/IGGFKGYbAnGSavISpYg= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-310-hBIsd64nMoeGa24QQzArnQ-1; Tue, 03 Mar 2026 07:34:52 -0500 X-MC-Unique: hBIsd64nMoeGa24QQzArnQ-1 X-Mimecast-MFC-AGG-ID: hBIsd64nMoeGa24QQzArnQ_1772541290 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B63E21800451; Tue, 3 Mar 2026 12:34:49 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C868930001BF; Tue, 3 Mar 2026 12:34:43 +0000 (UTC) From: Albert Esteve Date: Tue, 03 Mar 2026 13:33:49 +0100 Subject: [PATCH v2 6/6] dma-buf: heaps: coherent: Turn heap into a module Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-b4-dmabuf-heap-coherent-rmem-v2-6-65a4653b3378@redhat.com> References: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> In-Reply-To: <20260303-b4-dmabuf-heap-coherent-rmem-v2-0-65a4653b3378@redhat.com> To: Sumit Semwal , Benjamin Gaignard , Brian Starkey , John Stultz , "T.J. Mercier" , =?utf-8?q?Christian_K=C3=B6nig?= , Marek Szyprowski , Robin Murphy , Rob Herring , Saravana Kannan Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, iommu@lists.linux.dev, devicetree@vger.kernel.org, Albert Esteve , echanude@redhat.com, mripard@redhat.com X-Developer-Signature: v=1; a=ed25519-sha256; t=1772541243; l=1661; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=VpcxdAAdPOpdXHrvDAVNa63WDfXimLnTVF72oWUsomU=; b=QxLarHWId/17xsaNHf0kOi/7HBFfW/egmdOu/FyJddRAF7ngzFqWmINEC5Qr2jLAbTmJHKsWf pslZnCKJzvSCDN0Mk2i5dfcBvOuyHDZZAcrLTIFyGGITZiyHm0McL7b X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Following the current efforts to make CMA heap as module, we can do the same and turn the Coherent heap into a module as well, by changing the Kconfig into a tristate and importing the proper dma-buf namespaces. This heap won't be able to unload (same as happens with the CMA heap), since we're missing a big part of the infrastructure that would allow to make it safe. Signed-off-by: Albert Esteve --- drivers/dma-buf/heaps/Kconfig | 2 +- drivers/dma-buf/heaps/coherent_heap.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig index aeb475e585048..2f84a1018b900 100644 --- a/drivers/dma-buf/heaps/Kconfig +++ b/drivers/dma-buf/heaps/Kconfig @@ -14,7 +14,7 @@ config DMABUF_HEAPS_CMA regions, you should say Y here. =20 config DMABUF_HEAPS_COHERENT - bool "DMA-BUF Coherent Reserved-Memory Heap" + tristate "DMA-BUF Coherent Reserved-Memory Heap" depends on DMABUF_HEAPS && OF_RESERVED_MEM && DMA_DECLARE_COHERENT help Choose this option to enable coherent reserved-memory dma-buf heaps. diff --git a/drivers/dma-buf/heaps/coherent_heap.c b/drivers/dma-buf/heaps/= coherent_heap.c index d033d737bb9df..cdf8efa6c1564 100644 --- a/drivers/dma-buf/heaps/coherent_heap.c +++ b/drivers/dma-buf/heaps/coherent_heap.c @@ -424,3 +424,6 @@ static int __init coherent_heap_register(void) } module_init(coherent_heap_register); MODULE_DESCRIPTION("DMA-BUF heap for coherent reserved-memory regions"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("DMA_BUF"); +MODULE_IMPORT_NS("DMA_BUF_HEAP"); --=20 2.52.0