[RFC PATCH 0/3] Dynamic allocation of reserved_mem array.

From: Oreoluwa Babatunde
Date: Thu Oct 19 2023 - 14:49:36 EST


The reserved_mem array is used to store the data of the different
reserved memory regions specified in the DT of a device.
The array stores information such as the name, node, starting address,
and size of a reserved memory region.

The array is currently statically allocated with a size of
MAX_RESERVED_REGIONS(64). This means that any system that specifies a
number of reserved memory regions greater than MAX_RESERVED_REGIONS(64)
will not have enough space to store the information for all the regions.

Therefore, this series changes the reserved_mem array from a static
array with a fixed size of MAX_RESERVED_REGIONS(64), to a dynamically
allocated array, using memblock_alloc(), based on the number of reserved
memory regions specified in the DT.

Memory gotten from memblock_alloc() is only writable after paging_init()
is called, but the reserved memory regions need to be reserved before
then so that the system does not create page table mappings for them.

It is possible to call memblock_resverve() and memblock_mark_nomap() on
the reserved memory regions and not need to save them to the array until
we dynamically allocate the array after paging_init(), but this becomes
an issue with reserved memory regions that do not have their starting
address specified in the DT.
i.e. regions specified with the "alloc_ranges" and "size" properties.

Therefore, this series achieves the allocation and population of the
reserved_mem array in two steps:

1. Before paging_init()
Before paging_init() is called, iterate through the reserved_mem
nodes in the DT and do the following:
- Allocate memory for dynamically placed reserved memory regions and
store their starting address in the static allocated reserved_mem
array.
- Call memblock_reserve() and memblock_mark_nomap() on all the
reserved memory regions.
- Count the total number of reserved_mem nodes in the DT.

2. After paging_init()
After paging_init() is called:
- Allocate new memory for the reserved_mem array based on the number
of reserved memory nodes in the DT.
- Transfer all the information that was stored in the static array
into the new array.
- Store the rest of the reserved_mem regions in the new array.
i.e. reserved memory nodes specified with the "reg" property.

The static array is no longer needed after this point, but there is
currently no obvious way to free the memory. Therefore, the size of the
initial static array is now defined using a config option.
Since its size would vary depending on the user, scaling it can help get
some memory savings.

Oreoluwa Babatunde (3):
of: reserved_mem: Change the order that reserved_mem regions are
stored
of: reserved_mem: Add code to dynamically allocate reserved_mem array
of: reserved_mem: Make MAX_RESERVED_REGIONS a config option

arch/arm64/kernel/setup.c | 4 ++
drivers/of/Kconfig | 13 ++++
drivers/of/fdt.c | 63 +++++++++++++++----
drivers/of/of_private.h | 3 +-
drivers/of/of_reserved_mem.c | 108 ++++++++++++++++++++++----------
include/linux/of_fdt.h | 1 +
include/linux/of_reserved_mem.h | 9 +++
7 files changed, 154 insertions(+), 47 deletions(-)

--
2.17.1