DMA-BUF Heaps (destaging ION)
Subsystems (3)
v4 cover DMA-BUF Heaps (destaging ION)
Here is another RFC of 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 v4:
* Add fd_flags per Benjamin's request to specify the creation flags for the dmabuf fd.
* Added some optimization in the system heap to allocate contiguous pages where possible.
* Reworked the kselftest code to use vgem rather then introducing a dummy importer
* Other cleanups suggested by Benjamin and Andrew.
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.
* Making sure the performance issues from potentially unnecessary cache-management operations can be resolved properly for system and cma heaps(outstanding issue from ION).
That said, the main user-interface is shaping up and I wanted to get some input on the device model (particularly from GreKH) and any other API/ABI specific input.
thanks
-john
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 | 241 ++++++++++++++++ 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 | 261 ++++++++++++++++++ drivers/dma-buf/heaps/heap-helpers.h | 55 ++++ drivers/dma-buf/heaps/system_heap.c | 162 +++++++++++ include/linux/dma-heap.h | 59 ++++ include/uapi/linux/dma-heap.h | 56 ++++ tools/testing/selftests/dmabuf-heaps/Makefile | 11 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 232 ++++++++++++++++ 14 files changed, 1294 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
RFC PATCH v4 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!
---
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 then 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)
--- MAINTAINERS | 18 +++ drivers/dma-buf/Kconfig | 8 ++ drivers/dma-buf/Makefile | 1 + drivers/dma-buf/dma-heap.c | 241 ++++++++++++++++++++++++++++++++++ include/linux/dma-heap.h | 59 +++++++++ include/uapi/linux/dma-heap.h | 56 ++++++++ 6 files changed, 383 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
@@ -4725,6 +4725,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
@@ -38,4 +38,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
@@ -1,4 +1,5 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.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 +241 -0
241 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 +56 -0
56 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
RFC PATCH v4 2/5 dma-buf: heaps: Add heap helpers
In reply to John StultzAdd 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!
---
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
--- drivers/dma-buf/Makefile | 1 + drivers/dma-buf/heaps/Makefile | 2 + drivers/dma-buf/heaps/heap-helpers.c | 261 +++++++++++++++++++++++++++ drivers/dma-buf/heaps/heap-helpers.h | 55 ++++++ 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
@@ -1,4 +1,5 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.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
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-y += heap-helpers.o
drivers/dma-buf/heaps/heap-helpers.c new file +261 -0
261 lines not on this page · raw message
drivers/dma-buf/heaps/heap-helpers.h new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v4 3/5 dma-buf: heaps: Add system heap to dmabuf heaps
In reply to John StultzThis 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!
---
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)
--- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/heaps/Kconfig | 6 ++ drivers/dma-buf/heaps/Makefile | 1 + drivers/dma-buf/heaps/system_heap.c | 162 ++++++++++++++++++++++++++++ 4 files changed, 171 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
@@ -46,4 +46,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
@@ -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
@@ -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 +162 -0
162 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
RFC PATCH v4 4/5 dma-buf: heaps: Add CMA heap to dmabuf heaps
In reply to John StultzThis 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!
---
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
* Use the fd_flags when creating dmabuf fd (Suggested by Benjamin)
* Use precalculated pagecount (Suggested by Andrew)
--- 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
@@ -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
@@ -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 · 6 lines
2.17.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC][PATCH 4/5 v4] dma-buf: heaps: Add CMA heap to dmabuf heaps
Quoted 180 lines · 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 butao <[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]>Signed-off-by John Stultz <[email protected]>---
v2:
* Switch allocate to return dmabuf fd
* Simplify init code
* Checkpatch fixups
v3:
* Switch to inline function forto_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
* Use thefd_flagswhen creating dmabuf fd (Suggested by Benjamin)
* Use precalculated pagecount (Suggested by Andrew)--- 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.cdrivers/dma-buf/heaps/Kconfig +8
-0@@ -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@@ -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.odrivers/dma-buf/heaps/cma_heap.c new file +102
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* DMABUF CMA heap exporter +* +* Copyright (C) 2012, 2019 Linaro Ltd. +* Author: <[email protected]> for ST-Ericsson. +*/ + +#include <linux/device.h> +#include <linux/dma-buf.h> +#include <linux/dma-heap.h> +#include <linux/slab.h> +#include <linux/errno.h> +#include <linux/err.h> +#include <linux/cma.h> +#include <linux/scatterlist.h> +#include <linux/highmem.h> + +#include "heap-helpers.h" + +struct cma_heap { +struct dma_heap *heap; +struct cma *cma; +}; + +static void cma_heap_free (struct heap_helper_buffer *buffer) +{ +struct cma_heap *cma_heap =dma_heap_get_data (buffer->heap_buffer.heap); +unsigned long nr_pages = buffer->pagecount; +struct page *pages = buffer->priv_virt; + +/* free page list */ +kfree (buffer->pages); +/* release memory */ +cma_release (cma_heap->cma, pages, nr_pages); +kfree (buffer); +} + +/* dmabuf heap CMA operations functions */ +static int cma_heap_allocate (struct dma_heap *heap, +unsigned long len, +unsigned long fd_flags, +unsigned long heap_flags) +{ +struct cma_heap *cma_heap =dma_heap_get_data (heap); +struct heap_helper_buffer *helper_buffer; +struct page *pages; +size_t size =PAGE_ALIGN (len); +unsigned long nr_pages = size >>PAGE_SHIFT ; +unsigned long align =get_order (size); +DEFINE_DMA_BUF_EXPORT_INFO (exp_info); +struct dma_buf *dmabuf; +int ret = -ENOMEM ; +pgoff_t pg; + +if (align >CONFIG_CMA_ALIGNMENT ) + align =CONFIG_CMA_ALIGNMENT ; + + helper_buffer =kzalloc (sizeof (*helper_buffer),GFP_KERNEL ); +if (!helper_buffer) +return -ENOMEM ; + +INIT_HEAP_HELPER_BUFFER (helper_buffer, cma_heap_free); + helper_buffer->heap_buffer.flags = heap_flags; + helper_buffer->heap_buffer.heap = heap; + helper_buffer->heap_buffer.size = len; + + pages =cma_alloc (cma_heap->cma, nr_pages, align, false); +if (!pages) + goto free_buf; + +if (PageHighMem (pages)) { +unsigned long nr_clear_pages = nr_pages; +struct page *page = pages; + +while (nr_clear_pages >0 ) { +void *vaddr =kmap_atomic (page); + +memset (vaddr,0 ,PAGE_SIZE ); +kunmap_atomic (vaddr); + page++; + nr_clear_pages--; + } + }else { +memset (page_address (pages),0 , size); + } + + helper_buffer->pagecount = nr_pages; + helper_buffer->pages =kmalloc_array (helper_buffer->pagecount, +sizeof (*helper_buffer->pages), +GFP_KERNEL ); +if (!helper_buffer->pages) { + ret = -ENOMEM ; + goto free_cma; + } + +for (pg =0 ; pg < helper_buffer->pagecount; pg++) { + helper_buffer->pages[pg] = &pages[pg]; +if (!helper_buffer->pages[pg]) + goto free_pages; + } +
drivers/dma-buf/heaps/cma_heap.c line 112
Quoted 10 lines
drivers/dma-buf/heaps/cma_heap.c new file +10
-0+/* create the dmabuf */ + exp_info.ops = &heap_helper_ops; + exp_info.size = len; + exp_info.flags = fd_flags; + exp_info.priv = &helper_buffer->heap_buffer; + dmabuf =dma_buf_export (&exp_info); +if (IS_ERR (dmabuf)) { + ret =PTR_ERR (dmabuf); + goto free_pages; + }
Can the dmabuf be created in the framework layer?
each heap needs to add the same code here, which is not very good.
Quoted 58 lines
drivers/dma-buf/heaps/cma_heap.c new file +57
-0+ + helper_buffer->heap_buffer.dmabuf = dmabuf; + helper_buffer->priv_virt = pages; + + ret =dma_buf_fd (dmabuf, fd_flags); +if (ret <0 ) { +dma_buf_put (dmabuf); +/* just return, as put will call release and that will free */ +return ret; + } + +return ret; + +free_pages: +kfree (helper_buffer->pages); +free_cma: +cma_release (cma_heap->cma, pages, nr_pages); +free_buf: +kfree (helper_buffer); +return ret; +} + +static struct dma_heap_ops cma_heap_ops = { + .allocate = cma_heap_allocate, +}; + +static int __add_cma_heap (struct cma *cma,void *data) +{ +struct cma_heap *cma_heap; +struct dma_heap_export_info exp_info; + + cma_heap =kzalloc (sizeof (*cma_heap),GFP_KERNEL ); +if (!cma_heap) +return -ENOMEM ; + cma_heap->cma = cma; + + exp_info.name =cma_get_name (cma); + exp_info.ops = &cma_heap_ops; + exp_info.priv = cma_heap; + + cma_heap->heap =dma_heap_add (&exp_info); +if (IS_ERR (cma_heap->heap)) { +int ret =PTR_ERR (cma_heap->heap); + +kfree (cma_heap); +return ret; + } + +return 0 ; +} + +static int add_cma_heaps (void ) +{ +cma_for_each_area (__add_cma_heap,NULL ); +return 0 ; +} +device_initcall (add_cma_heaps);
Re: [RFC][PATCH 4/5 v4] dma-buf: heaps: Add CMA heap to dmabuf heaps
Quoted 188 lines · Xiaqing (A)
Quoted 184 lines · 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 butao <[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]>Signed-off-by John Stultz <[email protected]>---
v2:
* Switch allocate to return dmabuf fd
* Simplify init code
* Checkpatch fixups
v3:
* Switch to inline function forto_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
* Use thefd_flagswhen creating dmabuf fd (Suggested by Benjamin)
* Use precalculated pagecount (Suggested by Andrew)--- 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.cdrivers/dma-buf/heaps/Kconfig +8
-0@@ -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@@ -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.odrivers/dma-buf/heaps/cma_heap.c new file +106
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* DMABUF CMA heap exporter +* +* Copyright (C) 2012, 2019 Linaro Ltd. +* Author: <[email protected]> for ST-Ericsson. +*/ + +#include <linux/device.h> +#include <linux/dma-buf.h> +#include <linux/dma-heap.h> +#include <linux/slab.h> +#include <linux/errno.h> +#include <linux/err.h> +#include <linux/cma.h> +#include <linux/scatterlist.h> +#include <linux/highmem.h> + +#include "heap-helpers.h" + +struct cma_heap { +struct dma_heap *heap; +struct cma *cma; +}; + +static void cma_heap_free (struct heap_helper_buffer *buffer) +{ +struct cma_heap *cma_heap =dma_heap_get_data (buffer->heap_buffer.heap); +unsigned long nr_pages = buffer->pagecount; +struct page *pages = buffer->priv_virt; + +/* free page list */ +kfree (buffer->pages); +/* release memory */ +cma_release (cma_heap->cma, pages, nr_pages); +kfree (buffer); +} + +/* dmabuf heap CMA operations functions */ +static int cma_heap_allocate (struct dma_heap *heap, +unsigned long len, +unsigned long fd_flags, +unsigned long heap_flags) +{ +struct cma_heap *cma_heap =dma_heap_get_data (heap); +struct heap_helper_buffer *helper_buffer; +struct page *pages; +size_t size =PAGE_ALIGN (len); +unsigned long nr_pages = size >>PAGE_SHIFT ; +unsigned long align =get_order (size); +DEFINE_DMA_BUF_EXPORT_INFO (exp_info); +struct dma_buf *dmabuf; +int ret = -ENOMEM ; +pgoff_t pg; + +if (align >CONFIG_CMA_ALIGNMENT ) + align =CONFIG_CMA_ALIGNMENT ; + + helper_buffer =kzalloc (sizeof (*helper_buffer),GFP_KERNEL ); +if (!helper_buffer) +return -ENOMEM ; + +INIT_HEAP_HELPER_BUFFER (helper_buffer, cma_heap_free); + helper_buffer->heap_buffer.flags = heap_flags; + helper_buffer->heap_buffer.heap = heap; + helper_buffer->heap_buffer.size = len; + + pages =cma_alloc (cma_heap->cma, nr_pages, align, false); +if (!pages) + goto free_buf; + +if (PageHighMem (pages)) { +unsigned long nr_clear_pages = nr_pages; +struct page *page = pages; + +while (nr_clear_pages >0 ) { +void *vaddr =kmap_atomic (page); + +memset (vaddr,0 ,PAGE_SIZE ); +kunmap_atomic (vaddr); + page++; + nr_clear_pages--; + } + }else { +memset (page_address (pages),0 , size); + } + + helper_buffer->pagecount = nr_pages; + helper_buffer->pages =kmalloc_array (helper_buffer->pagecount, +sizeof (*helper_buffer->pages), +GFP_KERNEL ); +if (!helper_buffer->pages) { + ret = -ENOMEM ; + goto free_cma; + } + +for (pg =0 ; pg < helper_buffer->pagecount; pg++) { + helper_buffer->pages[pg] = &pages[pg]; +if (!helper_buffer->pages[pg]) + goto free_pages; + } + +/* create the dmabuf */ + exp_info.ops = &heap_helper_ops; + exp_info.size = len; + exp_info.flags = fd_flags;
Quoted 8 lines
drivers/dma-buf/heaps/cma_heap.c line 112
Quoted 6 lines
drivers/dma-buf/heaps/cma_heap.c new file +6
-0+ exp_info.priv = &helper_buffer->heap_buffer; + dmabuf =dma_buf_export (&exp_info); +if (IS_ERR (dmabuf)) { + ret =PTR_ERR (dmabuf); + goto free_pages; + }Can the dmabuf be created in the framework layer?
each heap needs to add the same code here, which is not very good.
No that would make errors cases more difficult to handle because you will need to call a new heap's function to clean everything.
Quoted 60 lines
Quoted 59 lines
drivers/dma-buf/heaps/cma_heap.c new file +57
-0+ + helper_buffer->heap_buffer.dmabuf = dmabuf; + helper_buffer->priv_virt = pages; + + ret =dma_buf_fd (dmabuf, fd_flags); +if (ret <0 ) { +dma_buf_put (dmabuf); +/* just return, as put will call release and that will free */ +return ret; + } + +return ret; + +free_pages: +kfree (helper_buffer->pages); +free_cma: +cma_release (cma_heap->cma, pages, nr_pages); +free_buf: +kfree (helper_buffer); +return ret; +} + +static struct dma_heap_ops cma_heap_ops = { + .allocate = cma_heap_allocate, +}; + +static int __add_cma_heap (struct cma *cma,void *data) +{ +struct cma_heap *cma_heap; +struct dma_heap_export_info exp_info; + + cma_heap =kzalloc (sizeof (*cma_heap),GFP_KERNEL ); +if (!cma_heap) +return -ENOMEM ; + cma_heap->cma = cma; + + exp_info.name =cma_get_name (cma); + exp_info.ops = &cma_heap_ops; + exp_info.priv = cma_heap; + + cma_heap->heap =dma_heap_add (&exp_info); +if (IS_ERR (cma_heap->heap)) { +int ret =PTR_ERR (cma_heap->heap); + +kfree (cma_heap); +return ret; + } + +return 0 ; +} + +static int add_cma_heaps (void ) +{ +cma_for_each_area (__add_cma_heap,NULL ); +return 0 ; +} +device_initcall (add_cma_heaps);
Re: [RFC][PATCH 4/5 v4] dma-buf: heaps: Add CMA heap to dmabuf heaps
In reply to Xiaqing (A)Quoted 188 lines · Xiaqing (A)
Quoted 184 lines · 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 butao <[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]>Signed-off-by John Stultz <[email protected]>---
v2:
* Switch allocate to return dmabuf fd
* Simplify init code
* Checkpatch fixups
v3:
* Switch to inline function forto_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
* Use thefd_flagswhen creating dmabuf fd (Suggested by Benjamin)
* Use precalculated pagecount (Suggested by Andrew)--- 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.cdrivers/dma-buf/heaps/Kconfig +8
-0@@ -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@@ -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.odrivers/dma-buf/heaps/cma_heap.c new file +106
-0@@ -0,0 +1,169 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* DMABUF CMA heap exporter +* +* Copyright (C) 2012, 2019 Linaro Ltd. +* Author: <[email protected]> for ST-Ericsson. +*/ + +#include <linux/device.h> +#include <linux/dma-buf.h> +#include <linux/dma-heap.h> +#include <linux/slab.h> +#include <linux/errno.h> +#include <linux/err.h> +#include <linux/cma.h> +#include <linux/scatterlist.h> +#include <linux/highmem.h> + +#include "heap-helpers.h" + +struct cma_heap { +struct dma_heap *heap; +struct cma *cma; +}; + +static void cma_heap_free (struct heap_helper_buffer *buffer) +{ +struct cma_heap *cma_heap =dma_heap_get_data (buffer->heap_buffer.heap); +unsigned long nr_pages = buffer->pagecount; +struct page *pages = buffer->priv_virt; + +/* free page list */ +kfree (buffer->pages); +/* release memory */ +cma_release (cma_heap->cma, pages, nr_pages); +kfree (buffer); +} + +/* dmabuf heap CMA operations functions */ +static int cma_heap_allocate (struct dma_heap *heap, +unsigned long len, +unsigned long fd_flags, +unsigned long heap_flags) +{ +struct cma_heap *cma_heap =dma_heap_get_data (heap); +struct heap_helper_buffer *helper_buffer; +struct page *pages; +size_t size =PAGE_ALIGN (len); +unsigned long nr_pages = size >>PAGE_SHIFT ; +unsigned long align =get_order (size); +DEFINE_DMA_BUF_EXPORT_INFO (exp_info); +struct dma_buf *dmabuf; +int ret = -ENOMEM ; +pgoff_t pg; + +if (align >CONFIG_CMA_ALIGNMENT ) + align =CONFIG_CMA_ALIGNMENT ; + + helper_buffer =kzalloc (sizeof (*helper_buffer),GFP_KERNEL ); +if (!helper_buffer) +return -ENOMEM ; + +INIT_HEAP_HELPER_BUFFER (helper_buffer, cma_heap_free); + helper_buffer->heap_buffer.flags = heap_flags; + helper_buffer->heap_buffer.heap = heap; + helper_buffer->heap_buffer.size = len; + + pages =cma_alloc (cma_heap->cma, nr_pages, align, false); +if (!pages) + goto free_buf; + +if (PageHighMem (pages)) { +unsigned long nr_clear_pages = nr_pages; +struct page *page = pages; + +while (nr_clear_pages >0 ) { +void *vaddr =kmap_atomic (page); + +memset (vaddr,0 ,PAGE_SIZE ); +kunmap_atomic (vaddr); + page++; + nr_clear_pages--; + } + }else { +memset (page_address (pages),0 , size); + } + + helper_buffer->pagecount = nr_pages; + helper_buffer->pages =kmalloc_array (helper_buffer->pagecount, +sizeof (*helper_buffer->pages), +GFP_KERNEL ); +if (!helper_buffer->pages) { + ret = -ENOMEM ; + goto free_cma; + } + +for (pg =0 ; pg < helper_buffer->pagecount; pg++) { + helper_buffer->pages[pg] = &pages[pg]; +if (!helper_buffer->pages[pg]) + goto free_pages; + } + +/* create the dmabuf */ + exp_info.ops = &heap_helper_ops; + exp_info.size = len; + exp_info.flags = fd_flags;
Quoted 8 lines
drivers/dma-buf/heaps/cma_heap.c line 112
Quoted 6 lines
drivers/dma-buf/heaps/cma_heap.c new file +6
-0+ exp_info.priv = &helper_buffer->heap_buffer; + dmabuf =dma_buf_export (&exp_info); +if (IS_ERR (dmabuf)) { + ret =PTR_ERR (dmabuf); + goto free_pages; + }Can the dmabuf be created in the framework layer?
each heap needs to add the same code here, which is not very good.
Benjamin's point is true, that it might not be best to try to handle it in the framework layer, but there is a fair amount of boilerplate that I'll see if I can refactor into some helper functions.
thanks for the feedback!
-john
RFC PATCH v4 5/5 kselftests: Add dma-heap test
In reply to John StultzAdd 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]>
---
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
--- tools/testing/selftests/dmabuf-heaps/Makefile | 11 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 232 ++++++++++++++++++ 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 +11 -0
@@ -0,0 +1,11 @@ +# 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 +232 -0
232 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 54 lines · John Stultz
Here is another RFC of 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-heapAnd the userspace changes here:
https://android-review.googlesource.com/c/device/linaro/hikey/+/909436Compared 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 v4:
* Addfd_flagsper Benjamin's request to specify the creation flags for the dmabuf fd.
* Added some optimization in the system heap to allocate contiguous pages where possible.
* Reworked the kselftest code to use vgem rather then introducing a dummy importer
* Other cleanups suggested by Benjamin and Andrew.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.
* Making sure the performance issues from potentially unnecessary cache-management operations can be resolved properly for system and cma heaps(outstanding issue from ION).
Quoted 4 lines
That said, the main user-interface is shaping up and I wanted to get some input on the device model (particularly from GreKH) and any other API/ABI specific input.
For the whole series:
Thanks,
Benjamin
Quoted 62 lines
thanks
-johnCc 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 butao <[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]>Andrew F. Davis (1):
dma-buf: Add dma-buf heaps frameworkJohn 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 testMAINTAINERS | 18 ++ drivers/dma-buf/Kconfig | 10 + drivers/dma-buf/Makefile | 2 + drivers/dma-buf/dma-heap.c | 241 ++++++++++++++++ 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 | 261 ++++++++++++++++++ drivers/dma-buf/heaps/heap-helpers.h | 55 ++++ drivers/dma-buf/heaps/system_heap.c | 162 +++++++++++ include/linux/dma-heap.h | 59 ++++ include/uapi/linux/dma-heap.h | 56 ++++ tools/testing/selftests/dmabuf-heaps/Makefile | 11 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 232 ++++++++++++++++ 14 files changed, 1294 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--
2.17.1