Skip to contentSkip to conversation

DMA-BUF Heaps (destaging ION)

PATCH Superseded John Stultz 6 participants 39 messages Jun 24, 2019 – Jul 25, 2019

Subsystems (3)

v6 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v6 cover DMA-BUF Heaps (destaging ION)

Here is 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 for now, since any such accounting should be implemented by dma-buf core or the heaps themselves.

New in v6:
* Number of cleanups and error path fixes suggested by Brian Starkey, many thanks for his close review and suggestions!

Outstanding concerns:
* Need to better understand various secure heap implementations. Some concern that heap private flags will be needed, but its also possible that dma-buf heaps can't solve everyone's needs, in which case, a vendor's secure buffer driver can implement their own dma-buf exporter. So I'm not too worried here.

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]>

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                       |  10 +
 drivers/dma-buf/Makefile                      |   2 +
 drivers/dma-buf/dma-heap.c                    | 249 +++++++++++++++++
 drivers/dma-buf/heaps/Kconfig                 |  14 +
 drivers/dma-buf/heaps/Makefile                |   4 +
 drivers/dma-buf/heaps/cma_heap.c              | 169 +++++++++++
 drivers/dma-buf/heaps/heap-helpers.c          | 262 ++++++++++++++++++
 drivers/dma-buf/heaps/heap-helpers.h          |  54 ++++
 drivers/dma-buf/heaps/system_heap.c           | 121 ++++++++
 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      | 234 ++++++++++++++++
 14 files changed, 1260 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 · 1 line
2.17.1
#1 lore raw

PATCH v6 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 Xu YiPing <[email protected]>
Cc "Chenfeng (puck)" <[email protected]>
Cc "Xiaqing (A)" <[email protected]>
Cc Yudongbin <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Change-Id I4af43a137ad34ff6f7da4d6b2864f3cd86fb7652

---
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

---
 MAINTAINERS                   |  18 +++
 drivers/dma-buf/Kconfig       |   8 ++
 drivers/dma-buf/Makefile      |   1 +
 drivers/dma-buf/dma-heap.c    | 249 ++++++++++++++++++++++++++++++++++
 include/linux/dma-heap.h      |  59 ++++++++
 include/uapi/linux/dma-heap.h |  55 ++++++++
 6 files changed, 390 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 d0ed735994a5..851dbd006cdf 100644
@@ -4813,6 +4813,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 +8 -0
index d5f915830b68..9b93f86f597c 100644
@@ -39,4 +39,12 @@ 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 allows userspace 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 +249 -0

249 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 v6 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 Xu YiPing <[email protected]>
Cc "Chenfeng (puck)" <[email protected]>
Cc "Xiaqing (A)" <[email protected]>
Cc Yudongbin <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Change-Id I48d43656e7783f266d877e363116b5187639f996

---
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

---
 drivers/dma-buf/Makefile             |   1 +
 drivers/dma-buf/heaps/Makefile       |   2 +
 drivers/dma-buf/heaps/heap-helpers.c | 262 +++++++++++++++++++++++++++
 drivers/dma-buf/heaps/heap-helpers.h |  54 ++++++
 4 files changed, 319 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 +262 -0

262 lines not on this page · raw message

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

54 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 v6 2/5] dma-buf: heaps: Add heap helpers

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

Quoted 2 lines
drivers/dma-buf/heaps/heap-helpers.c new file +2 -0
+void INIT_HEAP_HELPER_BUFFER(struct heap_helper_buffer *buffer,
+			     void (*free)(struct heap_helper_buffer *))

Please use a lower case naming following the naming scheme for the rest of the file.

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

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;
+}

Unless I'm misreading the patches this is used for the same pages that also might be dma mapped. In this case you need to use flush_kernel_vmap_range and invalidate_kernel_vmap_range in the right places to ensure coherency between the vmap and device view. Please also document the buffer ownership, as this really can get complicated.

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

Quoted 10 lines
drivers/dma-buf/heaps/heap-helpers.c new file +10 -0
+static vm_fault_t dma_heap_vm_fault(struct vm_fault *vmf)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct heap_helper_buffer *buffer = vma->vm_private_data;
+
+	vmf->page = buffer->pages[vmf->pgoff];
+	get_page(vmf->page);
+
+	return 0;
+}

Is there any exlusion between mmap / vmap and the device accessing the data? Without that you are going to run into a lot of coherency problems.

#4 lore raw

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

Quoted 6 lines · Christoph Hellwig

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

Quoted 3 lines
drivers/dma-buf/heaps/heap-helpers.c new file +2 -0
+void INIT_HEAP_HELPER_BUFFER(struct heap_helper_buffer *buffer,
+                          void (*free)(struct heap_helper_buffer *))

Please use a lower case naming following the naming scheme for the rest of the file.

Yes! Apologies as this was a hold-over from when the initialization function was an inline function in the style of INIT_WORK/INIT_LIST_HEAD. No longer appropriate that its a function. I'll change it.

Quoted 8 lines
Quoted 8 lines
drivers/dma-buf/heaps/heap-helpers.c new file +8 -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);
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     return vaddr;
+}

Unless I'm misreading the patches this is used for the same pages that also might be dma mapped. In this case you need to use flush_kernel_vmap_range and invalidate_kernel_vmap_range in the right places to ensure coherency between the vmap and device view. Please also document the buffer ownership, as this really can get complicated.

Forgive me I wasn't familiar with those calls, but this seems like it would apply to all dma-buf exporters if so, and I don't see any similar flush_kernel_vmap_range calls there (both functions are seemingly only used by xfs, md and bio).

We do have the dma_heap_dma_buf_begin_cpu_access()/dma_heap_dma_buf_end_cpu_access() hooks (see more on these below) which sync the buffers for each attachment (via dma_sync_sg_for_cpu/device), and are used around cpu access to the buffers. Are you suggesting the flush_kernel_vmap_range() call be added to those hooks or is the dma_sync_sg_for_* calls sufficient there?

Quoted 6 lines
Quoted 6 lines
drivers/dma-buf/heaps/heap-helpers.c new file +6 -0
+static vm_fault_t dma_heap_vm_fault(struct vm_fault *vmf)
+{
+     struct vm_area_struct *vma = vmf->vma;
+     struct heap_helper_buffer *buffer = vma->vm_private_data;
+
+     vmf->page = buffer->pages[vmf->pgoff];
Quoted 8 lines

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

Quoted 4 lines
drivers/dma-buf/heaps/heap-helpers.c new file +4 -0
+     get_page(vmf->page);
+
+     return 0;
+}

