devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rob Herring <robh@kernel.org>
To: Oreoluwa Babatunde <quic_obabatun@quicinc.com>
Cc: catalin.marinas@arm.com, will@kernel.org, frowand.list@gmail.com,
	vgupta@kernel.org, arnd@arndb.de, olof@lixom.net, soc@kernel.org,
	guoren@kernel.org, monstr@monstr.eu, palmer@dabbelt.com,
	aou@eecs.berkeley.edu, dinguyen@kernel.org,
	chenhuacai@kernel.org, tsbogend@alpha.franken.de,
	jonas@southpole.se, stefan.kristiansson@saunalahti.fi,
	shorne@gmail.com, mpe@ellerman.id.au, ysato@users.sourceforge.jp,
	dalias@libc.org, glaubitz@physik.fu-berlin.de, richard@nod.at,
	anton.ivanov@cambridgegreys.com, johannes@sipsolutions.net,
	chris@zankel.net, jcmvbkbc@gmail.com,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	linux-arm-msm@vger.kernel.org, kernel@quicinc.com
Subject: Re: [PATCH 00/46] Dynamic allocation of reserved_mem array.
Date: Thu, 1 Feb 2024 13:46:53 -0600	[thread overview]
Message-ID: <20240201194653.GA1328565-robh@kernel.org> (raw)
In-Reply-To: <51dc64bb-3101-4b4a-a54f-c0df6c0b264c@quicinc.com>

On Thu, Feb 01, 2024 at 09:08:06AM -0800, Oreoluwa Babatunde wrote:
> 
> On 1/30/2024 4:07 PM, Rob Herring wrote:
> > On Fri, Jan 26, 2024 at 03:53:39PM -0800, Oreoluwa Babatunde wrote:
> >> The reserved_mem array is used to store data for the different
> >> reserved memory regions defined in the DT of a device.  The array
> >> stores information such as region name, node, start-address, and size
> >> of the reserved memory regions.
> >>
> >> 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 extends the use of the static array for
> >> reserved_mem, and introduces a dynamically allocated array using
> >> memblock_alloc() based on the number of reserved memory regions
> >> specified in the DT.
> >>
> >> Some architectures such as arm64 require the page tables to be setup
> >> before memblock allocated memory is writable.  Therefore, the dynamic
> >> allocation of the reserved_mem array will need to be done after the
> >> page tables have been setup on these architectures. In most cases that
> >> will be after paging_init().
> >>
> >> Reserved memory regions can be divided into 2 groups.
> >> i) Statically-placed reserved memory regions
> >> i.e. regions defined in the DT using the @reg property.
> >> ii) Dynamically-placed reserved memory regions.
> >> i.e. regions specified in the DT using the @alloc_ranges
> >>     and @size properties.
> >>
> >> It is possible to call memblock_reserve() and memblock_mark_nomap() on
> >> the statically-placed reserved memory regions and not need to save them
> >> to the reserved_mem array until memory is allocated for it using
> >> memblock, which will be after the page tables have been setup.
> >> For the dynamically-placed reserved memory regions, it is not possible
> >> to wait to store its information because the starting address is
> >> allocated only at run time, and hence they need to be stored somewhere
> >> after they are allocated.
> >> Waiting until after the page tables have been setup to allocate memory
> >> for the dynamically-placed regions is also not an option because the
> >> allocations will come from memory that have already been added to the
> >> page tables, which is not good for memory that is supposed to be
> >> reserved and/or marked as nomap.
> >>
> >> Therefore, this series splits up the processing of the reserved memory
> >> regions into two stages, of which the first stage is carried out by
> >> early_init_fdt_scan_reserved_mem() and the second is carried out by
> >> fdt_init_reserved_mem().
> >>
> >> The early_init_fdt_scan_reserved_mem(), which is called before the page
> >> tables are setup is used to:
> >> 1. Call memblock_reserve() and memblock_mark_nomap() on all the
> >>    statically-placed reserved memory regions as needed.
> >> 2. Allocate memory from memblock for the dynamically-placed reserved
> >>    memory regions and store them in the static array for reserved_mem.
> >>    memblock_reserve() and memblock_mark_nomap() are also called as
> >>    needed on all the memory allocated for the dynamically-placed
> >>    regions.
> >> 3. Count the total number of reserved memory regions found in the DT.
> >>
> >> fdt_init_reserved_mem(), which should be called after the page tables
> >> have been setup, is used to carry out the following:
> >> 1. Allocate memory for the reserved_mem array based on the number of
> >>    reserved memory regions counted as mentioned above.
> >> 2. Copy all the information for the dynamically-placed reserved memory
> >>    regions from the static array into the new allocated memory for the
> >>    reserved_mem array.
> >> 3. Add the information for the statically-placed reserved memory into
> >>    reserved_mem array.
> >> 4. Run the region specific init functions for each of the reserve memory
> >>    regions saved in the reserved_mem array.
> > I don't see the need for fdt_init_reserved_mem() to be explicitly called 
> > by arch code. I said this already, but that can be done at the same time 
> > as unflattening the DT. The same conditions are needed for both: we need 
> > to be able to allocate memory from memblock.
> >
> > To put it another way, if fdt_init_reserved_mem() can be called "early", 
> > then unflattening could be moved earlier as well. Though I don't think 
> > we should optimize that. I'd rather see all arches call the DT functions 
> > at the same stages.
> Hi Rob,
> 
> The reason we moved fdt_init_reserved_mem() back into the arch specific code
> was because we realized that there was no apparently obvious way to call
> early_init_fdt_scan_reserved_mem() and fdt_init_reserved_mem() in the correct
> order that will work for all archs if we placed fdt_init_reserved_mem() inside the
> unflatten_devicetree() function.
> 
> early_init_fdt_scan_reserved_mem() needs to be
> called first before fdt_init_reserved_mem(). But on some archs,
> unflatten_devicetree() is called before early_init_fdt_scan_reserved_mem(), which
> means that if we have fdt_init_reserved_mem() inside the unflatten_devicetree()
> function, it will be called before early_init_fdt_scan_reserved_mem().
> 
> This is connected to your other comments on Patch 7 & Patch 14.
> I agree, unflatten_devicetree() should NOT be getting called before we reserve
> memory for the reserved memory regions because that could cause memory to be
> allocated from regions that should be reserved.
> 
> Hence, resolving this issue should allow us to call fdt_init_reserved_mem() from
> the  unflatten_devicetree() function without it changing the order that we are
> trying to have.

There's one issue I've found which is unflatten_device_tree() isn't 
called for ACPI case on arm64. Turns out we need /reserved-memory 
handled in that case too. However, I think we're going to change 
calling unflatten_device_tree() unconditionally for another reason[1]. 

[1] https://lore.kernel.org/all/efe6a7886c3491cc9c225a903efa2b1e.sboyd@kernel.org/

> 
> I will work on implementing this and send another revision.

I think we should go with a simpler route that's just copy the an 
initial array in initdata to a properly sized, allocated array like the 
patch below. Of course it will need some arch fixes and a follow-on 
patch to increase the initial array size.

8<--------------------------------------------------------------------
From: Rob Herring <robh@kernel.org>
Date: Wed, 31 Jan 2024 16:26:23 -0600
Subject: [PATCH] of: reserved-mem: Re-allocate reserved_mem array to actual
 size

In preparation to increase the static reserved_mem array size yet again,
copy the initial array to an allocated array sized based on the actual
size needed. Now increasing the the size of the static reserved_mem
array only eats up the initdata space. For platforms with reasonable
number of reserved regions, we have a net gain in free memory.

In order to do memblock allocations, fdt_init_reserved_mem() is moved a
bit later to unflatten_device_tree(). On some arches this is effectively
a nop.

Signed-off-by: Rob Herring <robh@kernel.org>
---
RFC as this is compile tested only. This is an alternative to this
series[1].

[1] https://lore.kernel.org/all/20240126235425.12233-1-quic_obabatun@quicinc.com/
---
 drivers/of/fdt.c             |  4 ++--
 drivers/of/of_reserved_mem.c | 18 +++++++++++++-----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index bf502ba8da95..14360f5191ae 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -645,8 +645,6 @@ void __init early_init_fdt_scan_reserved_mem(void)
 			break;
 		memblock_reserve(base, size);
 	}
-
-	fdt_init_reserved_mem();
 }
 
 /**
@@ -1328,6 +1326,8 @@ bool __init early_init_dt_scan(void *params)
  */
 void __init unflatten_device_tree(void)
 {
+	fdt_init_reserved_mem();
+
 	__unflatten_device_tree(initial_boot_params, NULL, &of_root,
 				early_init_dt_alloc_memory_arch, false);
 
diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 7ec94cfcbddb..ae323d6b25ad 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -27,7 +27,8 @@
 #include "of_private.h"
 
 #define MAX_RESERVED_REGIONS	64
-static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
+static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS] __initdata;
+static struct reserved_mem *reserved_mem_p;
 static int reserved_mem_count;
 
 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
@@ -354,6 +355,13 @@ void __init fdt_init_reserved_mem(void)
 			}
 		}
 	}
+
+	reserved_mem_p = memblock_alloc(sizeof(struct reserved_mem) * reserved_mem_count,
+					sizeof(struct reserved_mem));
+	if (WARN(!reserved_mem_p, "of: reserved-memory allocation failed, continuing with __initdata array!\n"))
+		reserved_mem_p = reserved_mem;
+	else
+		memcpy(reserved_mem_p, reserved_mem, sizeof(struct reserved_mem) * reserved_mem_count);
 }
 
 static inline struct reserved_mem *__find_rmem(struct device_node *node)
@@ -364,8 +372,8 @@ static inline struct reserved_mem *__find_rmem(struct device_node *node)
 		return NULL;
 
 	for (i = 0; i < reserved_mem_count; i++)
-		if (reserved_mem[i].phandle == node->phandle)
-			return &reserved_mem[i];
+		if (reserved_mem_p[i].phandle == node->phandle)
+			return &reserved_mem_p[i];
 	return NULL;
 }
 
@@ -507,8 +515,8 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
 
 	name = kbasename(np->full_name);
 	for (i = 0; i < reserved_mem_count; i++)
-		if (!strcmp(reserved_mem[i].name, name))
-			return &reserved_mem[i];
+		if (!strcmp(reserved_mem_p[i].name, name))
+			return &reserved_mem_p[i];
 
 	return NULL;
 }
-- 
2.43.0



  reply	other threads:[~2024-02-01 19:46 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-26 23:53 [PATCH 00/46] Dynamic allocation of reserved_mem array Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 01/46] of: reserved_mem: Change the order that reserved_mem regions are stored Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 02/46] of: reserved_mem: Introduce new early reserved memory scan function Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 03/46] ARC: reserved_mem: Implement the new processing order for reserved memory Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 04/46] ARM: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 05/46] arm64: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 06/46] csky: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 07/46] Loongarch: " Oreoluwa Babatunde
2024-01-31 15:27   ` Rob Herring
2024-02-01 17:17     ` Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 08/46] microblaze: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 09/46] mips: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 10/46] nios2: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 11/46] openrisc: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 12/46] powerpc: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 13/46] riscv: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 14/46] sh: " Oreoluwa Babatunde
2024-01-31 15:41   ` Rob Herring
2024-02-01 17:15     ` Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 15/46] um: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 16/46] xtensa: " Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 17/46] of: reserved_mem: Delete the early_init_fdt_scan_reserved_mem() function Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 18/46] of: reserved_mem: Add code to dynamically allocate reserved_mem array Oreoluwa Babatunde
2024-01-29  3:39   ` kernel test robot
2024-01-29 13:57   ` kernel test robot
2024-01-26 23:53 ` [PATCH 19/46] ARC: resrved_mem: Move fdt_init_reserved_mem() below unflatten_device_tree() Oreoluwa Babatunde
2024-01-26 23:53 ` [PATCH 20/46] ARM: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 21/46] arm64: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 22/46] csky: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 23/46] microblaze: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 24/46] mips: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 25/46] nios2: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 26/46] powerpc: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 27/46] riscv: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 28/46] um: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 29/46] xtensa: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 30/46] of: reserved_mem: Add code to use unflattened DT for reserved_mem nodes Oreoluwa Babatunde
2024-01-28  4:29   ` kernel test robot
2024-01-28  6:06   ` kernel test robot
2024-01-29 18:58   ` kernel test robot
2024-01-31 17:53   ` kernel test robot
2024-01-26 23:54 ` [PATCH 31/46] of: reserved_mem: Rename fdt_* functions to refelct use of unflattened devicetree APIs Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 32/46] ARC: reserved_mem: Switch fdt_init_reserved_mem() to dt_init_reserved_mem() Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 33/46] ARM: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 34/46] arm64: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 35/46] csky: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 36/46] loongarch: reserved_mem: Switch fdt_init_reserved_mem to dt_init_reserved_mem Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 37/46] microblaze: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 38/46] mips: reserved_mem: Switch fdt_init_reserved_mem() to dt_init_reserved_mem() Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 39/46] nios2: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 40/46] openrisc: reserved_mem: Switch fdt_init_reserved_mem to dt_init_reserved_mem Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 41/46] powerpc: reserved_mem: Switch fdt_init_reserved_mem() to dt_init_reserved_mem() Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 42/46] riscv: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 43/46] sh: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 44/46] um: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 45/46] xtensa: " Oreoluwa Babatunde
2024-01-26 23:54 ` [PATCH 46/46] of: reserved_mem: Delete the fdt_init_reserved_mem() function Oreoluwa Babatunde
2024-01-31  0:07 ` [PATCH 00/46] Dynamic allocation of reserved_mem array Rob Herring
2024-02-01 17:08   ` Oreoluwa Babatunde
2024-02-01 19:46     ` Rob Herring [this message]
2024-02-01 21:10       ` Oreoluwa Babatunde
2024-02-02 15:29         ` Rob Herring
2024-02-07 21:13           ` Oreoluwa Babatunde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240201194653.GA1328565-robh@kernel.org \
    --to=robh@kernel.org \
    --cc=anton.ivanov@cambridgegreys.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=arnd@arndb.de \
    --cc=catalin.marinas@arm.com \
    --cc=chenhuacai@kernel.org \
    --cc=chris@zankel.net \
    --cc=dalias@libc.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dinguyen@kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=glaubitz@physik.fu-berlin.de \
    --cc=guoren@kernel.org \
    --cc=jcmvbkbc@gmail.com \
    --cc=johannes@sipsolutions.net \
    --cc=jonas@southpole.se \
    --cc=kernel@quicinc.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=monstr@monstr.eu \
    --cc=mpe@ellerman.id.au \
    --cc=olof@lixom.net \
    --cc=palmer@dabbelt.com \
    --cc=quic_obabatun@quicinc.com \
    --cc=richard@nod.at \
    --cc=shorne@gmail.com \
    --cc=soc@kernel.org \
    --cc=stefan.kristiansson@saunalahti.fi \
    --cc=tsbogend@alpha.franken.de \
    --cc=vgupta@kernel.org \
    --cc=will@kernel.org \
    --cc=ysato@users.sourceforge.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).