Skip to contentSkip to conversation

lmb: Add config for the number of memory and reserved regions

PATCH New Patrick Delaunay 2 participants 17 messages Mar 10, 2021 – Apr 23, 2021

Subsystems (12)
lore
Share & tools
lore
#0 lore raw

v2 cover lmb: Add config for the number of memory and reserved regions

Hi,

It is the V2 of [1]

I propose a update of the lmb library to allow the configuration with Kconfig of the number of memory regions and of reserved regions in lmb library with:
- CONFIG_LMB_RESERVED_REGIONS
- CONFIG_LMB_MEMORY_REGIONS

This serie avoids issue on stm32mp15 platform,
as the kernel device tree defines many and non contiguous reserved regions.

It is based on latest memblock library in kernel (it was a preliminary step for dynamic allocation of regions array to avoid any static limitation).

PS: in patch 1, I manage CONFIG_LMB dependency in lib/Kconfig file, with
default y depends on ...
but it can do also in each arch by an imply or a select as it is done in [2].
I am not sure what is the best solution for this dependency.

In this V2 series, I keep the default value of 8 regions defined in struct lmb_region with:
- CONFIG_LMB_USE_MAX_REGIONS = n
- CONFIG_LMB_MAX_REGIONS = 8

When the array is located in struct lmb_region (with CONFIG_LMB_USE_MAX_REGIONS = n), the compiler do some optimization useful for size-constraint target, for example the nokia_rx51 target increases by 152 bytes.

arm: (for 3/3 boards) all +50.7 bss +2.7 text +48.0
nokia_rx51 : all +152 bss +8 text +144
u-boot: add: 2/0, grow: 5/-2 bytes: 192/-40 (152)
function old new delta
lmb_remove_region - 96 +96
lmb_init - 44 +44
lmb_add_region 416 440 +24
__lmb_alloc_base 224 236 +12
images 288 296 +8
lmb_overlaps_region 80 84 +4
lmb_alloc_addr 108 112 +4
lmb_init_and_reserve_range 56 36 -20
lmb_init_and_reserve 100 80 -20

But it is not the case for all platform (STM32MP15 for example).

To optimize this size impact, I reduce the the regions array size and add a test on overflow management in the lmb, but it is not enough.

So I try to optimized the lmb code to avoid the size impact when CONFIG_LMB_USE_MAX_REGIONS = y but it is not working and I prefer kept a code align with Linux kernel memblock library and kept the current behavior a this.

With v2 the CI/CD build is now OK [3]

[1] lmb: Add config for the number of memory and reserved regions
http://patchwork.ozlabs.org/project/uboot/list/?series=227570&state=*

[2] arch: Move NEEDS_MANUAL_RELOC symbol to Kconfig
http://patchwork.ozlabs.org/project/uboot/list/?series=212154&state=*

[3] https://source.denx.de/u-boot/custodians/u-boot-stm/-/pipelines/6616

Changes in v2:
- add a max parameter extracted from v1 [3/5]
- new in v2: introduced MAX_LMB_REGIONS
- new in V2: reduce size impact by correct regions array size
- new in V2: add test for overflow protection in lmb libary
- rework v1 [4/5] to keep current optimization by default when CONFIG_LMB_USE_MAX_REGIONS=n
- Add CONFIG_LMB_USE_MAX_REGIONS=n and reduce LMB_MEMORY_REGIONS to reduce the used size in stack