Is there any exlusion between mmap / vmap and the device accessing the data? Without that you are going to run into a lot of coherency problems.

This has actually been a concern of mine recently, but at the higher dma-buf core level. Conceptually, there is the dma_buf_map_attachment() and dma_buf_unmap_attachment() calls drivers use to map the buffer to an attached device, and there are the dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() calls which are to be done when touching the cpu mapped pages. These look like serializing functions, but actually don't seem to have any enforcement mechanism to exclude parallel access.

To me it seems like adding the exclusion between those operations should be done at the dmabuf core level, and would actually be helpful for optimizing some of the cache maintenance rules w/ dmabuf. Does this sound like something closer to what your suggesting, or am I misunderstanding your point?

Again, I really appreciate the review and feedback!

Thanks so much!
-john
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

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

Quoted 6 lines · John Stultz
Quoted 4 lines · Christoph Hellwig

Is there any exlusion between mmap / vmap and the device accessing the data? Without that you are going to run into a lot of coherency problems.

dma_fence is basically the way to handle exclusion between different device access (since device access tends to be asynchronous). For device<->device access, each driver is expected to take care of any cache(s) that the device might have. (Ie. device writing to buffer should flush it's caches if needed before signalling fence to let reading device know that it is safe to read, etc.)

_begin/end_cpu_access() is intended to be the exclusion for CPU access (which is synchronous)

BR,
-R

Quoted 19 lines

This has actually been a concern of mine recently, but at the higher dma-buf core level. Conceptually, there is the dma_buf_map_attachment() and dma_buf_unmap_attachment() calls drivers use to map the buffer to an attached device, and there are the dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() calls which are to be done when touching the cpu mapped pages. These look like serializing functions, but actually don't seem to have any enforcement mechanism to exclude parallel access.

To me it seems like adding the exclusion between those operations should be done at the dmabuf core level, and would actually be helpful for optimizing some of the cache maintenance rules w/ dmabuf. Does this sound like something closer to what your suggesting, or am I misunderstanding your point?

Again, I really appreciate the review and feedback!

Thanks so much!
-john

#6 lore raw

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

Quoted 15 lines · Rob Clark
Quoted 6 lines · John Stultz
Quoted 4 lines · Christoph Hellwig

Is there any exlusion between mmap / vmap and the device accessing the data? Without that you are going to run into a lot of coherency problems.

dma_fence is basically the way to handle exclusion between different device access (since device access tends to be asynchronous). For device<->device access, each driver is expected to take care of any cache(s) that the device might have. (Ie. device writing to buffer should flush it's caches if needed before signalling fence to let reading device know that it is safe to read, etc.)

Quoted 2 lines

_begin/end_cpu_access() is intended to be the exclusion for CPU access (which is synchronous)

What I mean is that we need a clear state machine (preferably including ownership tracking ala dma-debug) where a piece of memory has one owner at a time that can access it. Only the owner can access is at that time, and at any owner switch we need to flush/invalidate all relevant caches. And with memory that is vmaped and mapped to userspace that can get really complicated.

The above sounds like you have some of that in place, but we'll really need clear rules to make sure we don't have holes in the scheme.

#7 lore raw

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

Quoted 26 lines · Christoph Hellwig
Quoted 15 lines · Rob Clark
Quoted 6 lines · John Stultz
Quoted 4 lines · Christoph Hellwig

Is there any exlusion between mmap / vmap and the device accessing the data? Without that you are going to run into a lot of coherency problems.

dma_fence is basically the way to handle exclusion between different device access (since device access tends to be asynchronous). For device<->device access, each driver is expected to take care of any cache(s) that the device might have. (Ie. device writing to buffer should flush it's caches if needed before signalling fence to let reading device know that it is safe to read, etc.)

Quoted 2 lines

_begin/end_cpu_access() is intended to be the exclusion for CPU access (which is synchronous)

What I mean is that we need a clear state machine (preferably including ownership tracking ala dma-debug) where a piece of memory has one owner at a time that can access it. Only the owner can access is at that time, and at any owner switch we need to flush/invalidate all relevant caches. And with memory that is vmaped and mapped to userspace that can get really complicated.

Quoted 3 lines

The above sounds like you have some of that in place, but we'll really need clear rules to make sure we don't have holes in the scheme.

Well then lets think on this. A given buffer can have 3 owners states (CPU-owned, Device-owned, and Un-owned). These are based on the caching state from the CPU perspective.

If a buffer is CPU-owned then we (Linux) can write to the buffer safely without worry that the data is stale or that it will be accessed by the device without having been flushed. Device-owned buffers should not be accessed by the CPU, and inter-device synchronization should be handled by fencing as Rob points out. Un-owned is how a buffer starts for consistency and to prevent unneeded cache operations on unwritten buffers.

We also need to track the mapping states, 4 states for this, CPU-mapped, Device-mapped, CPU/Device-mapped, unmapped. Should be self explanatory, map_dma_buf maps towards the device, mmap/vmap/kmap towards the CPU. Leaving a buffer mapped by the CPU while device access takes place is safe as long as ownership is taken before any access. One more point, we assume reference counting for the below discussion, for instance unmap_dma_buf refers to the last device unmapping, map_dma_buf refers only to the first.

This gives 12 combined states, if we assume a buffer will always be owned when it has someone mapping it, either CPU or device or both, then we can drop 3 states. If a buffer is only mapped into one space, then that space owns it, this drops 2 cross-owned states. Lastly if not mapped by either space then the buffer becomes un-owned (and the backing memory can be freed or migrated as needed). Leaving us 5 valid states.

* Un-Owned Un-Mapped
* Device-Owned Device-Mapped
* Device-Owned CPU/Device-Mapped
* CPU-Owned CPU-Mapped
* CPU-Owned CPU/Device-Mapped

There are 6 DMA-BUF operations (classes) on a buffer:

* map_dma_buf
* unmap_dma_buf
* begin_cpu_access
* end_cpu_access
* mmap/vmap/kmap
* ummanp/vunmap/kunmap

From all this I've suggest the following state-machine(in DOT language):

Note: Buffers start in "Un-Owned Un-Mapped" and can only be freed from that state.

Note: Commented out states/transitions are not valid but here to prove completeness

-------------------------------------------------------------------

digraph dma_buf_buffer_states

{
	label = "DMA-BUF Buffer states";

	uo_um [ label="Un-Owned\nUn-Mapped" ];

// uo_dm [ label="Un-Owned\nDevice-Mapped" ];
// uo_cm [ label="Un-Owned\nCPU-Mapped" ];
// uo_cdm [ label="Un-Owned\nCPU/Device-Mapped" ];

//	do_um [ label="Device-Owned\nUn-Mapped" ];
	do_dm [ label="Device-Owned\nDevice-Mapped" ];
//	do_cm [ label="Device-Owned\nCPU-Mapped" ];
	do_cdm [ label="Device-Owned\nCPU/Device-Mapped" ];

// co_um [ label="CPU-Owned\nUn-Mapped" ];

//	co_dm [ label="CPU-Owned\nDevice-Mapped" ];
	co_cm [ label="CPU-Owned\nCPU-Mapped" ];
	co_cdm [ label="CPU-Owned\nCPU/Device-Mapped" ];

	/* From Un-Owned Un-Mapped */
		uo_um -> do_dm		[ label="map_dma_buf" ];
//		uo_um ->		[ label="unmap_dma_buf" ];
//		uo_um -> 		[ label="begin_cpu_access" ];
//		uo_um ->		[ label="end_cpu_access" ];
		uo_um -> co_cm		[ label="mmap/vmap/kmap" ];

// uo_um -> [ label="ummanp/vunmap/kunmap" ];

	/* From Device-Owned Device-Mapped */
		do_dm -> do_dm		[ label="map_dma_buf" ];
		do_dm -> uo_um		[ label="unmap_dma_buf" ];
//		do_dm -> 		[ label="begin_cpu_access" ];
//		do_dm ->		[ label="end_cpu_access" ];
		do_dm -> do_cdm		[ label="mmap/vmap/kmap" ];

// do_dm -> [ label="ummanp/vunmap/kunmap" ];

	/* From Device-Owned CPU/Device-Mapped */
		do_cdm -> do_cdm	[ label="map_dma_buf" ];
		do_cdm -> co_cm		[ label="unmap_dma_buf" ];
		do_cdm -> co_cdm	[ label="begin_cpu_access" ];
//		do_cdm ->		[ label="end_cpu_access" ];
		do_cdm -> do_cdm	[ label="mmap/vmap/kmap" ];
		do_cdm -> do_dm		[ label="ummanp/vunmap/kunmap" ];

	/* From CPU-Owned CPU-Mapped */
		co_cm -> co_cdm		[ label="map_dma_buf" ];
//		co_cm -> 		[ label="unmap_dma_buf" ];
//		co_cm -> 		[ label="begin_cpu_access" ];
		co_cm -> co_cm		[ label="end_cpu_access" ];
//		co_cm ->		[ label="mmap/vmap/kmap" ];
		co_cm -> uo_um		[ label="ummanp/vunmap/kunmap" ];

	/* From CPU-Owned CPU/Device-Mapped */
		co_cdm -> co_cdm	[ label="map_dma_buf" ];
		co_cdm -> co_cm		[ label="unmap_dma_buf" ];
//		co_cdm -> 		[ label="begin_cpu_access" ];
		co_cdm -> do_cdm	[ label="end_cpu_access" ];
		co_cdm -> co_cdm	[ label="mmap/vmap/kmap" ];

// co_cdm -> [ label="ummanp/vunmap/kunmap" ];

	{
		rank = same;
		co_cm -> do_dm [ style=invis ];
		rankdir = LR;
	}

	{
		rank = same;
		co_cdm -> do_cdm [ style=invis ];
		rankdir = LR;
	}
}

-------------------------------------------------------------------

If we consider this the "official" model, then we can start optimizing cache operations, and start forbidding some nonsensical operations.

What do y'all think?

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

#8 lore raw

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

Quoted 10 lines · Andrew F. Davis

Well then lets think on this. A given buffer can have 3 owners states (CPU-owned, Device-owned, and Un-owned). These are based on the caching state from the CPU perspective.

If a buffer is CPU-owned then we (Linux) can write to the buffer safely without worry that the data is stale or that it will be accessed by the device without having been flushed. Device-owned buffers should not be accessed by the CPU, and inter-device synchronization should be handled by fencing as Rob points out. Un-owned is how a buffer starts for consistency and to prevent unneeded cache operations on unwritten buffers.

CPU owned also needs to be split into which mapping owns it - in the normal DMA this is the kernel direct mapping, but in dma-buf it seems the primary way of using it in kernel space is the vmap, in addition to that the mappings can also be exported to userspace, which is another mapping that is possibly not cache coherent with the kernel one.

#9 lore raw

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

Quoted 13 lines · Christoph Hellwig
Quoted 10 lines · Andrew F. Davis

Well then lets think on this. A given buffer can have 3 owners states (CPU-owned, Device-owned, and Un-owned). These are based on the caching state from the CPU perspective.

If a buffer is CPU-owned then we (Linux) can write to the buffer safely without worry that the data is stale or that it will be accessed by the device without having been flushed. Device-owned buffers should not be accessed by the CPU, and inter-device synchronization should be handled by fencing as Rob points out. Un-owned is how a buffer starts for consistency and to prevent unneeded cache operations on unwritten buffers.

Quoted 5 lines

CPU owned also needs to be split into which mapping owns it - in the normal DMA this is the kernel direct mapping, but in dma-buf it seems the primary way of using it in kernel space is the vmap, in addition to that the mappings can also be exported to userspace, which is another mapping that is possibly not cache coherent with the kernel one.

Just for some history, dmabuf->vmap() is optional, and mostly added for the benefit of drm/udl (usb display, where CPU needs to read out and encode (?) the video stream.. most of the drm drivers are using tmpfs to get backing pages, and if there is any kernel direct mapping it is unused.

It is probably ok for any allocator that does care about a kernel direct mapping to simply not implement vmap.

BR,
-R

#10 lore raw

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

In reply to John Stultz
Quoted 35 lines · John Stultz
Quoted 6 lines · Christoph Hellwig

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

Quoted 3 lines
drivers/dma-buf/heaps/heap-helpers.c new file +2 -0
+void INIT_HEAP_HELPER_BUFFER(struct heap_helper_buffer *buffer,
+                          void (*free)(struct heap_helper_buffer *))

Please use a lower case naming following the naming scheme for the rest of the file.

Yes! Apologies as this was a hold-over from when the initialization function was an inline function in the style of INIT_WORK/INIT_LIST_HEAD. No longer appropriate that its a function. I'll change it.

Quoted 8 lines
Quoted 8 lines
drivers/dma-buf/heaps/heap-helpers.c new file +8 -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);
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+     return vaddr;
+}

Unless I'm misreading the patches this is used for the same pages that also might be dma mapped. In this case you need to use flush_kernel_vmap_range and invalidate_kernel_vmap_range in the right places to ensure coherency between the vmap and device view. Please also document the buffer ownership, as this really can get complicated.

Forgive me I wasn't familiar with those calls, but this seems like it would apply to all dma-buf exporters if so, and I don't see any similar flush_kernel_vmap_range calls there (both functions are seemingly only used by xfs, md and bio).

Quoted 6 lines

We do have the dma_heap_dma_buf_begin_cpu_access()/dma_heap_dma_buf_end_cpu_access() hooks (see more on these below) which sync the buffers for each attachment (via dma_sync_sg_for_cpu/device), and are used around cpu access to the buffers. Are you suggesting the flush_kernel_vmap_range() call be added to those hooks or is the dma_sync_sg_for_* calls sufficient there?

dma_sync_sg_for_* only operates on the kernel direct mapping, that is what you get from page_address/kmap* for the page. But with vmap your create another alias in kernel virtual space, which dma_sync_sg_for_* can't know about. Now for most CPUs caches are indexed by physical addresses so this doesn't matter, but a significant minority of CPUs (parisc, some arm, some mips) index by virtual address, in which case we need non-empy flush_kernel_vmap_range and invalidate_kernel_vmap_range helper to deal with that vmap alias.

#11 lore raw

PATCH v6 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 Xu YiPing <[email protected]>
Cc "Chenfeng (puck)" <[email protected]>
Cc "Xiaqing (A)" <[email protected]>
Cc Yudongbin <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Change-Id I4dc5ff54ccb1f7ca3ac8675661114ca33813654b

---
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

---
 drivers/dma-buf/Kconfig             |   2 +
 drivers/dma-buf/heaps/Kconfig       |   6 ++
 drivers/dma-buf/heaps/Makefile      |   1 +
 drivers/dma-buf/heaps/system_heap.c | 121 ++++++++++++++++++++++++++++
 4 files changed, 130 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 9b93f86f597c..434cfe646dad 100644
@@ -47,4 +47,6 @@ menuconfig DMABUF_HEAPS
 	  this allows userspace 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 +121 -0

121 lines not on this page · raw message

Signature · 1 line
2.17.1
#12 lore raw

PATCH v6 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 Xu YiPing <[email protected]>
Cc "Chenfeng (puck)" <[email protected]>
Cc "Xiaqing (A)" <[email protected]>
Cc Yudongbin <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Change-Id Ic2b0c5dfc0dbaff5245bd1c50170c64b06c73051

---
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

---
 drivers/dma-buf/heaps/Kconfig    |   8 ++
 drivers/dma-buf/heaps/Makefile   |   1 +
 drivers/dma-buf/heaps/cma_heap.c | 169 +++++++++++++++++++++++++++++++
 3 files changed, 178 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 +169 -0

169 lines not on this page · raw message

Signature · 1 line
2.17.1
#13 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

This and the previous one seem very much duplicated boilerplate code. Why can't just normal branches for allocating and freeing normal pages vs cma? We even have an existing helper for that with dma_alloc_contiguous().

#14 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 3 lines · Christoph Hellwig

This and the previous one seem very much duplicated boilerplate code.

So yes, there is some duplicate boilerplate between the system and cma heaps particularly in the allocation function, where we allocate and set up the helper buffer structure, allocate the pages, then create and export the dmabuf. I took a pass at trying to minimize some of this earlier, but those efforts ended up adding helper functions that take a ton of arguments, and had some trouble properly handling error paths without leaking memory, so I left it as is.

I'll try to take another pass myself but if you have specific suggestions for improving here, I'd be happy to try them.

Quoted 3 lines

Why can't just normal branches for allocating and freeing normal pages vs cma? We even have an existing helper for that with dma_alloc_contiguous().

Apologies, I'm not sure I'm understanding your suggestion here. dma_alloc_contiguous() does have some interesting optimizations (avoiding allocating single page from cma), though its focus on default area vs specific device area doesn't quite match up the usage model for dma heaps. Instead of allocating memory for a single device, we want to be able to allow userland, for a given usage mode, to be able to allocate a dmabuf from a specific heap of memory which will satisfy the usage mode for that buffer pipeline (across multiple devices).

Now, indeed, the system and cma heaps in this patchset are a bit simple/trivial (though useful with my devices that require contiguous buffers for the display driver), but more complex ION heaps have traditionally stayed out of tree in vendor code, in many cases making incompatible tweaks to the ION core dmabuf exporter logic. That's why dmabuf heaps is trying to destage ION in a way that allows heaps to implement their exporter logic themselves, so we can start pulling those more complicated heaps out of their vendor hidey-holes and get some proper upstream review.

But I suspect I just am confused as to what your suggesting. Maybe could you expand a bit? Apologies for being a bit dense.

Thanks again for the input!
-john
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#15 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 10 lines · John Stultz

Apologies, I'm not sure I'm understanding your suggestion here. dma_alloc_contiguous() does have some interesting optimizations (avoiding allocating single page from cma), though its focus on default area vs specific device area doesn't quite match up the usage model for dma heaps. Instead of allocating memory for a single device, we want to be able to allow userland, for a given usage mode, to be able to allocate a dmabuf from a specific heap of memory which will satisfy the usage mode for that buffer pipeline (across multiple devices).

Quoted 5 lines

Now, indeed, the system and cma heaps in this patchset are a bit simple/trivial (though useful with my devices that require contiguous buffers for the display driver), but more complex ION heaps have traditionally stayed out of tree in vendor code, in many cases making incompatible tweaks to the ION core dmabuf exporter logic.

So what would the more complicated heaps be?

Quoted 8 lines

That's why
dmabuf heaps is trying to destage ION in a way that allows heaps to implement their exporter logic themselves, so we can start pulling those more complicated heaps out of their vendor hidey-holes and get some proper upstream review.

But I suspect I just am confused as to what your suggesting. Maybe could you expand a bit? Apologies for being a bit dense.

My suggestion is to merge the system and CMA heaps. CMA (at least the system-wide CMA area) is really just an optimization to get large contigous regions more easily. We should make use of it as transparent as possible, just like we do in the DMA code.

#16 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 26 lines · Christoph Hellwig
Quoted 10 lines · John Stultz

Apologies, I'm not sure I'm understanding your suggestion here. dma_alloc_contiguous() does have some interesting optimizations (avoiding allocating single page from cma), though its focus on default area vs specific device area doesn't quite match up the usage model for dma heaps. Instead of allocating memory for a single device, we want to be able to allow userland, for a given usage mode, to be able to allocate a dmabuf from a specific heap of memory which will satisfy the usage mode for that buffer pipeline (across multiple devices).

Quoted 5 lines

Now, indeed, the system and cma heaps in this patchset are a bit simple/trivial (though useful with my devices that require contiguous buffers for the display driver), but more complex ION heaps have traditionally stayed out of tree in vendor code, in many cases making incompatible tweaks to the ION core dmabuf exporter logic.

So what would the more complicated heaps be?

Quoted 6 lines

That's why
dmabuf heaps is trying to destage ION in a way that allows heaps to implement their exporter logic themselves, so we can start pulling those more complicated heaps out of their vendor hidey-holes and get some proper upstream review.

Quoted 7 lines
Quoted 2 lines

But I suspect I just am confused as to what your suggesting. Maybe could you expand a bit? Apologies for being a bit dense.

My suggestion is to merge the system and CMA heaps. CMA (at least the system-wide CMA area) is really just an optimization to get large contigous regions more easily. We should make use of it as transparent as possible, just like we do in the DMA code.

CMA has made possible to get large regions of memories and to give some priority on device allocating pages on it. I don't think that possible with system
heap so I suggest to keep CMA heap if we want to be able to port a maximum of applications on dma-buf-heap.

Benjamin

#17 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 5 lines · Benjamin Gaignard

CMA has made possible to get large regions of memories and to give some priority on device allocating pages on it. I don't think that possible with system
heap so I suggest to keep CMA heap if we want to be able to port a maximum of applications on dma-buf-heap.

Yes, CMA is a way to better allocate contigous regions, but it isn't the only way to do that.

So at least for the system default CMA area it really should be a helper for the system heap, especially given that CMA is an optional feature and we can do high order contigous allocations using alloc_pages as well.

Something like:

	if (!require_contigous && order > 0) {
		for (i = 0; i < nr_pages; i++)
			page[i] = alloc_page();
		goto done;
	} else if (order > 0)
		page = cma_alloc();
		if (page)
			goto done;
	}

	page = alloc_pages(order);
#18 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

In reply to Christoph Hellwig
Quoted 25 lines · Christoph Hellwig
Quoted 10 lines · John Stultz

Apologies, I'm not sure I'm understanding your suggestion here. dma_alloc_contiguous() does have some interesting optimizations (avoiding allocating single page from cma), though its focus on default area vs specific device area doesn't quite match up the usage model for dma heaps. Instead of allocating memory for a single device, we want to be able to allow userland, for a given usage mode, to be able to allocate a dmabuf from a specific heap of memory which will satisfy the usage mode for that buffer pipeline (across multiple devices).

Quoted 5 lines

Now, indeed, the system and cma heaps in this patchset are a bit simple/trivial (though useful with my devices that require contiguous buffers for the display driver), but more complex ION heaps have traditionally stayed out of tree in vendor code, in many cases making incompatible tweaks to the ION core dmabuf exporter logic.

So what would the more complicated heaps be?

Quoted 6 lines

That's why
dmabuf heaps is trying to destage ION in a way that allows heaps to implement their exporter logic themselves, so we can start pulling those more complicated heaps out of their vendor hidey-holes and get some proper upstream review.

Quoted 8 lines
Quoted 2 lines

But I suspect I just am confused as to what your suggesting. Maybe could you expand a bit? Apologies for being a bit dense.

My suggestion is to merge the system and CMA heaps. CMA (at least the system-wide CMA area) is really just an optimization to get large contigous regions more easily. We should make use of it as transparent as possible, just like we do in the DMA code.

It's not just an optimization for Ion though. Ion was designed to let the callers choose between system and multiple CMA heaps. On other systems there may be multiple CMA regions dedicated to a specific purpose or placed at a specific address. The callers need to be able to choose exactly whether they want a particular CMA region or discontiguous regions.

Thanks,
Laura

#19 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 2 lines · Laura Abbott

It's not just an optimization for Ion though. Ion was designed to let the callers choose between system and multiple CMA heaps.

Who cares about ion? That some out of tree android crap that should not be relevant for upstream except as an example for how not to design things..

Quoted 5 lines

On other
systems there may be multiple CMA regions dedicated to a specific purpose or placed at a specific address. The callers need to be able to choose exactly whether they want a particular CMA region or discontiguous regions.

At least in cma is only used either with the global pool or a per-device cma pool. I think if you want to make this new dma-buf API fit in with the rest with the kernel you follow that model, and pass in a struct device to select the particular cma area, similar how the DMA allocator works.

#20 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 8 lines · Christoph Hellwig
Quoted 2 lines · Laura Abbott

It's not just an optimization for Ion though. Ion was designed to let the callers choose between system and multiple CMA heaps.

Who cares about ion? That some out of tree android crap that should not be relevant for upstream except as an example for how not to design things..

Tell us how you really feel about ION :)

