* [PATCH v2 2/3] pbl: cleanup early malloc
2026-05-18 18:32 [PATCH v2 1/3] PBL: add common define for early malloc size Sascha Hauer
@ 2026-05-18 18:32 ` Sascha Hauer
2026-05-18 18:32 ` [PATCH v2 3/3] ARM: rockchip: initialize PBL malloc Sascha Hauer
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2026-05-18 18:32 UTC (permalink / raw)
To: Barebox List; +Cc: Ahmad Fatoum
All architectures using PBL define the same variables free_mem_ptr and
free_mem_end_ptr and initialize them. create a helper for that in
pbl/malloc.c and rename simple_*() to pbl_*().
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/arm/cpu/uncompress.c | 6 +---
arch/mips/boot/uncompress.c | 6 +---
arch/riscv/boot/uncompress.c | 6 +---
arch/riscv/include/asm/barebox-riscv.h | 6 ----
include/linux/decompress/mm.h | 44 ++++----------------------
include/pbl.h | 7 ++--
pbl/Makefile | 1 +
pbl/malloc.c | 43 +++++++++++++++++++++++++
8 files changed, 57 insertions(+), 62 deletions(-)
create mode 100644 pbl/malloc.c
diff --git a/arch/arm/cpu/uncompress.c b/arch/arm/cpu/uncompress.c
index 4bade72ba4..db6cd7fa11 100644
--- a/arch/arm/cpu/uncompress.c
+++ b/arch/arm/cpu/uncompress.c
@@ -27,9 +27,6 @@
#include "entry.h"
-unsigned long free_mem_ptr;
-unsigned long free_mem_end_ptr;
-
extern unsigned char input_data[];
extern unsigned char input_data_end[];
@@ -78,8 +75,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
handoff_data = (void *)barebox_base + uncompressed_len + MAX_BSS_SIZE;
- free_mem_ptr = barebox_base - PBL_MALLOC_SIZE;
- free_mem_end_ptr = barebox_base;
+ pbl_malloc_init(barebox_base - PBL_MALLOC_SIZE, PBL_MALLOC_SIZE);
#ifdef DEBUG
print_pbl_mem_layout(membase, endmem, barebox_base);
diff --git a/arch/mips/boot/uncompress.c b/arch/mips/boot/uncompress.c
index 5c6ba39875..d3025f4b9b 100644
--- a/arch/mips/boot/uncompress.c
+++ b/arch/mips/boot/uncompress.c
@@ -16,9 +16,6 @@
extern void *input_data;
extern void *input_data_end;
-unsigned long free_mem_ptr;
-unsigned long free_mem_end_ptr;
-
void barebox_pbl_start(void *fdt, void *fdt_end, unsigned long ram_size);
void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
@@ -34,8 +31,7 @@ void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
memset(__bss_start, 0, __bss_stop - __bss_start);
/* set 128 KiB at the end of the MALLOC_BASE for early malloc */
- free_mem_ptr = TEXT_BASE - PBL_MALLOC_SIZE;
- free_mem_end_ptr = free_mem_ptr + PBL_MALLOC_SIZE;
+ pbl_malloc_init(barebox_base - TEXT_BASE - PBL_MALLOC_SIZE, PBL_MALLOC_SIZE);
piggy_len = (unsigned long)&input_data_end - (unsigned long)&input_data;
diff --git a/arch/riscv/boot/uncompress.c b/arch/riscv/boot/uncompress.c
index e51f1b0121..e827f4b6bb 100644
--- a/arch/riscv/boot/uncompress.c
+++ b/arch/riscv/boot/uncompress.c
@@ -23,9 +23,6 @@
#include "entry.h"
-unsigned long free_mem_ptr;
-unsigned long free_mem_end_ptr;
-
void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
void *fdt)
{
@@ -63,8 +60,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
pr_debug("memory at 0x%08lx, size 0x%08lx\n", membase, memsize);
- free_mem_ptr = riscv_mem_early_malloc(membase, endmem);
- free_mem_end_ptr = riscv_mem_early_malloc_end(membase, endmem);
+ pbl_malloc_init(riscv_mem_early_malloc(), PBL_MALLOC_SIZE);
/*
* Enable MMU early to enable caching for faster decompression.
diff --git a/arch/riscv/include/asm/barebox-riscv.h b/arch/riscv/include/asm/barebox-riscv.h
index a3ae07277d..1ea9614d81 100644
--- a/arch/riscv/include/asm/barebox-riscv.h
+++ b/arch/riscv/include/asm/barebox-riscv.h
@@ -57,12 +57,6 @@ static inline unsigned long riscv_mem_early_malloc(unsigned long membase,
return riscv_mem_stack(membase, endmem) - PBL_MALLOC_SIZE;
}
-static inline unsigned long riscv_mem_early_malloc_end(unsigned long membase,
- unsigned long endmem)
-{
- return riscv_mem_stack(membase, endmem);
-}
-
static inline unsigned long riscv_mem_ramoops(unsigned long membase,
unsigned long endmem)
{
diff --git a/include/linux/decompress/mm.h b/include/linux/decompress/mm.h
index 1891a51368..27b13ec9c8 100644
--- a/include/linux/decompress/mm.h
+++ b/include/linux/decompress/mm.h
@@ -14,6 +14,8 @@
#ifdef STATIC
+#include <pbl.h>
+
/* Code active when included from pre-boot environment: */
/*
@@ -26,45 +28,11 @@
#define STATIC_RW_DATA static
#endif
-/* A trivial malloc implementation, adapted from
- * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
- */
-STATIC_RW_DATA unsigned long malloc_ptr;
-STATIC_RW_DATA int malloc_count;
-
-static __maybe_unused void *simple_malloc(int size)
-{
- void *p;
-
- if (size < 0)
- return NULL;
- if (!malloc_ptr)
- malloc_ptr = free_mem_ptr;
-
- malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
-
- p = (void *)malloc_ptr;
- malloc_ptr += size;
-
- if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
- return NULL;
-
- malloc_count++;
- return p;
-}
-
-static __maybe_unused void simple_free(void *where)
-{
- malloc_count--;
- if (!malloc_count)
- malloc_ptr = free_mem_ptr;
-}
-
-#define large_malloc(a) simple_malloc(a)
-#define large_free(a) simple_free(a)
+#define large_malloc(a) pbl_malloc(a)
+#define large_free(a) pbl_free(a)
-#define MALLOC simple_malloc
-#define FREE simple_free
+#define MALLOC pbl_malloc
+#define FREE pbl_free
#else
diff --git a/include/pbl.h b/include/pbl.h
index 5a444fb6fe..fe4367825c 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -12,9 +12,6 @@
#include <linux/compiler.h>
#include <linux/sizes.h>
-extern unsigned long free_mem_ptr;
-extern unsigned long free_mem_end_ptr;
-
void pbl_barebox_uncompress(void *dest, void *compressed_start, unsigned int len);
int pbl_dtbz_uncompress(void *dest, void *compressed_start, unsigned long len);
@@ -36,6 +33,10 @@ int pbl_load_fdt(const void *fdt, void *dest, int destsize);
#define PBL_MALLOC_SIZE SZ_128K
+void *pbl_malloc(int size);
+void pbl_free(void *where);
+void pbl_malloc_init(unsigned long base, size_t size);
+
#endif
void __noreturn barebox_pbl_entry(ulong, ulong, void *);
diff --git a/pbl/Makefile b/pbl/Makefile
index b78124cdcd..45cfbf5fba 100644
--- a/pbl/Makefile
+++ b/pbl/Makefile
@@ -5,6 +5,7 @@
#
pbl-y += misc.o
pbl-y += string.o
+pbl-y += malloc.o
pbl-$(CONFIG_HAVE_IMAGE_COMPRESSION) += decomp.o
pbl-$(CONFIG_LIBFDT) += fdt.o
pbl-$(CONFIG_PBL_CONSOLE) += console.o
diff --git a/pbl/malloc.c b/pbl/malloc.c
new file mode 100644
index 0000000000..d5ee8ac7ee
--- /dev/null
+++ b/pbl/malloc.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <pbl.h>
+
+static unsigned long free_mem_ptr;
+static unsigned long free_mem_end_ptr;
+static unsigned long malloc_ptr;
+static int malloc_count;
+
+void *pbl_malloc(int size)
+{
+ void *p;
+
+ if (size < 0)
+ return NULL;
+ if (!malloc_ptr)
+ malloc_ptr = free_mem_ptr;
+
+ malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
+
+ p = (void *)malloc_ptr;
+ malloc_ptr += size;
+
+ if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
+ return NULL;
+
+ malloc_count++;
+ return p;
+}
+
+void pbl_free(void *where)
+{
+ malloc_count--;
+ if (!malloc_count)
+ malloc_ptr = free_mem_ptr;
+}
+
+void pbl_malloc_init(unsigned long base, size_t size)
+{
+ free_mem_ptr = base;
+ malloc_ptr = base;
+ malloc_count = 0;
+ free_mem_end_ptr = base + size;
+}
\ No newline at end of file
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2 1/3] PBL: add common define for early malloc size
2026-05-18 18:32 [PATCH v2 1/3] PBL: add common define for early malloc size Sascha Hauer
2026-05-18 18:32 ` [PATCH v2 2/3] pbl: cleanup early malloc Sascha Hauer
2026-05-18 18:32 ` [PATCH v2 3/3] ARM: rockchip: initialize PBL malloc Sascha Hauer
@ 2026-05-19 5:23 ` Ahmad Fatoum
2026-05-19 6:15 ` Sascha Hauer
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2026-05-19 5:23 UTC (permalink / raw)
To: Sascha Hauer, Barebox List
On 5/18/26 20:32, Sascha Hauer wrote:
> Current all architectures using early malloc in the PBL use a pool size
> of 128KiB. Add a common define for it.
>
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> arch/arm/cpu/common.c | 3 ++-
> arch/arm/cpu/uncompress.c | 2 +-
> arch/arm/include/asm/barebox-arm.h | 2 --
> arch/mips/boot/uncompress.c | 4 ++--
> arch/riscv/include/asm/barebox-riscv.h | 2 +-
> include/pbl.h | 3 +++
> 6 files changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/arch/arm/cpu/common.c b/arch/arm/cpu/common.c
> index ccadb05ed9..6b82ee8b81 100644
> --- a/arch/arm/cpu/common.c
> +++ b/arch/arm/cpu/common.c
> @@ -4,6 +4,7 @@
> #include <common.h>
> #include <init.h>
> #include <elf.h>
> +#include <pbl.h>
> #include <linux/sizes.h>
> #include <asm/system_info.h>
> #include <asm/barebox-arm.h>
> @@ -114,7 +115,7 @@ void print_pbl_mem_layout(ulong membase, ulong endmem, ulong barebox_base)
> printf("arm_mem_barebox_image = 0x%08lx+0x%08lx\n",
> barebox_base, arm_mem_barebox_image_end(endmem) - barebox_base);
> printf("arm_mem_early_malloc = 0x%08lx+0x%08x\n",
> - barebox_base - ARM_MEM_EARLY_MALLOC_SIZE, ARM_MEM_EARLY_MALLOC_SIZE);
> + barebox_base - PBL_MALLOC_SIZE, PBL_MALLOC_SIZE);
> printf("membase = 0x%08lx+0x%08lx\n",
> membase, endmem - membase);
> }
> diff --git a/arch/arm/cpu/uncompress.c b/arch/arm/cpu/uncompress.c
> index 38f7dbc113..4bade72ba4 100644
> --- a/arch/arm/cpu/uncompress.c
> +++ b/arch/arm/cpu/uncompress.c
> @@ -78,7 +78,7 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
>
> handoff_data = (void *)barebox_base + uncompressed_len + MAX_BSS_SIZE;
>
> - free_mem_ptr = barebox_base - ARM_MEM_EARLY_MALLOC_SIZE;
> + free_mem_ptr = barebox_base - PBL_MALLOC_SIZE;
> free_mem_end_ptr = barebox_base;
>
> #ifdef DEBUG
> diff --git a/arch/arm/include/asm/barebox-arm.h b/arch/arm/include/asm/barebox-arm.h
> index 611b2bb2d6..9349e62fde 100644
> --- a/arch/arm/include/asm/barebox-arm.h
> +++ b/arch/arm/include/asm/barebox-arm.h
> @@ -140,8 +140,6 @@ static inline unsigned long arm_mem_ttb(unsigned long endmem)
> return endmem;
> }
>
> -#define ARM_MEM_EARLY_MALLOC_SIZE SZ_128K
> -
> static inline unsigned long arm_mem_ramoops(unsigned long endmem)
> {
> endmem = arm_mem_ttb(endmem);
> diff --git a/arch/mips/boot/uncompress.c b/arch/mips/boot/uncompress.c
> index 0630d03c6b..5c6ba39875 100644
> --- a/arch/mips/boot/uncompress.c
> +++ b/arch/mips/boot/uncompress.c
> @@ -34,8 +34,8 @@ void __section(.text_entry) barebox_pbl_start(void *fdt, void *fdt_end,
> memset(__bss_start, 0, __bss_stop - __bss_start);
>
> /* set 128 KiB at the end of the MALLOC_BASE for early malloc */
> - free_mem_ptr = TEXT_BASE - SZ_128K;
> - free_mem_end_ptr = free_mem_ptr + SZ_128K;
> + free_mem_ptr = TEXT_BASE - PBL_MALLOC_SIZE;
> + free_mem_end_ptr = free_mem_ptr + PBL_MALLOC_SIZE;
>
> piggy_len = (unsigned long)&input_data_end - (unsigned long)&input_data;
>
> diff --git a/arch/riscv/include/asm/barebox-riscv.h b/arch/riscv/include/asm/barebox-riscv.h
> index 083889817b..a3ae07277d 100644
> --- a/arch/riscv/include/asm/barebox-riscv.h
> +++ b/arch/riscv/include/asm/barebox-riscv.h
> @@ -54,7 +54,7 @@ static inline unsigned long riscv_mem_stack(unsigned long membase,
> static inline unsigned long riscv_mem_early_malloc(unsigned long membase,
> unsigned long endmem)
> {
> - return riscv_mem_stack(membase, endmem) - SZ_128K;
> + return riscv_mem_stack(membase, endmem) - PBL_MALLOC_SIZE;
> }
>
> static inline unsigned long riscv_mem_early_malloc_end(unsigned long membase,
> diff --git a/include/pbl.h b/include/pbl.h
> index 514f114370..5a444fb6fe 100644
> --- a/include/pbl.h
> +++ b/include/pbl.h
> @@ -10,6 +10,7 @@
>
> #include <linux/types.h>
> #include <linux/compiler.h>
> +#include <linux/sizes.h>
>
> extern unsigned long free_mem_ptr;
> extern unsigned long free_mem_end_ptr;
> @@ -33,6 +34,8 @@ int pbl_barebox_verify(const void *compressed_start, unsigned int len,
> const void *hash, unsigned int hash_len);
> int pbl_load_fdt(const void *fdt, void *dest, int destsize);
>
> +#define PBL_MALLOC_SIZE SZ_128K
> +
> #endif
>
> void __noreturn barebox_pbl_entry(ulong, ulong, void *);
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
^ permalink raw reply [flat|nested] 5+ messages in thread