Skip to contentSkip to conversation

of/fdt: Some bug fixes and cleanups

PATCH Superseded Yuntao Wang 5 participants 22 messages Nov 13, 2025 – Nov 17, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover of/fdt: Some bug fixes and cleanups

This patch series fixes several bugs related to dt_root_addr_cells and dt_root_size_cells, and performs some cleanup.

Changelog:

v1: Consolidate duplicate code into helper functions

Links:

v1: https://lore.kernel.org/linux-devicetree/[email protected]/t/

Yuntao Wang (7):
of/fdt: Consolidate duplicate code into helper functions of/fdt: Fix the len check in early_init_dt_check_for_elfcorehdr() of/fdt: Fix the len check in
early_init_dt_check_for_usable_mem_range()
of/fdt: Fix incorrect use of dt_root_addr_cells in
early_init_dt_check_kho()
of/fdt: Simplify the logic of early_init_dt_scan_memory() of/reserved_mem: Simplify the logic of __reserved_mem_reserve_reg() of/reserved_mem: Simplify the logic of
fdt_scan_reserved_mem_reg_nodes()

 drivers/of/fdt.c             | 93 ++++++++++++++++++++++--------------
 drivers/of/of_reserved_mem.c | 37 ++++----------
 include/linux/of_fdt.h       |  5 ++
 3 files changed, 72 insertions(+), 63 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/7 of/fdt: Consolidate duplicate code into helper functions

Currently, there are many pieces of nearly identical code scattered across different places. Consolidate the duplicate code into helper functions to improve maintainability and reduce the likelihood of errors.

---
 drivers/of/fdt.c       | 41 +++++++++++++++++++++++++++++++++++++++++
 include/linux/of_fdt.h |  5 +++++
 2 files changed, 46 insertions(+)
drivers/of/fdt.c +41 -0
index 0edd639898a6..5e0eabc1449f 100644
@@ -625,6 +625,47 @@ const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
 	return fdt_getprop(initial_boot_params, node, name, size);
 }
 
+const __be32 *__init of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries)
+{
+	const __be32 *prop;
+	int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
+
+	prop = of_get_flat_dt_prop(node, name, &len);
+	if (!prop) {
+		*entries = 0;
+		return NULL;
+	}
+
+	if (len % elen) {
+		*entries = -1;
+		return NULL;
+	}
+
+	*entries = len / elen;
+	return prop;
+}
+
+bool __init of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size)
+{
+	const __be32 *prop;
+	int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
+
+	prop = of_get_flat_dt_prop(node, name, &len);
+	if (!prop || len < elen)
+		return false;
+
+	of_fdt_read_addr_size(prop, addr, size);
+	return true;
+}
+
+void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
+{
+	*addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
+	*size = dt_mem_next_cell(dt_root_size_cells, &prop);
+}
+
 /**
  * of_fdt_is_compatible - Return true if given node from the given blob has
  * compat in its compatible list
include/linux/of_fdt.h +5 -0
index b8d6c0c20876..3a0805ff6c7b 100644
@@ -55,6 +55,11 @@ extern int of_get_flat_dt_subnode_by_name(unsigned long node,
 					  const char *uname);
 extern const void *of_get_flat_dt_prop(unsigned long node, const char *name,
 				       int *size);
+extern const __be32 *of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries);
+extern bool of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size);
+extern void of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size);
 extern int of_flat_dt_is_compatible(unsigned long node, const char *name);
 extern unsigned long of_get_flat_dt_root(void);
 extern uint32_t of_get_flat_dt_phandle(unsigned long node);
Signature · 1 line
2.51.0
#5 lore raw

PATCH v2 2/7 of/fdt: Fix the len check in early_init_dt_check_for_elfcorehdr()

In reply to Yuntao Wang

The len value is in bytes, while dt_root_addr_cells + dt_root_size_cells is in cells (4 bytes per cell). Comparing them directly is incorrect.

Use a helper function to simplify the code and address this issue.

Fixes f7e7ce93aac1 ("of: fdt: Add generic support for handling elf core headers property")
Fixes e62aaeac426ab1dd ("arm64: kdump: provide /proc/vmcore file")
---
 drivers/of/fdt.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/of/fdt.c +2 -8
index 5e0eabc1449f..80b7236efdc6 100644
@@ -853,21 +853,15 @@ static void __init early_init_dt_check_for_initrd(unsigned long node)
  */
 static void __init early_init_dt_check_for_elfcorehdr(unsigned long node)
 {
-	const __be32 *prop;
-	int len;
-
 	if (!IS_ENABLED(CONFIG_CRASH_DUMP))
 		return;
 
 	pr_debug("Looking for elfcorehdr property... ");
 
-	prop = of_get_flat_dt_prop(node, "linux,elfcorehdr", &len);
-	if (!prop || (len < (dt_root_addr_cells + dt_root_size_cells)))
+	if (!of_fdt_get_addr_size(node, "linux,elfcorehdr",
+	                          &elfcorehdr_addr, &elfcorehdr_size))
 		return;
 
-	elfcorehdr_addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
-	elfcorehdr_size = dt_mem_next_cell(dt_root_size_cells, &prop);
-
 	pr_debug("elfcorehdr_start=0x%llx elfcorehdr_size=0x%llx\n",
 		 elfcorehdr_addr, elfcorehdr_size);
 }
Signature · 1 line
2.51.0
#6 lore raw

PATCH v2 3/7 of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()

In reply to Yuntao Wang

The len value is in bytes, while dt_root_addr_cells + dt_root_size_cells is in cells (4 bytes per cell). Modulo calculation between them is incorrect, the units must be converted first.

Use helper functions to simplify the code and fix this issue.

Fixes fb319e77a0e7 ("of: fdt: Add memory for devices by DT property "linux,usable-memory-range"")
Fixes 2af2b50acf9b9c38 ("of: fdt: Add generic support for handling usable memory range property")
Fixes 8f579b1c4e347b23 ("arm64: limit memory regions based on DT property, usable-memory-range")
---
 drivers/of/fdt.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/of/fdt.c +7 -7
index 80b7236efdc6..d0caaab42aa7 100644
@@ -884,7 +884,7 @@ static unsigned long chosen_node_offset = -FDT_ERR_NOTFOUND;
 void __init early_init_dt_check_for_usable_mem_range(void)
 {
 	struct memblock_region rgn[MAX_USABLE_RANGES] = {0};
-	const __be32 *prop, *endp;
+	const __be32 *prop;
 	int len, i;
 	unsigned long node = chosen_node_offset;
 
@@ -893,14 +893,14 @@ void __init early_init_dt_check_for_usable_mem_range(void)
 
 	pr_debug("Looking for usable-memory-range property... ");
 
-	prop = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len);
-	if (!prop || (len % (dt_root_addr_cells + dt_root_size_cells)))
+	prop = of_fdt_get_addr_size_prop(node, "linux,usable-memory-range", &len);
+	if (!prop)
 		return;
 