Quoted 6 lines
Quoted 5 lines

On other
systems there may be multiple CMA regions dedicated to a specific purpose or placed at a specific address. The callers need to be able to choose exactly whether they want a particular CMA region or discontiguous regions.

Quoted 6 lines

At least in cma is only used either with the global pool or a per-device cma pool. I think if you want to make this new dma-buf API fit in with the rest with the kernel you follow that model, and pass in a struct device to select the particular cma area, similar how the DMA allocator works.

This is a central allocator, it is not tied to any one device. If we knew the one device ahead of time we would just use the existing dma_alloc.

We might be able to solve some of that with late mapping after all the devices attach to the buffer, but even then, which device's CMA area would we chose to use from all the attached devices?

I can agree that allocating from per-device CMA using Heaps doesn't make much sense, but for global pools I'm not sure I see any way to allow devices to select which pool is right for a specific use. They don't have the full use-case information like the application does, the selection needs to be made from the application.

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

#21 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 7 lines · Andrew F. Davis

This is a central allocator, it is not tied to any one device. If we knew the one device ahead of time we would just use the existing dma_alloc.

We might be able to solve some of that with late mapping after all the devices attach to the buffer, but even then, which device's CMA area would we chose to use from all the attached devices?

Quoted 5 lines

I can agree that allocating from per-device CMA using Heaps doesn't make much sense, but for global pools I'm not sure I see any way to allow devices to select which pool is right for a specific use. They don't have the full use-case information like the application does, the selection needs to be made from the application.

