Skip to contentSkip to conversation

board_init: Do not reserve MALLOC_F area on stack if non-zero MALLOC_F_ADDR

PATCH In review Marek Vasut 3 participants 5 messages Jun 25, 2022 – Jul 9, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 board_init: Do not reserve MALLOC_F area on stack if non-zero MALLOC_F_ADDR

In case the MALLOC_F_ADDR is set to non-zero value, the early malloc area is not going to be placed just below stack top, but elsewhere. Do not reserve MALLOC_F bytes in this case, as that wastes stack space and may even cause insufficient stack space in SPL.

This functionality is particularly useful on i.MX8M, where the insufficient stack space can be triggered.

Cc Albert ARIBAUD <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Peng Fan <[email protected]>
Cc Simon Glass <[email protected]>
Cc Stefano Babic <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Tom Rini <[email protected]>
---
 common/init/board_init.c | 2 ++
 1 file changed, 2 insertions(+)
common/init/board_init.c +2 -0
index eab5ee13953..6a550261778 100644
@@ -78,8 +78,10 @@ __weak void board_init_f_init_stack_protection(void)
 ulong board_init_f_alloc_reserve(ulong top)
 {
 	/* Reserve early malloc arena */
+#ifndef CONFIG_MALLOC_F_ADDR
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
+#endif
 #endif
 	/* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
 	top = rounddown(top-sizeof(struct global_data), 16);
Signature · 1 line
2.35.1
#1 lore raw

Hi Marek,

Quoted 17 lines · Marek Vasut

In case the MALLOC_F_ADDR is set to non-zero value, the early malloc area is not going to be placed just below stack top, but elsewhere. Do not reserve MALLOC_F bytes in this case, as that wastes stack space and may even cause insufficient stack space in SPL.

This functionality is particularly useful on i.MX8M, where the insufficient stack space can be triggered.

Cc Albert ARIBAUD <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Peng Fan <[email protected]>
Cc Simon Glass <[email protected]>
Cc Stefano Babic <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Tom Rini <[email protected]>
Quoted 3 lines
---
 common/init/board_init.c | 2 ++
 1 file changed, 2 insertions(+)

Please can you migrate the option to Kconfig first? I suspect that will simplify the logic and avoid the #ifndef

Quoted 17 lines
common/init/board_init.c +2 -1
index eab5ee13953..6a550261778 100644
@@ -78,8 +78,10 @@ __weak void board_init_f_init_stack_protection(void)
 ulong board_init_f_alloc_reserve(ulong top)
 {
        /* Reserve early malloc arena */
+#ifndef CONFIG_MALLOC_F_ADDR
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
        top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
+#endif
 #endif
        /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
        top = rounddown(top-sizeof(struct global_data), 16);
--
Quoted 2 lines

2.35.1

Regards,
Simon

#2 lore raw
Quoted 24 lines · Simon Glass

Hi Marek,

Quoted 17 lines · Marek Vasut

In case the MALLOC_F_ADDR is set to non-zero value, the early malloc area is not going to be placed just below stack top, but elsewhere. Do not reserve MALLOC_F bytes in this case, as that wastes stack space and may even cause insufficient stack space in SPL.

This functionality is particularly useful on i.MX8M, where the insufficient stack space can be triggered.

Cc Albert ARIBAUD <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Peng Fan <[email protected]>
Cc Simon Glass <[email protected]>
Cc Stefano Babic <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Tom Rini <[email protected]>
Quoted 3 lines
---
 common/init/board_init.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 2 lines

Please can you migrate the option to Kconfig first? I suspect that will simplify the logic and avoid the #ifndef

That was my first reaction as well. But, I'm not so sure after looking
at things more. An issue is that CONFIG_MALLOC_F_ADDR should probably be CONFIG_SPL_MALLOC_F_ADDR as it's only used in SPL. But it's also not an either/or around CONFIG_SPL_SYS_MALLOC_F_LEN.

It would be good to move to Kconfig (but I also don't see a common default for the handful of SoCs using it), and maybe a !CONFIG_VAL() test instead of ifndef.

Quoted 23 lines
Quoted 18 lines
common/init/board_init.c +2 -1
index eab5ee13953..6a550261778 100644
@@ -78,8 +78,10 @@ __weak void board_init_f_init_stack_protection(void)
 ulong board_init_f_alloc_reserve(ulong top)
 {
        /* Reserve early malloc arena */
+#ifndef CONFIG_MALLOC_F_ADDR
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
        top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
+#endif
 #endif
        /* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
        top = rounddown(top-sizeof(struct global_data), 16);
--
Quoted 2 lines

2.35.1

Regards,
Simon

Signature · 1 line
Tom
#3 lore raw

On 6/30/22 15:36, Tom Rini wrote:

Hi,

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

In case the MALLOC_F_ADDR is set to non-zero value, the early malloc area is not going to be placed just below stack top, but elsewhere. Do not reserve MALLOC_F bytes in this case, as that wastes stack space and may even cause insufficient stack space in SPL.

This functionality is particularly useful on i.MX8M, where the insufficient stack space can be triggered.

[...]

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
  common/init/board_init.c | 2 ++
  1 file changed, 2 insertions(+)

Please can you migrate the option to Kconfig first? I suspect that will simplify the logic and avoid the #ifndef

Quoted 8 lines

That was my first reaction as well. But, I'm not so sure after looking
at things more. An issue is that CONFIG_MALLOC_F_ADDR should probably be CONFIG_SPL_MALLOC_F_ADDR as it's only used in SPL. But it's also not an either/or around CONFIG_SPL_SYS_MALLOC_F_LEN.

It would be good to move to Kconfig (but I also don't see a common default for the handful of SoCs using it), and maybe a !CONFIG_VAL() test instead of ifndef.

See
[PATCH] board_init: Convert CONFIG_MALLOC_F_ADDR to Kconfig

#4 lore raw
In reply to Marek Vasut
Quoted 8 lines · Marek Vasut

In case the MALLOC_F_ADDR is set to non-zero value, the early malloc area is not going to be placed just below stack top, but elsewhere. Do not reserve MALLOC_F bytes in this case, as that wastes stack space and may even cause insufficient stack space in SPL.

This functionality is particularly useful on i.MX8M, where the insufficient stack space can be triggered.

Quoted 8 lines
Cc Albert ARIBAUD <[email protected]>
Cc Fabio Estevam <[email protected]>
Cc Peng Fan <[email protected]>
Cc Simon Glass <[email protected]>
Cc Stefano Babic <[email protected]>
Cc Thomas Chou <[email protected]>
Cc Tom Rini <[email protected]>

Applied to u-boot/next, thanks!

Signature · 1 line
Tom