From 93e5e5f4cbefccab206a79f86e98c2c39a9d2440 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Tue, 11 Aug 2026 23:16:47 +0000 Subject: lmb: Return -EFAULT when requested region is not part of memory map lmb_alloc_addr() is documented to return -EINVAL when the requested memory region is not part of the LMB memory map. However, -EINVAL is also used to e.g. indicate that a NULL pointer is passed as the addr parameter or when the requested memory region partially overlaps an existing region. Change lmb_alloc_addr() to return -EFAULT when the requested memory region is not part of the LMB memory map to make the type of error known to callers. Also extend unit tests to validate that the return code has stay the same when the requested memory region partially overlaps. No caller of lmb_alloc_addr() is checking what type of error code is returned, so this change has no intended behavior change. Signed-off-by: Jonas Karlman Reviewed-by: Randolph Sapp --- include/lmb.h | 2 +- lib/lmb.c | 4 +++- test/lib/lmb.c | 10 +++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/lmb.h b/include/lmb.h index ed472e9ef2e..028dabb19e8 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -124,7 +124,7 @@ struct lmb { * Return: 0 on success, -ve value on failure * * When the allocation is of type @LMB_MEM_ALLOC_ADDR, the return value can - * be -EINVAL if the requested memory region is not part of the LMB memory + * be -EFAULT if the requested memory region is not part of the LMB memory * map, and -EEXIST if the requested region is already allocated. */ int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr, diff --git a/lib/lmb.c b/lib/lmb.c index 77440a48486..f7c2e826d06 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -752,9 +752,11 @@ static int _lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) base + size - 1, 1)) /* ok, reserve the memory */ return lmb_reserve(base, size, flags); + + return -EINVAL; } - return -EINVAL; + return -EFAULT; } int lmb_alloc_mem(enum lmb_mem_type type, u64 align, phys_addr_t *addr, diff --git a/test/lib/lmb.c b/test/lib/lmb.c index b6259bef442..b93b903f99f 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -779,11 +779,19 @@ static int test_alloc_addr(struct unit_test_state *uts, const phys_addr_t ram) /* check that allocating outside memory fails */ if (ram_end != 0) { ret = lmb_alloc_addr(ram_end, 1, LMB_NONE); + ut_asserteq(ret, -EFAULT); + ret = lmb_alloc_addr(ram_end - 1, 2, LMB_NOMAP); + ut_asserteq(ret, -EINVAL); + ret = lmb_alloc_addr(ram_end - 1, 2, LMB_NOOVERWRITE); ut_asserteq(ret, -EINVAL); } if (ram != 0) { ret = lmb_alloc_addr(ram - 1, 1, LMB_NONE); - ut_asserteq(ret, -EINVAL); + ut_asserteq(ret, -EFAULT); + ret = lmb_alloc_addr(ram - 1, 2, LMB_NOMAP); + ut_asserteq(ret, -EEXIST); + ret = lmb_alloc_addr(ram - 1, 2, LMB_NOOVERWRITE); + ut_asserteq(ret, -EEXIST); } lmb_pop(&store); -- cgit v1.3.1 From 480644c06e202abec58543aec502f218939fff1e Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Tue, 11 Aug 2026 23:16:48 +0000 Subject: lmb: Return -EFAULT when freeing unallocated memory regions Make lmb_free() return -EFAULT when the requested memory region is not allocated, instead of the generic -1 error value. Document the updated error code in the public API comment and change the LMB unit test to check for the new -EFAULT errno value. Signed-off-by: Jonas Karlman Reviewed-by: Randolph Sapp --- include/lmb.h | 2 ++ lib/lmb.c | 2 +- test/lib/lmb.c | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/lmb.h b/include/lmb.h index 028dabb19e8..157a24baf97 100644 --- a/include/lmb.h +++ b/include/lmb.h @@ -168,6 +168,8 @@ int lmb_is_reserved_flags(phys_addr_t addr, int flags); * @flags: Memory region attributes * * Return: 0 on success, negative error code on failure. + * + * The return value can be -EFAULT when the region has not been allocated. */ long lmb_free(phys_addr_t base, phys_size_t size, u32 flags); diff --git a/lib/lmb.c b/lib/lmb.c index f7c2e826d06..ca00047f624 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -286,7 +286,7 @@ static long _lmb_free(struct alist *lmb_rgn_lst, phys_addr_t base, /* Didn't find the region */ if (i == lmb_rgn_lst->count) - return -1; + return -EFAULT; /* Check to see if we are removing entire region */ if (rgnbegin == base && rgnend == end) { diff --git a/test/lib/lmb.c b/test/lib/lmb.c index b93b903f99f..168c66ae649 100644 --- a/test/lib/lmb.c +++ b/test/lib/lmb.c @@ -477,7 +477,7 @@ static int lib_test_lmb_at_0(struct unit_test_state *uts) 0, 0, 0, 0); /* check that this was an error by freeing b */ ret = lmb_free(b, 4, LMB_NONE); - ut_asserteq(ret, -1); + ut_asserteq(ret, -EFAULT); ASSERT_LMB(mem_lst, used_lst, ram, ram_size, 1, a, ram_size - 4, 0, 0, 0, 0); -- cgit v1.3.1 From 94a09715bf4c8dfdefbefcd341d0c9da04c5a6c1 Mon Sep 17 00:00:00 2001 From: Jonas Karlman Date: Tue, 11 Aug 2026 23:16:49 +0000 Subject: boot: image-fdt: Restore suppression of irrelevant ERROR message The commit 623f6c5b6ab7 ("boot: image-fdt: free old dtb reservations") removed the suppression of ERROR messages when -EINVAL was returned due to the memory region not being part of the LMB memory map. This causes an irrelevant ERROR message during boot, e.g.: Model: Radxa ROCK 3B [...] ERROR: reserving fdt memory region failed (addr=10f000 size=100 flags=2): -22 or Model: Rockchip RK3288 Asus Tinker Board S [...] ERROR: reserving fdt memory region failed (addr=fe000000 size=1000000 flags=4): -22 FDT correctly contains reserved-memory for 10f000 or fe000000 and U-Boot correctly does not make these regions available in the LMB memory map: memory[0] [0x200000-0xefffffff], 0xefe00000 bytes, flags: none memory[1] [0x100000000-0x1ffffffff], 0x100000000 bytes, flags: none or memory[0] [0x0-0x7fffffff], 0x80000000 bytes, flags: none With lmb_alloc_mem() and lmb_free() both returning -EFAULT when the requested memory region is not part of the LMB memory map it should be safe to ignore these errors when FDT memreserve and reserved-memory is being processed. Print -EFAULT errors using a debug message to restore suppression of this irrelevant ERROR message when memory region is not part of the LMB memory map. Fixes: 623f6c5b6ab7 ("boot: image-fdt: free old dtb reservations") Signed-off-by: Jonas Karlman Reviewed-by: Randolph Sapp --- boot/image-fdt.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/boot/image-fdt.c b/boot/image-fdt.c index 9e0e0f93edd..956a3d97c42 100644 --- a/boot/image-fdt.c +++ b/boot/image-fdt.c @@ -91,10 +91,10 @@ static void boot_fdt_handle_region(u64 addr, u64 size, u32 flags, bool free) ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &rsv_addr, size, flags); - if (!ret) { - debug(" %s fdt memory region: addr=%llx size=%llx flags=%x\n", - free ? "freed" : "reserved", (unsigned long long)addr, - (unsigned long long)size, flags); + if (!ret || ret == -EFAULT) { + debug(" %s fdt memory region%s: addr=%llx size=%llx flags=%x ret=%ld\n", + free ? "free" : "reserve", ret ? " failed" : "", + (unsigned long long)addr, (unsigned long long)size, flags, ret); } else { printf("ERROR: %s fdt memory region failed (addr=%llx size=%llx flags=%x): %ld\n", free ? "freeing" : "reserving", (unsigned long long)addr, -- cgit v1.3.1