devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
@ 2025-12-10  0:20 ` Oreoluwa Babatunde
  2025-12-10 14:07   ` Rob Herring
                     ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Oreoluwa Babatunde @ 2025-12-10  0:20 UTC (permalink / raw)
  To: robh, m.szyprowski, ye.li
  Cc: oreoluwa.babatunde, kernel, saravanak, akpm, david,
	lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb, mhocko,
	robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako

When initializing the default cma region, the "cma=" kernel parameter
takes priority over a DT defined linux,cma-default region. Hence, give
the reserved_mem framework the ability to detect this so that the DT
defined cma region can skip initialization accordingly.

Signed-off-by: Oreoluwa Babatunde <oreoluwa.babatunde@oss.qualcomm.com>
---
 drivers/of/of_reserved_mem.c | 19 +++++++++++++++++--
 include/linux/cma.h          |  1 +
 kernel/dma/contiguous.c      | 16 ++++++++++------
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 2e9ea751ed2d..bef68a4916b5 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -158,7 +158,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 	phys_addr_t base, size;
 	int len;
 	const __be32 *prop;
-	bool nomap;
+	bool nomap, default_cma;
 
 	prop = of_get_flat_dt_prop(node, "reg", &len);
 	if (!prop)
@@ -171,6 +171,12 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 	}
 
 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
+	default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
+
+	if (default_cma && cma_skip_dt_default_reserved_mem()) {
+		pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
+		return -EINVAL;
+	}
 
 	while (len >= t_len) {
 		base = dt_mem_next_cell(dt_root_addr_cells, &prop);
@@ -256,12 +262,15 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
 
 	fdt_for_each_subnode(child, fdt, node) {
 		const char *uname;
+		bool default_cma = of_get_flat_dt_prop(child, "linux,cma-default", NULL);
 
 		prop = of_get_flat_dt_prop(child, "reg", &len);
 		if (!prop)
 			continue;
 		if (!of_fdt_device_is_available(fdt, child))
 			continue;
+		if (default_cma && cma_skip_dt_default_reserved_mem())
+			continue;
 
 		uname = fdt_get_name(fdt, child, NULL);
 		if (len && len % t_len != 0) {
@@ -406,7 +415,7 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam
 	phys_addr_t base = 0, align = 0, size;
 	int len;
 	const __be32 *prop;
-	bool nomap;
+	bool nomap, default_cma;
 	int ret;
 
 	prop = of_get_flat_dt_prop(node, "size", &len);
@@ -430,6 +439,12 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam
 	}
 
 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
+	default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
+
+	if (default_cma && cma_skip_dt_default_reserved_mem()) {
+		pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
+		return -EINVAL;
+	}
 
 	/* Need adjust the alignment to satisfy the CMA requirement */
 	if (IS_ENABLED(CONFIG_CMA)
diff --git a/include/linux/cma.h b/include/linux/cma.h
index 62d9c1cf6326..3d3047029950 100644
--- a/include/linux/cma.h
+++ b/include/linux/cma.h
@@ -47,6 +47,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
 					unsigned int order_per_bit,
 					const char *name,
 					struct cma **res_cma);
+extern bool cma_skip_dt_default_reserved_mem(void);
 extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
 			      bool no_warn);
 extern bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count);
diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
index d9b9dcba6ff7..9071c08650e3 100644
--- a/kernel/dma/contiguous.c
+++ b/kernel/dma/contiguous.c
@@ -90,6 +90,16 @@ static int __init early_cma(char *p)
 }
 early_param("cma", early_cma);
 
+/*
+ * cma_skip_dt_default_reserved_mem - This is called from the
+ * reserved_mem framework to detect if the default cma region is being
+ * set by the "cma=" kernel parameter.
+ */
+bool __init cma_skip_dt_default_reserved_mem(void)
+{
+	return size_cmdline != -1;
+}
+
 #ifdef CONFIG_DMA_NUMA_CMA
 
 static struct cma *dma_contiguous_numa_area[MAX_NUMNODES];
