diff mbox series

[RFC,2/3] of: reserved_mem: Add code to dynamically allocate reserved_mem array

Message ID 20231019184825.9712-3-quic_obabatun@quicinc.com (mailing list archive)
State Superseded
Headers show
Series Dynamic allocation of reserved_mem array. | expand

Commit Message

Oreoluwa Babatunde Oct. 19, 2023, 6:48 p.m. UTC
The reserved_mem array is statically allocated with a size of
MAX_RESERVED_REGIONS(64). Therefore, if the number of reserved_mem
regions exceeds this size, there will not be enough space to store
all the data.

To fix this, dynamically allocate memory for the reserved_mem array
based on the number of reserved memory regions speicified in the DT.

Signed-off-by: Oreoluwa Babatunde <quic_obabatun@quicinc.com>
---
 drivers/of/fdt.c             |  9 ++++--
 drivers/of/of_private.h      |  2 ++
 drivers/of/of_reserved_mem.c | 53 ++++++++++++++++++++++++++++++++++--
 3 files changed, 59 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index d51a1176a7b9..408fd15b7db0 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -598,7 +598,7 @@  static void save_reserved_mem_reg_nodes(unsigned long node, const char *uname)
  */
 int __init fdt_scan_reserved_mem(bool save_only)
 {
-	int node, child;
+	int node, child, count = 0;
 	const void *fdt = initial_boot_params;
 
 	node = fdt_path_offset(fdt, "/reserved-memory");
@@ -626,8 +626,13 @@  int __init fdt_scan_reserved_mem(bool save_only)
 
 		err = __reserved_mem_reserve_reg(child, uname);
 		if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL))
-			__reserved_mem_alloc_size(child, uname);
+			err = __reserved_mem_alloc_size(child, uname);
+
+		if (err == 0)
+			count++;
 	}
+	if (!save_only)
+		update_reserved_mem_max_cnt(count);
 	return 0;
 }
 
diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h
index e52b27b8392d..ca1ba9f06660 100644
--- a/drivers/of/of_private.h
+++ b/drivers/of/of_private.h
@@ -178,4 +178,6 @@  static inline struct device_node *__of_get_dma_parent(const struct device_node *
 void fdt_reserved_mem_save_node(unsigned long node, const char *uname,
 			       phys_addr_t base, phys_addr_t size);
 
+void update_reserved_mem_max_cnt(int max_count);
+
 #endif /* _LINUX_OF_PRIVATE_H */
diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 13e694f5e316..203828ca118e 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -27,7 +27,10 @@ 
 #include "of_private.h"
 
 #define MAX_RESERVED_REGIONS	64
-static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
+static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS];
+static struct reserved_mem *reserved_mem __refdata = reserved_mem_array;
+
+static int total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
 static int reserved_mem_count;
 
 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
@@ -55,6 +58,45 @@  static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
 	return err;
 }
 
+void __init update_reserved_mem_max_cnt(int max_count)
+{
+	total_reserved_mem_cnt = max_count;
+}
+
+static int alloc_reserved_mem_array(void)
+{
+	struct reserved_mem *new_array;
+	size_t alloc_size, copy_size, memset_size;
+
+	alloc_size = array_size(total_reserved_mem_cnt, sizeof(*new_array));
+	if (alloc_size == SIZE_MAX)
+		return -1;
+
+	new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
+	if (!new_array)
+		return -ENOMEM;
+
+	copy_size = array_size(reserved_mem_count, sizeof(*new_array));
+	if (copy_size == SIZE_MAX)
+		goto overlow_err;
+
+	if (total_reserved_mem_cnt >= reserved_mem_count)
+		memset_size = alloc_size - copy_size;
+	else
+		memset_size = copy_size - alloc_size;
+
+	memcpy(new_array, reserved_mem, copy_size);
+	memset(new_array + reserved_mem_count, 0, memset_size);
+
+	reserved_mem = new_array;
+	return 0;
+
+overlow_err:
+	memblock_free(new_array, alloc_size);
+	total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
+	return -1;
+}
+
 /*
  * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
  */
@@ -63,7 +105,7 @@  void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
 {
 	struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
 
-	if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
+	if (reserved_mem_count == total_reserved_mem_cnt) {
 		pr_err("not enough space for all defined regions.\n");
 		return;
 	}
@@ -304,9 +346,14 @@  static void __init __rmem_check_for_overlap(void)
  */
 void __init fdt_init_reserved_mem(void)
 {
-	int i;
+	int i, ret = 0;
 	bool save_only = true;
 
+	ret = alloc_reserved_mem_array();
+	if (ret) {
+		pr_err("Failed to store reserved_mem nodes in array with err: %d", ret);
+		return;
+	}
 	fdt_scan_reserved_mem(save_only);
 
 	/* check for overlapping reserved regions */