Well, the examples we had before was that we clear want to use the per-device CMA area. And at least in upstream a CMA area either is global or attached to a device, as we otherwise wouldn't find it.

#22 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · John Stultz

Apologies, I'm not sure I'm understanding your suggestion here. dma_alloc_contiguous() does have some interesting optimizations (avoiding allocating single page from cma), though its focus on default area vs specific device area doesn't quite match up the usage model for dma heaps. Instead of allocating memory for a single device, we want to be able to allow userland, for a given usage mode, to be able to allocate a dmabuf from a specific heap of memory which will satisfy the usage mode for that buffer pipeline (across multiple devices).

Quoted 8 lines
Quoted 5 lines

Now, indeed, the system and cma heaps in this patchset are a bit simple/trivial (though useful with my devices that require contiguous buffers for the display driver), but more complex ION heaps have traditionally stayed out of tree in vendor code, in many cases making incompatible tweaks to the ION core dmabuf exporter logic.

So what would the more complicated heaps be?

https://patchwork.kernel.org/patch/10863957/

It's actually a more simple heap type IMHO, but the logic inside is incompatible with the system/CMA heaps, if you move any of their code into the core framework then this heap stops working. Leading to out of tree hacks on the core to get it back functional. I see the same for the "complex" heaps with ION.