-	endp = prop + (len / sizeof(__be32));
-	for (i = 0; i < MAX_USABLE_RANGES && prop < endp; i++) {
-		rgn[i].base = dt_mem_next_cell(dt_root_addr_cells, &prop);
-		rgn[i].size = dt_mem_next_cell(dt_root_size_cells, &prop);
+	len = min(len, MAX_USABLE_RANGES);
+
+	for (i = 0; i < len; i++) {
+		of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);
 
 		pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n",
 			 i, &rgn[i].base, &rgn[i].size);
Signature · 1 line
2.51.0
#9 lore raw

PATCH v2 4/7 of/fdt: Fix incorrect use of dt_root_addr_cells in early_init_dt_check_kho()

In reply to Yuntao Wang

When reading the fdt_size value, the argument passed to dt_mem_next_cell() is dt_root_addr_cells, but it should be dt_root_size_cells.

The same issue occurs when reading the scratch_size value.

Use a helper function to simplify the code and fix these issues.

Fixes 274cdcb1c004 ("arm64: add KHO support")
---
 drivers/of/fdt.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/of/fdt.c +2 -12
index d0caaab42aa7..4c45a97d6652 100644
@@ -918,26 +918,16 @@ static void __init early_init_dt_check_kho(void)
 {
 	unsigned long node = chosen_node_offset;
 	u64 fdt_start, fdt_size, scratch_start, scratch_size;
-	const __be32 *p;
-	int l;
 
 	if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER) || (long)node < 0)
 		return;
 
-	p = of_get_flat_dt_prop(node, "linux,kho-fdt", &l);
-	if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32))
+	if (!of_fdt_get_addr_size(node, "linux,kho-fdt", &fdt_start, &fdt_size))
 		return;
 
-	fdt_start = dt_mem_next_cell(dt_root_addr_cells, &p);
-	fdt_size = dt_mem_next_cell(dt_root_addr_cells, &p);
-
-	p = of_get_flat_dt_prop(node, "linux,kho-scratch", &l);
-	if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32))
+	if (!of_fdt_get_addr_size(node, "linux,kho-scratch", &scratch_start, &scratch_size))
 		return;
 
-	scratch_start = dt_mem_next_cell(dt_root_addr_cells, &p);
-	scratch_size = dt_mem_next_cell(dt_root_addr_cells, &p);
-
 	kho_populate(fdt_start, fdt_size, scratch_start, scratch_size);
 }
 
Signature · 1 line
2.51.0
#10 lore raw

PATCH v2 5/7 of/fdt: Simplify the logic of early_init_dt_scan_memory()

In reply to Yuntao Wang

Use the existing helper functions to simplify the logic of early_init_dt_scan_memory()

---
 drivers/of/fdt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/of/fdt.c +6 -8
index 4c45a97d6652..b6b059960fc2 100644
@@ -1027,7 +1027,7 @@ int __init early_init_dt_scan_memory(void)
 
 	fdt_for_each_subnode(node, fdt, 0) {
 		const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
-		const __be32 *reg, *endp;
+		const __be32 *reg;
 		int l;
 		bool hotpluggable;
 
@@ -1038,23 +1038,21 @@ int __init early_init_dt_scan_memory(void)
 		if (!of_fdt_device_is_available(fdt, node))
 			continue;
 
-		reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
+		reg = of_fdt_get_addr_size_prop(node, "linux,usable-memory", &l);
 		if (reg == NULL)
-			reg = of_get_flat_dt_prop(node, "reg", &l);
+			reg = of_fdt_get_addr_size_prop(node, "reg", &l);
 		if (reg == NULL)
 			continue;
 
-		endp = reg + (l / sizeof(__be32));
 		hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
 
-		pr_debug("memory scan node %s, reg size %d,\n",
+		pr_debug("memory scan node %s, reg {addr,size} entries %d,\n",
 			 fdt_get_name(fdt, node, NULL), l);
 
-		while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
+		while (l-- > 0) {
 			u64 base, size;
 
-			base = dt_mem_next_cell(dt_root_addr_cells, &reg);
-			size = dt_mem_next_cell(dt_root_size_cells, &reg);
+			of_fdt_read_addr_size(reg, &base, &size);
 
 			if (size == 0)
 				continue;
Signature · 1 line
2.51.0
#16 lore raw

PATCH v2 6/7 of/reserved_mem: Simplify the logic of __reserved_mem_reserve_reg()

In reply to Yuntao Wang

Use the existing helper functions to simplify the logic of __reserved_mem_reserve_reg()

---
 drivers/of/of_reserved_mem.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/of/of_reserved_mem.c +5 -9
index 2e9ea751ed2d..b8527f3e335e 100644
@@ -154,17 +154,16 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base,
 static int __init __reserved_mem_reserve_reg(unsigned long node,
 					     const char *uname)
 {
-	int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
 	phys_addr_t base, size;
 	int len;
 	const __be32 *prop;
 	bool nomap;
 
-	prop = of_get_flat_dt_prop(node, "reg", &len);
-	if (!prop)
+	prop = of_fdt_get_addr_size_prop(node, "reg", &len);
+	if (!len)
 		return -ENOENT;
 
-	if (len && len % t_len != 0) {
+	if (len < 0) {
 		pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
 		       uname);
 		return -EINVAL;
@@ -172,9 +171,8 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 
 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
 
-	while (len >= t_len) {
-		base = dt_mem_next_cell(dt_root_addr_cells, &prop);
-		size = dt_mem_next_cell(dt_root_size_cells, &prop);
+	while (len-- > 0) {
+		of_fdt_read_addr_size(prop, &base, &size);
 
 		if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
 			/* Architecture specific contiguous memory fixup. */
@@ -187,8 +185,6 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 			pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
 			       uname, &base, (unsigned long)(size / SZ_1M));
 		}
-
-		len -= t_len;
 	}
 	return 0;
 }
Signature · 1 line
2.51.0
#18 lore raw

PATCH v2 7/7 of/reserved_mem: Simplify the logic of fdt_scan_reserved_mem_reg_nodes()

In reply to Yuntao Wang

Use the existing helper functions to simplify the logic of fdt_scan_reserved_mem_reg_nodes()

---
 drivers/of/of_reserved_mem.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
drivers/of/of_reserved_mem.c +4 -19
index b8527f3e335e..96771ab073c0 100644
@@ -226,12 +226,9 @@ static void __init __rmem_check_for_overlap(void);
  */
 void __init fdt_scan_reserved_mem_reg_nodes(void)
 {
-	int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
 	const void *fdt = initial_boot_params;
 	phys_addr_t base, size;
-	const __be32 *prop;
 	int node, child;
-	int len;
 
 	if (!fdt)
 		return;
@@ -253,28 +250,16 @@ void __init fdt_scan_reserved_mem_reg_nodes(void)
 	fdt_for_each_subnode(child, fdt, node) {
 		const char *uname;
 
-		prop = of_get_flat_dt_prop(child, "reg", &len);
-		if (!prop)
-			continue;
 		if (!of_fdt_device_is_available(fdt, child))
 			continue;
 
-		uname = fdt_get_name(fdt, child, NULL);
-		if (len && len % t_len != 0) {
-			pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
-			       uname);
+		if (!of_fdt_get_addr_size(child, "reg", &base, &size))
 			continue;
-		}
-
-		if (len > t_len)
-			pr_warn("%s() ignores %d regions in node '%s'\n",
-				__func__, len / t_len - 1, uname);
 
-		base = dt_mem_next_cell(dt_root_addr_cells, &prop);
-		size = dt_mem_next_cell(dt_root_size_cells, &prop);
-
-		if (size)
+		if (size) {
+			uname = fdt_get_name(fdt, child, NULL);
 			fdt_reserved_mem_save_node(child, uname, base, size);
+		}
 	}
 
 	/* check for overlapping reserved regions */
Signature · 1 line
2.51.0
#7 lore raw

Re: [PATCH v2 3/7] of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()

In reply to Yuntao Wang
kernel test robot noticed the following build errors:

Hi Yuntao,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.18-rc5 next-20251113] [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/Yuntao-Wang/of-fdt-Consolidate-duplicate-code-into-helper-functions/20251114-004121
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20251113155104.226617-4-yuntao.wang%40linux.dev
patch subject: [PATCH v2 3/7] of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()
config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20251114/[email protected]/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 0bba1e76581bad04e7d7f09f5115ae5e2989e0d9)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251114/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/of/fdt.c:903:31: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]
     903 |                 of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);
         |                                             ^~~~~~~~~~~~
   drivers/of/fdt.c:663:60: note: passing argument to parameter 'addr' here
     663 | void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
         |                                                            ^
   drivers/of/fdt.c:903:45: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]
     903 |                 of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);
         |                                                           ^~~~~~~~~~~~
   drivers/of/fdt.c:663:71: note: passing argument to parameter 'size' here
     663 | void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
         |                                                                       ^

