[RFC PATCH v2 5/6] of: reserved_mem: Add code to dynamically allocate reserved_mem array

From: Oreoluwa Babatunde
Date: Sun Dec 03 2023 - 23:14:37 EST


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.

Therefore, extend the use of the static array by introducing a
dynamically allocated array based on the number of reserved memory
regions specified in the DT.

Before paging_init() runs, the static array is used to store the
dynamically-placed regions.
After paging_init(), memory is dynamically allocated for the
reserved_mem array, and all entries from the static array is copied
over to the new array, and all other statically-placed regions are
added in as well.

Signed-off-by: Oreoluwa Babatunde <quic_obabatun@xxxxxxxxxxx>
---
drivers/of/fdt.c | 13 +++++++---
drivers/of/of_private.h | 1 +
drivers/of/of_reserved_mem.c | 48 +++++++++++++++++++++++++++++++++---
3 files changed, 55 insertions(+), 7 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 12769dd53c34..2f1eabbd6869 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -563,8 +563,8 @@ static int __init __reserved_mem_check_root(unsigned long node)
*/
static int __init fdt_scan_reserved_mem(void)
{
- int node, child;
- int dynamic_nodes_cnt = 0;
+ int node, child, err = 0;
+ int dynamic_nodes_cnt = 0, count = 0;
int dynamic_nodes[MAX_RESERVED_REGIONS];
const void *fdt = initial_boot_params;

@@ -579,7 +579,6 @@ static int __init fdt_scan_reserved_mem(void)

fdt_for_each_subnode(child, fdt, node) {
const char *uname;
- int err;

if (!of_fdt_device_is_available(fdt, child))
continue;
@@ -587,6 +586,8 @@ static int __init fdt_scan_reserved_mem(void)
uname = fdt_get_name(fdt, child, NULL);

err = __reserved_mem_reserve_reg(child, uname);
+ if (!err)
+ count++;

if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
dynamic_nodes[dynamic_nodes_cnt] = child;
@@ -600,8 +601,12 @@ static int __init fdt_scan_reserved_mem(void)
child = dynamic_nodes[i];
uname = fdt_get_name(fdt, child, NULL);

- __reserved_mem_alloc_size(child, uname);
+ err = __reserved_mem_alloc_size(child, uname);
+ if (!err)
+ count++;
}
+ update_reserved_mem_max_cnt(count);
+
return 0;
}

diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h
index 575e2b4119e0..ef56b2ea185c 100644
--- a/drivers/of/of_private.h
+++ b/drivers/of/of_private.h
@@ -179,5 +179,6 @@ static inline struct device_node *__of_get_dma_parent(const struct device_node *
void init_reserved_mem(void);
void dt_reserved_mem_save_node(struct device_node *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 2ef9edcb8c93..01cd6a571dc2 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -26,7 +26,9 @@

#include "of_private.h"

-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 = 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,
@@ -54,6 +56,42 @@ 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;
+
+ memset_size = alloc_size - copy_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;
+}
+
/*
* dt_reserved_mem_save_node() - save dt node for second pass initialization
*/
@@ -62,7 +100,7 @@ void __init dt_reserved_mem_save_node(struct device_node *node, const char *unam
{
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;
}
@@ -346,7 +384,11 @@ static void __init __rmem_check_for_overlap(void)
*/
void __init init_reserved_mem(void)
{
- int i;
+ int i, ret;
+
+ ret = alloc_reserved_mem_array();
+ if (ret)
+ pr_err("Failed to allocate memory for reserved_mem array with err: %d", ret);

scan_reserved_mem_reg_nodes();

--
2.17.1