@@ -463,12 +473,6 @@ static int __init rmem_cma_setup(struct reserved_mem *rmem)
 	struct cma *cma;
 	int err;
 
-	if (size_cmdline != -1 && default_cma) {
-		pr_info("Reserved memory: bypass %s node, using cmdline CMA params instead\n",
-			rmem->name);
-		return -EBUSY;
-	}
-
 	if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
 	    of_get_flat_dt_prop(node, "no-map", NULL))
 		return -EINVAL;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
  2025-12-10  0:20 ` [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param Oreoluwa Babatunde
@ 2025-12-10 14:07   ` Rob Herring
  2025-12-16 22:21     ` Oreoluwa Babatunde
  2025-12-12 11:19   ` kernel test robot
                     ` (3 subsequent siblings)
  4 siblings, 1 reply; 8+ messages in thread
From: Rob Herring @ 2025-12-10 14:07 UTC (permalink / raw)
  To: Oreoluwa Babatunde
  Cc: m.szyprowski, ye.li, kernel, saravanak, akpm, david,
	lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb, mhocko,
	robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako

On Tue, Dec 9, 2025 at 6:20 PM Oreoluwa Babatunde
<oreoluwa.babatunde@oss.qualcomm.com> wrote:
>
> When initializing the default cma region, the "cma=" kernel parameter
> takes priority over a DT defined linux,cma-default region. Hence, give
> the reserved_mem framework the ability to detect this so that the DT
> defined cma region can skip initialization accordingly.

Please explain here why this is a new problem. Presumably the
RESERVEDMEM_OF_DECLARE hook after commit xxxx gets called before the
early_param hook. And why is it now earlier?

I don't really like the state/ordering having to be worried about in 2 places.

> Signed-off-by: Oreoluwa Babatunde <oreoluwa.babatunde@oss.qualcomm.com>
> ---
>  drivers/of/of_reserved_mem.c | 19 +++++++++++++++++--
>  include/linux/cma.h          |  1 +
>  kernel/dma/contiguous.c      | 16 ++++++++++------
>  3 files changed, 28 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
> index 2e9ea751ed2d..bef68a4916b5 100644
> --- a/drivers/of/of_reserved_mem.c
> +++ b/drivers/of/of_reserved_mem.c
> @@ -158,7 +158,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>         phys_addr_t base, size;
>         int len;
>         const __be32 *prop;
> -       bool nomap;
> +       bool nomap, default_cma;
>
>         prop = of_get_flat_dt_prop(node, "reg", &len);
>         if (!prop)
> @@ -171,6 +171,12 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>         }
>
>         nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
> +       default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
> +
> +       if (default_cma && cma_skip_dt_default_reserved_mem()) {
> +               pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
> +               return -EINVAL;
> +       }
>
>         while (len >= t_len) {
>                 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
> @@ -256,12 +262,15 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
>
>         fdt_for_each_subnode(child, fdt, node) {
>                 const char *uname;
> +               bool default_cma = of_get_flat_dt_prop(child, "linux,cma-default", NULL);
>
>                 prop = of_get_flat_dt_prop(child, "reg", &len);
>                 if (!prop)
>                         continue;
>                 if (!of_fdt_device_is_available(fdt, child))
>                         continue;
> +               if (default_cma && cma_skip_dt_default_reserved_mem())
> +                       continue;
>
>                 uname = fdt_get_name(fdt, child, NULL);
>                 if (len && len % t_len != 0) {
> @@ -406,7 +415,7 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam
>         phys_addr_t base = 0, align = 0, size;
>         int len;
>         const __be32 *prop;
> -       bool nomap;
> +       bool nomap, default_cma;
>         int ret;
>
>         prop = of_get_flat_dt_prop(node, "size", &len);
> @@ -430,6 +439,12 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam
>         }
>
>         nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
> +       default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
> +
> +       if (default_cma && cma_skip_dt_default_reserved_mem()) {
> +               pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
> +               return -EINVAL;
> +       }
>
>         /* Need adjust the alignment to satisfy the CMA requirement */
>         if (IS_ENABLED(CONFIG_CMA)
> diff --git a/include/linux/cma.h b/include/linux/cma.h
> index 62d9c1cf6326..3d3047029950 100644
> --- a/include/linux/cma.h
> +++ b/include/linux/cma.h
> @@ -47,6 +47,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
>                                         unsigned int order_per_bit,
>                                         const char *name,
>                                         struct cma **res_cma);
> +extern bool cma_skip_dt_default_reserved_mem(void);
>  extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
>                               bool no_warn);
>  extern bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count);
> diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
> index d9b9dcba6ff7..9071c08650e3 100644
> --- a/kernel/dma/contiguous.c
> +++ b/kernel/dma/contiguous.c
> @@ -90,6 +90,16 @@ static int __init early_cma(char *p)
>  }
>  early_param("cma", early_cma);
>
> +/*
> + * cma_skip_dt_default_reserved_mem - This is called from the
> + * reserved_mem framework to detect if the default cma region is being
> + * set by the "cma=" kernel parameter.
> + */
> +bool __init cma_skip_dt_default_reserved_mem(void)
> +{
> +       return size_cmdline != -1;
> +}
> +
>  #ifdef CONFIG_DMA_NUMA_CMA
>
>  static struct cma *dma_contiguous_numa_area[MAX_NUMNODES];
> @@ -463,12 +473,6 @@ static int __init rmem_cma_setup(struct reserved_mem *rmem)
>         struct cma *cma;
>         int err;
>
> -       if (size_cmdline != -1 && default_cma) {
> -               pr_info("Reserved memory: bypass %s node, using cmdline CMA params instead\n",
> -                       rmem->name);
> -               return -EBUSY;
> -       }
> -
>         if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
>             of_get_flat_dt_prop(node, "no-map", NULL))
>                 return -EINVAL;
> --
> 2.34.1
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
  2025-12-10  0:20 ` [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param Oreoluwa Babatunde
  2025-12-10 14:07   ` Rob Herring
@ 2025-12-12 11:19   ` kernel test robot
  2025-12-12 11:19   ` kernel test robot
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: kernel test robot @ 2025-12-12 11:19 UTC (permalink / raw)
  To: Oreoluwa Babatunde, robh, m.szyprowski, ye.li
  Cc: oe-kbuild-all, oreoluwa.babatunde, kernel, saravanak, akpm, david,
	lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb, mhocko,
	robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako

Hi Oreoluwa,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on v6.18]
[cannot apply to robh/for-next linus/master next-20251212]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oreoluwa-Babatunde/of-reserved_mem-Allow-reserved_mem-framework-detect-cma-kernel-param/20251210-082127
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20251210002027.1171519-1-oreoluwa.babatunde%40oss.qualcomm.com
patch subject: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
config: arc-randconfig-001-20251212 (https://download.01.org/0day-ci/archive/20251212/202512121907.gdTOodx3-lkp@intel.com/config)
compiler: arc-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251212/202512121907.gdTOodx3-lkp@intel.com/reproduce)

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/202512121907.gdTOodx3-lkp@intel.com/

All errors (new ones prefixed by >>):

   arc-linux-ld: drivers/of/of_reserved_mem.o: in function `fdt_scan_reserved_mem_reg_nodes':
>> of_reserved_mem.c:(.init.text+0x46e): undefined reference to `cma_skip_dt_default_reserved_mem'
>> arc-linux-ld: of_reserved_mem.c:(.init.text+0x46e): undefined reference to `cma_skip_dt_default_reserved_mem'
   arc-linux-ld: drivers/of/of_reserved_mem.o: in function `fdt_scan_reserved_mem':
   of_reserved_mem.c:(.init.text+0x618): undefined reference to `cma_skip_dt_default_reserved_mem'
   arc-linux-ld: of_reserved_mem.c:(.init.text+0x618): undefined reference to `cma_skip_dt_default_reserved_mem'
   arc-linux-ld: of_reserved_mem.c:(.init.text+0x77c): undefined reference to `cma_skip_dt_default_reserved_mem'
   arc-linux-ld: drivers/of/of_reserved_mem.o:of_reserved_mem.c:(.init.text+0x77c): more undefined references to `cma_skip_dt_default_reserved_mem' follow

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
  2025-12-10  0:20 ` [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param Oreoluwa Babatunde
  2025-12-10 14:07   ` Rob Herring
  2025-12-12 11:19   ` kernel test robot
@ 2025-12-12 11:19   ` kernel test robot
  2025-12-16  3:09   ` Joy Zou
       [not found]   ` <X-TH#1.CAL_JsqL6VVQ7K_ZAbHJ8Gb7ei_jusLx6wRn=AdOVgV50dX0ejQ@mail.gmail.com>
  4 siblings, 0 replies; 8+ messages in thread
From: kernel test robot @ 2025-12-12 11:19 UTC (permalink / raw)
  To: Oreoluwa Babatunde, robh, m.szyprowski, ye.li
  Cc: oe-kbuild-all, oreoluwa.babatunde, kernel, saravanak, akpm, david,
	lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb, mhocko,
	robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako

Hi Oreoluwa,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on v6.18]
[cannot apply to robh/for-next linus/master next-20251212]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Oreoluwa-Babatunde/of-reserved_mem-Allow-reserved_mem-framework-detect-cma-kernel-param/20251210-082127
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20251210002027.1171519-1-oreoluwa.babatunde%40oss.qualcomm.com
patch subject: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
config: nios2-randconfig-r073-20251212 (https://download.01.org/0day-ci/archive/20251212/202512121807.G5FMUgvU-lkp@intel.com/config)
compiler: nios2-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251212/202512121807.G5FMUgvU-lkp@intel.com/reproduce)

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/202512121807.G5FMUgvU-lkp@intel.com/

All errors (new ones prefixed by >>):

   nios2-linux-ld: drivers/of/of_reserved_mem.o: in function `__reserved_mem_reserve_reg':
>> drivers/of/of_reserved_mem.c:176: undefined reference to `cma_skip_dt_default_reserved_mem'
>> drivers/of/of_reserved_mem.c:176:(.init.text+0x5b8): relocation truncated to fit: R_NIOS2_CALL26 against `cma_skip_dt_default_reserved_mem'
   nios2-linux-ld: drivers/of/of_reserved_mem.o: in function `__reserved_mem_alloc_size':
   drivers/of/of_reserved_mem.c:444: undefined reference to `cma_skip_dt_default_reserved_mem'
   drivers/of/of_reserved_mem.c:444:(.init.text+0x8a4): relocation truncated to fit: R_NIOS2_CALL26 against `cma_skip_dt_default_reserved_mem'
   nios2-linux-ld: drivers/of/of_reserved_mem.o: in function `fdt_scan_reserved_mem_reg_nodes':
   drivers/of/of_reserved_mem.c:272: undefined reference to `cma_skip_dt_default_reserved_mem'
   drivers/of/of_reserved_mem.c:272:(.init.text+0xbb0): relocation truncated to fit: R_NIOS2_CALL26 against `cma_skip_dt_default_reserved_mem'


vim +176 drivers/of/of_reserved_mem.c

   150	
   151	/*
   152	 * __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
   153	 */
   154	static int __init __reserved_mem_reserve_reg(unsigned long node,
   155						     const char *uname)
   156	{
   157		int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
   158		phys_addr_t base, size;
   159		int len;
   160		const __be32 *prop;
   161		bool nomap, default_cma;
   162	
   163		prop = of_get_flat_dt_prop(node, "reg", &len);
   164		if (!prop)
   165			return -ENOENT;
   166	
   167		if (len && len % t_len != 0) {
   168			pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
   169			       uname);
   170			return -EINVAL;
   171		}
   172	
   173		nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
   174		default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
   175	
 > 176		if (default_cma && cma_skip_dt_default_reserved_mem()) {
   177			pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
   178			return -EINVAL;
   179		}
   180	
   181		while (len >= t_len) {
   182			base = dt_mem_next_cell(dt_root_addr_cells, &prop);
   183			size = dt_mem_next_cell(dt_root_size_cells, &prop);
   184	
   185			if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
   186				/* Architecture specific contiguous memory fixup. */
   187				if (of_flat_dt_is_compatible(node, "shared-dma-pool") &&
   188				    of_get_flat_dt_prop(node, "reusable", NULL))
   189					dma_contiguous_early_fixup(base, size);
   190				pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
   191					uname, &base, (unsigned long)(size / SZ_1M));
   192			} else {
   193				pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
   194				       uname, &base, (unsigned long)(size / SZ_1M));
   195			}
   196	
   197			len -= t_len;
   198		}
   199		return 0;
   200	}
   201	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
  2025-12-10  0:20 ` [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param Oreoluwa Babatunde
                     ` (2 preceding siblings ...)
  2025-12-12 11:19   ` kernel test robot
@ 2025-12-16  3:09   ` Joy Zou
       [not found]   ` <X-TH#1.CAL_JsqL6VVQ7K_ZAbHJ8Gb7ei_jusLx6wRn=AdOVgV50dX0ejQ@mail.gmail.com>
  4 siblings, 0 replies; 8+ messages in thread
From: Joy Zou @ 2025-12-16  3:09 UTC (permalink / raw)
  To: Oreoluwa Babatunde
  Cc: robh, m.szyprowski, ye.li, oreoluwa.babatunde, kernel, saravanak,
	akpm, david, lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb,
	mhocko, robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako

On Tue, Dec 09, 2025 at 04:20:27PM -0800, Oreoluwa Babatunde wrote:
> When initializing the default cma region, the "cma=" kernel parameter
> takes priority over a DT defined linux,cma-default region. Hence, give
> the reserved_mem framework the ability to detect this so that the DT
> defined cma region can skip initialization accordingly.
> 
Hi Oreoluwa,
  Have tested i.MX6ULL 9x9, i.MX6ULL 14x14, i.MX6SLL EVK and i.MX7D
SABRE-SD boards.
  Before this patch, i.MX6ULL 9x9 failed to allocate CMA memory when using
"cma=" kernel parameter. After applying the patch, CMA allocation works
correctly on all tested platforms.

Tested-by: Joy Zou <joy.zou@nxp.com>

BR
Joy Zou
> Signed-off-by: Oreoluwa Babatunde <oreoluwa.babatunde@oss.qualcomm.com>
> ---
>  drivers/of/of_reserved_mem.c | 19 +++++++++++++++++--
>  include/linux/cma.h          |  1 +
>  kernel/dma/contiguous.c      | 16 ++++++++++------
>  3 files changed, 28 insertions(+), 8 deletions(-)
> 2.34.1

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
  2025-12-10 14:07   ` Rob Herring