Andrew

Quoted 14 lines
Quoted 8 lines

That's why
dmabuf heaps is trying to destage ION in a way that allows heaps to implement their exporter logic themselves, so we can start pulling those more complicated heaps out of their vendor hidey-holes and get some proper upstream review.

But I suspect I just am confused as to what your suggesting. Maybe could you expand a bit? Apologies for being a bit dense.

My suggestion is to merge the system and CMA heaps. CMA (at least the system-wide CMA area) is really just an optimization to get large contigous regions more easily. We should make use of it as transparent as possible, just like we do in the DMA code.

#23 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 7 lines · Andrew F. Davis

https://patchwork.kernel.org/patch/10863957/

It's actually a more simple heap type IMHO, but the logic inside is incompatible with the system/CMA heaps, if you move any of their code into the core framework then this heap stops working. Leading to out of tree hacks on the core to get it back functional. I see the same for the "complex" heaps with ION.

Well, this mostly is just another allocator (gen_pool). And given that the whole dma-buf infrastucture assumes things are backed by pages we really shouldn't need much more than an alloc and a free callback (and maybe the pgprot to map it) and handle the rest in common code.

#24 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Andrew F. Davis

https://patchwork.kernel.org/patch/10863957/

It's actually a more simple heap type IMHO, but the logic inside is incompatible with the system/CMA heaps, if you move any of their code into the core framework then this heap stops working. Leading to out of tree hacks on the core to get it back functional. I see the same for the "complex" heaps with ION.