Patrick Delaunay (8):
lmb: move CONFIG_LMB in Kconfig
lmb: remove lmb_region.size
lmb: add a max parameter in the struct lmb_region
lmb: move MAX_LMB_REGIONS value in Kconfig
lmb: correct size of the regions array
test: lmb: add test for overflow protection in lmb_add_region
lmb: Add 2 config to define the max number of regions
configs: stm32mp15: increase the number of reserved memory region in
lmb

 arch/arc/include/asm/config.h        |  2 -
 arch/arm/include/asm/config.h        |  1 -
 arch/m68k/include/asm/config.h       |  1 -
 arch/microblaze/include/asm/config.h |  2 -
 arch/mips/include/asm/config.h       |  1 -
 arch/nds32/include/asm/config.h      |  1 -
 arch/powerpc/include/asm/config.h    |  1 -
 arch/riscv/include/asm/config.h      |  1 -
 arch/sh/include/asm/config.h         |  2 -
 arch/x86/include/asm/config.h        |  1 -
 arch/xtensa/include/asm/config.h     |  2 -
 configs/stm32mp15_basic_defconfig    |  3 ++
 configs/stm32mp15_trusted_defconfig  |  3 ++
 include/configs/10m50_devboard.h     |  5 ---
 include/configs/3c120_devboard.h     |  5 ---
 include/configs/sandbox.h            |  2 -
 include/configs/x86-common.h         |  2 -
 include/image.h                      |  2 +-
 include/lmb.h                        | 40 +++++++++++++++--
 lib/Kconfig                          | 44 ++++++++++++++++++-
 lib/lmb.c                            | 17 +++++---
 scripts/config_whitelist.txt         |  1 -
 test/lib/lmb.c                       | 64 ++++++++++++++++++++++++++++
 23 files changed, 160 insertions(+), 43 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/8 lmb: move CONFIG_LMB in Kconfig

Migrate CONFIG_LMB in Kconfig.

---

(no changes since v1)

 arch/arc/include/asm/config.h        | 2 --
 arch/arm/include/asm/config.h        | 1 -
 arch/m68k/include/asm/config.h       | 1 -
 arch/microblaze/include/asm/config.h | 2 --
 arch/mips/include/asm/config.h       | 1 -
 arch/nds32/include/asm/config.h      | 1 -
 arch/powerpc/include/asm/config.h    | 1 -
 arch/riscv/include/asm/config.h      | 1 -
 arch/sh/include/asm/config.h         | 2 --
 arch/x86/include/asm/config.h        | 1 -
 arch/xtensa/include/asm/config.h     | 2 --
 include/configs/10m50_devboard.h     | 5 -----
 include/configs/3c120_devboard.h     | 5 -----
 include/configs/sandbox.h            | 2 --
 include/configs/x86-common.h         | 2 --
 include/image.h                      | 2 +-
 lib/Kconfig                          | 7 +++++++
 scripts/config_whitelist.txt         | 1 -
 18 files changed, 8 insertions(+), 31 deletions(-)
arch/arc/include/asm/config.h +0 -2
index d88c361488..46e94be141 100644
@@ -8,6 +8,4 @@
 
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
-#define CONFIG_LMB
-
 #endif /*__ASM_ARC_CONFIG_H_ */
arch/arm/include/asm/config.h +0 -1
index bf692ce279..14860d89b6 100644
@@ -6,7 +6,6 @@
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
 
-#define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #if defined(CONFIG_ARCH_LS1021A) || \
arch/m68k/include/asm/config.h +0 -1
index 7ea443673a..221eb93d58 100644
@@ -6,7 +6,6 @@
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
 
-#define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #endif
arch/microblaze/include/asm/config.h +0 -2
index 3ae71b3e01..221eb93d58 100644
@@ -6,8 +6,6 @@
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
 
-#define CONFIG_LMB
-
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #endif
arch/mips/include/asm/config.h +0 -1
index 7ea443673a..221eb93d58 100644
@@ -6,7 +6,6 @@
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
 
-#define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #endif
arch/nds32/include/asm/config.h +0 -1
index 8964a58f27..6c1cbce7ef 100644
@@ -7,6 +7,5 @@
 
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
-#define CONFIG_LMB
 
 #endif
arch/powerpc/include/asm/config.h +0 -1
index c9c9964630..99b410dc9b 100644
@@ -18,7 +18,6 @@
   #define HWCONFIG_BUFFER_SIZE 256
 #endif
 
-#define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #ifndef CONFIG_MAX_MEM_MAPPED
arch/riscv/include/asm/config.h +0 -1
index 156cb94dc0..d911007537 100644
@@ -7,7 +7,6 @@
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
 
-#define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #endif
arch/sh/include/asm/config.h +0 -2
index e1cd322152..406156dff5 100644
@@ -8,8 +8,6 @@
 
 #include <asm/processor.h>
 
-#define CONFIG_LMB
-
 /* Timer */
 #define CONFIG_SYS_TIMER_COUNTS_DOWN
 #define CONFIG_SYS_TIMER_COUNTER	(TMU_BASE + 0xc)	/* TCNT0 */
