Skip to contentSkip to conversation

spl: full-featured heap cleanups

PATCH Superseded Simon Goldschmidt 4 participants 12 messages Mar 11, 2019 – Mar 19, 2019 Arm (32-bit)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover spl: full-featured heap cleanups

Some platforms cannot use simple malloc even in very early stages, e.g. when using FAT before DRAM is available. Such platforms currently often use non-Kconfig defines to initialize full malloc and rely on simple heap before that.

This series makes some adjustments to ensure SPL behaves the same with simple or full malloc: when CONFIG_SPL_SYS_MALLOC_F_LEN is != 0, both heap types can be used (by changing CONFIG_SPL_SYS_MALLOC_SIMPLE), without manually supplying an address range for the full heap.

Simon Goldschmidt (6):
spl: add Kconfig option to clear bss early
spl: arm: implement SPL_CLEAR_BSS_F
dlmalloc: fix malloc range at end of ram
dlmalloc: be compatible to tiny printf
spl: support using full malloc with SYS_MALLOC_F_LEN
arm: socfpga: a10: move SPL stack size to Kconfig

 arch/arm/lib/crt0.S               | 22 ++++++++++++++++++++++
 common/dlmalloc.c                 |  6 +++++-
 common/spl/Kconfig                | 11 +++++++++++
 common/spl/spl.c                  |  5 +++++
 configs/socfpga_arria10_defconfig |  1 +
 include/configs/socfpga_common.h  | 14 --------------
 6 files changed, 44 insertions(+), 15 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/6 spl: add Kconfig option to clear bss early

This introduces a new Kconfig option SPL_CLEAR_BSS_F. If enabled, it clears the bss before calling board_init_f() instead of clearing it before calling board_init_r().

This also ensures that variables placed in BSS can be shared between board_init_f() and board_init_r() in SPL.

---

 common/spl/Kconfig | 11 +++++++++++
 1 file changed, 11 insertions(+)
common/spl/Kconfig +11 -0
index 206c24076d..5da8697994 100644
@@ -156,6 +156,17 @@ config SPL_STACK_R_MALLOC_SIMPLE_LEN
 	  to give board_init_r() a larger heap then the initial heap in
 	  SRAM which is limited to SYS_MALLOC_F_LEN bytes.
 
+config SPL_CLEAR_BSS_F
+	bool "Clear BSS section before calling board_init_f"
+	help
+	  The BSS section is initialized to zero. In SPL, this is normally done
+	  before calling board_init_r().
+	  For platforms using BSS in board_init_f() already, enable this to
+	  clear the BSS section before calling board_init_f() instead of
+	  clearing it before calling board_init_r(). This also ensures that
+	  variables placed in BSS can be shared between board_init_f() and
+	  board_init_r().
+
 config SPL_SEPARATE_BSS
 	bool "BSS section is in a different memory region from text"
 	help
Signature · 1 line
2.17.1
#2 lore raw

PATCH 1/6 spl: add Kconfig option to clear bss early

Hi Simon,

Quoted 22 lines · Simon Goldschmidt

This introduces a new Kconfig option SPL_CLEAR_BSS_F. If enabled, it clears the bss before calling board_init_f() instead of clearing it before calling board_init_r().

This also ensures that variables placed in BSS can be shared between board_init_f() and board_init_r() in SPL.

---

  common/spl/Kconfig | 11 +++++++++++
  1 file changed, 11 insertions(+)
common/spl/Kconfig +1 -0
index 206c24076d..5da8697994 100644
@@ -156,6 +156,17 @@ config SPL_STACK_R_MALLOC_SIMPLE_LEN
  	  to give board_init_r() a larger heap then the initial heap in
  	  SRAM which is limited to SYS_MALLOC_F_LEN bytes.
 
+config SPL_CLEAR_BSS_F

common/spl/Kconfig config SPL_STACK_R_MALLOC_SIMPLE_LEN · line 169

Quoted 10 lines
common/spl/Kconfig +10 -0
+	bool "Clear BSS section before calling board_init_f"
+	help
+	  The BSS section is initialized to zero. In SPL, this is normally done
+	  before calling board_init_r().
+	  For platforms using BSS in board_init_f() already, enable this to
+	  clear the BSS section before calling board_init_f() instead of
+	  clearing it before calling board_init_r(). This also ensures that
+	  variables placed in BSS can be shared between board_init_f() and
+	  board_init_r().
+

You should probably depend this option on "ARM", as your implementation is currently only for this arch. Otherwise users might expect this to work on other platforms as well.

Thanks,
Stefan

#3 lore raw