Quoted 5 lines

Well, this mostly is just another allocator (gen_pool). And given that the whole dma-buf infrastucture assumes things are backed by pages we really shouldn't need much more than an alloc and a free callback (and maybe the pgprot to map it) and handle the rest in common code.

But that's just it, dma-buf does not assume buffers are backed by normal kernel managed memory, it is up to the buffer exporter where and when to allocate the memory. The memory backed by this SRAM buffer does not have the normal struct page backing. So moving the map, sync, etc functions to common code would fail for this and many other heap types. This was a major problem with Ion that prompted this new design.

Each heap type may need to do something different depending on its backing memory, moving everything to common code that is common to System and CMA heaps is would lead those being the only upstreamable heaps.

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

#25 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 6 lines · Andrew F. Davis

But that's just it, dma-buf does not assume buffers are backed by normal kernel managed memory, it is up to the buffer exporter where and when to allocate the memory. The memory backed by this SRAM buffer does not have the normal struct page backing. So moving the map, sync, etc functions to common code would fail for this and many other heap types. This was a major problem with Ion that prompted this new design.

The code clearly shows it has page backing, e.g. this:

+ sg_set_page(table->sgl, pfn_to_page(PFN_DOWN(buffer->paddr)), buffer->len, 0);

and the fact that it (and the dma-buf API) uses scatterlists, which requires pages.

#26 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 12 lines · Christoph Hellwig
Quoted 6 lines · Andrew F. Davis

But that's just it, dma-buf does not assume buffers are backed by normal kernel managed memory, it is up to the buffer exporter where and when to allocate the memory. The memory backed by this SRAM buffer does not have the normal struct page backing. So moving the map, sync, etc functions to common code would fail for this and many other heap types. This was a major problem with Ion that prompted this new design.

The code clearly shows it has page backing, e.g. this:

(quoted diff) +1 -0
+	sg_set_page(table->sgl, pfn_to_page(PFN_DOWN(buffer->paddr)), buffer->len, 0);
Quoted 3 lines

and the fact that it (and the dma-buf API) uses scatterlists, which requires pages.

Pages yes, but not "normal" pages from the kernel managed area. page_to_pfn() will return bad values on the pages returned by this allocator and so will any of the kernel sync/map functions. Therefor those operations cannot be common and need special per-heap handling.

Andrew

#27 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 4 lines · Andrew F. Davis

Pages yes, but not "normal" pages from the kernel managed area. page_to_pfn() will return bad values on the pages returned by this allocator and so will any of the kernel sync/map functions. Therefor those operations cannot be common and need special per-heap handling.

Well, that means this thing is buggy and abuses the scatterlist API and we can't merge it anyway, so it is irrelevant.

#28 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Andrew F. Davis

Pages yes, but not "normal" pages from the kernel managed area. page_to_pfn() will return bad values on the pages returned by this allocator and so will any of the kernel sync/map functions. Therefor those operations cannot be common and need special per-heap handling.

Well, that means this thing is buggy and abuses the scatterlist API and we can't merge it anyway, so it is irrelevant.

Since when do scatterlists need to only have kernel virtual backed memory pages? Device memory is stored in scatterlists and dma_sync_sg_for_* would fail just the same when the cache ops were attempted.

#29 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 7 lines · Andrew F. Davis
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Andrew F. Davis

Pages yes, but not "normal" pages from the kernel managed area. page_to_pfn() will return bad values on the pages returned by this allocator and so will any of the kernel sync/map functions. Therefor those operations cannot be common and need special per-heap handling.

Quoted 8 lines
Quoted 3 lines

Well, that means this thing is buggy and abuses the scatterlist API and we can't merge it anyway, so it is irrelevant.

Since when do scatterlists need to only have kernel virtual backed memory pages? Device memory is stored in scatterlists and dma_sync_sg_for_* would fail just the same when the cache ops were attempted.

I'm not sure what you mean with virtual backed memory pages, as we don't really have that concept.

But a page in the scatterlist needs to be able to be used everywhere we'd normally use a page, e.g. page_to_phys, page_to_pfn, kmap, page_address (if !highmem) as consumers including the dma mapping interface do all that.

