Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

dma-buf: heaps: cma: Create CMA heap for each CMA reserved region

Aside from the main CMA region, it can be useful to allow userspace to
allocate from the other CMA reserved regions.

Indeed, those regions can have specific properties that can be useful to
a specific us-case.

For example, one of them platform I've been with has ECC enabled on the
entire memory but for a specific region. Using that region to allocate
framebuffers can be particular beneficial because enabling the ECC has a
performance and memory footprint cost.

Thus, exposing these regions as heaps user-space can allocate from and
import wherever needed allows to cover that use-case.

For now, only shared-dma-pools regions with the reusable property (ie,
backed by CMA) are supported, but eventually we'll want to support other
DMA pools types.

Since we collected all the CMA regions created during boot, we can
simply iterate over all of them to create the heaps.

This has a weird interaction with the recent work on the CMA name, in
particular the backward compatibility code created by commit
854acbe75ff4 ("dma-buf: heaps: Give default CMA heap a fixed name").

Indeed, the old name was either 'reserved', or the name of the
reserved-memory region device tree node if the linux,cma-default
property was set.

In both these cases, we have now collected this region during boot, and
we're using the same name. So we're now largely redundant with the
code to handle backward compatibility code, and we can thus remove it
and the associated Kconfig option.

Reviewed-by: T.J. Mercier <tjmercier@google.com>
Signed-off-by: Maxime Ripard <mripard@kernel.org>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
[sumits: rebased the doc to latest]
Link: https://lore.kernel.org/r/20251013-dma-buf-ecc-heap-v8-5-04ce150ea3d9@kernel.org

authored by

Maxime Ripard and committed by
Sumit Semwal
4f5f8baf 8f1fc1bf

+22 -30
+6 -3
Documentation/userspace-api/dma-buf-heaps.rst
··· 24 24 ``CMA_SIZE_MBYTES`` or ``CMA_SIZE_PERCENTAGE`` Kconfig options. Prior 25 25 to Linux 6.17, its name wasn't stable and could be called 26 26 ``reserved``, ``linux,cma``, or ``default-pool``, depending on the 27 - platform. From Linux 6.17 onwards, the creation of these heaps is 28 - controlled through the ``DMABUF_HEAPS_CMA_LEGACY`` Kconfig option for 29 - backwards compatibility. 27 + platform. 28 + 29 + - A heap will be created for each reusable region in the device tree 30 + with the ``shared-dma-pool`` compatible, using the full device tree 31 + node name as its name. The buffer semantics are identical to 32 + ``default-cma-region``. 30 33 31 34 Naming Convention 32 35 =================
-10
drivers/dma-buf/heaps/Kconfig
··· 12 12 Choose this option to enable dma-buf CMA heap. This heap is backed 13 13 by the Contiguous Memory Allocator (CMA). If your system has these 14 14 regions, you should say Y here. 15 - 16 - config DMABUF_HEAPS_CMA_LEGACY 17 - bool "Legacy DMA-BUF CMA Heap" 18 - default y 19 - depends on DMABUF_HEAPS_CMA 20 - help 21 - Add a duplicate CMA-backed dma-buf heap with legacy naming derived 22 - from the CMA area's devicetree node, or "reserved" if the area is not 23 - defined in the devicetree. This uses the same underlying allocator as 24 - CONFIG_DMABUF_HEAPS_CMA.
+16 -17
drivers/dma-buf/heaps/cma_heap.c
··· 22 22 #include <linux/io.h> 23 23 #include <linux/mm.h> 24 24 #include <linux/module.h> 25 + #include <linux/of.h> 26 + #include <linux/of_reserved_mem.h> 25 27 #include <linux/scatterlist.h> 26 28 #include <linux/slab.h> 27 29 #include <linux/vmalloc.h> ··· 411 409 return 0; 412 410 } 413 411 414 - static int __init add_default_cma_heap(void) 412 + static int __init add_cma_heaps(void) 415 413 { 416 414 struct cma *default_cma = dev_get_cma_area(NULL); 417 - const char *legacy_cma_name; 415 + unsigned int i; 418 416 int ret; 419 417 420 - if (!default_cma) 421 - return 0; 418 + if (default_cma) { 419 + ret = __add_cma_heap(default_cma, DEFAULT_CMA_NAME); 420 + if (ret) 421 + return ret; 422 + } 422 423 423 - ret = __add_cma_heap(default_cma, DEFAULT_CMA_NAME); 424 - if (ret) 425 - return ret; 424 + for (i = 0; i < dma_areas_num; i++) { 425 + struct cma *cma = dma_areas[i]; 426 426 427 - if (IS_ENABLED(CONFIG_DMABUF_HEAPS_CMA_LEGACY)) { 428 - legacy_cma_name = cma_get_name(default_cma); 429 - if (!strcmp(legacy_cma_name, DEFAULT_CMA_NAME)) { 430 - pr_warn("legacy name and default name are the same, skipping legacy heap\n"); 431 - return 0; 427 + ret = __add_cma_heap(cma, cma_get_name(cma)); 428 + if (ret) { 429 + pr_warn("Failed to add CMA heap %s", cma_get_name(cma)); 430 + continue; 432 431 } 433 432 434 - ret = __add_cma_heap(default_cma, legacy_cma_name); 435 - if (ret) 436 - pr_warn("failed to add legacy heap: %pe\n", 437 - ERR_PTR(ret)); 438 433 } 439 434 440 435 return 0; 441 436 } 442 - module_init(add_default_cma_heap); 437 + module_init(add_cma_heaps); 443 438 MODULE_DESCRIPTION("DMA-BUF CMA Heap");