PATCH 1/6 spl: add Kconfig option to clear bss early

Am 12.03.2019 um 07:02 schrieb Stefan Roese:

Quoted 24 lines

Hi Simon,

Quoted 21 lines · Simon Goldschmidt

This introduces a new Kconfig option SPL_CLEAR_BSS_F. If enabled, it clears the bss before calling board_init_f() instead of clearing it before calling board_init_r().

This also ensures that variables placed in BSS can be shared between board_init_f() and board_init_r() in SPL.

---

   common/spl/Kconfig | 11 +++++++++++
   1 file changed, 11 insertions(+)
common/spl/Kconfig +0 -0
index 206c24076d..5da8697994 100644
@@ -156,6 +156,17 @@ config SPL_STACK_R_MALLOC_SIMPLE_LEN
   	  to give board_init_r() a larger heap then the initial heap in
   	  SRAM which is limited to SYS_MALLOC_F_LEN bytes.
 
Quoted 15 lines

common/spl/Kconfig config SPL_STACK_R_MALLOC_SIMPLE_LEN · line 169

Quoted 11 lines
common/spl/Kconfig +11 -0
+config SPL_CLEAR_BSS_F
+	bool "Clear BSS section before calling board_init_f"
+	help
+	  The BSS section is initialized to zero. In SPL, this is normally done
+	  before calling board_init_r().
+	  For platforms using BSS in board_init_f() already, enable this to
+	  clear the BSS section before calling board_init_f() instead of
+	  clearing it before calling board_init_r(). This also ensures that
+	  variables placed in BSS can be shared between board_init_f() and
+	  board_init_r().
+

You should probably depend this option on "ARM", as your implementation is currently only for this arch. Otherwise users might expect this to work on other platforms as well.

Hmm, ok. I thought it might be worth leaving this visible for everyone. But you're right that people migh rely on this to work where it's not implemented...

I'll add that dependency in v2.

Regards,
Simon

#4 lore raw

PATCH 2/6 spl: arm: implement SPL_CLEAR_BSS_F

In reply to Simon Goldschmidt

This implements the new option to clear BSS early in SPL for standard arm crt0.

BSS is cleared before calling board_init_f() and thus not cleared before calling board_init_r() as it is not relocated in SPL.

---

 arch/arm/lib/crt0.S | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/arm/lib/crt0.S +22 -0
index fe312db690..b06e54e144 100644
@@ -80,6 +80,26 @@ ENTRY(_main)
 	mov	r9, r0
 	bl	board_init_f_init_reserve
 
+#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_CLEAR_BSS_F)
+	ldr	r0, =__bss_start
+
+#ifdef CONFIG_USE_ARCH_MEMSET
+	ldr	r3, =__bss_end
+	mov	r1, #0x00000000		/* prepare zero to clear BSS */
+
+	subs	r2, r3, r0		/* r2 = memset len */
+	bl	memset
+#else
+	ldr	r1, =__bss_end
+	mov	r2, #0x00000000		/* prepare zero to clear BSS */
+
+clbss_l:cmp	r0, r1			/* while not at end of BSS */
+	strlo	r2, [r0]		/* clear 32-bit BSS word */
+	addlo	r0, r0, #4		/* move to next */
+	blo	clbss_l
+#endif
+#endif
+
 	mov	r0, #0
 	bl	board_init_f
 
@@ -124,6 +144,7 @@ here:
 	movne	sp, r0
 	movne	r9, r0
 # endif
+#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_CLEAR_BSS_F)
 	ldr	r0, =__bss_start	/* this is auto-relocated! */
 
 #ifdef CONFIG_USE_ARCH_MEMSET
@@ -141,6 +162,7 @@ clbss_l:cmp	r0, r1			/* while not at end of BSS */
 	addlo	r0, r0, #4		/* move to next */
 	blo	clbss_l
 #endif
+#endif
 
 #if ! defined(CONFIG_SPL_BUILD)
 	bl coloured_LED_init
Signature · 1 line
2.17.1
#5 lore raw

PATCH 3/6 dlmalloc: fix malloc range at end of ram

In reply to Simon Goldschmidt

If the malloc range passed to mem_malloc_init() is at the end of address range and 'start + size' overflows to 0, following allocations fail as mem_malloc_end is zero (which looks like uninitialized).

Fix this by subtracting 1 of 'start + size' overflows to zero.

---

 common/dlmalloc.c | 4 ++++
 1 file changed, 4 insertions(+)
