dma-buf: heaps: Support carved-out heaps
v2 cover dma-buf: heaps: Support carved-out heaps
Hi,
This series is the follow-up of the discussion that John and I had some time ago here:
https://lore.kernel.org/all/CANDhNCquJn6bH3KxKf65BWiTYLVqSd9892-xtFDHHqqyrroCMQ@mail.gmail.com/
The initial problem we were discussing was that I'm currently working on a platform which has a memory layout with ECC enabled. However, enabling the ECC has a number of drawbacks on that platform: lower performance, increased memory usage, etc. So for things like framebuffers, the trade-off isn't great and thus there's a memory region with ECC disabled to allocate from for such use cases.
After a suggestion from John, I chose to first start using heap allocations flags to allow for userspace to ask for a particular ECC setup. This is then backed by a new heap type that runs from reserved memory chunks flagged as such, and the existing DT properties to specify the ECC properties.
After further discussion, it was considered that flags were not the right solution, and relying on the names of the heaps would be enough to let userspace know the kind of buffer it deals with.
Thus, even though the uAPI part of it has been dropped in this second version, we still need a driver to create heaps out of carved-out memory regions. In addition to the original usecase, a similar driver can be found in BSPs from most vendors, so I believe it would be a useful addition to the kernel.
I submitted a draft PR to the DT schema for the bindings used in this PR:
https://github.com/devicetree-org/dt-schema/pull/138
Let me know what you think,
Maxime
---
Changes in v2:
- Add vmap/vunmap operations
- Drop ECC flags uapi
- Rebase on top of 6.14
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Maxime Ripard (2):
dma-buf: heaps: system: Remove global variable
dma-buf: heaps: Introduce a new heap for reserved memory
drivers/dma-buf/heaps/Kconfig | 8 + drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/carveout_heap.c | 360 ++++++++++++++++++++++++++++++++++ drivers/dma-buf/heaps/system_heap.c | 17 +- 4 files changed, 381 insertions(+), 5 deletions(-)
---
base-commit: fcbf30774e82a441890b722bf0c26542fb82150f
change-id: 20240515-dma-buf-ecc-heap-28a311d2c94e
Best regards,
Signature · 1 line
Maxime Ripard <[email protected]>
PATCH v2 1/2 dma-buf: heaps: system: Remove global variable
The system heap has been using its struct dma_heap pointer but wasn't using it anywhere.
Since we'll need additional parameters to attach to that heap type, let's create a private structure and set it as the dma_heap drvdata, removing the global variable in the process.
--- drivers/dma-buf/heaps/system_heap.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/dma-buf/heaps/system_heap.c +12 -5
@@ -19,11 +19,13 @@#include <linux/module.h> #include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/vmalloc.h> -static struct dma_heap *sys_heap; +struct system_heap { +struct dma_heap *heap; +};struct system_heap_buffer {struct dma_heap *heap;struct list_head attachments;struct mutex lock;
@@ -422,17 +424,22 @@ static const struct dma_heap_ops system_heap_ops = { };static int __init system_heap_create (void ) {struct dma_heap_export_info exp_info; +struct system_heap *sys_heap; + + sys_heap =kzalloc (sizeof (*sys_heap),GFP_KERNEL ); +if (!sys_heap) +return -ENOMEM ; exp_info.name ="system" ; exp_info.ops = &system_heap_ops; - exp_info.priv =NULL ; + exp_info.priv = sys_heap; - sys_heap =dma_heap_add (&exp_info); -if (IS_ERR (sys_heap)) -return PTR_ERR (sys_heap); + sys_heap->heap =dma_heap_add (&exp_info); +if (IS_ERR (sys_heap->heap)) +return PTR_ERR (sys_heap->heap);return 0 ; }module_init (system_heap_create);
Signature · 1 line
2.49.0
PATCH v2 2/2 dma-buf: heaps: Introduce a new heap for reserved memory
In reply to Maxime RipardSome reserved memory regions might have particular memory setup or attributes that make them good candidates for heaps.
Let's provide a heap type that will create a new heap for each reserved memory region flagged as such.
--- drivers/dma-buf/heaps/Kconfig | 8 + drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/carveout_heap.c | 360 ++++++++++++++++++++++++++++++++++ 3 files changed, 369 insertions(+)
drivers/dma-buf/heaps/Kconfig +8 -0
@@ -1,5 +1,13 @@ +config DMABUF_HEAPS_CARVEOUT + bool "Carveout Heaps" + depends on DMABUF_HEAPS + help + Choose this option to enable the carveout dmabuf heap. The carveout + heap is backed by pages from reserved memory regions flagged as + exportable. If in doubt, say Y. + config DMABUF_HEAPS_SYSTEM bool "DMA-BUF System Heap" depends on DMABUF_HEAPS help Choose this option to enable the system dmabuf heap. The system heap
drivers/dma-buf/heaps/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_DMABUF_HEAPS_CARVEOUT) += carveout_heap.o obj-$(CONFIG_DMABUF_HEAPS_SYSTEM) += system_heap.o obj-$(CONFIG_DMABUF_HEAPS_CMA) += cma_heap.o
drivers/dma-buf/heaps/carveout_heap.c new file +360 -0
360 lines not on this page · raw message
Signature · 1 line
2.49.0
Christian König
maintainerRe: [PATCH v2 1/2] dma-buf: heaps: system: Remove global variable
In reply to Maxime RipardAm 01.04.25 um 17:12 schrieb Maxime Ripard:
Quoted 37 lines
The system heap has been using its struct
dma_heappointer but wasn't using it anywhere.Since we'll need additional parameters to attach to that heap type, let's create a private structure and set it as the
dma_heapdrvdata, removing the global variable in the process.Signed-off-by Maxime Ripard <[email protected]>--- drivers/dma-buf/heaps/system_heap.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/dma-buf/heaps/system_heap.c +3
-1@@ -19,11 +19,13 @@#include <linux/module.h> #include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/vmalloc.h> -static struct dma_heap *sys_heap; +struct system_heap { +struct dma_heap *heap; +};struct system_heap_buffer {struct dma_heap *heap;struct list_head attachments;struct mutex lock;@@ -422,17 +424,22 @@ static const struct dma_heap_ops system_heap_ops = { };static int __init system_heap_create (void ) {struct dma_heap_export_info exp_info;
drivers/dma-buf/heaps/system_heap.c static const struct dma_heap_ops system_heap_ops = { · line 437
Quoted 10 lines
drivers/dma-buf/heaps/system_heap.c +6
-1+struct system_heap *sys_heap; + + sys_heap =kzalloc (sizeof (*sys_heap),GFP_KERNEL ); +if (!sys_heap) +return -ENOMEM ; exp_info.name ="system" ; exp_info.ops = &system_heap_ops; - exp_info.priv =NULL ; + exp_info.priv = sys_heap;
Why do you even need this?
drivers/dma-buf/heaps/system_heap.c static const struct dma_heap_ops system_heap_ops = { · line 441
Quoted 7 lines
drivers/dma-buf/heaps/system_heap.c +3
-3- sys_heap =dma_heap_add (&exp_info); -if (IS_ERR (sys_heap)) -return PTR_ERR (sys_heap); + sys_heap->heap =dma_heap_add (&exp_info); +if (IS_ERR (sys_heap->heap)) +return PTR_ERR (sys_heap->heap);
That's clearly missing freeing sys_heap again.
Christian.
Quoted 5 lines
drivers/dma-buf/heaps/system_heap.c +0
-0return 0 ; }module_init (system_heap_create);
Re: [PATCH v2 1/2] dma-buf: heaps: system: Remove global variable
Quoted 46 lines · Christian König
Am 01.04.25 um 17:12 schrieb Maxime Ripard:
Quoted 43 lines
The system heap has been using its struct
dma_heappointer but wasn't using it anywhere.Since we'll need additional parameters to attach to that heap type, let's create a private structure and set it as the
dma_heapdrvdata, removing the global variable in the process.Signed-off-by Maxime Ripard <[email protected]>--- drivers/dma-buf/heaps/system_heap.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)drivers/dma-buf/heaps/system_heap.c +8
-1@@ -19,11 +19,13 @@#include <linux/module.h> #include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/vmalloc.h> -static struct dma_heap *sys_heap; +struct system_heap { +struct dma_heap *heap; +};struct system_heap_buffer {struct dma_heap *heap;struct list_head attachments;struct mutex lock;@@ -422,17 +424,22 @@ static const struct dma_heap_ops system_heap_ops = { };static int __init system_heap_create (void ) {struct dma_heap_export_info exp_info; +struct system_heap *sys_heap; + + sys_heap =kzalloc (sizeof (*sys_heap),GFP_KERNEL ); +if (!sys_heap) +return -ENOMEM ;
Quoted 6 lines
drivers/dma-buf/heaps/system_heap.c static const struct dma_heap_ops system_heap_ops = { · line 437
Quoted 4 lines
drivers/dma-buf/heaps/system_heap.c +1
-1exp_info.name ="system" ; exp_info.ops = &system_heap_ops; - exp_info.priv =NULL ; + exp_info.priv = sys_heap;Why do you even need this?
Urgh, sorry. I'm not even sure how I ended up with that patch, but you're correct. I'll send a much simpler version.
Maxime