@ 2025-12-16 22:21     ` Oreoluwa Babatunde
  0 siblings, 0 replies; 8+ messages in thread
From: Oreoluwa Babatunde @ 2025-12-16 22:21 UTC (permalink / raw)
  To: Rob Herring
  Cc: m.szyprowski, ye.li, kernel, saravanak, akpm, david,
	lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb, mhocko,
	robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako



Hi Rob,

On 12/10/2025 6:07 AM, Rob Herring wrote:
> On Tue, Dec 9, 2025 at 6:20 PM Oreoluwa Babatunde
> <oreoluwa.babatunde@oss.qualcomm.com> wrote:
>>
>> When initializing the default cma region, the "cma=" kernel parameter
>> takes priority over a DT defined linux,cma-default region. Hence, give
>> the reserved_mem framework the ability to detect this so that the DT
>> defined cma region can skip initialization accordingly.
> 
> Please explain here why this is a new problem. Presumably the
> RESERVEDMEM_OF_DECLARE hook after commit xxxx gets called before the
> early_param hook. And why is it now earlier?

ACK. I will add more of this info in the next patch version.

> 
> I don't really like the state/ordering having to be worried about in 2 places.

The advantage to having the state visible to the reserved_mem code is that
we can skip adding the DT node to the resrved_mem array since it actually
won't be used.

If this is still not preferred, another option would be to use a helper
function  in contiguous.c to call dma_contiguous_early_fixup() for the
reserved_mem code. This way, "size_cmdline" can be checked internally within
the file and it can call dma_contiguous_early_fixup based on that.

> 
>> Signed-off-by: Oreoluwa Babatunde <oreoluwa.babatunde@oss.qualcomm.com>
>> ---
>>  drivers/of/of_reserved_mem.c | 19 +++++++++++++++++--
>>  include/linux/cma.h          |  1 +
>>  kernel/dma/contiguous.c      | 16 ++++++++++------
>>  3 files changed, 28 insertions(+), 8 deletions(-)
>>
>> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
>> index 2e9ea751ed2d..bef68a4916b5 100644
>> --- a/drivers/of/of_reserved_mem.c
>> +++ b/drivers/of/of_reserved_mem.c
>> @@ -158,7 +158,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>>         phys_addr_t base, size;
>>         int len;
>>         const __be32 *prop;
>> -       bool nomap;
>> +       bool nomap, default_cma;
>>
>>         prop = of_get_flat_dt_prop(node, "reg", &len);
>>         if (!prop)
>> @@ -171,6 +171,12 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>>         }
>>
>>         nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
>> +       default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
>> +
>> +       if (default_cma && cma_skip_dt_default_reserved_mem()) {
>> +               pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
>> +               return -EINVAL;
>> +       }
>>
>>         while (len >= t_len) {
>>                 base = dt_mem_next_cell(dt_root_addr_cells, &prop);
>> @@ -256,12 +262,15 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
>>
>>         fdt_for_each_subnode(child, fdt, node) {
>>                 const char *uname;
>> +               bool default_cma = of_get_flat_dt_prop(child, "linux,cma-default", NULL);
>>
>>                 prop = of_get_flat_dt_prop(child, "reg", &len);
>>                 if (!prop)
>>                         continue;
>>                 if (!of_fdt_device_is_available(fdt, child))
>>                         continue;
>> +               if (default_cma && cma_skip_dt_default_reserved_mem())
>> +                       continue;
>>
>>                 uname = fdt_get_name(fdt, child, NULL);
>>                 if (len && len % t_len != 0) {
>> @@ -406,7 +415,7 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam
>>         phys_addr_t base = 0, align = 0, size;
>>         int len;
>>         const __be32 *prop;
>> -       bool nomap;
>> +       bool nomap, default_cma;
>>         int ret;
>>
>>         prop = of_get_flat_dt_prop(node, "size", &len);
>> @@ -430,6 +439,12 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam
>>         }
>>
>>         nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
>> +       default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
>> +
>> +       if (default_cma && cma_skip_dt_default_reserved_mem()) {
>> +               pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
>> +               return -EINVAL;
>> +       }
>>
>>         /* Need adjust the alignment to satisfy the CMA requirement */
>>         if (IS_ENABLED(CONFIG_CMA)
>> diff --git a/include/linux/cma.h b/include/linux/cma.h
>> index 62d9c1cf6326..3d3047029950 100644
>> --- a/include/linux/cma.h
>> +++ b/include/linux/cma.h
>> @@ -47,6 +47,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
>>                                         unsigned int order_per_bit,
>>                                         const char *name,
>>                                         struct cma **res_cma);
>> +extern bool cma_skip_dt_default_reserved_mem(void);
>>  extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
>>                               bool no_warn);
>>  extern bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count);
>> diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
>> index d9b9dcba6ff7..9071c08650e3 100644
>> --- a/kernel/dma/contiguous.c
>> +++ b/kernel/dma/contiguous.c
>> @@ -90,6 +90,16 @@ static int __init early_cma(char *p)
>>  }
>>  early_param("cma", early_cma);
>>
>> +/*
>> + * cma_skip_dt_default_reserved_mem - This is called from the
>> + * reserved_mem framework to detect if the default cma region is being
>> + * set by the "cma=" kernel parameter.
>> + */
>> +bool __init cma_skip_dt_default_reserved_mem(void)
>> +{
>> +       return size_cmdline != -1;
>> +}
>> +
>>  #ifdef CONFIG_DMA_NUMA_CMA
>>
>>  static struct cma *dma_contiguous_numa_area[MAX_NUMNODES];
>> @@ -463,12 +473,6 @@ static int __init rmem_cma_setup(struct reserved_mem *rmem)
>>         struct cma *cma;
>>         int err;
>>
>> -       if (size_cmdline != -1 && default_cma) {
>> -               pr_info("Reserved memory: bypass %s node, using cmdline CMA params instead\n",
>> -                       rmem->name);
>> -               return -EBUSY;
>> -       }
>> -
>>         if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
>>             of_get_flat_dt_prop(node, "no-map", NULL))
>>                 return -EINVAL;
>> --
>> 2.34.1
>>
>>

Regards,
Oreoluwa

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
       [not found]   ` <X-TH#1.CAL_JsqL6VVQ7K_ZAbHJ8Gb7ei_jusLx6wRn=AdOVgV50dX0ejQ@mail.gmail.com>