2 errors generated.

vim +903 drivers/of/fdt.c

879
880 /**
881 * early_init_dt_check_for_usable_mem_range - Decode usable memory range
882 * location from flat tree
883 */
884 void __init early_init_dt_check_for_usable_mem_range(void)
885 {
886 struct memblock_region rgn[MAX_USABLE_RANGES] = {0};
887 const __be32 *prop;
888 int len, i;
889 unsigned long node = chosen_node_offset;
890
891 if ((long)node < 0)
892 return;
893
894 pr_debug("Looking for usable-memory-range property... ");
895
896 prop = of_fdt_get_addr_size_prop(node, "linux,usable-memory-range", &len);
897 if (!prop)
898 return;
899
900 len = min(len, MAX_USABLE_RANGES);
901
902 for (i = 0; i < len; i++) {

 > 903			of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);

904
905 pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n",
906 i, &rgn[i].base, &rgn[i].size);
907 }
908
909 memblock_cap_memory_range(rgn[0].base, rgn[0].size);
910 for (i = 1; i < MAX_USABLE_RANGES && rgn[i].size; i++)
911 memblock_add(rgn[i].base, rgn[i].size);
912 }
913

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#8 lore raw

Re: [PATCH v2 3/7] of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()

In reply to Yuntao Wang
kernel test robot noticed the following build errors:

Hi Yuntao,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.18-rc5 next-20251113] [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/Yuntao-Wang/of-fdt-Consolidate-duplicate-code-into-helper-functions/20251114-004121
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20251113155104.226617-4-yuntao.wang%40linux.dev
patch subject: [PATCH v2 3/7] of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()
config: arc-allnoconfig (https://download.01.org/0day-ci/archive/20251114/[email protected]/config)
compiler: arc-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251114/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/of/fdt.c: In function 'early_init_dt_check_for_usable_mem_range':

>> drivers/of/fdt.c:903:45: error: passing argument 2 of 'of_fdt_read_addr_size' from incompatible pointer type [-Wincompatible-pointer-types]
     903 |                 of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);
         |                                             ^~~~~~~~~~~~
         |                                             |
         |                                             phys_addr_t * {aka unsigned int *}
   drivers/of/fdt.c:663:60: note: expected 'u64 *' {aka 'long long unsigned int *'} but argument is of type 'phys_addr_t *' {aka 'unsigned int *'}
     663 | void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
         |                                                       ~~~~~^~~~
   drivers/of/fdt.c:903:59: error: passing argument 3 of 'of_fdt_read_addr_size' from incompatible pointer type [-Wincompatible-pointer-types]
     903 |                 of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);
         |                                                           ^~~~~~~~~~~~
         |                                                           |
         |                                                           phys_addr_t * {aka unsigned int *}
   drivers/of/fdt.c:663:71: note: expected 'u64 *' {aka 'long long unsigned int *'} but argument is of type 'phys_addr_t *' {aka 'unsigned int *'}
     663 | void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
         |                                                                  ~~~~~^~~~

vim +/of_fdt_read_addr_size +903 drivers/of/fdt.c

879
880 /**
881 * early_init_dt_check_for_usable_mem_range - Decode usable memory range
882 * location from flat tree
883 */
884 void __init early_init_dt_check_for_usable_mem_range(void)
885 {
886 struct memblock_region rgn[MAX_USABLE_RANGES] = {0};
887 const __be32 *prop;
888 int len, i;
889 unsigned long node = chosen_node_offset;
890
891 if ((long)node < 0)
892 return;
893
894 pr_debug("Looking for usable-memory-range property... ");
895
896 prop = of_fdt_get_addr_size_prop(node, "linux,usable-memory-range", &len);
897 if (!prop)
898 return;
899
900 len = min(len, MAX_USABLE_RANGES);
901
902 for (i = 0; i < len; i++) {

 > 903			of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);

904
905 pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n",
906 i, &rgn[i].base, &rgn[i].size);
907 }
908
909 memblock_cap_memory_range(rgn[0].base, rgn[0].size);
910 for (i = 1; i < MAX_USABLE_RANGES && rgn[i].size; i++)
911 memblock_add(rgn[i].base, rgn[i].size);
912 }
913

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#17 lore raw

Re: [PATCH v2 6/7] of/reserved_mem: Simplify the logic of __reserved_mem_reserve_reg()

In reply to Yuntao Wang
kernel test robot noticed the following build errors:

Hi Yuntao,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.18-rc5 next-20251113] [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/Yuntao-Wang/of-fdt-Consolidate-duplicate-code-into-helper-functions/20251114-004121
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20251113155104.226617-7-yuntao.wang%40linux.dev
patch subject: [PATCH v2 6/7] of/reserved_mem: Simplify the logic of __reserved_mem_reserve_reg()
config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20251114/[email protected]/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 0bba1e76581bad04e7d7f09f5115ae5e2989e0d9)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251114/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/of/of_reserved_mem.c:175:31: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]
     175 |                 of_fdt_read_addr_size(prop, &base, &size);
         |                                             ^~~~~
   include/linux/of_fdt.h:62:60: note: passing argument to parameter 'addr' here
      62 | extern void of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size);
         |                                                            ^
   drivers/of/of_reserved_mem.c:175:38: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]
     175 |                 of_fdt_read_addr_size(prop, &base, &size);
         |                                                    ^~~~~
   include/linux/of_fdt.h:62:71: note: passing argument to parameter 'size' here
      62 | extern void of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size);
         |                                                                       ^

2 errors generated.

vim +175 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 phys_addr_t base, size;
158 int len;
159 const __be32 *prop;
160 bool nomap;
161
162 prop = of_fdt_get_addr_size_prop(node, "reg", &len);
163 if (!len)
164 return -ENOENT;
165
166 if (len < 0) {
167 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
168 uname);
169 return -EINVAL;
170 }
171
172 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
173
174 while (len-- > 0) {

 > 175			of_fdt_read_addr_size(prop, &base, &size);

176
177 if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
178 /* Architecture specific contiguous memory fixup. */
179 if (of_flat_dt_is_compatible(node, "shared-dma-pool") &&
180 of_get_flat_dt_prop(node, "reusable", NULL))
181 dma_contiguous_early_fixup(base, size);
182 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
183 uname, &base, (unsigned long)(size / SZ_1M));
184 } else {
185 pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
186 uname, &base, (unsigned long)(size / SZ_1M));
187 }
188 }
189 return 0;
190 }
191

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#11 lore raw

Re: [PATCH v2 5/7] of/fdt: Simplify the logic of early_init_dt_scan_memory()

In reply to Yuntao Wang
Quoted 37 lines · Yuntao Wang

Use the existing helper functions to simplify the logic of early_init_dt_scan_memory()

---
 drivers/of/fdt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/of/fdt.c +3 -5
index 4c45a97d6652..b6b059960fc2 100644
@@ -1027,7 +1027,7 @@ int __init early_init_dt_scan_memory(void)
 
 	fdt_for_each_subnode(node, fdt, 0) {
 		const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
-		const __be32 *reg, *endp;
+		const __be32 *reg;
 		int l;
 		bool hotpluggable;
 
@@ -1038,23 +1038,21 @@ int __init early_init_dt_scan_memory(void)
 		if (!of_fdt_device_is_available(fdt, node))
 			continue;
 
-		reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
+		reg = of_fdt_get_addr_size_prop(node, "linux,usable-memory", &l);
 		if (reg == NULL)
-			reg = of_get_flat_dt_prop(node, "reg", &l);
+			reg = of_fdt_get_addr_size_prop(node, "reg", &l);
 		if (reg == NULL)
 			continue;
 
-		endp = reg + (l / sizeof(__be32));
 		hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
 
-		pr_debug("memory scan node %s, reg size %d,\n",

drivers/of/fdt.c int __init early_init_dt_scan_memory(void) · line 1055

Quoted 10 lines
drivers/of/fdt.c +3 -3
+		pr_debug("memory scan node %s, reg {addr,size} entries %d,\n",
 			 fdt_get_name(fdt, node, NULL), l);
 
-		while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
+		while (l-- > 0) {
 			u64 base, size;
 
-			base = dt_mem_next_cell(dt_root_addr_cells, &reg);
-			size = dt_mem_next_cell(dt_root_size_cells, &reg);
+			of_fdt_read_addr_size(reg, &base, &size);

This doesn't work. of_fdt_read_addr_size() needs to take an entry index to read each entry.

Rob

maintainer
#2 lore raw

Re: [PATCH v2 1/7] of/fdt: Consolidate duplicate code into helper functions

In reply to Yuntao Wang
Quoted 22 lines · Yuntao Wang

Currently, there are many pieces of nearly identical code scattered across different places. Consolidate the duplicate code into helper functions to improve maintainability and reduce the likelihood of errors.

---
 drivers/of/fdt.c       | 41 +++++++++++++++++++++++++++++++++++++++++
 include/linux/of_fdt.h |  5 +++++
 2 files changed, 46 insertions(+)
drivers/of/fdt.c +4 -0
index 0edd639898a6..5e0eabc1449f 100644
@@ -625,6 +625,47 @@ const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
 	return fdt_getprop(initial_boot_params, node, name, size);
 }
 
+const __be32 *__init of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries)
+{
+	const __be32 *prop;

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 641

Quoted 10 lines
drivers/of/fdt.c +10 -0
+	int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
+
+	prop = of_get_flat_dt_prop(node, name, &len);
+	if (!prop) {
+		*entries = 0;
+		return NULL;
+	}
+
+	if (len % elen) {
+		*entries = -1;

I don't think it's really important to distinguish a length error from any other error. Either we can read the property or we can't.

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 653

Quoted 12 lines
drivers/of/fdt.c +12 -0
+		return NULL;
+	}
+
+	*entries = len / elen;
+	return prop;
+}
+
+bool __init of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size)
+{
+	const __be32 *prop;
+	int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);

Still have 2 locations to get the same calculation wrong...

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 657

Quoted 4 lines
drivers/of/fdt.c +4 -0
+
+	prop = of_get_flat_dt_prop(node, name, &len);
+	if (!prop || len < elen)
+		return false;

Why doesn't calling of_fdt_get_addr_size_prop() work here? If 'len < elen', then 'len % elen' will also be true except in the 0 length case. For that case, of_fdt_get_addr_size_prop() needs to handle it too.