common/dlmalloc.c +4 -0
index edaad299bb..51d3bd671a 100644
@@ -603,6 +603,10 @@ void mem_malloc_init(ulong start, ulong size)
 	mem_malloc_start = start;
 	mem_malloc_end = start + size;
 	mem_malloc_brk = start;
+	if (start && size && !mem_malloc_end) {
+		/* overflow: malloc area is at end of address range */
+		mem_malloc_end--;
+	}
 
 	debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
 	      mem_malloc_end);
Signature · 1 line
2.17.1
#6 lore raw

PATCH 4/6 dlmalloc: be compatible to tiny printf

In reply to Simon Goldschmidt

Convert debug output from '%#lx' to '0x%lx' to be compatible with tiny printf used in SPL.

---

 common/dlmalloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/dlmalloc.c +1 -1
index 51d3bd671a..af6f43dcc9 100644
@@ -608,7 +608,7 @@ void mem_malloc_init(ulong start, ulong size)
 		mem_malloc_end--;
 	}
 
-	debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
+	debug("using memory 0x%lx-0x%lx for malloc()\n", mem_malloc_start,
 	      mem_malloc_end);
 #ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
 	memset((void *)mem_malloc_start, 0x0, size);
Signature · 1 line
2.17.1
#7 lore raw

PATCH 5/6 spl: support using full malloc with SYS_MALLOC_F_LEN

In reply to Simon Goldschmidt

Some platforms (like socfpga A10) need a big hep before SDRAM is available (e.g. because FAT is used). For such platforms, simple_malloc is often not a good option as it does not support freeing memory. These platforms often use the non-Kconfig defines CONFIG_SYS_SPL_MALLOC_START (and its SIZE).

This patch allows enabling CONFIG_SPL_SYS_MALLOC_F_LEN while leaving CONFIG_SPL_SYS_MALLOC_SIMPLE disabled. In this case, the full malloc heap is made available as early as the simple_malloc heap would be normally.

This way, platforms can drop the non-Kconfig options to set up the full heap and rely on the same automatically calculated heap allocation used for simple heap.

---

 common/spl/spl.c | 5 +++++
 1 file changed, 5 insertions(+)
common/spl/spl.c +5 -0
index 88d4b8a9bf..b89340eb27 100644
@@ -383,8 +383,13 @@ static int spl_common_init(bool setup_malloc)
 #ifdef CONFIG_MALLOC_F_ADDR
 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
 #endif
+#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
 		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
 		gd->malloc_ptr = 0;
+#else
+		mem_malloc_init(gd->malloc_base, CONFIG_VAL(SYS_MALLOC_F_LEN));
+		gd->flags |= GD_FLG_FULL_MALLOC_INIT;
+#endif
 	}
 #endif
 	ret = bootstage_init(true);
Signature · 1 line
2.17.1
#8 lore raw

PATCH 5/6 spl: support using full malloc with SYS_MALLOC_F_LEN

Hi Simon,

Quoted 25 lines · Simon Goldschmidt

Some platforms (like socfpga A10) need a big hep before SDRAM is available (e.g. because FAT is used). For such platforms, simple_malloc is often not a good option as it does not support freeing memory. These platforms often use the non-Kconfig defines CONFIG_SYS_SPL_MALLOC_START (and its SIZE).

This patch allows enabling CONFIG_SPL_SYS_MALLOC_F_LEN while leaving CONFIG_SPL_SYS_MALLOC_SIMPLE disabled. In this case, the full malloc heap is made available as early as the simple_malloc heap would be normally.

This way, platforms can drop the non-Kconfig options to set up the full heap and rely on the same automatically calculated heap allocation used for simple heap.

---

 common/spl/spl.c | 5 +++++
 1 file changed, 5 insertions(+)
common/spl/spl.c +0 -0
index 88d4b8a9bf..b89340eb27 100644

common/spl/spl.c static int spl_common_init(bool setup_malloc) · line 386

Quoted 4 lines
common/spl/spl.c +1 -0
@@ -383,8 +383,13 @@ static int spl_common_init(bool setup_malloc)
 #ifdef CONFIG_MALLOC_F_ADDR
                gd->malloc_base = CONFIG_MALLOC_F_ADDR;
 #endif
+#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)

Can we use if() instead of #if here?

Quoted 10 lines
common/spl/spl.c +4 -1
                gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
                gd->malloc_ptr = 0;
+#else
+               mem_malloc_init(gd->malloc_base, CONFIG_VAL(SYS_MALLOC_F_LEN));
+               gd->flags |= GD_FLG_FULL_MALLOC_INIT;
+#endif
        }
 #endif
        ret = bootstage_init(true);
--
Quoted 2 lines

2.17.1

