Hi,
The recent introduction of heaps in the optee driver [1] made possible the creation of heaps as modules.
It's generally a good idea if possible, including for the already existing system and CMA heaps.
The system one is pretty trivial, the CMA one is a bit more involved, especially since we have a call from kernel/dma/contiguous.c to the CMA heap code. This was solved by turning the logic around and making the CMA heap call into the contiguous DMA code.
Let me know what you think, Maxime
1: https://lore.kernel.org/dri-devel/20250911135007.1275833-4-jens.wiklander@li...
Signed-off-by: Maxime Ripard mripard@kernel.org --- Changes in v2: - Collect tags - Don't export dma_contiguous_default_area anymore, but export dev_get_cma_area instead - Mentioned that heap modules can't be removed - Link to v1: https://lore.kernel.org/r/20260225-dma-buf-heaps-as-modules-v1-0-2109225a090...
--- Maxime Ripard (9): dma: contiguous: Turn heap registration logic around dma: contiguous: Make dev_get_cma_area() a proper function dma: contiguous: Make dma_contiguous_default_area static mm: cma: Export dev_get_cma_area() mm: cma: Export cma_alloc and cma_release mm: cma: Export cma_get_name dma-buf: heaps: Export mem_accounting parameter dma-buf: heaps: cma: Turn the heap into a module dma-buf: heaps: system: Turn the heap into a module
drivers/dma-buf/dma-heap.c | 1 + drivers/dma-buf/heaps/Kconfig | 4 ++-- drivers/dma-buf/heaps/cma_heap.c | 21 +++++---------------- drivers/dma-buf/heaps/system_heap.c | 5 +++++ include/linux/dma-map-ops.h | 14 ++++++-------- kernel/dma/contiguous.c | 37 ++++++++++++++++++++++++++++++++++--- mm/cma.c | 3 +++ 7 files changed, 56 insertions(+), 29 deletions(-) --- base-commit: 499a718536dc0e1c1d1b6211847207d58acd9916 change-id: 20260225-dma-buf-heaps-as-modules-1034b3ec9f2a
Best regards,
The CMA heap instantiation was initially developed by having the contiguous DMA code call into the CMA heap to create a new instance every time a reserved memory area is probed.
Turning the CMA heap into a module would create a dependency of the kernel on a module, which doesn't work.
Let's turn the logic around and do the opposite: store all the reserved memory CMA regions into the contiguous DMA code, and provide an iterator for the heap to use when it probes.
Reviewed-by: T.J. Mercier tjmercier@google.com Signed-off-by: Maxime Ripard mripard@kernel.org --- drivers/dma-buf/heaps/cma_heap.c | 18 ++---------------- include/linux/dma-map-ops.h | 5 +++++ kernel/dma/contiguous.c | 26 ++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c index bd3370b9a3f6d4e18885a1d0e8ba3f659b85ef47..f8a3d87f3ccee9630383ba28502eb40b10671cc2 100644 --- a/drivers/dma-buf/heaps/cma_heap.c +++ b/drivers/dma-buf/heaps/cma_heap.c @@ -28,23 +28,10 @@ #include <linux/slab.h> #include <linux/vmalloc.h>
#define DEFAULT_CMA_NAME "default_cma_region"
-static struct cma *dma_areas[MAX_CMA_AREAS] __initdata; -static unsigned int dma_areas_num __initdata; - -int __init dma_heap_cma_register_heap(struct cma *cma) -{ - if (dma_areas_num >= ARRAY_SIZE(dma_areas)) - return -EINVAL; - - dma_areas[dma_areas_num++] = cma; - - return 0; -} - struct cma_heap { struct dma_heap *heap; struct cma *cma; };
@@ -412,22 +399,21 @@ static int __init __add_cma_heap(struct cma *cma, const char *name) }
static int __init add_cma_heaps(void) { struct cma *default_cma = dev_get_cma_area(NULL); + struct cma *cma; unsigned int i; int ret;
if (default_cma) { ret = __add_cma_heap(default_cma, DEFAULT_CMA_NAME); if (ret) return ret; }
- for (i = 0; i < dma_areas_num; i++) { - struct cma *cma = dma_areas[i]; - + for (i = 0; (cma = dma_contiguous_get_reserved_region(i)) != NULL; i++) { ret = __add_cma_heap(cma, cma_get_name(cma)); if (ret) { pr_warn("Failed to add CMA heap %s", cma_get_name(cma)); continue; } diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 60b63756df821d839436618f1fca2bfa3eabe075..3007c68a8ec5b85990d1938d04a2f05c1a71acdb 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -110,10 +110,11 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages, int count); struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp); void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size); +struct cma *dma_contiguous_get_reserved_region(unsigned int idx); #else /* CONFIG_DMA_CMA */ static inline struct cma *dev_get_cma_area(struct device *dev) { return NULL; } @@ -148,10 +149,14 @@ static inline void dma_free_contiguous(struct device *dev, struct page *page, __free_pages(page, get_order(size)); } static inline void dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) { } +static inline struct cma *dma_contiguous_get_reserved_region(unsigned int idx) +{ + return NULL; +} #endif /* CONFIG_DMA_CMA*/
#ifdef CONFIG_DMA_DECLARE_COHERENT int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr, dma_addr_t device_addr, size_t size); diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index c56004d314dc2e436cddf3b20a4ee6ce8178bee4..14bd54fb758537f01a6fe27318e7b683964e20b1 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -456,10 +456,32 @@ void dma_free_contiguous(struct device *dev, struct page *page, size_t size) #include <linux/of_reserved_mem.h>
#undef pr_fmt #define pr_fmt(fmt) fmt
+static struct cma *rmem_cma_areas[MAX_CMA_AREAS]; +static unsigned int rmem_cma_areas_num; + +static int rmem_cma_insert_area(struct cma *cma) +{ + if (rmem_cma_areas_num >= ARRAY_SIZE(rmem_cma_areas)) + return -EINVAL; + + rmem_cma_areas[rmem_cma_areas_num++] = cma; + + return 0; +} + +struct cma *dma_contiguous_get_reserved_region(unsigned int idx) +{ + if (idx >= rmem_cma_areas_num) + return NULL; + + return rmem_cma_areas[idx]; +} +EXPORT_SYMBOL_GPL(dma_contiguous_get_reserved_region); + static int rmem_cma_device_init(struct reserved_mem *rmem, struct device *dev) { dev->cma_area = rmem->priv; return 0; } @@ -504,13 +526,13 @@ static int __init rmem_cma_setup(struct reserved_mem *rmem) rmem->priv = cma;
pr_info("Reserved memory: created CMA memory pool at %pa, size %ld MiB\n", &rmem->base, (unsigned long)rmem->size / SZ_1M);
- err = dma_heap_cma_register_heap(cma); + err = rmem_cma_insert_area(cma); if (err) - pr_warn("Couldn't register CMA heap."); + pr_warn("Couldn't store CMA reserved area.");
return 0; } RESERVEDMEM_OF_DECLARE(cma, "shared-dma-pool", rmem_cma_setup); #endif
As we try to enable dma-buf heaps, and the CMA one in particular, to compile as modules, we need to export dev_get_cma_area(). It's currently implemented as an inline function that returns either the content of device->cma_area or dma_contiguous_default_area.
Thus, it means we need to export dma_contiguous_default_area, which isn't really something we want any module to have access to.
Instead, let's make dev_get_cma_area() a proper function we will be able to export so we can avoid exporting dma_contiguous_default_area.
Signed-off-by: Maxime Ripard mripard@kernel.org --- include/linux/dma-map-ops.h | 7 +------ kernel/dma/contiguous.c | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 3007c68a8ec5b85990d1938d04a2f05c1a71acdb..dc7d54ac5dd66bd22ef868d7894cc1cb2ee4c156 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -91,16 +91,11 @@ static inline void set_dma_ops(struct device *dev, #endif /* CONFIG_ARCH_HAS_DMA_OPS */
#ifdef CONFIG_DMA_CMA extern struct cma *dma_contiguous_default_area;
-static inline struct cma *dev_get_cma_area(struct device *dev) -{ - if (dev && dev->cma_area) - return dev->cma_area; - return dma_contiguous_default_area; -} +struct cma *dev_get_cma_area(struct device *dev);
void dma_contiguous_reserve(phys_addr_t addr_limit); int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, phys_addr_t limit, struct cma **res_cma, bool fixed);
diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index 14bd54fb758537f01a6fe27318e7b683964e20b1..9a9ed7248fb823105609c5db5a51113e54a40192 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -99,10 +99,18 @@ early_param("cma", early_cma); bool __init cma_skip_dt_default_reserved_mem(void) { return size_cmdline != -1; }
+struct cma *dev_get_cma_area(struct device *dev) +{ + if (dev && dev->cma_area) + return dev->cma_area; + + return dma_contiguous_default_area; +} + #ifdef CONFIG_DMA_NUMA_CMA
static struct cma *dma_contiguous_numa_area[MAX_NUMNODES]; static phys_addr_t numa_cma_size[MAX_NUMNODES] __initdata; static struct cma *dma_contiguous_pernuma_area[MAX_NUMNODES];
Now that dev_get_cma_area() is no longer inline, we don't have any user of dma_contiguous_default_area() outside of contiguous.c so we can make it static.
Signed-off-by: Maxime Ripard mripard@kernel.org --- include/linux/dma-map-ops.h | 2 -- kernel/dma/contiguous.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index dc7d54ac5dd66bd22ef868d7894cc1cb2ee4c156..526c6978a306ffacd5e30387a2fd21af3333cf6c 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -89,12 +89,10 @@ static inline void set_dma_ops(struct device *dev, { } #endif /* CONFIG_ARCH_HAS_DMA_OPS */
#ifdef CONFIG_DMA_CMA -extern struct cma *dma_contiguous_default_area; - struct cma *dev_get_cma_area(struct device *dev);
void dma_contiguous_reserve(phys_addr_t addr_limit); int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, phys_addr_t limit, struct cma **res_cma, bool fixed); diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index 9a9ed7248fb823105609c5db5a51113e54a40192..a4279d800d4658bf1c33b9b1da100eee1367d42f 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -51,11 +51,11 @@ #define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES #else #define CMA_SIZE_MBYTES 0 #endif
-struct cma *dma_contiguous_default_area; +static struct cma *dma_contiguous_default_area;
/* * Default global CMA area size can be defined in kernel's .config. * This is useful mainly for distro maintainers to create a kernel * that works correctly for most supported systems.
The CMA dma-buf heap uses the dev_get_cma_area() function to retrieve the default contiguous area.
Now that this function is no longer inlined, and since we want to turn the CMA heap into a module, let's export it.
Signed-off-by: Maxime Ripard mripard@kernel.org --- kernel/dma/contiguous.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index a4279d800d4658bf1c33b9b1da100eee1367d42f..ad50512d71d3088a73e4b1ac02d6e6122374888e 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -106,10 +106,11 @@ struct cma *dev_get_cma_area(struct device *dev) if (dev && dev->cma_area) return dev->cma_area;
return dma_contiguous_default_area; } +EXPORT_SYMBOL_GPL(dev_get_cma_area);
#ifdef CONFIG_DMA_NUMA_CMA
static struct cma *dma_contiguous_numa_area[MAX_NUMNODES]; static phys_addr_t numa_cma_size[MAX_NUMNODES] __initdata;
The CMA dma-buf heap uses cma_alloc() and cma_release() to allocate and free, respectively, its CMA buffers.
However, these functions are not exported. Since we want to turn the CMA heap into a module, let's export them both.
Reviewed-by: T.J. Mercier tjmercier@google.com Signed-off-by: Maxime Ripard mripard@kernel.org --- mm/cma.c | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/mm/cma.c b/mm/cma.c index 94b5da468a7d719e5144d33b06bcc7619c0fbcc9..be142b473f3bd41b9c7d8ba4397f018f6993d962 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -949,10 +949,11 @@ struct page *cma_alloc(struct cma *cma, unsigned long count, if (page) set_pages_refcounted(page, count);
return page; } +EXPORT_SYMBOL_GPL(cma_alloc);
static struct cma_memrange *find_cma_memrange(struct cma *cma, const struct page *pages, unsigned long count) { struct cma_memrange *cmr = NULL; @@ -1025,10 +1026,11 @@ bool cma_release(struct cma *cma, const struct page *pages,
__cma_release_frozen(cma, cmr, pages, count);
return true; } +EXPORT_SYMBOL_GPL(cma_release);
bool cma_release_frozen(struct cma *cma, const struct page *pages, unsigned long count) { struct cma_memrange *cmr;
The CMA dma-buf heap uses the cma_get_name() function to get the name of the heap instance it's going to create.
However, this function is not exported. Since we want to turn the CMA heap into a module, let's export it.
Reviewed-by: T.J. Mercier tjmercier@google.com Signed-off-by: Maxime Ripard mripard@kernel.org --- mm/cma.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/mm/cma.c b/mm/cma.c index be142b473f3bd41b9c7d8ba4397f018f6993d962..550effb9c4e01cc488b5744fe61d55a5b70a6d6c 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -50,10 +50,11 @@ unsigned long cma_get_size(const struct cma *cma)
const char *cma_get_name(const struct cma *cma) { return cma->name; } +EXPORT_SYMBOL_GPL(cma_get_name);
static unsigned long cma_bitmap_aligned_mask(const struct cma *cma, unsigned int align_order) { if (align_order <= cma->order_per_bit)
The mem_accounting kernel parameter is used by heaps to know if they should account allocations in their respective cgroup controllers.
Since we're going to allow heaps to compile as modules, we need to export that variable.
Reviewed-by: T.J. Mercier tjmercier@google.com Signed-off-by: Maxime Ripard mripard@kernel.org --- drivers/dma-buf/dma-heap.c | 1 + 1 file changed, 1 insertion(+)
diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index ac5f8685a649496c0e1c6decbf263b63fa472d04..a76bf3f8b071a3d5bf39a8513f31e9e8aa16e02f 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -51,10 +51,11 @@ static DEFINE_XARRAY_ALLOC(dma_heap_minors);
bool __read_mostly mem_accounting; module_param(mem_accounting, bool, 0444); MODULE_PARM_DESC(mem_accounting, "Enable cgroup-based memory accounting for dma-buf heap allocations (default=false)."); +EXPORT_SYMBOL_NS_GPL(mem_accounting, "DMA_BUF_HEAP");
static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, u32 fd_flags, u64 heap_flags) {
Now that all the symbols used by the CMA heap are exported, turning the CMA heap into a module becomes pretty easy: we just need to add the usual MODULE_* macros, import the proper namespaces and change the Kconfig symbol to a tristate.
This heap won't be able to unload though, since we're missing a lot of infrastructure to make it safe.
Reviewed-by: T.J. Mercier tjmercier@google.com Signed-off-by: Maxime Ripard mripard@kernel.org --- drivers/dma-buf/heaps/Kconfig | 2 +- drivers/dma-buf/heaps/cma_heap.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig index a5eef06c422644e8aadaf5aff2bd9a33c49c1ba3..aed0b9b4febf388376cfc41be9843980d010c4e8 100644 --- a/drivers/dma-buf/heaps/Kconfig +++ b/drivers/dma-buf/heaps/Kconfig @@ -4,11 +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" + tristate "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. diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c index f8a3d87f3ccee9630383ba28502eb40b10671cc2..7216a14262b04bb6130ddf26b7d009f7d15b03fd 100644 --- a/drivers/dma-buf/heaps/cma_heap.c +++ b/drivers/dma-buf/heaps/cma_heap.c @@ -422,5 +422,8 @@ static int __init add_cma_heaps(void)
return 0; } module_init(add_cma_heaps); MODULE_DESCRIPTION("DMA-BUF CMA Heap"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("DMA_BUF"); +MODULE_IMPORT_NS("DMA_BUF_HEAP");
Hi Maxime,
kernel test robot noticed the following build errors:
[auto build test ERROR on 499a718536dc0e1c1d1b6211847207d58acd9916]
url: https://github.com/intel-lab-lkp/linux/commits/Maxime-Ripard/dma-contiguous-... base: 499a718536dc0e1c1d1b6211847207d58acd9916 patch link: https://lore.kernel.org/r/20260227-dma-buf-heaps-as-modules-v2-8-454aee7e06c... patch subject: [PATCH v2 8/9] dma-buf: heaps: cma: Turn the heap into a module config: x86_64-randconfig-002-20260227 (https://download.01.org/0day-ci/archive/20260228/202602281751.KRAYsRVG-lkp@i...) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260228/202602281751.KRAYsRVG-lkp@i...)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot lkp@intel.com | Closes: https://lore.kernel.org/oe-kbuild-all/202602281751.KRAYsRVG-lkp@intel.com/
All errors (new ones prefixed by >>, old ones prefixed by <<):
ERROR: modpost: "dma_contiguous_get_reserved_region" [drivers/dma-buf/heaps/cma_heap.ko] undefined!
The system heap can be easily turned into a module by adding the usual MODULE_* macros, importing the proper namespaces and changing the Kconfig symbol to a tristate.
This heap won't be able to unload though, since we're missing a lot of infrastructure to make it safe.
Reviewed-by: T.J. Mercier tjmercier@google.com Signed-off-by: Maxime Ripard mripard@kernel.org --- drivers/dma-buf/heaps/Kconfig | 2 +- drivers/dma-buf/heaps/system_heap.c | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/dma-buf/heaps/Kconfig b/drivers/dma-buf/heaps/Kconfig index aed0b9b4febf388376cfc41be9843980d010c4e8..e273fb18feca091ccd9b406e68f86c12efb339e9 100644 --- a/drivers/dma-buf/heaps/Kconfig +++ b/drivers/dma-buf/heaps/Kconfig @@ -1,7 +1,7 @@ config DMABUF_HEAPS_SYSTEM - bool "DMA-BUF System Heap" + tristate "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.
diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index b3650d8fd6511a4a755612cfe3a9d9fee796f80e..1957403b0f2ae5e82ab39f5945dfe82808e93964 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -442,5 +442,10 @@ static int __init system_heap_create(void) return PTR_ERR(sys_heap);
return 0; } module_init(system_heap_create); + +MODULE_DESCRIPTION("DMA-BUF System Heap"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("DMA_BUF"); +MODULE_IMPORT_NS("DMA_BUF_HEAP");
linaro-mm-sig@lists.linaro.org