various memory related fixups
Subsystems (3)
v5 cover various memory related fixups
From: Randolph Sapp <[email protected]>
Nitpicks and fixes from the discovery thread on adding PocketBeagle2 support [1]. This does a lot of general setup required for the device, but these modifications themselves aren't device specific. For those specifically interested in PocketBeagle2 support and don't care about these details, my development branch is public [2].
That second patch may provoke some opinions, but honestly if that warning was still present I wouldn't have spent a week poking holes in both the EFI and LMB allocations systems. Please let me know if there is a specific usecase that it breaks though.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/StaticRocket/u-boot/tree/feature/pocketbeagle2
v2:
- Remove additional increment and decrement in lmb_free_fdt_regions
- Drop the patch to backfill EFI_CONVENTIONAL_MEMORY
- Adjust the removal loop nitpick patch description
- Change the reserve memory patch to use new end_addr_sp
v3:
- Update lmb flags to use the macro documentation for constants
- Change efi_mem_sort to use list_for_each_entry_safe
v4:
- Fix typos in LMB allocation flags macro documentation
- Rename end_addr_sp to initial_relocaddr
- Keep the map_sysmem dance in the efi u-boot reservation
- Use the active device tree pointed to by gd->fdt_blob to clean up old reservations
v5:
- Keep return value as long in boot_fdt_reserve_region
- Fix formatting in initial_relocaddr patch
Randolph Sapp (6):
lmb: allocation flags macro documentation boot_fdt_add_mem_rsv_regions: free old dtb reservations
efi_dt_fixup: use fdtdec_get_bool
efi_selftest_memory: check for duplicates first
efi_mem_sort: use list_for_each_entry_safe instead
memory: reserve from start_addr_sp to initial_relocaddr
arch/mips/lib/bootm.c | 2 +- boot/bootm.c | 2 +- boot/bootm_os.c | 2 +- boot/image-board.c | 2 +- boot/image-fdt.c | 55 ++++++++++++++++++-------- common/board_f.c | 9 ++++- include/asm-generic/global_data.h | 7 ++++ include/image.h | 2 +- include/lmb.h | 30 +++++++++++--- lib/efi_loader/efi_dt_fixup.c | 3 +- lib/efi_loader/efi_memory.c | 49 ++++++++--------------- lib/efi_selftest/efi_selftest_memory.c | 21 +++++----- lib/lmb.c | 41 ++++--------------- 13 files changed, 118 insertions(+), 107 deletions(-)
Signature · 1 line
2.53.0
PATCH v5 1/6 lmb: allocation flags macro documentation
From: Randolph Sapp <[email protected]>
Update the allocation flags inline documentation to follow the kernel object like macro documentation specification.
Use active voice for the short descriptions.
--- include/lmb.h | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-)
include/lmb.h +25 -5
@@ -19,16 +19,36 @@#define LMB_ALIST_INITIAL_SIZE 4/** -* DOC: Memory region attribute flags. +* define LMB_NONE - no special request * -* %LMB_NONE: No special request -* %LMB_NOMAP: Don't add to MMU configuration -* %LMB_NOOVERWRITE: The memory region cannot be overwritten/re-reserved -* %LMB_NONOTIFY: Do not notify other modules of changes to this memory region +* LMB Memory region attribute flag to indicate that there are no special +* requests for this region. Normally used as a placeholder value. */ #define LMB_NONE 0 + +/** +* define LMB_NOMAP - do not add to MMU configuration +* +* LMB Memory region attribute flag to indicate that the region will not be +* mapped by LMB. Normally used for reserved regions. +*/ #define LMB_NOMAP BIT(1) + +/** +* define LMB_NOOVERWRITE - do not overwrite/re-reserve +* +* LMB Memory region attribute flag to indicate that the region will not be +* overwritten or re-reserved. Normally used for reserved regions. +*/ #define LMB_NOOVERWRITE BIT(2) + +/** +* define LMB_NONOTIFY - do not notify other modules of changes +* +* LMB Memory region attribute flag to indicate that the region will not notify +* downstream allocators (currently just the EFI allocator) of changes to this +* region through lmb_map_update_notify(). +*/ #define LMB_NONOTIFY BIT(3)/**
Signature · 1 line
2.53.0
PATCH v5 2/6 boot_fdt_add_mem_rsv_regions: free old dtb reservations
In reply to [email protected]From: Randolph Sapp <[email protected]>
Add a free flag and an initial call to free allocations covered by the global FDT. This assumes that all calls to boot_fdt_add_mem_rsv_regions occur before the transition to the new device tree, thus we can access the currently active device tree through the global data pointer.
This allows us to clearly indicate to the user when a device tree reservation fails. How we handle this can still use some improvement. Right now we'll keep the default behavior and try to boot anyway.
This functionality was broken in:
5a6aa7d ("boot: fdt: Handle already reserved memory in boot_fdt_reserve_region()")
--- arch/mips/lib/bootm.c | 2 +- boot/bootm.c | 2 +- boot/bootm_os.c | 2 +- boot/image-board.c | 2 +- boot/image-fdt.c | 55 ++++++++++++++++++++++++++++++------------- include/image.h | 2 +- lib/lmb.c | 2 +- 7 files changed, 44 insertions(+), 23 deletions(-)
arch/mips/lib/bootm.c +1 -1
@@ -210,7 +210,7 @@ static int boot_reloc_fdt(struct bootm_headers *images) }#if CONFIG_IS_ENABLED (MIPS_BOOT_FDT ) &&CONFIG_IS_ENABLED (OF_LIBFDT ) -boot_fdt_add_mem_rsv_regions (images->ft_addr); +boot_fdt_add_mem_rsv_regions (images->ft_addr, false);return boot_relocate_fdt (&images->ft_addr, &images->ft_len);#else return 0 ;
boot/bootm.c +1 -1
@@ -1040,7 +1040,7 @@ int bootm_run_states(struct bootm_info *bmi, int states)#endif #if CONFIG_IS_ENABLED (OF_LIBFDT ) &&CONFIG_IS_ENABLED (LMB )if (!ret && (states &BOOTM_STATE_FDT )) { -boot_fdt_add_mem_rsv_regions (images->ft_addr); +boot_fdt_add_mem_rsv_regions (images->ft_addr, false); ret =boot_relocate_fdt (&images->ft_addr, &images->ft_len); }#endif
boot/bootm_os.c +1 -1
@@ -262,7 +262,7 @@ static void do_bootvx_fdt(struct bootm_headers *images)char **of_flat_tree = &images->ft_addr;if (*of_flat_tree) { -boot_fdt_add_mem_rsv_regions (*of_flat_tree); +boot_fdt_add_mem_rsv_regions (*of_flat_tree, false); ret =boot_relocate_fdt (of_flat_tree, &of_size);if (ret)
boot/image-board.c +1 -1
@@ -914,7 +914,7 @@ int image_setup_linux(struct bootm_headers *images)if (!CONFIG_IS_ENABLED (LMB ))return -EFAULT ;if (CONFIG_IS_ENABLED (OF_LIBFDT )) -boot_fdt_add_mem_rsv_regions (*of_flat_tree); +boot_fdt_add_mem_rsv_regions (*of_flat_tree, false);if (IS_ENABLED (CONFIG_SYS_BOOT_GET_CMDLINE )) { ret = boot_get_cmdline(&images->cmdline_start,
boot/image-fdt.c +38 -17
92 lines not on this page · raw message
include/image.h +1 -1
@@ -827,7 +827,7 @@ int boot_get_fdt(void *buf, const char *select, uint arch, struct bootm_headers *images, char **of_flat_tree, ulong *of_size); -void boot_fdt_add_mem_rsv_regions (void *fdt_blob); +void boot_fdt_add_mem_rsv_regions (void *fdt_blob,bool free);int boot_relocate_fdt (char **of_flat_tree,ulong *of_size); int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start,
lib/lmb.c +1 -1
@@ -581,7 +581,7 @@ static void lmb_reserve_common(void *fdt_blob)lmb_reserve_uboot_region ();if (CONFIG_IS_ENABLED (OF_LIBFDT ) && fdt_blob) -boot_fdt_add_mem_rsv_regions (fdt_blob); +boot_fdt_add_mem_rsv_regions (fdt_blob, false); }static __maybe_unused void lmb_reserve_common_spl (void )
Signature · 1 line
2.53.0
Re: [PATCHv5 2/6] boot_fdt_add_mem_rsv_regions: free old dtb reservations
Simon,
This looks ok to me, but I haven't been involved with the bootm code much. Can you taker a quick look?
Quoted 8 lines · On Wed, 22 Apr 2026 at 20:10, <[email protected]> wrote:
From: Randolph Sapp <[email protected]>
Add a free flag and an initial call to free allocations covered by the global FDT. This assumes that all calls to
boot_fdt_add_mem_rsv_regionsoccur before the transition to the new device tree, thus we can access the currently active device tree through the global data pointer.
Quoted 7 lines
This allows us to clearly indicate to the user when a device tree reservation fails. How we handle this can still use some improvement. Right now we'll keep the default behavior and try to boot anyway.
This functionality was broken in:
5a6aa7d ("boot: fdt: Handle already reserved memory inboot_fdt_reserve_region()")
[...]
either operation.
Quoted 3 lines
+ */
+static voidboot_fdt_reserve_region(u64 addr, u64 size, u32 flags, bool free)
{
The function name is not accurate anymore. Perhaps call it boot_fdt_handle_region()?
Quoted 105 lines
long ret; phys_addr_t rsv_addr; rsv_addr = (phys_addr_t)addr;boot/image-fdt.c +27
-16- ret =lmb_alloc_mem (LMB_MEM_ALLOC_ADDR ,0 , &rsv_addr, size, flags); +if (free) + ret =lmb_free (rsv_addr, size, flags); +else + ret =lmb_alloc_mem (LMB_MEM_ALLOC_ADDR ,0 , &rsv_addr, size, + flags); +if (!ret) { -debug (" reserving fdt memory region: addr=%llx size=%llx flags=%x\n" , - (unsigned long long )addr, +debug (" %s fdt memory region: addr=%llx size=%llx flags=%x\n" , + free ?"freed" :"reserved" , (unsigned long long )addr, (unsigned long long )size, flags); - }else if (ret != -EEXIST && ret != -EINVAL ) { -puts ("ERROR: reserving fdt memory region failed " ); -printf ("(addr=%llx size=%llx flags=%x)\n" , - (unsigned long long )addr, - (unsigned long long )size, flags); + }else { +printf ("ERROR: %s fdt memory region failed (addr=%llx size=%llx flags=%x): %ld\n" , + free ?"freeing" :"reserving" , (unsigned long long )addr, + (unsigned long long )size, flags, ret); } }/** -* boot_fdt_add_mem_rsv_regions - Mark the memreserve and reserved-memory -* sections as unusable +* boot_fdt_add_mem_rsv_regions - Handle FDT memreserve and reserved-memory +* sections * @fdt_blob: pointer to fdt blob base address +* @free: indicate if regions are being freed * -* Adds the and reserved-memorymemreserve regions in the dtb to the lmb block. -* Adding the memreserve regions prevents u-boot from using them to store the -* initrd or the fdt blob. +* Adds or removes reserved-memory and memreserve regions in the dtb to the lmb +* block. Adding the memreserve regions prevents u-boot from using them to store +* the initrd or the fdt blob. This function will attempt to clean the currently +* active reservations if a new device tree blob is given. */ -void boot_fdt_add_mem_rsv_regions (void *fdt_blob) +void boot_fdt_add_mem_rsv_regions (void *fdt_blob,bool free) {uint64_t addr, size;int i, total, ret;@@ -108,12 +124,16 @@ void boot_fdt_add_mem_rsv_regions(void *fdt_blob)if (fdt_check_header (fdt_blob) !=0 )return ; +/* Remove old regions */ +if (gd->fdt_blob != fdt_blob) +boot_fdt_add_mem_rsv_regions ((void *)gd->fdt_blob, true); +/* process memreserve sections */ total =fdt_num_mem_rsv (fdt_blob);for (i =0 ; i < total; i++) {if (fdt_get_mem_rsv (fdt_blob, i, &addr, &size) !=0 )continue ; -boot_fdt_reserve_region (addr, size,LMB_NOOVERWRITE ); +boot_fdt_reserve_region (addr, size,LMB_NOOVERWRITE , free); }/* process reserved-memory */ @@ -131,7 +151,8 @@ void boot_fdt_add_mem_rsv_regions(void *fdt_blob) flags =LMB_NOMAP ; addr = res.start; size = res.end - res.start +1 ; -boot_fdt_reserve_region (addr, size, flags); +boot_fdt_reserve_region (addr, size, flags, + free); } subnode =fdt_next_subnode (fdt_blob, subnode);include/image.h +1
-1@@ -827,7 +827,7 @@ int boot_get_fdt(void *buf, const char *select, uint arch, struct bootm_headers *images, char **of_flat_tree, ulong *of_size); -void boot_fdt_add_mem_rsv_regions (void *fdt_blob); +void boot_fdt_add_mem_rsv_regions (void *fdt_blob,bool free);int boot_relocate_fdt (char **of_flat_tree,ulong *of_size); int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start,lib/lmb.c +1
-2@@ -581,7 +581,7 @@ static void lmb_reserve_common(void *fdt_blob)lmb_reserve_uboot_region ();if (CONFIG_IS_ENABLED (OF_LIBFDT ) && fdt_blob) -boot_fdt_add_mem_rsv_regions (fdt_blob); +boot_fdt_add_mem_rsv_regions (fdt_blob, false); }static __maybe_unused void lmb_reserve_common_spl (void ) --
Quoted 2 lines
2.53.0
With the change above
PATCH v5 3/6 efi_dt_fixup: use fdtdec_get_bool
In reply to [email protected]From: Randolph Sapp <[email protected]>
Use the more straightforward fdtdec_get_bool instead of fdt_getprop and a return code check.
--- lib/efi_loader/efi_dt_fixup.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
lib/efi_loader/efi_dt_fixup.c +1 -2
@@ -123,8 +123,7 @@ void efi_carve_out_dt_rsv(void *fdt) fdtdec_get_is_enabled(fdt, subnode)) {bool nomap; - nomap = !!fdt_getprop (fdt, subnode,"no-map" , -NULL ); + nomap =fdtdec_get_bool (fdt, subnode,"no-map" );efi_reserve_memory (fdt_addr, fdt_size, nomap); } subnode =fdt_next_subnode (fdt, subnode);
Signature · 1 line
2.53.0
PATCH v5 4/6 efi_selftest_memory: check for duplicates first
In reply to [email protected]From: Randolph Sapp <[email protected]>
Check for duplicate memory mappings before reporting any incorrect attributes. Could be that second allocation has the correct type while the first doesn't. Knowing there is a duplicate in this scenario is more helpful than just reporting the first mismatch.
--- lib/efi_selftest/efi_selftest_memory.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
lib/efi_selftest/efi_selftest_memory.c +10 -11
@@ -60,7 +60,7 @@ static int find_in_memory_map(efi_uintn_t map_size, u64 addr, int memory_type) {efi_uintn_t i; -bool found = false; +struct efi_mem_desc *match =NULL ;for (i =0 ; map_size; ++i, map_size -= desc_size) {struct efi_mem_desc *entry = &memory_map[i];
@@ -72,24 +72,23 @@ static int find_in_memory_map(efi_uintn_t map_size,if (addr >= entry->physical_start && addr < entry->physical_start + - (entry->num_pages <<EFI_PAGE_SHIFT )) { -if (found) { + (entry->num_pages <<EFI_PAGE_SHIFT )) { +if (match) {efi_st_error ("Duplicate memory map entry\n" );return EFI_ST_FAILURE ; } - found = true; -if (memory_type != entry->type) { -efi_st_error - ("Wrong memory type %d, expected %d\n" , - entry->type, memory_type); -return EFI_ST_FAILURE ; - } + match = entry; } } -if (!found) { +if (!match) {efi_st_error ("Missing memory map entry\n" );return EFI_ST_FAILURE ; } +if (memory_type != match->type) { +efi_st_error ("Wrong memory type %d, expected %d\n" , match->type, + memory_type); +return EFI_ST_FAILURE ; + }return EFI_ST_SUCCESS ; }
Signature · 1 line
2.53.0
PATCH v5 5/6 efi_mem_sort: use list_for_each_entry_safe instead
In reply to [email protected]From: Randolph Sapp <[email protected]>
Use list_for_each_entry_safe and comparisons against the current and next efi_mem_desc. This reduces the computation required for merging regions, prevents unnecessary additional iterations of the list, and requires less temporary values.
--- lib/efi_loader/efi_memory.c | 47 +++++++++++++------------------------ 1 file changed, 16 insertions(+), 31 deletions(-)
lib/efi_loader/efi_memory.c +16 -31
@@ -128,44 +128,29 @@ static uint64_t desc_get_end(struct efi_mem_desc *desc)*/ static void efi_mem_sort (void ) { -struct efi_mem_list *lmem; -struct efi_mem_list *prevmem =NULL ; -bool merge_again = true; +struct efi_mem_list *curmem, *nextmem =NULL ;list_sort (NULL , &efi_mem, efi_mem_cmp);/* Now merge entries that can be merged */ -while (merge_again) { - merge_again = false; -list_for_each_entry (lmem, &efi_mem, link) { -struct efi_mem_desc *prev; -struct efi_mem_desc *cur; -uint64_t pages; +list_for_each_entry_safe (curmem, nextmem, &efi_mem, link) { +struct efi_mem_desc *cur; +struct efi_mem_desc *next; -if (!prevmem) { - prevmem = lmem; -continue ; - } +/* Exit when we've got nothing to compare with */ +if (&nextmem->link == &efi_mem) +break ; - cur = &lmem->desc; - prev = &prevmem->desc; - -if ((desc_get_end (cur) == prev->physical_start) && - (prev->type == cur->type) && - (prev->attribute == cur->attribute)) { -/* There is an existing map before, reuse it */ - pages = cur->num_pages; - prev->num_pages += pages; - prev->physical_start -= pages <<EFI_PAGE_SHIFT ; - prev->virtual_start -= pages <<EFI_PAGE_SHIFT ; -list_del (&lmem->link); -free (lmem); - - merge_again = true; -break ; - } + cur = &curmem->desc; + next = &nextmem->desc; - prevmem = lmem; +if ((cur->physical_start ==desc_get_end (next)) && + (cur->type == next->type) && + (cur->attribute == next->attribute)) { +/* There is another similar map coming up, reuse it */ + next->num_pages += cur->num_pages; +list_del (&curmem->link); +free (curmem); } } }
Signature · 1 line
2.53.0
PATCH v5 6/6 memory: reserve from start_addr_sp to initial_relocaddr
In reply to [email protected]From: Randolph Sapp <[email protected]>
Add a new global data struct member called initial_relocaddr. This stores the original value of relocaddr, directly from setup_dest_addr. This is specifically to avoid any adjustments made by other init functions.
Reserve the memory from gd->start_addr_sp - CONFIG_STACK_SIZE to gd->initial_relocaddr instead of gd->ram_top. This allows platform specific relocation addresses to work without unnecessarily painting over a large range.
Since PRAM comes out of this initial area up to initial_relocaddr, we no longer need to account for it separately.
--- common/board_f.c | 9 ++++++- include/asm-generic/global_data.h | 7 ++++++ lib/efi_loader/efi_memory.c | 2 +- lib/lmb.c | 39 +++++-------------------------- 4 files changed, 22 insertions(+), 35 deletions(-)
common/board_f.c +8 -1
@@ -330,6 +330,8 @@ __weak int arch_setup_dest_addr(void)static int setup_dest_addr (void ) { +int ret; +debug ("Monitor len: %08x\n" , gd->mon_len);/* * Ram is setup, size stored in gd !!
@@ -356,7 +358,12 @@ static int setup_dest_addr(void) gd->relocaddr = gd->ram_top;debug ("Ram top: %08llX\n" , (unsigned long long )gd->ram_top); -return arch_setup_dest_addr (); + ret =arch_setup_dest_addr (); +if (ret !=0 ) +return ret; + + gd->initial_relocaddr = gd->relocaddr; +return ret; }#ifdef CFG_PRAM
include/asm-generic/global_data.h +7 -0
@@ -107,6 +107,13 @@ struct global_data {* GDB using the 'add-symbol-file u-boot <relocaddr>' command. */ unsigned long relocaddr; +/** +* @initial_relocaddr: end of memory currently in use by uboot +* +* This should be the original value of relocaddr before any other +* allocations or reservations shift it. +*/ +unsigned long initial_relocaddr;/** * @irq_sp: IRQ stack pointer */
lib/efi_loader/efi_memory.c +1 -1
@@ -869,7 +869,7 @@ static void add_u_boot_and_runtime(void)/* Add U-Boot */ uboot_start = ((uintptr_t )map_sysmem (gd->start_addr_sp,0 ) - uboot_stack_size) & ~EFI_PAGE_MASK ; - uboot_pages = ((uintptr_t )map_sysmem (gd->ram_top -1 ,0 ) - + uboot_pages = ((uintptr_t )map_sysmem (gd->initial_relocaddr,0 ) - uboot_start +EFI_PAGE_MASK ) >>EFI_PAGE_SHIFT ;efi_update_memory_map (uboot_start, uboot_pages,EFI_BOOT_SERVICES_CODE , false, false);
lib/lmb.c +6 -33
@@ -534,46 +534,19 @@ static long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)static void lmb_reserve_uboot_region (void ) { -int bank; -ulong end, bank_end; +ulong size;phys_addr_t rsv_start; -ulong pram =0 ; rsv_start = gd->start_addr_sp -CONFIG_STACK_SIZE ; - end = gd->ram_top; + size = gd->initial_relocaddr - rsv_start; -/* -* Reserve memory from aligned address below the bottom of U-Boot stack -* until end of RAM area to prevent LMB from overwriting that memory. -*/ -debug ("## Current stack ends at 0x%08lx " , (ulong )rsv_start); +debug ("## Current stack ends at 0x%08lx\n" , (ulong )rsv_start); -#ifdef CFG_PRAM - pram =env_get_ulong ("pram" ,10 ,CFG_PRAM ); - pram = pram <<10 ;/* size is in kB */ -#endif - -for (bank =0 ; bank <CONFIG_NR_DRAM_BANKS ; bank++) { -if (!gd->bd->bi_dram[bank].size || - rsv_start < gd->bd->bi_dram[bank].start) -continue ; -/* Watch out for RAM at end of address space! */ - bank_end = gd->bd->bi_dram[bank].start + - gd->bd->bi_dram[bank].size -1 ; -if (rsv_start > bank_end) -continue ; -if (bank_end > end) - bank_end = end -1 ; +lmb_reserve (rsv_start, size,LMB_NOOVERWRITE ); -lmb_reserve (rsv_start, bank_end - rsv_start - pram +1 , +if (gd->flags &GD_FLG_SKIP_RELOC ) +lmb_reserve ((phys_addr_t )(uintptr_t )_start, gd->mon_len,LMB_NOOVERWRITE ); - -if (gd->flags &GD_FLG_SKIP_RELOC ) -lmb_reserve ((phys_addr_t )(uintptr_t )_start, - gd->mon_len,LMB_NOOVERWRITE ); - -break ; - } }static void lmb_reserve_common (void *fdt_blob)
Signature · 1 line
2.53.0
Re: [PATCHv5 6/6] memory: reserve from start_addr_sp to initial_relocaddr
Hi Randolph,
Quoted 8 lines · On Wed, 22 Apr 2026 at 20:10, <[email protected]> wrote:
From: Randolph Sapp <[email protected]>
Add a new global data struct member called
initial_relocaddr. This stores the original value of relocaddr, directly fromsetup_dest_addr. This is specifically to avoid any adjustments made by other init functions.
Quoted 7 lines
Reserve the memory from
gd->start_addr_sp-CONFIG_STACK_SIZEtogd->initial_relocaddrinstead ofgd->ram_top. This allows platform specific relocation addresses to work without unnecessarily painting over a large range.Since PRAM comes out of this initial area up to
initial_relocaddr, we no longer need to account for it separately.
Several CI tests are complaining with this series.
https://source.denx.de/u-boot/custodians/u-boot-tpm/-/pipelines/29989
Can you have a look?
Thanks
/Ilias
Quoted 127 lines
Signed-off-by Randolph Sapp <[email protected]>--- common/board_f.c | 9 ++++++- include/asm-generic/global_data.h | 7 ++++++ lib/efi_loader/efi_memory.c | 2 +- lib/lmb.c | 39 +++++-------------------------- 4 files changed, 22 insertions(+), 35 deletions(-)common/board_f.c +8
-1@@ -330,6 +330,8 @@ __weak int arch_setup_dest_addr(void)static int setup_dest_addr (void ) { +int ret; +debug ("Monitor len: %08x\n" , gd->mon_len);/* * Ram is setup, size stored in gd !! @@ -356,7 +358,12 @@ static int setup_dest_addr(void) gd->relocaddr = gd->ram_top;debug ("Ram top: %08llX\n" , (unsigned long long )gd->ram_top); -return arch_setup_dest_addr (); + ret =arch_setup_dest_addr (); +if (ret !=0 ) +return ret; + + gd->initial_relocaddr = gd->relocaddr; +return ret; }#ifdef CFG_PRAM include/asm-generic/global_data.h +7
-0@@ -107,6 +107,13 @@ struct global_data {* GDB using the 'add-symbol-file u-boot <relocaddr>' command. */ unsigned long relocaddr; +/** +* @initial_relocaddr: end of memory currently in use by uboot +* +* This should be the original value of relocaddr before any other +* allocations or reservations shift it. +*/ +unsigned long initial_relocaddr;/** * @irq_sp: IRQ stack pointer */ lib/efi_loader/efi_memory.c +1
-1@@ -869,7 +869,7 @@ static void add_u_boot_and_runtime(void)/* Add U-Boot */ uboot_start = ((uintptr_t )map_sysmem (gd->start_addr_sp,0 ) - uboot_stack_size) & ~EFI_PAGE_MASK ; - uboot_pages = ((uintptr_t )map_sysmem (gd->ram_top -1 ,0 ) - + uboot_pages = ((uintptr_t )map_sysmem (gd->initial_relocaddr,0 ) - uboot_start +EFI_PAGE_MASK ) >>EFI_PAGE_SHIFT ;efi_update_memory_map (uboot_start, uboot_pages,EFI_BOOT_SERVICES_CODE , false, false);lib/lmb.c +6
-34@@ -534,46 +534,19 @@ static long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)static void lmb_reserve_uboot_region (void ) { -int bank; -ulong end, bank_end; +ulong size;phys_addr_t rsv_start; -ulong pram =0 ; rsv_start = gd->start_addr_sp -CONFIG_STACK_SIZE ; - end = gd->ram_top; + size = gd->initial_relocaddr - rsv_start; -/* -* Reserve memory from aligned address below the bottom of U-Boot stack -* until end of RAM area to prevent LMB from overwriting that memory. -*/ -debug ("## Current stack ends at 0x%08lx " , (ulong )rsv_start); +debug ("## Current stack ends at 0x%08lx\n" , (ulong )rsv_start); -#ifdef CFG_PRAM - pram =env_get_ulong ("pram" ,10 ,CFG_PRAM ); - pram = pram <<10 ;/* size is in kB */ -#endif - -for (bank =0 ; bank <CONFIG_NR_DRAM_BANKS ; bank++) { -if (!gd->bd->bi_dram[bank].size || - rsv_start < gd->bd->bi_dram[bank].start) -continue ; -/* Watch out for RAM at end of address space! */ - bank_end = gd->bd->bi_dram[bank].start + - gd->bd->bi_dram[bank].size -1 ; -if (rsv_start > bank_end) -continue ; -if (bank_end > end) - bank_end = end -1 ; +lmb_reserve (rsv_start, size,LMB_NOOVERWRITE ); -lmb_reserve (rsv_start, bank_end - rsv_start - pram +1 , +if (gd->flags &GD_FLG_SKIP_RELOC ) +lmb_reserve ((phys_addr_t )(uintptr_t )_start, gd->mon_len,LMB_NOOVERWRITE ); - -if (gd->flags &GD_FLG_SKIP_RELOC ) -lmb_reserve ((phys_addr_t )(uintptr_t )_start, - gd->mon_len,LMB_NOOVERWRITE ); - -break ; - } }static void lmb_reserve_common (void *fdt_blob) --2.53.0
Re: [PATCHv5 6/6] memory: reserve from start_addr_sp to initial_relocaddr
Quoted 83 lines · Ilias Apalodimas
Hi Randolph,
Quoted 8 lines · On Wed, 22 Apr 2026 at 20:10, <[email protected]> wrote:
From: Randolph Sapp <[email protected]>
Add a new global data struct member called
initial_relocaddr. This stores the original value of relocaddr, directly fromsetup_dest_addr. This is specifically to avoid any adjustments made by other init functions.Quoted 7 lines
Reserve the memory from
gd->start_addr_sp-CONFIG_STACK_SIZEtogd->initial_relocaddrinstead ofgd->ram_top. This allows platform specific relocation addresses to work without unnecessarily painting over a large range.Since PRAM comes out of this initial area up to
initial_relocaddr, we no longer need to account for it separately.Several CI tests are complaining with this series.
https://source.denx.de/u-boot/custodians/u-boot-tpm/-/pipelines/29989Can you have a look?
Thanks
/IliasQuoted 56 lines
Signed-off-by Randolph Sapp <[email protected]>--- common/board_f.c | 9 ++++++- include/asm-generic/global_data.h | 7 ++++++ lib/efi_loader/efi_memory.c | 2 +- lib/lmb.c | 39 +++++-------------------------- 4 files changed, 22 insertions(+), 35 deletions(-)common/board_f.c +8
-1@@ -330,6 +330,8 @@ __weak int arch_setup_dest_addr(void)static int setup_dest_addr (void ) { +int ret; +debug ("Monitor len: %08x\n" , gd->mon_len);/* * Ram is setup, size stored in gd !! @@ -356,7 +358,12 @@ static int setup_dest_addr(void) gd->relocaddr = gd->ram_top;debug ("Ram top: %08llX\n" , (unsigned long long )gd->ram_top); -return arch_setup_dest_addr (); + ret =arch_setup_dest_addr (); +if (ret !=0 ) +return ret; + + gd->initial_relocaddr = gd->relocaddr; +return ret; }#ifdef CFG_PRAM include/asm-generic/global_data.h +7
-0@@ -107,6 +107,13 @@ struct global_data {* GDB using the 'add-symbol-file u-boot <relocaddr>' command. */ unsigned long relocaddr; +/** +* @initial_relocaddr: end of memory currently in use by uboot +* +* This should be the original value of relocaddr before any other +* allocations or reservations shift it. +*/ +unsigned long initial_relocaddr;/** * @irq_sp: IRQ stack pointer */ lib/efi_loader/efi_memory.c +0
-0
Quoted 8 lines
lib/efi_loader/efi_memory.c static void add_u_boot_and_runtime(void) · line 872
Quoted 8 lines
lib/efi_loader/efi_memory.c +1
-1@@ -869,7 +869,7 @@ static void add_u_boot_and_runtime(void)/* Add U-Boot */ uboot_start = ((uintptr_t )map_sysmem (gd->start_addr_sp,0 ) - uboot_stack_size) & ~EFI_PAGE_MASK ; - uboot_pages = ((uintptr_t)map_sysmem(gd->ram_top - 1, 0) - + uboot_pages = ((uintptr_t)map_sysmem(gd->initial_relocaddr, 0) -
Ugh, It's specifically when gd->initial_relocaddr == gd->ram_top. Something in map_sysmem gets angry. Seems weird to me though. The ram_top value is supposed to be addressable.
It's ram_size - 1 on physical boards. GDB tells me that is not the case in the sandbox though. Fun.
Quoted 63 lines
Quoted 63 lines
lib/efi_loader/efi_memory.c +0
-0uboot_start + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;efi_update_memory_map (uboot_start, uboot_pages,EFI_BOOT_SERVICES_CODE , false, false);lib/lmb.c +6
-34@@ -534,46 +534,19 @@ static long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags)static void lmb_reserve_uboot_region (void ) { -int bank; -ulong end, bank_end; +ulong size;phys_addr_t rsv_start; -ulong pram =0 ; rsv_start = gd->start_addr_sp -CONFIG_STACK_SIZE ; - end = gd->ram_top; + size = gd->initial_relocaddr - rsv_start; -/* -* Reserve memory from aligned address below the bottom of U-Boot stack -* until end of RAM area to prevent LMB from overwriting that memory. -*/ -debug ("## Current stack ends at 0x%08lx " , (ulong )rsv_start); +debug ("## Current stack ends at 0x%08lx\n" , (ulong )rsv_start); -#ifdef CFG_PRAM - pram =env_get_ulong ("pram" ,10 ,CFG_PRAM ); - pram = pram <<10 ;/* size is in kB */ -#endif - -for (bank =0 ; bank <CONFIG_NR_DRAM_BANKS ; bank++) { -if (!gd->bd->bi_dram[bank].size || - rsv_start < gd->bd->bi_dram[bank].start) -continue ; -/* Watch out for RAM at end of address space! */ - bank_end = gd->bd->bi_dram[bank].start + - gd->bd->bi_dram[bank].size -1 ; -if (rsv_start > bank_end) -continue ; -if (bank_end > end) - bank_end = end -1 ; +lmb_reserve (rsv_start, size,LMB_NOOVERWRITE ); -lmb_reserve (rsv_start, bank_end - rsv_start - pram +1 , +if (gd->flags &GD_FLG_SKIP_RELOC ) +lmb_reserve ((phys_addr_t )(uintptr_t )_start, gd->mon_len,LMB_NOOVERWRITE ); - -if (gd->flags &GD_FLG_SKIP_RELOC ) -lmb_reserve ((phys_addr_t )(uintptr_t )_start, - gd->mon_len,LMB_NOOVERWRITE ); - -break ; - } }static void lmb_reserve_common (void *fdt_blob) --2.53.0
Quoted 43 lines · rs
From: Randolph Sapp <[email protected]>
Nitpicks and fixes from the discovery thread on adding PocketBeagle2 support [1]. This does a lot of general setup required for the device, but these modifications themselves aren't device specific. For those specifically interested in PocketBeagle2 support and don't care about these details, my development branch is public [2].
That second patch may provoke some opinions, but honestly if that warning was still present I wouldn't have spent a week poking holes in both the EFI and LMB allocations systems. Please let me know if there is a specific usecase that it breaks though.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/StaticRocket/u-boot/tree/feature/pocketbeagle2v2:
- Remove additional increment and decrement inlmb_free_fdt_regions
- Drop the patch to backfillEFI_CONVENTIONAL_MEMORY
- Adjust the removal loop nitpick patch description
- Change the reserve memory patch to use newend_addr_sp
v3:
- Update lmb flags to use the macro documentation for constants
- Changeefi_mem_sortto uselist_for_each_entry_safe
v4:
- Fix typos in LMB allocation flags macro documentation
- Renameend_addr_sptoinitial_relocaddr
- Keep themap_sysmemdance in the efi u-boot reservation
- Use the active device tree pointed to bygd->fdt_blobto clean up old reservations
v5:
- Keep return value as long inboot_fdt_reserve_region
- Fix formatting ininitial_relocaddrpatchRandolph Sapp (6):
lmb: allocation flags macro documentationboot_fdt_add_mem_rsv_regions: free old dtb reservations
efi_dt_fixup: usefdtdec_get_bool
efi_selftest_memory: check for duplicates first
efi_mem_sort: uselist_for_each_entry_safeinstead
memory: reserve fromstart_addr_sptoinitial_relocaddr
Quoted 14 lines
arch/mips/lib/bootm.c | 2 +- boot/bootm.c | 2 +- boot/bootm_os.c | 2 +- boot/image-board.c | 2 +- boot/image-fdt.c | 55 ++++++++++++++++++-------- common/board_f.c | 9 ++++- include/asm-generic/global_data.h | 7 ++++ include/image.h | 2 +- include/lmb.h | 30 +++++++++++--- lib/efi_loader/efi_dt_fixup.c | 3 +- lib/efi_loader/efi_memory.c | 49 ++++++++--------------- lib/efi_selftest/efi_selftest_memory.c | 21 +++++----- lib/lmb.c | 41 ++++--------------- 13 files changed, 118 insertions(+), 107 deletions(-)
Any more comments on this series?
Quoted 60 lines · Randolph Sapp
Quoted 43 lines · rs
From: Randolph Sapp <[email protected]>
Nitpicks and fixes from the discovery thread on adding PocketBeagle2 support [1]. This does a lot of general setup required for the device, but these modifications themselves aren't device specific. For those specifically interested in PocketBeagle2 support and don't care about these details, my development branch is public [2].
That second patch may provoke some opinions, but honestly if that warning was still present I wouldn't have spent a week poking holes in both the EFI and LMB allocations systems. Please let me know if there is a specific usecase that it breaks though.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/StaticRocket/u-boot/tree/feature/pocketbeagle2v2:
- Remove additional increment and decrement inlmb_free_fdt_regions
- Drop the patch to backfillEFI_CONVENTIONAL_MEMORY
- Adjust the removal loop nitpick patch description
- Change the reserve memory patch to use newend_addr_sp
v3:
- Update lmb flags to use the macro documentation for constants
- Changeefi_mem_sortto uselist_for_each_entry_safe
v4:
- Fix typos in LMB allocation flags macro documentation
- Renameend_addr_sptoinitial_relocaddr
- Keep themap_sysmemdance in the efi u-boot reservation
- Use the active device tree pointed to bygd->fdt_blobto clean up old reservations
v5:
- Keep return value as long inboot_fdt_reserve_region
- Fix formatting ininitial_relocaddrpatchRandolph Sapp (6):
lmb: allocation flags macro documentationboot_fdt_add_mem_rsv_regions: free old dtb reservations
efi_dt_fixup: usefdtdec_get_bool
efi_selftest_memory: check for duplicates first
efi_mem_sort: uselist_for_each_entry_safeinstead
memory: reserve fromstart_addr_sptoinitial_relocaddrQuoted 14 lines
arch/mips/lib/bootm.c | 2 +- boot/bootm.c | 2 +- boot/bootm_os.c | 2 +- boot/image-board.c | 2 +- boot/image-fdt.c | 55 ++++++++++++++++++-------- common/board_f.c | 9 ++++- include/asm-generic/global_data.h | 7 ++++ include/image.h | 2 +- include/lmb.h | 30 +++++++++++--- lib/efi_loader/efi_dt_fixup.c | 3 +- lib/efi_loader/efi_memory.c | 49 ++++++++--------------- lib/efi_selftest/efi_selftest_memory.c | 21 +++++----- lib/lmb.c | 41 ++++--------------- 13 files changed, 118 insertions(+), 107 deletions(-)
Quoted 1 line
Any more comments on this series?
Seen the emails, i got limited time to take a closer look. I'll try doing it withing the week
Cheers
/Ilias