Skip to contentSkip to conversation

DMA-BUF Heaps (destaging ION)

PATCH Superseded John Stultz 3 participants 10 messages Jul 24, 2019 – Jul 25, 2019

Subsystems (3)

v7 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v7 cover DMA-BUF Heaps (destaging ION)

Here is yet another pass at the dma-buf heaps patchset Andrew and I have been working on which tries to destage a fair chunk of ION functionality.

The patchset implements per-heap devices which can be opened directly and then an ioctl is used to allocate a dmabuf from the heap.

The interface is similar, but much simpler then IONs, only providing an ALLOC ioctl.

Also, I've provided relatively simple system and cma heaps.

I've booted and tested these patches with AOSP on the HiKey960 using the kernel tree here:
https://git.linaro.org/people/john.stultz/android-dev.git/log/?h=dev/dma-buf-heap

And the userspace changes here:
https://android-review.googlesource.com/c/device/linaro/hikey/+/909436

Compared to ION, this patchset is missing the system-contig, carveout and chunk heaps, as I don't have a device that uses those, so I'm unable to do much useful validation there. Additionally we have no upstream users of chunk or carveout, and the system-contig has been deprecated in the common/andoid-* kernels, so this should be ok.

I've also removed the stats accounting, since any such accounting should be implemented by dma-buf core or the heaps themselves.