@ 2025-12-18  9:55     ` Marek Szyprowski
  2025-12-18 14:42       ` Rob Herring
  0 siblings, 1 reply; 8+ messages in thread
From: Marek Szyprowski @ 2025-12-18  9:55 UTC (permalink / raw)
  To: Rob Herring, Oreoluwa Babatunde
  Cc: ye.li, kernel, saravanak, akpm, david, lorenzo.stoakes,
	Liam.Howlett, vbabka, rppt, surenb, mhocko, robin.murphy,
	devicetree, linux-kernel, linux-mm, iommu, quic_c_gdjako

On 10.12.2025 15:07, Rob Herring wrote:
> On Tue, Dec 9, 2025 at 6:20 PM Oreoluwa Babatunde
> <oreoluwa.babatunde@oss.qualcomm.com> wrote:
>> When initializing the default cma region, the "cma=" kernel parameter
>> takes priority over a DT defined linux,cma-default region. Hence, give
>> the reserved_mem framework the ability to detect this so that the DT
>> defined cma region can skip initialization accordingly.
> Please explain here why this is a new problem. Presumably the
> RESERVEDMEM_OF_DECLARE hook after commit xxxx gets called before the
> early_param hook. And why is it now earlier?
>
> I don't really like the state/ordering having to be worried about in 2 places.