arch/x86/include/asm/config.h +0 -1
index 7ea443673a..221eb93d58 100644
@@ -6,7 +6,6 @@
 #ifndef _ASM_CONFIG_H_
 #define _ASM_CONFIG_H_
 
-#define CONFIG_LMB
 #define CONFIG_SYS_BOOT_RAMDISK_HIGH
 
 #endif
arch/xtensa/include/asm/config.h +0 -2
index 5a95fc93f7..a1096ab196 100644
@@ -9,8 +9,6 @@
 
 #include <asm/arch/core.h>
 
-#define CONFIG_LMB
-
 /*
  * Make boot parameters available in the MMUv2 virtual memory layout by
  * restricting used physical memory to the first 128MB.
include/configs/10m50_devboard.h +0 -5
index 768b4a6dfc..3ffc744928 100644
@@ -34,11 +34,6 @@
  */
 #define CONFIG_BOOTP_BOOTFILESIZE
 
-/*
- * FDT options
- */
-#define CONFIG_LMB
-
 /*
  * MEMORY ORGANIZATION
  * -Monitor at top of sdram.
include/configs/3c120_devboard.h +0 -5
index 30bbd716b2..3f065ff315 100644
@@ -34,11 +34,6 @@
  */
 #define CONFIG_BOOTP_BOOTFILESIZE
 
-/*
- * FDT options
- */
-#define CONFIG_LMB
-
 /*
  * MEMORY ORGANIZATION
  * -Monitor at top of sdram.
include/configs/sandbox.h +0 -2
index e0708fe573..6abb78611a 100644
@@ -22,8 +22,6 @@
 #define CONFIG_SYS_TIMER_RATE		1000000
 #endif
 
-#define CONFIG_LMB
-
 #define CONFIG_HOST_MAX_DEVICES 4
 
 /*
include/configs/x86-common.h +0 -2
index 641ed2c5ec..f186dd6b82 100644
@@ -16,8 +16,6 @@
  */
 #define CONFIG_PHYSMEM
 
-#define CONFIG_LMB
-
 #define CONFIG_SYS_BOOTM_LEN		(16 << 20)
 
 /* SATA AHCI storage */
include/image.h +1 -1
index 138c83dd28..a8ca76b21f 100644
@@ -434,7 +434,7 @@ typedef struct bootm_headers {
 #define	BOOTM_STATE_OS_GO	(0x00000400)
 	int		state;
 
-#ifdef CONFIG_LMB
+#if defined(CONFIG_LMB) && !defined(USE_HOSTCC)
 	struct lmb	lmb;		/* for memory mgmt */
 #endif
 } bootm_headers_t;
lib/Kconfig +7 -0
index b35a71ac36..43bd7190b9 100644
@@ -699,6 +699,13 @@ config LIB_ELF
 	  Support basic elf loading/validating functions.
 	  This supports for 32 bit and 64 bit versions.
 
+config LMB
+	bool "Enable the logical memory blocks library (lmb)"
+	default y if ARC || ARM || M68K || MICROBLAZE || MIPS || NDS32 || \
+		     NIOS2 || PPC || RISCV || SANDBOX || SH || X86 || XTENSA
+	help
+	  Support the library logical memory blocks.
+
 endmenu
 
 config PHANDLE_CHECK_SEQ
scripts/config_whitelist.txt +0 -1
index 7340e95bd4..d464f04b6b 100644
@@ -954,7 +954,6 @@ CONFIG_LEGACY_BOOTCMD_ENV
 CONFIG_LINUX
 CONFIG_LINUX_RESET_VEC
 CONFIG_LITTLETON_LCD
-CONFIG_LMB
 CONFIG_LMS283GF05
 CONFIG_LOADADDR
 CONFIG_LOADCMD
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 1/8 lmb: move CONFIG_LMB in Kconfig

Quoted 3 lines · Patrick Delaunay

Migrate CONFIG_LMB in Kconfig.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/87b45b3a/attachment.sig>
#3 lore raw

PATCH v2 2/8 lmb: remove lmb_region.size

In reply to Patrick Delaunay

Remove the unused field size of struct lmb_region as it is initialized to 0 and never used after in lmb library.

See Linux kernel commit 4734b594c6ca ("memblock: Remove memblock_type.size and add memblock.memory_size instead")