New in v7:
* Longer Kconfig description to quiet checkpatch warnings
* Re-add compat_ioctl bits (Hridya noticed 32bit userland wasn't working)
* Make init_heap_helper_buffer lowercase, as suggested by Christoph
* Add dmabuf export helper to reduce boilerplate code
* Add system heap DOS avoidance suggested by Laura from ION code

Thoughts and feedback would be greatly appreciated!

thanks
-john

Cc Laura Abbott <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Liam Mark <[email protected]>
Cc Pratik Patel <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Vincent Donnefort <[email protected]>
Cc Sudipto Paul <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Cc Hridya Valsaraju <[email protected]>

Andrew F. Davis (1):
dma-buf: Add dma-buf heaps framework

John Stultz (4):
dma-buf: heaps: Add heap helpers
dma-buf: heaps: Add system heap to dmabuf heaps
dma-buf: heaps: Add CMA heap to dmabuf heaps
kselftests: Add dma-heap test

 MAINTAINERS                                   |  18 ++
 drivers/dma-buf/Kconfig                       |  11 +
 drivers/dma-buf/Makefile                      |   2 +
 drivers/dma-buf/dma-heap.c                    | 252 ++++++++++++++++
 drivers/dma-buf/heaps/Kconfig                 |  14 +
 drivers/dma-buf/heaps/Makefile                |   4 +
 drivers/dma-buf/heaps/cma_heap.c              | 164 +++++++++++
 drivers/dma-buf/heaps/heap-helpers.c          | 276 ++++++++++++++++++
 drivers/dma-buf/heaps/heap-helpers.h          |  58 ++++
 drivers/dma-buf/heaps/system_heap.c           | 123 ++++++++
 include/linux/dma-heap.h                      |  59 ++++
 include/uapi/linux/dma-heap.h                 |  55 ++++
 tools/testing/selftests/dmabuf-heaps/Makefile |   9 +
 .../selftests/dmabuf-heaps/dmabuf-heap.c      | 235 +++++++++++++++
 14 files changed, 1280 insertions(+)
 create mode 100644 drivers/dma-buf/dma-heap.c
 create mode 100644 drivers/dma-buf/heaps/Kconfig
 create mode 100644 drivers/dma-buf/heaps/Makefile
 create mode 100644 drivers/dma-buf/heaps/cma_heap.c
 create mode 100644 drivers/dma-buf/heaps/heap-helpers.c
 create mode 100644 drivers/dma-buf/heaps/heap-helpers.h
 create mode 100644 drivers/dma-buf/heaps/system_heap.c
 create mode 100644 include/linux/dma-heap.h
 create mode 100644 include/uapi/linux/dma-heap.h
 create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile
 create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH v7 1/5 dma-buf: Add dma-buf heaps framework

From: "Andrew F. Davis" <[email protected]>

This framework allows a unified userspace interface for dma-buf exporters, allowing userland to allocate specific types of memory for use in dma-buf sharing.

Each heap is given its own device node, which a user can allocate a dma-buf fd from using the DMA_HEAP_IOC_ALLOC.

This code is an evoluiton of the Android ION implementation, and a big thanks is due to its authors/maintainers over time for their effort:
Rebecca Schultz Zavin, Colin Cross, Benjamin Gaignard, Laura Abbott, and many other contributors!

Cc Laura Abbott <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Liam Mark <[email protected]>
Cc Pratik Patel <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Vincent Donnefort <[email protected]>
Cc Sudipto Paul <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Cc Hridya Valsaraju <[email protected]>

---
v2:
* Folded down fixes I had previously shared in implementing heaps
* Make flags a u64 (Suggested by Laura)
* Add PAGE_ALIGN() fix to the core alloc funciton
* IOCTL fixups suggested by Brian
* Added fixes suggested by Benjamin
* Removed core stats mgmt, as that should be implemented by per-heap code
* Changed alloc to return a dma-buf fd, rather than a buffer (as it simplifies error handling)
v3:
* Removed scare-quotes in MAINTAINERS email address
* Get rid of .release function as it didn't do anything (from Christoph)
* Renamed filp to file (suggested by Christoph)
* Split out ioctl handling to separate function (suggested by Christoph)
* Add comment documenting PAGE_ALIGN usage (suggested by Brian)
* Switch from idr to Xarray (suggested by Brian)
* Fixup cdev creation (suggested by Brian)
* Avoid EXPORT_SYMBOL until we finalize modules (suggested by Brian)
* Make struct dma_heap internal only (folded in from Andrew)
* Small cleanups suggested by GregKH
* Provide class->devnode callback to get consistent /dev/ subdirectory naming (Suggested by Bjorn)
v4:
* Folded down dma-heap.h change that was in a following patch
* Added fd_flags entry to allocation structure and pass it through to heap code for use on dma-buf fd creation (suggested by Benjamin)
v5:
* Minor cleanups
v6:
* Improved error path handling, minor whitespace fixes, both suggested by Brian
v7:
* Longer Kconfig description to quiet checkpatch warnings
* Re-add compat_ioctl bits (Hridya noticed 32bit userland wasn't working)

---
 MAINTAINERS                   |  18 +++
 drivers/dma-buf/Kconfig       |   9 ++
 drivers/dma-buf/Makefile      |   1 +
 drivers/dma-buf/dma-heap.c    | 252 ++++++++++++++++++++++++++++++++++
 include/linux/dma-heap.h      |  59 ++++++++
 include/uapi/linux/dma-heap.h |  55 ++++++++
 6 files changed, 394 insertions(+)
 create mode 100644 drivers/dma-buf/dma-heap.c
 create mode 100644 include/linux/dma-heap.h
 create mode 100644 include/uapi/linux/dma-heap.h
MAINTAINERS +18 -0
index 783569e3c4b4..189f1973b5d6 100644
@@ -4900,6 +4900,24 @@ F:	include/linux/*fence.h
 F:	Documentation/driver-api/dma-buf.rst
 T:	git git://anongit.freedesktop.org/drm/drm-misc
 
+DMA-BUF HEAPS FRAMEWORK
+M:	Sumit Semwal <[email protected]>
+R:	Andrew F. Davis <[email protected]>
+R:	Benjamin Gaignard <[email protected]>
+R:	Liam Mark <[email protected]>
+R:	Laura Abbott <[email protected]>
+R:	Brian Starkey <[email protected]>
+R:	John Stultz <[email protected]>
+S:	Maintained
+L:	[email protected]
+L:	[email protected]
+L:	[email protected] (moderated for non-subscribers)
+F:	include/uapi/linux/dma-heap.h
+F:	include/linux/dma-heap.h
+F:	drivers/dma-buf/dma-heap.c
+F:	drivers/dma-buf/heaps/*
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+
 DMA GENERIC OFFLOAD ENGINE SUBSYSTEM
 M:	Vinod Koul <[email protected]>
 L:	[email protected]
drivers/dma-buf/Kconfig +9 -0
index b6a9c2f1bc41..162e24e1e429 100644
@@ -39,4 +39,13 @@ config UDMABUF
 	  A driver to let userspace turn memfd regions into dma-bufs.
 	  Qemu can use this to create host dmabufs for guest framebuffers.
 
+menuconfig DMABUF_HEAPS
+	bool "DMA-BUF Userland Memory Heaps"
+	select DMA_SHARED_BUFFER
+	help
+	  Choose this option to enable the DMA-BUF userland memory heaps,
+	  this options creates per heap chardevs in /dev/dma_heap/ which
+	  allows userspace to use to allocate dma-bufs that can be shared
+	  between drivers.
+
 endmenu
drivers/dma-buf/Makefile +1 -0
index e8c7310cb800..1cb3dd104825 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
 	 reservation.o seqno-fence.o
+obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
 obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
 obj-$(CONFIG_UDMABUF)		+= udmabuf.o
drivers/dma-buf/dma-heap.c new file +252 -0

252 lines not on this page · raw message

include/linux/dma-heap.h new file +59 -0

59 lines not on this page · raw message

include/uapi/linux/dma-heap.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

PATCH v7 2/5 dma-buf: heaps: Add heap helpers

In reply to John Stultz

Add generic helper dmabuf ops for dma heaps, so we can reduce the amount of duplicative code for the exported dmabufs.

This code is an evolution of the Android ION implementation, so thanks to its original authors and maintainters:
Rebecca Schultz Zavin, Colin Cross, Laura Abbott, and others!

Cc Laura Abbott <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Liam Mark <[email protected]>
Cc Pratik Patel <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Vincent Donnefort <[email protected]>
Cc Sudipto Paul <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Cc Hridya Valsaraju <[email protected]>

---
v2:
* Removed cache management performance hack that I had accidentally folded in.
* Removed stats code that was in helpers
* Lots of checkpatch cleanups
v3:
* Uninline INIT_HEAP_HELPER_BUFFER (suggested by Christoph)
* Switch to WARN on buffer destroy failure (suggested by Brian)
* buffer->kmap_cnt decrementing cleanup (suggested by Christoph)
* Extra buffer->vaddr checking in dma_heap_dma_buf_kmap (suggested by Brian)
* Switch to_helper_buffer from macro to inline function (suggested by Benjamin)
* Rename kmap->vmap (folded in from Andrew)
* Use vmap for vmapping - not begin_cpu_access (folded in from Andrew)
* Drop kmap for now, as its optional (folded in from Andrew)
* Fold dma_heap_map_user into the single caller (foled in from Andrew)
* Folded in patch from Andrew to track page list per heap not sglist, which simplifies the tracking logic
v4:
* Moved dma-heap.h change out to previous patch
v6:
* Minor cleanups and typo fixes suggested by Brian
v7:
* Removed stray ;
* Make init_heap_helper_buffer lowercase, as suggested by Christoph
* Add dmabuf export helper to reduce boilerplate code

---
 drivers/dma-buf/Makefile             |   1 +
 drivers/dma-buf/heaps/Makefile       |   2 +
 drivers/dma-buf/heaps/heap-helpers.c | 276 +++++++++++++++++++++++++++
 drivers/dma-buf/heaps/heap-helpers.h |  58 ++++++
 4 files changed, 337 insertions(+)
 create mode 100644 drivers/dma-buf/heaps/Makefile
 create mode 100644 drivers/dma-buf/heaps/heap-helpers.c
 create mode 100644 drivers/dma-buf/heaps/heap-helpers.h
drivers/dma-buf/Makefile +1 -0
index 1cb3dd104825..e3e3dca29e46 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
 	 reservation.o seqno-fence.o
+obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
 obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
 obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
drivers/dma-buf/heaps/Makefile new file +2 -0
new file mode 100644
index 000000000000..de49898112db
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-y					+= heap-helpers.o
drivers/dma-buf/heaps/heap-helpers.c new file +276 -0

276 lines not on this page · raw message

drivers/dma-buf/heaps/heap-helpers.h new file +58 -0

58 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

Re: [PATCH v7 2/5] dma-buf: heaps: Add heap helpers

drivers/dma-buf/heaps/heap-helpers.c line 29

Quoted 3 lines
drivers/dma-buf/heaps/heap-helpers.c new file +3 -0
+struct dma_buf *heap_helper_export_dmabuf(
+				struct heap_helper_buffer *helper_buffer,
+				int fd_flags)

Indentation seems odd here as it doesn't follow any of the usual schools for multi-level prototypes. But maybe shortening some identifier would help avoiding that problem altogether :)

drivers/dma-buf/heaps/heap-helpers.c line 50

Quoted 10 lines
drivers/dma-buf/heaps/heap-helpers.c new file +10 -0
+static void *dma_heap_map_kernel(struct heap_helper_buffer *buffer)
+{
+	void *vaddr;
+
+	vaddr = vmap(buffer->pages, buffer->pagecount, VM_MAP, PAGE_KERNEL);
+	if (!vaddr)
+		return ERR_PTR(-ENOMEM);
+
+	return vaddr;
+}

Note that a lot of systems don't have highmem at all or don't support CMA in highmem. So for contigous allocations that aren't highmem we could skip the vmap here altogether and just use the direct mapping.

#4 lore raw

PATCH v7 3/5 dma-buf: heaps: Add system heap to dmabuf heaps

In reply to John Stultz

This patch adds system heap to the dma-buf heaps framework.

This allows applications to get a page-allocator backed dma-buf for non-contiguous memory.

This code is an evolution of the Android ION implementation, so thanks to its original authors and maintainters:
Rebecca Schultz Zavin, Colin Cross, Laura Abbott, and others!

Cc Laura Abbott <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Liam Mark <[email protected]>
Cc Pratik Patel <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Vincent Donnefort <[email protected]>
Cc Sudipto Paul <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Cc Hridya Valsaraju <[email protected]>

---
v2:
* Switch allocate to return dmabuf fd
* Simplify init code
* Checkpatch fixups
* Droped dead system-contig code
v3:
* Whitespace fixups from Benjamin
* Make sure we're zeroing the allocated pages (from Liam)
* Use PAGE_ALIGN() consistently (suggested by Brian)
* Fold in new registration style from Andrew
* Avoid needless dynamic allocation of sys_heap (suggested by Christoph)
* Minor cleanups
* Folded in changes from Andrew to use simplified page list from the heap helpers
v4:
* Optimization to allocate pages in chunks, similar to old pagepool code
* Use fd_flags when creating dmabuf fd (Suggested by Benjamin) v5:
* Back out large order page allocations (was leaking memory, as the page array didn't properly track order size)
v6:
* Minor whitespace change suggested by Brian
* Remove unused variable
v7:
* Use newly lower-cased init_heap_helper_buffer helper
* Add system heap DOS avoidance suggested by Laura from ION code
* Use new dmabuf export helper

---
 drivers/dma-buf/Kconfig             |   2 +
 drivers/dma-buf/heaps/Kconfig       |   6 ++
 drivers/dma-buf/heaps/Makefile      |   1 +
 drivers/dma-buf/heaps/system_heap.c | 123 ++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+)
 create mode 100644 drivers/dma-buf/heaps/Kconfig
 create mode 100644 drivers/dma-buf/heaps/system_heap.c
drivers/dma-buf/Kconfig +2 -0
index 162e24e1e429..657ce743abda 100644
@@ -48,4 +48,6 @@ menuconfig DMABUF_HEAPS
 	  allows userspace to use to allocate dma-bufs that can be shared
 	  between drivers.
 
+source "drivers/dma-buf/heaps/Kconfig"
+
 endmenu
drivers/dma-buf/heaps/Kconfig new file +6 -0
new file mode 100644
index 000000000000..205052744169
@@ -0,0 +1,6 @@
+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
+	  is backed by pages from the buddy allocator. If in doubt, say Y.
drivers/dma-buf/heaps/Makefile +1 -0
index de49898112db..d1808eca2581 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-y					+= heap-helpers.o
+obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)	+= system_heap.o
drivers/dma-buf/heaps/system_heap.c new file +123 -0

123 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

Re: [PATCH v7 3/5] dma-buf: heaps: Add system heap to dmabuf heaps

drivers/dma-buf/heaps/system_heap.c line 23

Quoted 3 lines
drivers/dma-buf/heaps/system_heap.c new file +3 -0
+struct system_heap {
+	struct dma_heap *heap;
+} sys_heap;

It seems like this structure could be removed and if would improve the code flow.

Quoted 8 lines
drivers/dma-buf/heaps/system_heap.c new file +8 -0
+static struct dma_heap_ops system_heap_ops = {
+	.allocate = system_heap_allocate,
+};
+
+static int system_heap_create(void)
+{
+	struct dma_heap_export_info exp_info;
+	int ret = 0;

drivers/dma-buf/heaps/system_heap.c line 121

Quoted 10 lines
drivers/dma-buf/heaps/system_heap.c new file +10 -0
+
+	exp_info.name = "system_heap";
+	exp_info.ops = &system_heap_ops;
+	exp_info.priv = &sys_heap;
+
+	sys_heap.heap = dma_heap_add(&exp_info);
+	if (IS_ERR(sys_heap.heap))
+		ret = PTR_ERR(sys_heap.heap);
+
+	return ret;

The data structures here seem a little odd. I think you want to:

- mark all dma_heap_ops instanes consts, as we generally do that for all structures containing function pointers
- move the name into dma_heap_ops.
- remove the dma_heap_export_info structure, which is a bit pointless
- don't bother setting a private data, as you don't need it. If other heaps need private data I'd suggest to switch to embedding the dma_heap structure into containing structure insted so that you can use container_of to get at it.
- also why is the free callback passed as a callback rather than kept in dma_heap_ops, next to the paired alloc one?

#6 lore raw

Re: [PATCH v7 3/5] dma-buf: heaps: Add system heap to dmabuf heaps

Quoted 28 lines · Christoph Hellwig

drivers/dma-buf/heaps/system_heap.c line 23

Quoted 3 lines
drivers/dma-buf/heaps/system_heap.c new file +3 -0
+struct system_heap {
+	struct dma_heap *heap;
+} sys_heap;

It seems like this structure could be removed and if would improve the code flow.

Quoted 8 lines
drivers/dma-buf/heaps/system_heap.c new file +8 -0
+static struct dma_heap_ops system_heap_ops = {
+	.allocate = system_heap_allocate,
+};
+
+static int system_heap_create(void)
+{
+	struct dma_heap_export_info exp_info;
+	int ret = 0;

drivers/dma-buf/heaps/system_heap.c line 121

Quoted 10 lines
drivers/dma-buf/heaps/system_heap.c new file +10 -0
+
+	exp_info.name = "system_heap";
+	exp_info.ops = &system_heap_ops;
+	exp_info.priv = &sys_heap;
+
+	sys_heap.heap = dma_heap_add(&exp_info);
+	if (IS_ERR(sys_heap.heap))
+		ret = PTR_ERR(sys_heap.heap);
+
+	return ret;

The data structures here seem a little odd. I think you want to:

Quoted 4 lines

- mark all dma_heap_ops instanes consts, as we generally do that for all structures containing function pointers
- move the name into dma_heap_ops.
- remove the dma_heap_export_info structure, which is a bit pointless

The idea here is to keep the struct dma_heap as an opaque pointer for everyone but the core framework. No one should be touching the guts of that struct (would be 'private' if we were using C++ but this is the best we can do with C), exposing it to the exporters or the importers will break this isolation.

This export style also matches DMA-BUF: you pass in a filled out _export struct and it passes back a dma_buf handle. DMA-BUF unfortunately made the internals of that struct public so they are widely used directly (and abused in some cases) and that prevents the internals from being easily refactored when needed.

Andrew

Quoted 7 lines

- don't bother setting a private data, as you don't need it. If other heaps need private data I'd suggest to switch to embedding the dma_heap structure into containing structure insted so that you can use container_of to get at it.
- also why is the free callback passed as a callback rather than kept in dma_heap_ops, next to the paired alloc one?

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

Re: [PATCH v7 3/5] dma-buf: heaps: Add system heap to dmabuf heaps

In reply to Christoph Hellwig
Quoted 7 lines · Christoph Hellwig

drivers/dma-buf/heaps/system_heap.c line 23

Quoted 4 lines
drivers/dma-buf/heaps/system_heap.c new file +3 -0
+struct system_heap {
+     struct dma_heap *heap;
+} sys_heap;

It seems like this structure could be removed and if would improve the code flow.

Good point. We actually keep a few things in the cma version of this, and I think I copied that over when I started here, but never cleaned it up.

Quoted 12 lines
Quoted 12 lines
drivers/dma-buf/heaps/system_heap.c new file +12 -0
+static struct dma_heap_ops system_heap_ops = {
+     .allocate = system_heap_allocate,
+};
+
+static int system_heap_create(void)
+{
+     struct dma_heap_export_info exp_info;
+     int ret = 0;
+
+     exp_info.name = "system_heap";
+     exp_info.ops = &system_heap_ops;
+     exp_info.priv = &sys_heap;
Quoted 8 lines

drivers/dma-buf/heaps/system_heap.c line 121

Quoted 6 lines
drivers/dma-buf/heaps/system_heap.c new file +6 -0
+
+     sys_heap.heap = dma_heap_add(&exp_info);
+     if (IS_ERR(sys_heap.heap))
+             ret = PTR_ERR(sys_heap.heap);
+
+     return ret;

The data structures here seem a little odd. I think you want to:

Yea. There is some awkwardness, and some is due to using the helper infrastructure, but some is just clutter and I'll revise that.

Quoted 2 lines

- mark all dma_heap_ops instanes consts, as we generally do that for all structures containing function pointers

Done.

Quoted 1 line

- move the name into dma_heap_ops.

I'm not sure this is useful, as there are cases where there are multiple heaps that use the same ops. Specifically the multiple CMA heaps.

Quoted 1 line

- remove the dma_heap_export_info structure, which is a bit pointless

Andrew and I went back and forth on this a bit. It looks like he just responded so I'll defer to his answer.

Quoted 4 lines

- don't bother setting a private data, as you don't need it. If other heaps need private data I'd suggest to switch to embedding the dma_heap structure into containing structure insted so that you can use container_of to get at it.

Fair. There is some cases where we use the priv data, but I'll try to see if I can minimize it. And again, I think having the dma_heap structure be internal/private to the heap implementations made it difficult to be a contained structure. So it goes back to the export_info structure point above.

Quoted 2 lines

- also why is the free callback passed as a callback rather than kept in dma_heap_ops, next to the paired alloc one?

This one is due to the optional heap helpers infrastructure. If a heap implements its own dma_buf_ops, it can have release directly call the buffer free function. However, since we tried to minimize the code we have the heap helpers infrastructure which implements a shared dma_buf_op, we need some way for the helper release function to call back to the heap specific free. We could put it in the dma_heaps_ops like you suggest, but that brings some confusion as well, as nothing in the dma-heaps core would call it, it would only be a tool for the helper infrastructure to trace back to the heap specific free call. This is why its passed to the heap_helper initializer. I agree it feels a little odd, so I'd welcome alternate approaches.

Very much appreciate the review and feedback! I'll try to address as much of this as I can in the next revision.

thanks
-john

#8 lore raw

PATCH v7 4/5 dma-buf: heaps: Add CMA heap to dmabuf heaps

In reply to John Stultz

This adds a CMA heap, which allows userspace to allocate a dma-buf of contiguous memory out of a CMA region.

This code is an evolution of the Android ION implementation, so thanks to its original author and maintainters:
Benjamin Gaignard, Laura Abbott, and others!

Cc Laura Abbott <[email protected]>
Cc Benjamin Gaignard <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Liam Mark <[email protected]>
Cc Pratik Patel <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Vincent Donnefort <[email protected]>
Cc Sudipto Paul <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Cc Hridya Valsaraju <[email protected]>

---
v2:
* Switch allocate to return dmabuf fd
* Simplify init code
* Checkpatch fixups
v3:
* Switch to inline function for to_cma_heap()
* Minor cleanups suggested by Brian
* Fold in new registration style from Andrew
* Folded in changes from Andrew to use simplified page list from the heap helpers
v4:
* Use the fd_flags when creating dmabuf fd (Suggested by Benjamin)
* Use precalculated pagecount (Suggested by Andrew) v6:
* Changed variable names to improve clarity, as suggested by Brian
v7:
* Use newly lower-cased init_heap_helper_buffer helper
* Use new dmabuf export helper

---
 drivers/dma-buf/heaps/Kconfig    |   8 ++
 drivers/dma-buf/heaps/Makefile   |   1 +
 drivers/dma-buf/heaps/cma_heap.c | 164 +++++++++++++++++++++++++++++++
 3 files changed, 173 insertions(+)
 create mode 100644 drivers/dma-buf/heaps/cma_heap.c
drivers/dma-buf/heaps/Kconfig +8 -0
index 205052744169..a5eef06c4226 100644
@@ -4,3 +4,11 @@ config DMABUF_HEAPS_SYSTEM
 	help
 	  Choose this option to enable the system dmabuf heap. The system heap
 	  is backed by pages from the buddy allocator. If in doubt, say Y.
+
+config DMABUF_HEAPS_CMA
+	bool "DMA-BUF CMA Heap"
+	depends on DMABUF_HEAPS && DMA_CMA
+	help
+	  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.
drivers/dma-buf/heaps/Makefile +1 -0
index d1808eca2581..6e54cdec3da0 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-y					+= heap-helpers.o
 obj-$(CONFIG_DMABUF_HEAPS_SYSTEM)	+= system_heap.o
+obj-$(CONFIG_DMABUF_HEAPS_CMA)		+= cma_heap.o
drivers/dma-buf/heaps/cma_heap.c new file +164 -0

164 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

PATCH v7 5/5 kselftests: Add dma-heap test

In reply to John Stultz

Add very trivial allocation and import test for dma-heaps, utilizing the vgem driver as a test importer.

A good chunk of this code taken from:
tools/testing/selftests/android/ion/ionmap_test.c Originally by Laura Abbott <[email protected]>

Cc Benjamin Gaignard <[email protected]>
Cc Sumit Semwal <[email protected]>
Cc Liam Mark <[email protected]>
Cc Pratik Patel <[email protected]>
Cc Brian Starkey <[email protected]>
Cc Vincent Donnefort <[email protected]>
Cc Sudipto Paul <[email protected]>
Cc Andrew F. Davis <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Cc Hridya Valsaraju <[email protected]>

---
v2:
* Switched to use reworked dma-heap apis
v3:
* Add simple mmap
* Utilize dma-buf testdev to test importing
v4:
* Rework to use vgem
* Pass in fd_flags to match interface changes
* Skip . and .. dirs
v6:
* Number of style/cleanups suggested by Brian
v7:
* Whitespace fixup for checkpatch

---
 tools/testing/selftests/dmabuf-heaps/Makefile |   9 +
 .../selftests/dmabuf-heaps/dmabuf-heap.c      | 235 ++++++++++++++++++
 2 files changed, 244 insertions(+)
 create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile
 create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
tools/testing/selftests/dmabuf-heaps/Makefile new file +9 -0
new file mode 100644
index 000000000000..8c4c36e2972d
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -static -O3 -Wl,-no-as-needed -Wall
+#LDLIBS += -lrt -lpthread -lm
+
+# these are all "safe" tests that don't modify
+# system time or require escalated privileges
+TEST_GEN_PROGS = dmabuf-heap
+
+include ../lib.mk
tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c new file +235 -0

235 lines not on this page · raw message

Signature · 1 line
2.17.1