If you want to dma map memory that does not have page backing you need to use dma_map_resource.

#30 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 20 lines · Christoph Hellwig
Quoted 7 lines · Andrew F. Davis
Quoted 6 lines · Christoph Hellwig
Quoted 4 lines · Andrew F. Davis

Pages yes, but not "normal" pages from the kernel managed area. page_to_pfn() will return bad values on the pages returned by this allocator and so will any of the kernel sync/map functions. Therefor those operations cannot be common and need special per-heap handling.

Quoted 8 lines
Quoted 3 lines

Well, that means this thing is buggy and abuses the scatterlist API and we can't merge it anyway, so it is irrelevant.

Since when do scatterlists need to only have kernel virtual backed memory pages? Device memory is stored in scatterlists and dma_sync_sg_for_* would fail just the same when the cache ops were attempted.

I'm not sure what you mean with virtual backed memory pages, as we don't really have that concept.

Quoted 8 lines

But a page in the scatterlist needs to be able to be used everywhere we'd normally use a page, e.g. page_to_phys, page_to_pfn, kmap, page_address (if !highmem) as consumers including the dma mapping interface do all that.

If you want to dma map memory that does not have page backing you need to use dma_map_resource.

I probably should have worded that better.

It does have page backing, what I meant by "page_to_pfn() will return bad values" is not that it won't give you the correct pfn, it will, but that then that pfn is not part of the normal memory space (lowmem/highmem) it's device memory, so cache ops won't work. But you should not be doing that on device memory anyway.

That is a problem with Ion I want to avoid, it assumed all buffers were in DDR and so would do cache operations on them unconditionally, too many assumptions were made as too much was moved into the common core code and not enough was left for the heaps themselves to decide.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#31 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · John Stultz

Apologies, I'm not sure I'm understanding your suggestion here. dma_alloc_contiguous() does have some interesting optimizations (avoiding allocating single page from cma), though its focus on default area vs specific device area doesn't quite match up the usage model for dma heaps. Instead of allocating memory for a single device, we want to be able to allow userland, for a given usage mode, to be able to allocate a dmabuf from a specific heap of memory which will satisfy the usage mode for that buffer pipeline (across multiple devices).

Quoted 7 lines
Quoted 5 lines

Now, indeed, the system and cma heaps in this patchset are a bit simple/trivial (though useful with my devices that require contiguous buffers for the display driver), but more complex ION heaps have traditionally stayed out of tree in vendor code, in many cases making incompatible tweaks to the ION core dmabuf exporter logic.

So what would the more complicated heaps be?

Mostly secure heaps, but there have been work in the past with specially aligned chunk heaps in the past. Unfortunately since every vendor tree also include many of their own hacks to the core ION code (usually without changelogs or comments) its hard to decipher much of it.

Some examples:
msm:
https://github.com/Panchajanya1999/msm-4.14/blob/Pie/drivers/staging/android/ion/ion_cma_secure_heap.c

exynos:
https://github.com/exynos-linux-stable/starlte/tree/tw90-android-p/drivers/staging/android/ion/exynos

hisi:
https://github.com/hexdebug/android_kernel_huawei_hi3660/blob/master/drivers/staging/android/ion/ion_seccm_heap.c
https://github.com/hexdebug/android_kernel_huawei_hi3660/blob/master/drivers/staging/android/ion/ion_secsg_heap.c
https://github.com/hexdebug/android_kernel_huawei_hi3660/blob/master/drivers/staging/android/ion/ion_fama_misc_heap.c

mediatek:
https://android.googlesource.com/kernel/mediatek/+/android-mtk-3.18/drivers/staging/android/ion/mtk/ion_mm_heap.c

But Andrew's example is probably a better example against the dmabuf heaps frameworks

Quoted 6 lines
Quoted 6 lines

That's why
dmabuf heaps is trying to destage ION in a way that allows heaps to implement their exporter logic themselves, so we can start pulling those more complicated heaps out of their vendor hidey-holes and get some proper upstream review.

Quoted 7 lines
Quoted 2 lines

But I suspect I just am confused as to what your suggesting. Maybe could you expand a bit? Apologies for being a bit dense.

My suggestion is to merge the system and CMA heaps. CMA (at least the system-wide CMA area) is really just an optimization to get large contigous regions more easily. We should make use of it as transparent as possible, just like we do in the DMA code.

I'm still not understanding how this would work. Benjamin and Laura already commented on this point, but for a simple example, with the HiKey boards, the DRM driver requires contiguous memory for the framebuffer, but the GPU can handle non-contiguous. Thus the target framebuffers that we pass to the display has to be CMA allocated, but all the other graphics buffers that the GPU will render to and composite can be system.

Having the separate heaps allows the gralloc code to allocate the proper buffer depending on the planned usage (GRALLOC_USAGE_HW_FB or not), and that way we don't waste CMA on buffers that don't have to be contiguous.

Laura already touched on this, but similar logic can be used for camera buffers, which can make sure we allocate from a specifically reserved CMA region that is only used for the camera so we can always be sure to have N free buffers immediately to capture with, etc.

But let me know if I'm still misunderstanding your suggestion.

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

#32 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 7 lines · John Stultz

I'm still not understanding how this would work. Benjamin and Laura already commented on this point, but for a simple example, with the HiKey boards, the DRM driver requires contiguous memory for the framebuffer, but the GPU can handle non-contiguous. Thus the target framebuffers that we pass to the display has to be CMA allocated, but all the other graphics buffers that the GPU will render to and composite can be system.

But that just means we need a flag that memory needs to be contiguous, which totally makes sense at the API level. But CMA is not the only source of contigous memory, so we should not conflate the two.

Quoted 4 lines

Laura already touched on this, but similar logic can be used for camera buffers, which can make sure we allocate from a specifically reserved CMA region that is only used for the camera so we can always be sure to have N free buffers immediately to capture with, etc.

And for that we already have per-device CMA areas hanging off struct device, which this should reuse instead of adding another duplicate CMA area lookup scheme.

#33 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · John Stultz

I'm still not understanding how this would work. Benjamin and Laura already commented on this point, but for a simple example, with the HiKey boards, the DRM driver requires contiguous memory for the framebuffer, but the GPU can handle non-contiguous. Thus the target framebuffers that we pass to the display has to be CMA allocated, but all the other graphics buffers that the GPU will render to and composite can be system.

No we have uses cases where graphic buffers can go directly to display without using GPU. For example we can grab frames from the camera and send them directly in display (same for video decoder) because we have planes for that.

Quoted 4 lines