Also I feel some updates should be made to the README, or perhaps Kconfig help.

Regards,
Simon

#9 lore raw

PATCH 5/6 spl: support using full malloc with SYS_MALLOC_F_LEN

Hi Simon,

you were replying to v1 where v2 has already been sent. However, this patch hasn't changed, so I'm commenting here.

Am 19.03.2019 um 02:24 schrieb Simon Glass:

Quoted 24 lines

Hi Simon,

Quoted 20 lines · Simon Goldschmidt

Some platforms (like socfpga A10) need a big hep before SDRAM is available (e.g. because FAT is used). For such platforms, simple_malloc is often not a good option as it does not support freeing memory. These platforms often use the non-Kconfig defines CONFIG_SYS_SPL_MALLOC_START (and its SIZE).

This patch allows enabling CONFIG_SPL_SYS_MALLOC_F_LEN while leaving CONFIG_SPL_SYS_MALLOC_SIMPLE disabled. In this case, the full malloc heap is made available as early as the simple_malloc heap would be normally.

This way, platforms can drop the non-Kconfig options to set up the full heap and rely on the same automatically calculated heap allocation used for simple heap.

---

  common/spl/spl.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 11 lines

common/spl/spl.c static int spl_common_init(bool setup_malloc) · line 386

Quoted 9 lines
common/spl/spl.c +1 -0
index 88d4b8a9bf..b89340eb27 100644
@@ -383,8 +383,13 @@ static int spl_common_init(bool setup_malloc)
  #ifdef CONFIG_MALLOC_F_ADDR
                 gd->malloc_base = CONFIG_MALLOC_F_ADDR;
  #endif
+#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)

Can we use if() instead of #if here?

Yes, that should work. Noted for v3.

Quoted 15 lines
Quoted 11 lines
common/spl/spl.c +4 -1
                 gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
                 gd->malloc_ptr = 0;
+#else
+               mem_malloc_init(gd->malloc_base, CONFIG_VAL(SYS_MALLOC_F_LEN));
+               gd->flags |= GD_FLG_FULL_MALLOC_INIT;
+#endif
         }
  #endif
         ret = bootstage_init(true);
--
Quoted 2 lines

2.17.1

Also I feel some updates should be made to the README, or perhaps Kconfig help.

Yes, you're right. Honestly, I didn't want to go through all the documentation mess unless getting at least some slightly positive feedback to this. Searching the readme files and Kconfig help to get them consistent is probably more work than coding these patches :-)

Regards,
Simon

#10 lore raw

PATCH 6/6 arm: socfpga: a10: move SPL stack size to Kconfig

In reply to Simon Goldschmidt

Instead of fixing the SPL stack to 64 KiB in the board config header via CONFIG_SYS_SPL_MALLOC_SIZE, let's just use CONFIG_SPL_SYS_MALLOC_F_LEN in the defconfig.

This also has the advandage that it removes sub-mach specific ifdefs in socfpga_common.h.

---

This patch was part of an socfpga series before. It didn't work there and was the reason for this series. It also serves as a demonstration of this series.

---
 configs/socfpga_arria10_defconfig |  1 +
 include/configs/socfpga_common.h  | 14 --------------
 2 files changed, 1 insertion(+), 14 deletions(-)
configs/socfpga_arria10_defconfig +1 -0
index f321a0ac3b..8d0479cc05 100644
@@ -2,6 +2,7 @@ CONFIG_ARM=y
 CONFIG_ARCH_SOCFPGA=y
 CONFIG_SYS_TEXT_BASE=0x01000040
 CONFIG_SYS_MALLOC_F_LEN=0x2000
+CONFIG_SPL_SYS_MALLOC_F_LEN=0x10000
 CONFIG_TARGET_SOCFPGA_ARRIA10_SOCDK=y
 CONFIG_SPL=y
 CONFIG_IDENT_STRING="socfpga_arria10"
include/configs/socfpga_common.h +0 -14
index 181af9b646..16c83900c3 100644
@@ -251,16 +251,6 @@ unsigned int cm_get_qspi_controller_clk_hz(void);
 #define CONFIG_SPL_TEXT_BASE		CONFIG_SYS_INIT_RAM_ADDR
 #define CONFIG_SPL_MAX_SIZE		CONFIG_SYS_INIT_RAM_SIZE
 