Quoted 19 lines
drivers/of/fdt.c +11 -0
+
+	of_fdt_read_addr_size(prop, addr, size);
+	return true;
+}
+
+void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
+{
+	*addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
+	*size = dt_mem_next_cell(dt_root_size_cells, &prop);
+}
+
 /**
  * of_fdt_is_compatible - Return true if given node from the given blob has
  * compat in its compatible list
include/linux/of_fdt.h +0 -0
index b8d6c0c20876..3a0805ff6c7b 100644

include/linux/of_fdt.h extern int of_get_flat_dt_subnode_by_name(unsigned long node, · line 63

Quoted 9 lines
include/linux/of_fdt.h +5 -0
@@ -55,6 +55,11 @@ extern int of_get_flat_dt_subnode_by_name(unsigned long node,
 					  const char *uname);
 extern const void *of_get_flat_dt_prop(unsigned long node, const char *name,
 				       int *size);
+extern const __be32 *of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries);
+extern bool of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size);
+extern void of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size);
 extern int of_flat_dt_is_compatible(unsigned long node, const char *name);

Looks like of_flat_dt_* would be more consistent with existing naming.

Quoted 5 lines
include/linux/of_fdt.h +0 -0
 extern unsigned long of_get_flat_dt_root(void);
 extern uint32_t of_get_flat_dt_phandle(unsigned long node);
Signature · 1 line
2.51.0
#3 lore raw

Re: [PATCH v2 1/7] of/fdt: Consolidate duplicate code into helper functions

Quoted 28 lines · Rob Herring
Quoted 27 lines · Yuntao Wang

Currently, there are many pieces of nearly identical code scattered across different places. Consolidate the duplicate code into helper functions to improve maintainability and reduce the likelihood of errors.

---
 drivers/of/fdt.c       | 41 +++++++++++++++++++++++++++++++++++++++++
 include/linux/of_fdt.h |  5 +++++
 2 files changed, 46 insertions(+)
drivers/of/fdt.c +9 -0
index 0edd639898a6..5e0eabc1449f 100644
@@ -625,6 +625,47 @@ const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
 	return fdt_getprop(initial_boot_params, node, name, size);
 }
 
+const __be32 *__init of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries)
+{
+	const __be32 *prop;
+	int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
+
+	prop = of_get_flat_dt_prop(node, name, &len);
+	if (!prop) {
+		*entries = 0;
Quoted 8 lines

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 641

Quoted 5 lines
drivers/of/fdt.c +5 -0
+		return NULL;
+	}
+
+	if (len % elen) {
+		*entries = -1;

I don't think it's really important to distinguish a length error from any other error. Either we can read the property or we can't.

Hi Rob,

I didn't originally split it into two checks, but later I noticed that in __reserved_mem_reserve_reg(), the two error conditions return different error codes. I was concerned about breaking compatibility, so I made the change this way.

If compatibility isn't an issue, I'd be happy to merge the two checks into one.

Quoted 16 lines

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 653

Quoted 13 lines
drivers/of/fdt.c +12 -0
+		return NULL;
+	}
+
+	*entries = len / elen;
+	return prop;
+}
+
+bool __init of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size)
+{
+	const __be32 *prop;
+	int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);

Still have 2 locations to get the same calculation wrong...

Quoted 8 lines

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 657

Quoted 4 lines
drivers/of/fdt.c +4 -0
+
+	prop = of_get_flat_dt_prop(node, name, &len);
+	if (!prop || len < elen)
+		return false;

Why doesn't calling of_fdt_get_addr_size_prop() work here? If 'len < elen', then 'len % elen' will also be true except in the 0 length case. For that case, of_fdt_get_addr_size_prop() needs to handle it too.

I'm fully in favor of calling of_fdt_get_addr_size_prop() directly here, that was my original intention as well, which is also why I placed this function right after of_fdt_get_addr_size_prop().

But again, due to compatibility concerns, I had to implement it this way.

For example, suppose prop points to data like:

[addr, size, other data]

With the previous len < elen check, addr and size could still be read successfully. But if we switch to the len % elen check, this type of data may fail.

If compatibility is not a concern, I can certainly change it to something like the following:

prop = of_fdt_get_addr_size_prop(node, name, &len); if (!prop || len != 1)
return false;

Quoted 23 lines
Quoted 23 lines
drivers/of/fdt.c +11 -0
+
+	of_fdt_read_addr_size(prop, addr, size);
+	return true;
+}
+
+void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
+{
+	*addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
+	*size = dt_mem_next_cell(dt_root_size_cells, &prop);
+}
+
 /**
  * of_fdt_is_compatible - Return true if given node from the given blob has
  * compat in its compatible list
include/linux/of_fdt.h +0 -0
index b8d6c0c20876..3a0805ff6c7b 100644
@@ -55,6 +55,11 @@ extern int of_get_flat_dt_subnode_by_name(unsigned long node,
 					  const char *uname);
 extern const void *of_get_flat_dt_prop(unsigned long node, const char *name,
 				       int *size);
Quoted 8 lines

include/linux/of_fdt.h extern int of_get_flat_dt_subnode_by_name(unsigned long node, · line 63

Quoted 6 lines
include/linux/of_fdt.h +5 -0
+extern const __be32 *of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries);
+extern bool of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size);
+extern void of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size);
 extern int of_flat_dt_is_compatible(unsigned long node, const char *name);

Looks like of_flat_dt_* would be more consistent with existing naming.

Naming is hard :-)

I spent quite a while thinking about the names of these functions.

In drivers/of/fdt.c and include/linux/of_fdt.h, there are several naming styles in use, such as of_fdt_, of_flat_dt_, and others.

I chose of_fdt_ as the prefix, or namespace, for these functions mainly because:

1. Compared to of_flat_dt_, it's simpler and shorter, and fdt can represent flat_dt, or flattened device tree.

2. of_fdt_ matches the file names drivers/of/fdt.c and include/linux/of_fdt.h better.

3. In the libfdt library, functions consistently use the fdt_ prefix, so using a similar of_fdt_ prefix in of/fdt.c seems reasonable.

But if you prefer the of_flat_dt_ nameing convention, I can change it.

Quoted 6 lines
Quoted 6 lines
include/linux/of_fdt.h +0 -0
 extern unsigned long of_get_flat_dt_root(void);
 extern uint32_t of_get_flat_dt_phandle(unsigned long node);
Signature · 1 line
2.51.0
#12 lore raw

Re: [PATCH v2 5/7] of/fdt: Simplify the logic of early_init_dt_scan_memory()

In reply to Rob Herring
Quoted 45 lines · Rob Herring
Quoted 44 lines · Yuntao Wang

Use the existing helper functions to simplify the logic of early_init_dt_scan_memory()

---
 drivers/of/fdt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/of/fdt.c +5 -6
index 4c45a97d6652..b6b059960fc2 100644
@@ -1027,7 +1027,7 @@ int __init early_init_dt_scan_memory(void)
 
 	fdt_for_each_subnode(node, fdt, 0) {
 		const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
-		const __be32 *reg, *endp;
+		const __be32 *reg;
 		int l;
 		bool hotpluggable;
 
@@ -1038,23 +1038,21 @@ int __init early_init_dt_scan_memory(void)
 		if (!of_fdt_device_is_available(fdt, node))
 			continue;
 
-		reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
+		reg = of_fdt_get_addr_size_prop(node, "linux,usable-memory", &l);
 		if (reg == NULL)
-			reg = of_get_flat_dt_prop(node, "reg", &l);
+			reg = of_fdt_get_addr_size_prop(node, "reg", &l);
 		if (reg == NULL)
 			continue;
 
-		endp = reg + (l / sizeof(__be32));
 		hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
 
-		pr_debug("memory scan node %s, reg size %d,\n",
+		pr_debug("memory scan node %s, reg {addr,size} entries %d,\n",
 			 fdt_get_name(fdt, node, NULL), l);
 
-		while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
+		while (l-- > 0) {
 			u64 base, size;
 
Quoted 8 lines

drivers/of/fdt.c int __init early_init_dt_scan_memory(void) · line 1055

Quoted 3 lines
drivers/of/fdt.c +1 -2
-			base = dt_mem_next_cell(dt_root_addr_cells, &reg);
-			size = dt_mem_next_cell(dt_root_size_cells, &reg);
+			of_fdt_read_addr_size(reg, &base, &size);

This doesn't work. of_fdt_read_addr_size() needs to take an entry index to read each entry.

Rob

Hi Rob,

This was entirely my mistake. I intended to pass &reg rather than reg, just like how dt_mem_next_cell() works.

So the correct definition of of_fdt_read_addr_size() should be:

void __init of_fdt_read_addr_size(const __be32 **prop, u64 *addr, u64 *size);

And the correct usage should be:

of_fdt_read_addr_size(&reg, &base, &size);

This bug was caused by my oversight — apologies for that.

I didn’t choose an interface like of_fdt_read_addr_size(reg, i, &base, &size) because in normal cases the data in prop is consumed sequentially, and I felt there was no need to introduce an entry index parameter, which would increase the API’s complexity.

There is another issue reported by kernel test robot:

drivers/of/fdt.c:903:31: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]

Given this, the problem exists regardless of which implementation we choose.

I’m considering two possible solutions:

1. Convert of_fdt_read_addr_size() into a macro.
2. Split it into two functions: of_fdt_read_addr() and of_fdt_read_size().

I’m leaning toward the second option.

What do you think? Or do you have a better approach?

Thanks,
Yuntao

#19 lore raw
In reply to Yuntao Wang
Quoted 6 lines · Yuntao Wang

This patch series fixes several bugs related to dt_root_addr_cells and dt_root_size_cells, and performs some cleanup.

Changelog:

v1: Consolidate duplicate code into helper functions

Your patchset has multiple checkpatch warnings and errors. In multiple patches.

Please run scripts/checkpatch.pl on the patches and fix reported warnings. After that, run also 'scripts/checkpatch.pl --strict' on the patches and (probably) fix more warnings. Some warnings can be ignored, especially from --strict run, but the code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Please run standard kernel tools for static analysis, like coccinelle, smatch and sparse, and fix reported warnings. Also please check for warnings when building with W=1 for gcc and clang. Most of these commands (checks or W=1 build) can build specific targets, like some directory, to narrow the scope to only your code. The code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Best regards,
Krzysztof

maintainer
#4 lore raw

Re: [PATCH v2 1/7] of/fdt: Consolidate duplicate code into helper functions

In reply to Yuntao Wang
Quoted 40 lines · Yuntao Wang
Quoted 28 lines · Rob Herring
Quoted 27 lines · Yuntao Wang

Currently, there are many pieces of nearly identical code scattered across different places. Consolidate the duplicate code into helper functions to improve maintainability and reduce the likelihood of errors.

---
 drivers/of/fdt.c       | 41 +++++++++++++++++++++++++++++++++++++++++
 include/linux/of_fdt.h |  5 +++++
 2 files changed, 46 insertions(+)
drivers/of/fdt.c +9 -0
index 0edd639898a6..5e0eabc1449f 100644
@@ -625,6 +625,47 @@ const void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
    return fdt_getprop(initial_boot_params, node, name, size);
 }
 
+const __be32 *__init of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries)
+{
+   const __be32 *prop;
+   int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
+
+   prop = of_get_flat_dt_prop(node, name, &len);
+   if (!prop) {
+           *entries = 0;
Quoted 8 lines

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 641

Quoted 5 lines
drivers/of/fdt.c +5 -0
+           return NULL;
+   }
+
+   if (len % elen) {
+           *entries = -1;

I don't think it's really important to distinguish a length error from any other error. Either we can read the property or we can't.

Quoted 8 lines

Hi Rob,

I didn't originally split it into two checks, but later I noticed that in __reserved_mem_reserve_reg(), the two error conditions return different error codes. I was concerned about breaking compatibility, so I made the change this way.

If compatibility isn't an issue, I'd be happy to merge the two checks into one.

You'll have to adjust the handling of -ENOENT case, but yes I think that is fine. IMO, the kernel can either read and parse a property or it can't. The exact reason it can't is generally not important.

Quoted 30 lines
Quoted 15 lines

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 653

Quoted 12 lines
drivers/of/fdt.c +12 -0
+           return NULL;
+   }
+
+   *entries = len / elen;
+   return prop;
+}
+
+bool __init of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size)
+{
+   const __be32 *prop;
+   int len, elen = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);

Still have 2 locations to get the same calculation wrong...

Quoted 8 lines

drivers/of/fdt.c const void *__init of_get_flat_dt_prop(unsigned long node, const char *name, · line 657

Quoted 4 lines
drivers/of/fdt.c +4 -0
+
+   prop = of_get_flat_dt_prop(node, name, &len);
+   if (!prop || len < elen)
+           return false;

Why doesn't calling of_fdt_get_addr_size_prop() work here? If 'len < elen', then 'len % elen' will also be true except in the 0 length case. For that case, of_fdt_get_addr_size_prop() needs to handle it too.

I'm fully in favor of calling of_fdt_get_addr_size_prop() directly here, that was my original intention as well, which is also why I placed this function right after of_fdt_get_addr_size_prop().

But again, due to compatibility concerns, I had to implement it this way.

Quoted 7 lines

For example, suppose prop points to data like:

[addr, size, other data]

With the previous len < elen check, addr and size could still be read successfully. But if we switch to the len % elen check, this type of data may fail.

Only if 'other data' is not a multiple of [addr,size], but that's completely invalid*.

(*The dts format does allow something as complex as '<0x12345678>, "string", /bits 8/ <0xab>', but you would have to be completely insane to do that when there's no type information in the DTB.)

Quoted 38 lines

If compatibility is not a concern, I can certainly change it to something like the following:

prop = of_fdt_get_addr_size_prop(node, name, &len); if (!prop || len != 1)
return false;

Quoted 23 lines
Quoted 23 lines
drivers/of/fdt.c +11 -0
+
+   of_fdt_read_addr_size(prop, addr, size);
+   return true;
+}
+
+void __init of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size)
+{
+   *addr = dt_mem_next_cell(dt_root_addr_cells, &prop);
+   *size = dt_mem_next_cell(dt_root_size_cells, &prop);
+}
+
 /**
  * of_fdt_is_compatible - Return true if given node from the given blob has
  * compat in its compatible list
include/linux/of_fdt.h +0 -0
index b8d6c0c20876..3a0805ff6c7b 100644
@@ -55,6 +55,11 @@ extern int of_get_flat_dt_subnode_by_name(unsigned long node,
                                      const char *uname);
 extern const void *of_get_flat_dt_prop(unsigned long node, const char *name,
                                   int *size);
Quoted 8 lines

include/linux/of_fdt.h extern int of_get_flat_dt_subnode_by_name(unsigned long node, · line 63

Quoted 6 lines
include/linux/of_fdt.h +5 -0
+extern const __be32 *of_fdt_get_addr_size_prop(unsigned long node,
+                                               const char *name, int *entries);
+extern bool of_fdt_get_addr_size(unsigned long node, const char *name,
+                                 u64 *addr, u64 *size);
+extern void of_fdt_read_addr_size(const __be32 *prop, u64 *addr, u64 *size);
 extern int of_flat_dt_is_compatible(unsigned long node, const char *name);

Looks like of_flat_dt_* would be more consistent with existing naming.

Quoted 1 line

Naming is hard :-)

Indeed.

Quoted 4 lines

I spent quite a while thinking about the names of these functions.

In drivers/of/fdt.c and include/linux/of_fdt.h, there are several naming styles in use, such as of_fdt_, of_flat_dt_, and others.

It's a bit of a mess...

Quoted 10 lines

I chose of_fdt_ as the prefix, or namespace, for these functions mainly because:

1. Compared to of_flat_dt_, it's simpler and shorter, and fdt can represent flat_dt, or flattened device tree.

2. of_fdt_ matches the file names drivers/of/fdt.c and include/linux/of_fdt.h better.

3. In the libfdt library, functions consistently use the fdt_ prefix, so using a similar of_fdt_ prefix in of/fdt.c seems reasonable.

If we started fresh, I would agree with all of this.

Quoted 1 line

But if you prefer the of_flat_dt_ nameing convention, I can change it.

I do primarily because that aligns with the other functions which read specific properties (e.g. of_flat_dt_is_compatible(), of_flat_dt_translate_address()).

Rob

maintainer
#13 lore raw

Re: [PATCH v2 5/7] of/fdt: Simplify the logic of early_init_dt_scan_memory()

In reply to Yuntao Wang
Quoted 68 lines · Yuntao Wang
Quoted 45 lines · Rob Herring
Quoted 44 lines · Yuntao Wang

Use the existing helper functions to simplify the logic of early_init_dt_scan_memory()

---
 drivers/of/fdt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/of/fdt.c +5 -6
index 4c45a97d6652..b6b059960fc2 100644
@@ -1027,7 +1027,7 @@ int __init early_init_dt_scan_memory(void)
 
    fdt_for_each_subnode(node, fdt, 0) {
            const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
-           const __be32 *reg, *endp;
+           const __be32 *reg;
            int l;
            bool hotpluggable;
 
@@ -1038,23 +1038,21 @@ int __init early_init_dt_scan_memory(void)
            if (!of_fdt_device_is_available(fdt, node))
                    continue;
 
-           reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
+           reg = of_fdt_get_addr_size_prop(node, "linux,usable-memory", &l);
            if (reg == NULL)
-                   reg = of_get_flat_dt_prop(node, "reg", &l);
+                   reg = of_fdt_get_addr_size_prop(node, "reg", &l);
            if (reg == NULL)
                    continue;
 
-           endp = reg + (l / sizeof(__be32));
            hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
 
-           pr_debug("memory scan node %s, reg size %d,\n",
+           pr_debug("memory scan node %s, reg {addr,size} entries %d,\n",
                     fdt_get_name(fdt, node, NULL), l);
 
-           while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
+           while (l-- > 0) {
                    u64 base, size;
 
Quoted 8 lines

drivers/of/fdt.c int __init early_init_dt_scan_memory(void) · line 1055

Quoted 3 lines
drivers/of/fdt.c +1 -2
-                   base = dt_mem_next_cell(dt_root_addr_cells, &reg);
-                   size = dt_mem_next_cell(dt_root_size_cells, &reg);
+                   of_fdt_read_addr_size(reg, &base, &size);

This doesn't work. of_fdt_read_addr_size() needs to take an entry index to read each entry.

Rob

Hi Rob,

This was entirely my mistake. I intended to pass &reg rather than reg, just like how dt_mem_next_cell() works.

So the correct definition of of_fdt_read_addr_size() should be:

void __init of_fdt_read_addr_size(const __be32 **prop, u64 *addr, u64 *size);

And the correct usage should be:

Quoted 8 lines

of_fdt_read_addr_size(&reg, &base, &size);

This bug was caused by my oversight — apologies for that.

I didn’t choose an interface like of_fdt_read_addr_size(reg, i, &base, &size) because in normal cases the data in prop is consumed sequentially, and I felt there was no need to introduce an entry index parameter, which would increase the API’s complexity.

Yes, but giving the index mirrors how the unflattened of_property APIs work. Not so much with the FDT, but we're trying to eliminate giving out raw pointers (with no lifetime) to the DT data. That doesn't work well with overlays and dynamic DTs.

Quoted 6 lines

There is another issue reported by kernel test robot:

drivers/of/fdt.c:903:31: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]

Given this, the problem exists regardless of which implementation we choose.

Quoted 8 lines

I’m considering two possible solutions:

1. Convert of_fdt_read_addr_size() into a macro.
2. Split it into two functions: of_fdt_read_addr() and of_fdt_read_size().

I’m leaning toward the second option.

What do you think? Or do you have a better approach?

Just use local u64 variables and then assign the values to the struct. This will not warn:

a_phys_addr = a_u64;

(It could silently truncate values, but I'm pretty sure no one runs 32-bit LPAE systems with a non-LPAE kernel on the very few systems that even still exist).

Rob

#14 lore raw

Re: [PATCH v2 5/7] of/fdt: Simplify the logic of early_init_dt_scan_memory()

Quoted 101 lines · Rob Herring
Quoted 68 lines · Yuntao Wang
Quoted 45 lines · Rob Herring
Quoted 44 lines · Yuntao Wang

Use the existing helper functions to simplify the logic of early_init_dt_scan_memory()

---
 drivers/of/fdt.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/of/fdt.c +5 -6
index 4c45a97d6652..b6b059960fc2 100644
@@ -1027,7 +1027,7 @@ int __init early_init_dt_scan_memory(void)
 
    fdt_for_each_subnode(node, fdt, 0) {
            const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
-           const __be32 *reg, *endp;
+           const __be32 *reg;
            int l;
            bool hotpluggable;
 
@@ -1038,23 +1038,21 @@ int __init early_init_dt_scan_memory(void)
            if (!of_fdt_device_is_available(fdt, node))
                    continue;
 
-           reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
+           reg = of_fdt_get_addr_size_prop(node, "linux,usable-memory", &l);
            if (reg == NULL)
-                   reg = of_get_flat_dt_prop(node, "reg", &l);
+                   reg = of_fdt_get_addr_size_prop(node, "reg", &l);
            if (reg == NULL)
                    continue;
 
-           endp = reg + (l / sizeof(__be32));
            hotpluggable = of_get_flat_dt_prop(node, "hotpluggable", NULL);
 
-           pr_debug("memory scan node %s, reg size %d,\n",
+           pr_debug("memory scan node %s, reg {addr,size} entries %d,\n",
                     fdt_get_name(fdt, node, NULL), l);
 
-           while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
+           while (l-- > 0) {
                    u64 base, size;
 
Quoted 8 lines

drivers/of/fdt.c int __init early_init_dt_scan_memory(void) · line 1055

Quoted 3 lines
drivers/of/fdt.c +1 -2
-                   base = dt_mem_next_cell(dt_root_addr_cells, &reg);
-                   size = dt_mem_next_cell(dt_root_size_cells, &reg);
+                   of_fdt_read_addr_size(reg, &base, &size);

This doesn't work. of_fdt_read_addr_size() needs to take an entry index to read each entry.

Rob

Hi Rob,

This was entirely my mistake. I intended to pass &reg rather than reg, just like how dt_mem_next_cell() works.

So the correct definition of of_fdt_read_addr_size() should be:

void __init of_fdt_read_addr_size(const __be32 **prop, u64 *addr, u64 *size);

And the correct usage should be:

Quoted 8 lines

of_fdt_read_addr_size(&reg, &base, &size);

This bug was caused by my oversight — apologies for that.

I didn’t choose an interface like of_fdt_read_addr_size(reg, i, &base, &size) because in normal cases the data in prop is consumed sequentially, and I felt there was no need to introduce an entry index parameter, which would increase the API’s complexity.

Yes, but giving the index mirrors how the unflattened of_property APIs work. Not so much with the FDT, but we're trying to eliminate giving out raw pointers (with no lifetime) to the DT data. That doesn't work well with overlays and dynamic DTs.

Quoted 6 lines

There is another issue reported by kernel test robot:

drivers/of/fdt.c:903:31: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]

Given this, the problem exists regardless of which implementation we choose.

Quoted 8 lines

I’m considering two possible solutions:

1. Convert of_fdt_read_addr_size() into a macro.
2. Split it into two functions: of_fdt_read_addr() and of_fdt_read_size().

I’m leaning toward the second option.

What do you think? Or do you have a better approach?

Just use local u64 variables and then assign the values to the struct. This will not warn:

Quoted 7 lines

a_phys_addr = a_u64;

(It could silently truncate values, but I'm pretty sure no one runs 32-bit LPAE systems with a non-LPAE kernel on the very few systems that even still exist).

Rob

Hi Rob,

The link to the v3 patch series:

https://lore.kernel.org/linux-devicetree/[email protected]/

Thanks,
Yuntao

#20 lore raw
In reply to Krzysztof Kozlowski
Quoted 25 lines · Krzysztof Kozlowski
Quoted 6 lines · Yuntao Wang

This patch series fixes several bugs related to dt_root_addr_cells and dt_root_size_cells, and performs some cleanup.

Changelog:

v1: Consolidate duplicate code into helper functions

Your patchset has multiple checkpatch warnings and errors. In multiple patches.

Please run scripts/checkpatch.pl on the patches and fix reported warnings. After that, run also 'scripts/checkpatch.pl --strict' on the patches and (probably) fix more warnings. Some warnings can be ignored, especially from --strict run, but the code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Please run standard kernel tools for static analysis, like coccinelle, smatch and sparse, and fix reported warnings. Also please check for warnings when building with W=1 for gcc and clang. Most of these commands (checks or W=1 build) can build specific targets, like some directory, to narrow the scope to only your code. The code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Quoted 2 lines

Best regards,
Krzysztof

Hi Krzysztof,

Many thanks for pointing out these issues, they have been fixed in the new patch series:

https://lore.kernel.org/linux-devicetree/[email protected]/

Thanks,
Yuntao

#21 lore raw
Quoted 27 lines · Yuntao Wang
Quoted 25 lines · Krzysztof Kozlowski
Quoted 6 lines · Yuntao Wang

This patch series fixes several bugs related to dt_root_addr_cells and dt_root_size_cells, and performs some cleanup.

Changelog:

v1: Consolidate duplicate code into helper functions

Your patchset has multiple checkpatch warnings and errors. In multiple patches.

Please run scripts/checkpatch.pl on the patches and fix reported warnings. After that, run also 'scripts/checkpatch.pl --strict' on the patches and (probably) fix more warnings. Some warnings can be ignored, especially from --strict run, but the code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Please run standard kernel tools for static analysis, like coccinelle, smatch and sparse, and fix reported warnings. Also please check for warnings when building with W=1 for gcc and clang. Most of these commands (checks or W=1 build) can build specific targets, like some directory, to narrow the scope to only your code. The code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Quoted 8 lines
Quoted 2 lines

Best regards,
Krzysztof

Hi Krzysztof,

Many thanks for pointing out these issues, they have been fixed in the new patch series:

https://lore.kernel.org/linux-devicetree/[email protected]/

Not really. I don't think you run checkpatch.

Best regards,
Krzysztof

#15 lore raw

Re: [PATCH v2 5/7] of/fdt: Simplify the logic of early_init_dt_scan_memory()

In reply to Rob Herring

Hi Rob,

Quoted 16 lines · Rob Herring
Quoted 6 lines · Yuntao Wang

There is another issue reported by kernel test robot:

drivers/of/fdt.c:903:31: error: incompatible pointer types passing 'phys_addr_t *' (aka 'unsigned int *') to parameter of type 'u64 *' (aka 'unsigned long long *') [-Wincompatible-pointer-types]

Given this, the problem exists regardless of which implementation we choose.

Quoted 8 lines

I’m considering two possible solutions:

1. Convert of_fdt_read_addr_size() into a macro.
2. Split it into two functions: of_fdt_read_addr() and of_fdt_read_size().

I’m leaning toward the second option.

What do you think? Or do you have a better approach?

Quoted 8 lines

Just use local u64 variables and then assign the values to the struct. This will not warn:

a_phys_addr = a_u64;

(It could silently truncate values, but I'm pretty sure no one runs 32-bit LPAE systems with a non-LPAE kernel on the very few systems that even still exist).

You mean running multi_v7_defconfig on anything found by 'git grep "#address-cells\s*=\s*<2>" -- arch/arm/boot/dts'?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds