mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 3/6] arch: introduce CONFIG_MALLOC_OFFSET
Date: Fri, 22 May 2026 12:53:09 +0200	[thread overview]
Message-ID: <20260522105852.2681680-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260522105852.2681680-1-a.fatoum@pengutronix.de>

With barebox proper placed at end of RAM, the current CONFIG_MALLOC_SIZE
puts us into a tight spot: We must determine the size barebox proper
will occupy to subtract CONFIG_MALLOC_SIZE from it, but we may not know
the size yet in early stages of PBL, especially when we have separate
first and second stages.

This complexity in calculation is imposed onto all users, even though
most users probably don't make use of CONFIG_MALLOC_SIZE and set it to
zero anyway to let barebox compute a suitable malloc area size.

CONFIG_MALLOC_OFFSET simplifies calculation by starting the malloc area
at the specified offset from end of RAM.

Boards that used to set CONFIG_MALLOC_SIZE will need to set the new
CONFIG_MALLOC_OFFSET to their value of CONFIG_MALLOC_SIZE + the area
barebox occupies at end of initial memory.

For all other boards, they can just keep the default of
CONFIG_MALLOC_OFFSET=0 and barebox will start the malloc area at half of
RAM or 1G before end, which ever is smaller.

This computation is independent of barebox size and can thus be used in
early PBL as well in a later commit.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/Kconfig                        | 14 +++++++++++
 common/Kconfig                      | 36 +++++++++++++++++++++++++++++
 include/asm-generic/memory_layout.h | 20 ++++++++++++++++
 3 files changed, 70 insertions(+)

diff --git a/arch/Kconfig b/arch/Kconfig
index 23e65d58d52b..bb46839a2938 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -98,6 +98,20 @@ config ARCH_HAS_MALLOC_SIZE
 	  This is selected by architectures, where CONFIG_MALLOC_SIZE
 	  can be used to specify an exact size of the malloc area.
 
+	  Eventually, this should only be selected by sandbox, with
+	  everyone else switched over to ARCH_HAS_MALLOC_OFFSET.
+
+config ARCH_HAS_MALLOC_OFFSET
+	bool
+	help
+	  This is selected by architectures, where CONFIG_MALLOC_OFFSET
+	  can be used to specify the start offset of the malloc region
+	  with respect to the end of the initial memory.
+
+	  As this offset contains also the region reserved for barebox,
+	  it greatly simplifies memory layout calculation and allows
+	  to reserve the malloc region very early on.
+
 config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	bool
 
diff --git a/common/Kconfig b/common/Kconfig
index 5ba4530d3f9b..b957965125ae 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -295,6 +295,42 @@ config MALLOC_SIZE
 	default 0
 	prompt "malloc area size" if ARCH_HAS_MALLOC_SIZE
 
+config MALLOC_OFFSET
+	hex
+	default 0
+	prompt "relative malloc base offset (optional)"
+	depends on ARCH_HAS_MALLOC_OFFSET
+	help
+	  Most boards should just leave this at the default zero and barebox
+	  will dynamically determine the start of its eventual malloc area.
+	  The currently employed heuristic is:
+
+	    - if the prebootloader reports 2GiB of initial memory or more,
+	      start the malloc area at offset 1GiB from initial memory end.
+
+	    - otherwise, start the malloc area at middle of initial memory
+
+	  The malloc area is used for dynamic allocations by barebox, e.g.
+	  to uncompress a kernel. The placement of final boot artifacts happens
+	  outside of the malloc area and is not restricted to the initial
+	  memory reported by the prebootloader.
+
+	  As this heuristic may not be suitable for devices with tight memory
+	  constraints, setting a non-zero value here allows customizing the
+	  offset used to start the malloc area.
+
+	  The MALLOC_OFFSET is calculated from end of initial memory to start
+	  of the malloc area and as such, it also contains the region allocated
+	  for barebox itself. This inclusion of the barebox area differentiates
+	  this option from non-zero CONFIG_MALLOC_SIZE, but in return allows
+	  barebox to compute the start of the malloc area very early before it
+	  can know how much space barebox proper will eventually occupy.
+
+	  The iomem command can be used to determine at runtime how much space
+	  barebox requires for itself.
+
+	  If unsure, keep the default of 0x0.
+
 config SCRATCH_SIZE
 	hex
 	default 0x8000
diff --git a/include/asm-generic/memory_layout.h b/include/asm-generic/memory_layout.h
index b5a0306975be..2c6daf9cb2c7 100644
--- a/include/asm-generic/memory_layout.h
+++ b/include/asm-generic/memory_layout.h
@@ -33,6 +33,11 @@
 #define STACK_SIZE  CONFIG_STACK_SIZE
 #define SCRATCH_SIZE	CONFIG_SCRATCH_SIZE
 
+#ifndef __ASSEMBLY__
+
+#include <linux/minmax.h>
+#include <linux/sizes.h>
+
 /*
  * This generates a useless load from the specified symbol
  * to ensure linker garbage collection doesn't delete it
@@ -40,4 +45,19 @@
 #define __keep_symbolref(sym)	\
 	__asm__ __volatile__("": :"r"(&sym) :)
 
+#ifdef CONFIG_MALLOC_OFFSET
+static inline unsigned long barebox_malloc_base(unsigned long membase,
+						unsigned long memsize)
+ {
+	 unsigned long offset = CONFIG_MALLOC_OFFSET;
+
+	 if (!offset)
+		 offset = min_t(unsigned long, memsize / 2, SZ_1G);
+
+	 return max_t(unsigned long, membase + memsize - offset, membase);
+}
+#endif
+
+#endif /* __ASSEMBLY__ */
+
 #endif /* __ASM_GENERIC_MEMORY_LAYOUT_H */
-- 
2.47.3




  parent reply	other threads:[~2026-05-22 11:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22 10:53 [PATCH master 0/6] ARM: unify pbl and proper malloc area start Ahmad Fatoum
2026-05-22 10:53 ` [PATCH master 1/6] arch: introduce new CONFIG_ARCH_HAS_MALLOC_SIZE Ahmad Fatoum
2026-05-22 10:53 ` [PATCH master 2/6] ARM: explicitly state CONFIG_MALLOC_SIZE in defconfigs Ahmad Fatoum
2026-05-22 10:53 ` Ahmad Fatoum [this message]
2026-05-22 10:53 ` [PATCH master 4/6] ARM: switch to CONFIG_MALLOC_OFFSET Ahmad Fatoum
2026-05-22 10:53 ` [PATCH master 5/6] ARM: configs: drop CONFIG_MALLOC_SIZE=0x0 as it's now the default Ahmad Fatoum
2026-05-22 10:53 ` [PATCH master 6/6] ARM: place PBL malloc area at start of barebox proper malloc area Ahmad Fatoum

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=20260522105852.2681680-4-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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