I also don't like this spaghetti, but it originates from 
commit 8a6e02d0c00e ("of: reserved_mem: Restructure how the reserved 
memory regions are processed") and the first fixup for it: 2c223f7239f3 
("of: reserved_mem: Restructure call site for 
dma_contiguous_early_fixup()").

It looks that it is really hard to make reserved memory 
initialization fully dynamic assuming that the cma related fixups have 
to be known before populating kernel memory pages tables. I also advised 
in 
https://lore.kernel.org/all/be70bdc4-bddd-4afe-8574-7e0889fd381c@samsung.com/ 
to simply increase the size of the static table to make it large enough for the sane use cases, but 
it turned out that this approach was already discussed and rejected:
https://lore.kernel.org/all/1650488954-26662-1-git-send-email-quic_pdaly@quicinc.com/

Maybe it would make sense to revert the mentioned changes and get back 
to such simple approach - to make the size of the static table 
configurable in the Kconfig?

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param
  2025-12-18  9:55     ` Marek Szyprowski
@ 2025-12-18 14:42       ` Rob Herring
  0 siblings, 0 replies; 8+ messages in thread
From: Rob Herring @ 2025-12-18 14:42 UTC (permalink / raw)
  To: Marek Szyprowski
  Cc: Oreoluwa Babatunde, ye.li, kernel, saravanak, akpm, david,
	lorenzo.stoakes, Liam.Howlett, vbabka, rppt, surenb, mhocko,
	robin.murphy, devicetree, linux-kernel, linux-mm, iommu,
	quic_c_gdjako

On Thu, Dec 18, 2025 at 3:55 AM Marek Szyprowski
<m.szyprowski@samsung.com> wrote:
>
> On 10.12.2025 15:07, Rob Herring wrote:
> > On Tue, Dec 9, 2025 at 6:20 PM Oreoluwa Babatunde
> > <oreoluwa.babatunde@oss.qualcomm.com> wrote:
> >> When initializing the default cma region, the "cma=" kernel parameter
> >> takes priority over a DT defined linux,cma-default region. Hence, give
> >> the reserved_mem framework the ability to detect this so that the DT
> >> defined cma region can skip initialization accordingly.
> > Please explain here why this is a new problem. Presumably the
> > RESERVEDMEM_OF_DECLARE hook after commit xxxx gets called before the
> > early_param hook. And why is it now earlier?
> >
> > I don't really like the state/ordering having to be worried about in 2 places.
>
> I also don't like this spaghetti, but it originates from
> commit 8a6e02d0c00e ("of: reserved_mem: Restructure how the reserved
> memory regions are processed") and the first fixup for it: 2c223f7239f3
> ("of: reserved_mem: Restructure call site for
> dma_contiguous_early_fixup()").

Honestly, this code wasn't great before. Every time it is touched it
breaks someone.

> It looks that it is really hard to make reserved memory
> initialization fully dynamic assuming that the cma related fixups have
> to be known before populating kernel memory pages tables. I also advised
> in
> https://lore.kernel.org/all/be70bdc4-bddd-4afe-8574-7e0889fd381c@samsung.com/
> to simply increase the size of the static table to make it large enough for the sane use cases, but
> it turned out that this approach was already discussed and rejected:
> https://lore.kernel.org/all/1650488954-26662-1-git-send-email-quic_pdaly@quicinc.com/

I guess the question is what's a sane limit? After 128, are we going
to accept 256? I really suspect we are just enabling some further
abuse of /reserved-memory downstream. For example, I could imagine
there's micromanaging the location of media/graphics buffers so they
end up in specific DRAM banks to optimize accesses. No one ever wants
to detail why they want/need more regions.

> Maybe it would make sense to revert the mentioned changes and get back
> to such simple approach - to make the size of the static table
> configurable in the Kconfig?

I'd rather not resort to a kconfig option.

We could go back to processing all the regions at the beginning
(growing the static size), and then just shrinking allocation. Or
maybe it could just be freed entirely. I don't think we really need to
keep the state around forever.

Rob

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-12-18 14:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20251210002053eucas1p1d1408ad0fb49a49bf4371687f8df7395@eucas1p1.samsung.com>
2025-12-10  0:20 ` [PATCH] of: reserved_mem: Allow reserved_mem framework detect "cma=" kernel param Oreoluwa Babatunde
2025-12-10 14:07   ` Rob Herring
2025-12-16 22:21     ` Oreoluwa Babatunde
2025-12-12 11:19   ` kernel test robot
2025-12-12 11:19   ` kernel test robot
2025-12-16  3:09   ` Joy Zou
     [not found]   ` <X-TH#1.CAL_JsqL6VVQ7K_ZAbHJ8Gb7ei_jusLx6wRn=AdOVgV50dX0ejQ@mail.gmail.com>
2025-12-18  9:55     ` Marek Szyprowski
2025-12-18 14:42       ` Rob Herring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).