-#if defined(CONFIG_TARGET_SOCFPGA_ARRIA10)
-/* SPL memory allocation configuration, this is for FAT implementation */
-#ifndef CONFIG_SYS_SPL_MALLOC_START
-#define CONFIG_SYS_SPL_MALLOC_SIZE	0x00010000
-#define CONFIG_SYS_SPL_MALLOC_START	(CONFIG_SYS_INIT_RAM_SIZE - \
-					 CONFIG_SYS_SPL_MALLOC_SIZE + \
-					 CONFIG_SYS_INIT_RAM_ADDR)
-#endif
-#endif
-
 /* SPL SDMMC boot support */
 #ifdef CONFIG_SPL_MMC_SUPPORT
 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
@@ -294,11 +284,7 @@ unsigned int cm_get_qspi_controller_clk_hz(void);
 /*
  * Stack setup
  */
-#if defined(CONFIG_TARGET_SOCFPGA_GEN5)
 #define CONFIG_SPL_STACK		CONFIG_SYS_INIT_SP_ADDR
-#elif defined(CONFIG_TARGET_SOCFPGA_ARRIA10)
-#define CONFIG_SPL_STACK		CONFIG_SYS_SPL_MALLOC_START
-#endif
 
 /* Extra Environment */
 #ifndef CONFIG_SPL_BUILD
Signature · 1 line
2.17.1
#11 lore raw

PATCH 6/6 arm: socfpga: a10: move SPL stack size to Kconfig

Quoted 10 lines · Simon Goldschmidt

Instead of fixing the SPL stack to 64 KiB in the board config header via
CONFIG_SYS_SPL_MALLOC_SIZE, let's just use CONFIG_SPL_SYS_MALLOC_F_LEN
in the defconfig.

This also has the advandage that it removes sub-mach specific ifdefs in
socfpga_common.h.

Quoted 8 lines

---

This patch was part of an socfpga series before. It didn't work there and
was the reason for this series. It also serves as a demonstration of this
series.

This patch breaking my series of A10 patches.

No SPL print out from the terminal console. I would help to debug in later.

Thanks.
TF

Quoted 57 lines

---
 configs/socfpga_arria10_defconfig |  1 + include/configs/socfpga_common.h  | 14 --------------

 2 files changed, 1 insertion(+), 14 deletions(-)
configs/socfpga_arria10_defconfig +0 -0
index f321a0ac3b..8d0479cc05 100644
@@ -2,6 +2,7 @@ CONFIG_ARM=y

 CONFIG_ARCH_SOCFPGA=y
 CONFIG_SYS_TEXT_BASE=0x01000040
 CONFIG_SYS_MALLOC_F_LEN=0x2000
+CONFIG_SPL_SYS_MALLOC_F_LEN=0x10000
 CONFIG_TARGET_SOCFPGA_ARRIA10_SOCDK=y
 CONFIG_SPL=y
 CONFIG_IDENT_STRING="socfpga_arria10"

include/configs/socfpga_common.h +0 -0
index 181af9b646..16c83900c3 100644
@@ -251,16 +251,6 @@ unsigned int
 cm_get_qspi_controller_clk_hz(void);
  #define CONFIG_SPL_TEXT_BASE		CONFIG_SYS_INIT_RAM_ADDR
  #define CONFIG_SPL_MAX_SIZE		CONFIG_SYS_INIT_RAM_SIZE

include/configs/socfpga_common.h +0 -2
-#if defined(CONFIG_TARGET_SOCFPGA_ARRIA10)
-/* SPL memory allocation configuration, this is for FAT

implementation */

include/configs/socfpga_common.h +1 -9
-#ifndef CONFIG_SYS_SPL_MALLOC_START
-#define CONFIG_SYS_SPL_MALLOC_SIZE	0x00010000
-#define CONFIG_SYS_SPL_MALLOC_START	(CONFIG_SYS_INIT_RAM_SIZE
- \
-					 CONFIG_SYS_SPL_MALLOC_SIZE
+ \
-					 CONFIG_SYS_INIT_RAM_ADDR)
-#endif
-#endif
-

 /* SPL SDMMC boot support */
 #ifdef CONFIG_SPL_MMC_SUPPORT
 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)

include/configs/socfpga_common.h +0 -4
@@ -294,11 +284,7 @@ unsigned int
 cm_get_qspi_controller_clk_hz(void);
  /*
   * Stack setup
   */
-#if defined(CONFIG_TARGET_SOCFPGA_GEN5)
  #define CONFIG_SPL_STACK		CONFIG_SYS_INIT_SP_ADDR
-#elif defined(CONFIG_TARGET_SOCFPGA_ARRIA10)
-#define CONFIG_SPL_STACK		CONFIG_SYS_SPL_MALLOC_START
-#endif


 /* Extra Environment */
 #ifndef CONFIG_SPL_BUILD