---

(no changes since v1)

 include/lmb.h | 1 -
 lib/lmb.c     | 6 ------
 2 files changed, 7 deletions(-)
include/lmb.h +0 -1
index e9f19b16ea..a3247544c1 100644
@@ -21,7 +21,6 @@ struct lmb_property {
 
 struct lmb_region {
 	unsigned long cnt;
-	phys_size_t size;
 	struct lmb_property region[MAX_LMB_REGIONS+1];
 };
 
lib/lmb.c +0 -6
index d126f8dc04..5cf419f439 100644
@@ -20,8 +20,6 @@ void lmb_dump_all_force(struct lmb *lmb)
 
 	printf("lmb_dump_all:\n");
 	printf("    memory.cnt		   = 0x%lx\n", lmb->memory.cnt);
-	printf("    memory.size		   = 0x%llx\n",
-	       (unsigned long long)lmb->memory.size);
 	for (i = 0; i < lmb->memory.cnt; i++) {
 		printf("    memory.reg[0x%lx].base   = 0x%llx\n", i,
 		       (unsigned long long)lmb->memory.region[i].base);
@@ -30,8 +28,6 @@ void lmb_dump_all_force(struct lmb *lmb)
 	}
 
 	printf("\n    reserved.cnt	   = 0x%lx\n", lmb->reserved.cnt);
-	printf("    reserved.size	   = 0x%llx\n",
-	       (unsigned long long)lmb->reserved.size);
 	for (i = 0; i < lmb->reserved.cnt; i++) {
 		printf("    reserved.reg[0x%lx].base = 0x%llx\n", i,
 		       (unsigned long long)lmb->reserved.region[i].base);
@@ -100,9 +96,7 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 void lmb_init(struct lmb *lmb)
 {
 	lmb->memory.cnt = 0;
-	lmb->memory.size = 0;
 	lmb->reserved.cnt = 0;
-	lmb->reserved.size = 0;
 }
 
 static void lmb_reserve_common(struct lmb *lmb, void *fdt_blob)
Signature · 1 line
2.17.1
#4 lore raw

PATCH v2 2/8 lmb: remove lmb_region.size

Quoted 7 lines · Patrick Delaunay

Remove the unused field size of struct lmb_region as it is initialized to 0 and never used after in lmb library.

See Linux kernel commit 4734b594c6ca ("memblock: Remove memblock_type.size and add memblock.memory_size instead")

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/1cb1fe1d/attachment.sig>
#5 lore raw

PATCH v2 3/8 lmb: add a max parameter in the struct lmb_region

In reply to Patrick Delaunay

Add a max parameter in lmb_region struct to handle test in lmb_add_region without using the MAX_LMB_REGIONS define.

This patch allows to modify these size independently for memory of reserved regions in the next patches.

---

Changes in v2:
- add a max parameter extracted from v1 [3/5]

 include/lmb.h | 1 +
 lib/lmb.c     | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)
include/lmb.h +1 -0
index a3247544c1..f3397c7f73 100644
@@ -21,6 +21,7 @@ struct lmb_property {
 
 struct lmb_region {
 	unsigned long cnt;
+	unsigned long max;
 	struct lmb_property region[MAX_LMB_REGIONS+1];
 };
 
lib/lmb.c +4 -1
index 5cf419f439..a926198d48 100644
@@ -95,6 +95,9 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 
 void lmb_init(struct lmb *lmb)
 {
+	lmb->memory.max = MAX_LMB_REGIONS;
+	lmb->reserved.max = MAX_LMB_REGIONS;
+
 	lmb->memory.cnt = 0;
 	lmb->reserved.cnt = 0;
 }
@@ -179,7 +182,7 @@ static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t
 
 	if (coalesced)
 		return coalesced;
-	if (rgn->cnt >= MAX_LMB_REGIONS)
+	if (rgn->cnt >= rgn->max)
 		return -1;
 
 	/* Couldn't coalesce the LMB, so add it to the sorted table. */
Signature · 1 line
2.17.1
#6 lore raw

PATCH v2 3/8 lmb: add a max parameter in the struct lmb_region

Quoted 8 lines · Patrick Delaunay

Add a max parameter in lmb_region struct to handle test in lmb_add_region without using the MAX_LMB_REGIONS define.

This patch allows to modify these size independently for memory of reserved regions in the next patches.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/88411a44/attachment.sig>
#7 lore raw

PATCH v2 4/8 lmb: move MAX_LMB_REGIONS value in Kconfig

In reply to Patrick Delaunay

Move MAX_LMB_REGIONS value in Kconfig, the max number of the regions in lmb library.

---

Changes in v2:
- new in v2: introduced MAX_LMB_REGIONS

 include/lmb.h |  4 +---
 lib/Kconfig   | 10 +++++++++-
 lib/lmb.c     |  4 ++--
 3 files changed, 12 insertions(+), 6 deletions(-)
include/lmb.h +1 -3
index f3397c7f73..d3adf8ef69 100644
@@ -12,8 +12,6 @@
  * Copyright (C) 2001 Peter Bergner, IBM Corp.
  */
 
-#define MAX_LMB_REGIONS 8
-
 struct lmb_property {
 	phys_addr_t base;
 	phys_size_t size;
@@ -22,7 +20,7 @@ struct lmb_property {
 struct lmb_region {
 	unsigned long cnt;
 	unsigned long max;
-	struct lmb_property region[MAX_LMB_REGIONS+1];
+	struct lmb_property region[CONFIG_LMB_MAX_REGIONS + 1];
 };
 
 struct lmb {
lib/Kconfig +9 -1
index 43bd7190b9..43798d7a91 100644
@@ -438,7 +438,7 @@ config LZ4
 	  is included. The LZ4 algorithm can run in-place as long as the
 	  compressed image is loaded to the end of the output buffer, and
 	  trades lower compression ratios for much faster decompression.
-	  
+
 	  NOTE: This implements the release version of the LZ4 frame
 	  format as generated by default by the 'lz4' command line tool.
 	  This is not the same as the outdated, less efficient legacy
@@ -706,6 +706,14 @@ config LMB
 	help
 	  Support the library logical memory blocks.
 
+
+config LMB_MAX_REGIONS
+	int "Number of memory and reserved regions in lmb lib"
+	depends on LMB
+	default 8
+	help
+	  Define the number of supported regions, memory and reserved, in the
+	  library logical memory blocks.
 endmenu
 
 config PHANDLE_CHECK_SEQ
lib/lmb.c +2 -2
index a926198d48..9fbc56619b 100644
@@ -95,8 +95,8 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 
 void lmb_init(struct lmb *lmb)
 {
-	lmb->memory.max = MAX_LMB_REGIONS;
-	lmb->reserved.max = MAX_LMB_REGIONS;
+	lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
+	lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
 
 	lmb->memory.cnt = 0;
 	lmb->reserved.cnt = 0;
Signature · 1 line
2.17.1
#8 lore raw

PATCH v2 4/8 lmb: move MAX_LMB_REGIONS value in Kconfig

Quoted 4 lines · Patrick Delaunay

Move MAX_LMB_REGIONS value in Kconfig, the max number of the regions in lmb library.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/dcc72a23/attachment.sig>
#9 lore raw

PATCH v2 5/8 lmb: correct size of the regions array

In reply to Patrick Delaunay

As in lmb_region, cnt < max and in the lmb library use region[i] only with i in 0...cnt, this region array size can be reduced by 1 element without overflow.

This patch allows to reduce the struct lmb size.

---

Changes in v2:
- new in V2: reduce size impact by correct regions array size

 include/lmb.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/lmb.h +1 -1
index d3adf8ef69..898ce15d07 100644
@@ -20,7 +20,7 @@ struct lmb_property {
 struct lmb_region {
 	unsigned long cnt;
 	unsigned long max;
-	struct lmb_property region[CONFIG_LMB_MAX_REGIONS + 1];
+	struct lmb_property region[CONFIG_LMB_MAX_REGIONS];
 };
 
 struct lmb {
Signature · 1 line
2.17.1
#10 lore raw

PATCH v2 5/8 lmb: correct size of the regions array

Quoted 7 lines · Patrick Delaunay

As in lmb_region, cnt < max and in the lmb library use region[i] only with i in 0...cnt, this region array size can be reduced by 1 element without overflow.

This patch allows to reduce the struct lmb size.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/7167b310/attachment.sig>
#11 lore raw

PATCH v2 6/8 test: lmb: add test for overflow protection in lmb_add_region

In reply to Patrick Delaunay

Add test for max number of memory regions and in reserved regions.

---

Changes in v2:
- new in V2: add test for overflow protection in lmb libary

 test/lib/lmb.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
test/lib/lmb.c +64 -0
index 644ee78758..0d8963fcbf 100644
@@ -659,3 +659,67 @@ static int lib_test_lmb_get_free_size(struct unit_test_state *uts)
 
 DM_TEST(lib_test_lmb_get_free_size,
 	UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+static int lib_test_lmb_max_regions(struct unit_test_state *uts)
+{
+	const phys_addr_t ram = 0x00000000;
+	const phys_size_t ram_size = 0x8000000;
+	const phys_size_t blk_size = 0x10000;
+	phys_addr_t offset;
+	struct lmb lmb;
+	int ret, i;
+
+	lmb_init(&lmb);
+
+	ut_asserteq(lmb.memory.cnt, 0);
+	ut_asserteq(lmb.memory.max, 8);
+	ut_asserteq(lmb.reserved.cnt, 0);
+	ut_asserteq(lmb.reserved.max, 8);
+
+	/*  Add 8 memory regions */
+	for (i = 0; i < 8; i++) {
+		offset = ram + 2 * i * ram_size;
+		ret = lmb_add(&lmb, offset, ram_size);
+		ut_asserteq(ret, 0);
+	}
+	ut_asserteq(lmb.memory.cnt, 8);
+	ut_asserteq(lmb.reserved.cnt, 0);
+
+	/*  error for the 9th memory regions */
+	offset = ram + 2 * 8 * ram_size;
+	ret = lmb_add(&lmb, offset, ram_size);
+	ut_asserteq(ret, -1);
+
+	ut_asserteq(lmb.memory.cnt, 8);
+	ut_asserteq(lmb.reserved.cnt, 0);
+
+	/*  reserve 8 regions */
+	for (i = 0; i < 8; i++) {
+		offset = ram + 2 * i * blk_size;
+		ret = lmb_reserve(&lmb, offset, blk_size);
+		ut_asserteq(ret, 0);
+	}
+
+	ut_asserteq(lmb.memory.cnt, 8);
+	ut_asserteq(lmb.reserved.cnt, 8);
+
+	/*  error for the 9th reserved blocks */
+	offset = ram + 2 * 8 * blk_size;
+	ret = lmb_reserve(&lmb, offset, blk_size);
+	ut_asserteq(ret, -1);
+
+	ut_asserteq(lmb.memory.cnt, 8);
+	ut_asserteq(lmb.reserved.cnt, 8);
+
+	/*  check each regions */
+	for (i = 0; i < 8; i++)
+		ut_asserteq(lmb.memory.region[i].base, ram + 2 * i * ram_size);
+
+	for (i = 0; i < 8; i++)
+		ut_asserteq(lmb.reserved.region[i].base, ram + 2 * i * blk_size);
+
+	return 0;
+}
+
+DM_TEST(lib_test_lmb_max_regions,
+	UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Signature · 1 line
2.17.1
#12 lore raw

PATCH v2 6/8 test: lmb: add test for overflow protection in lmb_add_region

Quoted 3 lines · Patrick Delaunay

Add test for max number of memory regions and in reserved regions.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/ccf1d163/attachment.sig>
#13 lore raw

PATCH v2 7/8 lmb: Add 2 config to define the max number of regions

In reply to Patrick Delaunay

Add 2 configs CONFIG_LMB_MEMORY_REGIONS and CONFIG_LMB_RESERVED_REGIONS to change independently the max number of the regions in lmb library.

When CONFIG_LMB_USE_MAX_REGIONS=y, move the lmb property arrays to struct lmb and manage the array size with the element 'max' of struct lmb_region; their are still allocated in stack.

When CONFIG_LMB_USE_MAX_REGIONS=n, keep the current location in struct lmb_region to allow compiler optimization.

Increase CONFIG_LMB_RESERVED_REGIONS is useful to avoid lmb errors in bootm when the number of reserved regions (not adjacent) is reached:
+ 1 region for relocated U-Boot
+ 1 region for initrd
+ 1 region for relocated linux device tree
+ reserved memory regions present in Linux device tree.

The current limit of 8 regions is reached with only 5 reserved regions in DT.

see Linux kernel commit bf23c51f1f49 ("memblock: Move memblock arrays to static storage in memblock.c and make their size a variable")

---

Changes in v2:
- rework v1 [4/5] to keep current optimization by default when CONFIG_LMB_USE_MAX_REGIONS=n

 include/lmb.h | 34 ++++++++++++++++++++++++++++++++++
 lib/Kconfig   | 29 ++++++++++++++++++++++++++++-
 lib/lmb.c     |  8 +++++++-
 3 files changed, 69 insertions(+), 2 deletions(-)
include/lmb.h +34 -0
index 898ce15d07..541e17093c 100644
@@ -12,20 +12,54 @@
  * Copyright (C) 2001 Peter Bergner, IBM Corp.
  */
 
+/**
+ * struct lmb_property - Description of one region.
+ *
+ * @base: Base address of the region.
+ * @size: Size of the region
+ */
 struct lmb_property {
 	phys_addr_t base;
 	phys_size_t size;
 };
 
+/**
+ * struct lmb_region - Description of a set of region.
+ *
+ * @cnt: Number of regions.
+ * @max: Size of the region array, max value of cnt.
+ * @region: Array of the region properties
+ */
 struct lmb_region {
 	unsigned long cnt;
 	unsigned long max;
+#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
 	struct lmb_property region[CONFIG_LMB_MAX_REGIONS];
+#else
+	struct lmb_property *region;
+#endif
 };
 
+/**
+ * struct lmb - Logical memory block handle.
+ *
+ * Clients provide storage for Logical memory block (lmb) handles.
+ * The content of the structure is managed by the lmb library.
+ * A lmb struct is  initialized by lmb_init() functions.
+ * The lmb struct is passed to all other lmb APIs.
+ *
+ * @memory: Description of memory regions.
+ * @reserved: Description of reserved regions.
+ * @memory_regions: Array of the memory regions (statically allocated)
+ * @reserved_regions: Array of the reserved regions (statically allocated)
+ */
 struct lmb {
 	struct lmb_region memory;
 	struct lmb_region reserved;
+#if !IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
+	struct lmb_property memory_regions[CONFIG_LMB_MEMORY_REGIONS];
+	struct lmb_property reserved_regions[CONFIG_LMB_RESERVED_REGIONS];
+#endif
 };
 
 extern void lmb_init(struct lmb *lmb);
lib/Kconfig +28 -1
index 43798d7a91..946b29ae5a 100644
@@ -706,14 +706,41 @@ config LMB
 	help
 	  Support the library logical memory blocks.
 
+config LMB_USE_MAX_REGIONS
+	bool "Use a commun number of memory and reserved regions in lmb lib"
+	depends on LMB
+	default y
+	help
+	  Define the number of supported memory regions in the library logical
+	  memory blocks.
+	  This feature allow to reduce the lmb library size by using compiler
+	  optimization when LMB_MEMORY_REGIONS == LMB_RESERVED_REGIONS.
 
 config LMB_MAX_REGIONS
 	int "Number of memory and reserved regions in lmb lib"
-	depends on LMB
+	depends on LMB && LMB_USE_MAX_REGIONS
 	default 8
 	help
 	  Define the number of supported regions, memory and reserved, in the
 	  library logical memory blocks.
+
+config LMB_MEMORY_REGIONS
+	int "Number of memory regions in lmb lib"
+	depends on LMB && !LMB_USE_MAX_REGIONS
+	default 8
+	help
+	  Define the number of supported memory regions in the library logical
+	  memory blocks.
+	  The minimal value is CONFIG_NR_DRAM_BANKS.
+
+config LMB_RESERVED_REGIONS
+	int "Number of reserved regions in lmb lib"
+	depends on LMB && !LMB_USE_MAX_REGIONS
+	default 8
+	help
+	  Define the number of supported reserved regions in the library logical
+	  memory blocks.
+
 endmenu
 
 config PHANDLE_CHECK_SEQ
lib/lmb.c +7 -1
index 9fbc56619b..c08c4d942b 100644
@@ -95,9 +95,15 @@ static void lmb_coalesce_regions(struct lmb_region *rgn, unsigned long r1,
 
 void lmb_init(struct lmb *lmb)
 {
+#if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS)
 	lmb->memory.max = CONFIG_LMB_MAX_REGIONS;
 	lmb->reserved.max = CONFIG_LMB_MAX_REGIONS;
-
+#else
+	lmb->memory.max = CONFIG_LMB_MEMORY_REGIONS;
+	lmb->reserved.max = CONFIG_LMB_RESERVED_REGIONS;
+	lmb->memory.region = lmb->memory_regions;
+	lmb->reserved.region = lmb->reserved_regions;
+#endif
 	lmb->memory.cnt = 0;
 	lmb->reserved.cnt = 0;
 }
Signature · 1 line
2.17.1
#14 lore raw

PATCH v2 7/8 lmb: Add 2 config to define the max number of regions

Quoted 18 lines · Patrick Delaunay

Add 2 configs CONFIG_LMB_MEMORY_REGIONS and CONFIG_LMB_RESERVED_REGIONS to change independently the max number of the regions in lmb library.

When CONFIG_LMB_USE_MAX_REGIONS=y, move the lmb property arrays to struct lmb and manage the array size with the element 'max' of struct lmb_region; their are still allocated in stack.

When CONFIG_LMB_USE_MAX_REGIONS=n, keep the current location in struct lmb_region to allow compiler optimization.

Increase CONFIG_LMB_RESERVED_REGIONS is useful to avoid lmb errors in bootm when the number of reserved regions (not adjacent) is reached:
+ 1 region for relocated U-Boot
+ 1 region for initrd
+ 1 region for relocated linux device tree
+ reserved memory regions present in Linux device tree.

Quoted 7 lines

The current limit of 8 regions is reached with only 5 reserved regions in DT.

see Linux kernel commit bf23c51f1f49 ("memblock: Move memblock arrays to static storage in memblock.c and make their size a variable")

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/32c757bf/attachment.sig>
#15 lore raw

PATCH v2 8/8 configs: stm32mp15: increase the number of reserved memory region in lmb

In reply to Patrick Delaunay

For the latest kernel device tree the max number of reserved regions in lmb library is reached: 8 with 5 reserved regions in device tree.

When a new region is added, the lmb allocation for the device tree relocation failed and boot with ramdisk failed.

This patch avoids this issue by increasing the max number of supported reserved memory in lmb library to 16.

---

Changes in v2:
- Add CONFIG_LMB_USE_MAX_REGIONS=n and reduce LMB_MEMORY_REGIONS to reduce the used size in stack

 configs/stm32mp15_basic_defconfig   | 3 +++
 configs/stm32mp15_trusted_defconfig | 3 +++
 2 files changed, 6 insertions(+)
configs/stm32mp15_basic_defconfig +3 -0
index 1c680984b8..a290224a46 100644
@@ -168,3 +168,6 @@ CONFIG_WDT_STM32MP=y
 CONFIG_ERRNO_STR=y
 # CONFIG_HEXDUMP is not set
 CONFIG_FDT_FIXUP_PARTITIONS=y
+# CONFIG_LMB_USE_MAX_REGIONS is not set
+CONFIG_LMB_MEMORY_REGIONS=2
+CONFIG_LMB_RESERVED_REGIONS=16
configs/stm32mp15_trusted_defconfig +3 -0
index 107041119f..ebd4c05891 100644
@@ -148,3 +148,6 @@ CONFIG_WDT_STM32MP=y
 CONFIG_ERRNO_STR=y
 # CONFIG_HEXDUMP is not set
 CONFIG_FDT_FIXUP_PARTITIONS=y
+# CONFIG_LMB_USE_MAX_REGIONS is not set
+CONFIG_LMB_MEMORY_REGIONS=2
+CONFIG_LMB_RESERVED_REGIONS=16
Signature · 1 line
2.17.1
#16 lore raw

PATCH v2 8/8 configs: stm32mp15: increase the number of reserved memory region in lmb

Quoted 10 lines · Patrick Delaunay

For the latest kernel device tree the max number of reserved regions in lmb library is reached: 8 with 5 reserved regions in device tree.

When a new region is added, the lmb allocation for the device tree relocation failed and boot with ramdisk failed.

This patch avoids this issue by increasing the max number of supported reserved memory in lmb library to 16.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210423/68a730fd/attachment.sig>