But that just means we need a flag that memory needs to be contiguous, which totally makes sense at the API level. But CMA is not the only source of contigous memory, so we should not conflate the two.

We have one file descriptor per heap to be able to add access control on each heap.
That wasn't possible with ION because the heap was selected given the flags in ioctl
structure and we can't do access control based on that. If we put flag to select the
allocation mechanism (system, CMA, other) in ioctl we come back to ION status. For me one allocation mechanism = one heap.

Quoted 9 lines
Quoted 5 lines

Laura already touched on this, but similar logic can be used for camera buffers, which can make sure we allocate from a specifically reserved CMA region that is only used for the camera so we can always be sure to have N free buffers immediately to capture with, etc.

And for that we already have per-device CMA areas hanging off struct device, which this should reuse instead of adding another duplicate CMA area lookup scheme.

#34 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 4 lines · Benjamin Gaignard
Quoted 3 lines

But that just means we need a flag that memory needs to be contiguous, which totally makes sense at the API level. But CMA is not the only source of contigous memory, so we should not conflate the two.

Quoted 8 lines

We have one file descriptor per heap to be able to add access control on each heap.
That wasn't possible with ION because the heap was selected given the flags in ioctl
structure and we can't do access control based on that. If we put flag to select the
allocation mechanism (system, CMA, other) in ioctl we come back to ION status. For me one allocation mechanism = one heap.

Well, I agree with your split for different fundamentally different allocators. But the point is that CMA (at least the system cma area) fundamentally isn't a different allocator. The per-device CMA area still are kinda the same, but you can just have one fd for each per-device CMA area to make your life simple.

#35 lore raw

Re: [PATCH v6 4/5] dma-buf: heaps: Add CMA heap to dmabuf heaps

Quoted 15 lines · Christoph Hellwig
Quoted 4 lines · Benjamin Gaignard
Quoted 3 lines

But that just means we need a flag that memory needs to be contiguous, which totally makes sense at the API level. But CMA is not the only source of contigous memory, so we should not conflate the two.

Quoted 8 lines

We have one file descriptor per heap to be able to add access control on each heap.
That wasn't possible with ION because the heap was selected given the flags in ioctl
structure and we can't do access control based on that. If we put flag to select the
allocation mechanism (system, CMA, other) in ioctl we come back to ION status. For me one allocation mechanism = one heap.

Quoted 5 lines

Well, I agree with your split for different fundamentally different allocators. But the point is that CMA (at least the system cma area) fundamentally isn't a different allocator. The per-device CMA area still are kinda the same, but you can just have one fd for each per-device CMA area to make your life simple.

Form my point of view default cma area is a specific allocator because it could migrate page to give them in priority to contigous allocation. It is also one of the last region where system page are going to be allocated. I don't think that system allocator does have the same features, either we would have use it rather develop CMA years ago ;-). In short CMA had solved lot of problems on our side so it had to have it own heap.

#36 lore raw

PATCH v6 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 Xu YiPing <[email protected]>
Cc "Chenfeng (puck)" <[email protected]>
Cc "Xiaqing (A)" <[email protected]>
Cc Yudongbin <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Chenbo Feng <[email protected]>
Cc Alistair Strachan <[email protected]>
Change-Id Ib98569fdda6378eb086b8092fb5d6bd419b8d431

---
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

---
 tools/testing/selftests/dmabuf-heaps/Makefile |   9 +
 .../selftests/dmabuf-heaps/dmabuf-heap.c      | 234 ++++++++++++++++++
 2 files changed, 243 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 +234 -0

234 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
#37 lore raw
In reply to John Stultz
Quoted 4 lines · John Stultz

Here is 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.

I've gotten bogged down with both work and personal tasks so I haven't had a chance to look too closely but, once again, I'm happy to see this continue to move forward.

Quoted 33 lines

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 for now, since any such accounting should be implemented by dma-buf core or the heaps themselves.

New in v6:
* Number of cleanups and error path fixes suggested by Brian Starkey, many thanks for his close review and suggestions!

Quoted 7 lines

Outstanding concerns:
* Need to better understand various secure heap implementations. Some concern that heap private flags will be needed, but its also possible that dma-buf heaps can't solve everyone's needs, in which case, a vendor's secure buffer driver can implement their own dma-buf exporter. So I'm not too worried here.

syzbot found a DoS with Ion which I ACKed a fix for.
https://lore.kernel.org/lkml/[email protected]/
This series doesn't have the page pooling so that particular bug may not be applicable but given this is not the first time I've seen Ion used as a DoS mechanism, it would be good to think about putting in some basic checks.

Thanks,
Laura

Quoted 55 lines

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]>

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                       |  10 +
  drivers/dma-buf/Makefile                      |   2 +
  drivers/dma-buf/dma-heap.c                    | 249 +++++++++++++++++
  drivers/dma-buf/heaps/Kconfig                 |  14 +
  drivers/dma-buf/heaps/Makefile                |   4 +
  drivers/dma-buf/heaps/cma_heap.c              | 169 +++++++++++
  drivers/dma-buf/heaps/heap-helpers.c          | 262 ++++++++++++++++++
  drivers/dma-buf/heaps/heap-helpers.h          |  54 ++++
  drivers/dma-buf/heaps/system_heap.c           | 121 ++++++++
  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      | 234 ++++++++++++++++
  14 files changed, 1260 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

#38 lore raw
Quoted 52 lines · Laura Abbott
Quoted 4 lines · John Stultz

Here is 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.

I've gotten bogged down with both work and personal tasks so I haven't had a chance to look too closely but, once again, I'm happy to see this continue to move forward.

Quoted 33 lines

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 for now, since any such accounting should be implemented by dma-buf core or the heaps themselves.

New in v6:
* Number of cleanups and error path fixes suggested by Brian Starkey, many thanks for his close review and suggestions!

Quoted 7 lines

Outstanding concerns:
* Need to better understand various secure heap implementations. Some concern that heap private flags will be needed, but its also possible that dma-buf heaps can't solve everyone's needs, in which case, a vendor's secure buffer driver can implement their own dma-buf exporter. So I'm not too worried here.

Quoted 6 lines

syzbot found a DoS with Ion which I ACKed a fix for.
https://lore.kernel.org/lkml/[email protected]/
This series doesn't have the page pooling so that particular bug may not be applicable but given this is not the first time I've seen Ion used as a DoS mechanism, it would be good to think about putting in some basic checks.

Yea, there's no shrinker right now (and my WIP page pool implementation steals the network core's pagepool, which is statically sized).

But the check in the alloc code seems reasonable so I can add it to what I have. Appreciate the suggestion!

thanks
-john