From 56f186a68b35e8b45136fd44b69c554a0bb0ce35 Mon Sep 17 00:00:00 2001 From: Sughosh Ganu Date: Mon, 3 Mar 2025 19:02:26 +0530 Subject: lmb: check if a region can be reserved by lmb_reserve() The logic used in lmb_alloc() takes into consideration the existing reserved regions, and ensures that the allocated region does not overlap with any existing allocated regions. The lmb_reserve() function is not doing any such checks -- the requested region might overlap with an existing region. This also shows up with lmb_alloc_addr() as this function ends up calling lmb_reserve(). Add a function which checks if the region requested is overlapping with an existing reserved region, and allow for the reservation to happen only if both the regions have LMB_NONE flag, which allows re-requesting of the region. In any other scenario of an overlap, have lmb_reserve() return -EEXIST, implying that the requested region is already reserved. Add corresponding test cases which check for overlapping reservation requests made through lmb_reserve() and lmb_alloc_addr(). And while here, fix some of the comments in the test function being touched. Signed-off-by: Sughosh Ganu Reviewed-by: Heinrich Schuchardt --- lib/lmb.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/lmb.c') diff --git a/lib/lmb.c b/lib/lmb.c index 93fc1bea07c..32c787f8adf 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -561,6 +561,39 @@ static __maybe_unused void lmb_reserve_common_spl(void) } } +/** + * lmb_can_reserve_region() - check if the region can be reserved + * @base: base address of region to be reserved + * @size: size of region to be reserved + * @flags: flag of the region to be reserved + * + * Go through all the reserved regions and ensure that the requested + * region does not overlap with any existing regions. An overlap is + * allowed only when the flag of the request region and the existing + * region is LMB_NONE. + * + * Return: true if region can be reserved, false otherwise + */ +static bool lmb_can_reserve_region(phys_addr_t base, phys_size_t size, + u32 flags) +{ + uint i; + struct lmb_region *lmb_reserved = lmb.used_mem.data; + + for (i = 0; i < lmb.used_mem.count; i++) { + u32 rgnflags = lmb_reserved[i].flags; + phys_addr_t rgnbase = lmb_reserved[i].base; + phys_size_t rgnsize = lmb_reserved[i].size; + + if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { + if (flags != LMB_NONE || flags != rgnflags) + return false; + } + } + + return true; +} + void lmb_add_memory(void) { int i; @@ -633,6 +666,9 @@ long lmb_reserve(phys_addr_t base, phys_size_t size, u32 flags) long ret = 0; struct alist *lmb_rgn_lst = &lmb.used_mem; + if (!lmb_can_reserve_region(base, size, flags)) + return -EEXIST; + ret = lmb_add_region_flags(lmb_rgn_lst, base, size, flags); if (ret) return ret; -- cgit v1.2.3 From e0a7ea3725d8931a2c31f29ed665ec1f22e37172 Mon Sep 17 00:00:00 2001 From: Sughosh Ganu Date: Mon, 3 Mar 2025 19:02:27 +0530 Subject: lmb: handle scenario of encompassing overlap The lmb_fix_over_lap_regions() function is called if the added region overlaps with an existing region. The function then fixes the overlap and removes the redundant region. However, it makes certain assumptions. One assumption is that the overlap would not encompass the existing region. Another assumption is that the overlap only occurs between two regions -- the scenario of the added region overlapping multiple existing regions is not being handled. Handle these cases by instead calling lmb_resize_regions(). Also remove the now superfluous lmb_fix_over_lap_regions(). Signed-off-by: Sughosh Ganu Reviewed-by: Heinrich Schuchardt --- lib/lmb.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'lib/lmb.c') diff --git a/lib/lmb.c b/lib/lmb.c index 32c787f8adf..26d9cafef41 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -96,25 +96,6 @@ static void lmb_coalesce_regions(struct alist *lmb_rgn_lst, unsigned long r1, lmb_remove_region(lmb_rgn_lst, r2); } -/*Assumption : base addr of region 1 < base addr of region 2*/ -static void lmb_fix_over_lap_regions(struct alist *lmb_rgn_lst, - unsigned long r1, unsigned long r2) -{ - struct lmb_region *rgn = lmb_rgn_lst->data; - - phys_addr_t base1 = rgn[r1].base; - phys_size_t size1 = rgn[r1].size; - phys_addr_t base2 = rgn[r2].base; - phys_size_t size2 = rgn[r2].size; - - if (base1 + size1 > base2 + size2) { - printf("This will not be a case any time\n"); - return; - } - rgn[r1].size = base2 + size2 - base1; - lmb_remove_region(lmb_rgn_lst, r2); -} - static long lmb_resize_regions(struct alist *lmb_rgn_lst, unsigned long idx_start, phys_addr_t base, phys_size_t size) @@ -235,8 +216,15 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); coalesced++; } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) { - /* fix overlapping area */ - lmb_fix_over_lap_regions(lmb_rgn_lst, i, i + 1); + /* fix overlapping areas */ + phys_addr_t rgnbase = rgn[i].base; + phys_size_t rgnsize = rgn[i].size; + + ret = lmb_resize_regions(lmb_rgn_lst, i, + rgnbase, rgnsize); + if (ret < 0) + return -1; + coalesced++; } } -- cgit v1.2.3 From 6e4df5886d27cff043561c8087f373e26cfe9f34 Mon Sep 17 00:00:00 2001 From: Sughosh Ganu Date: Mon, 3 Mar 2025 19:02:28 +0530 Subject: lmb: check for a region's coalescing with all existing regions The lmb_add_region_flags() first checks if the new region to be added can be coalesced with existing regions. The check stops if the two regions are adjecent but their flags do not match. However, it is possible that the newly added region might be adjacent with the next existing region and with matching flags. Check for this possibility by not breaking out of the loop. Signed-off-by: Sughosh Ganu Reviewed-by: Heinrich Schuchardt --- lib/lmb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/lmb.c') diff --git a/lib/lmb.c b/lib/lmb.c index 26d9cafef41..53af96fa2a9 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -190,7 +190,7 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, break; } else if (ret < 0) { if (flags != rgnflags) - break; + continue; rgn[i].size += size; coalesced++; break; -- cgit v1.2.3 From f5f0a0287134223c16ce64303df60c3708684e6a Mon Sep 17 00:00:00 2001 From: Sughosh Ganu Date: Mon, 3 Mar 2025 19:02:29 +0530 Subject: lmb: remove superfluous address overlap check from lmb_add_region_flags() U-Boot allows re-use of already reserved memory through the lmb_reserve() and lmb_alloc_addr() API's. This memory re-use is allowed only when the flag of the existing reserved region and that of the requested region is LMB_NONE. A check was put in the lmb_add_region_flags() in commit 8b8b35a4f5e to handle the scenario where an already reserved region was re-requested with region flag other than LMB_NONE -- the function then returns -EEXIST in such a scenario. The lmb_reserve() function now does a check for a reservation request with existing reserved regions, and returns -EEXIST in case of an overlap but when the flag check fails. Remove this now redundant check from lmb_add_region_flags(). Signed-off-by: Sughosh Ganu --- lib/lmb.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'lib/lmb.c') diff --git a/lib/lmb.c b/lib/lmb.c index 53af96fa2a9..9af942c6b42 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -195,9 +195,6 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, coalesced++; break; } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { - if (flags != LMB_NONE) - return -EEXIST; - ret = lmb_resize_regions(lmb_rgn_lst, i, base, size); if (ret < 0) return -1; -- cgit v1.2.3 From fa5b4f5a5f99d1f4ab995d07845d2bff50aaabb7 Mon Sep 17 00:00:00 2001 From: Sughosh Ganu Date: Mon, 3 Mar 2025 19:02:30 +0530 Subject: lmb: use a common function to check if regions overlap or are adjacent The functions to check if the two said regions are adjacent or overlap are pretty similar in nature. Club the functionality into a single function lmb_regions_check() and return the appropriate return value to signify this aspect. Signed-off-by: Sughosh Ganu --- lib/lmb.c | 71 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 30 deletions(-) (limited to 'lib/lmb.c') diff --git a/lib/lmb.c b/lib/lmb.c index 9af942c6b42..b42a512f6c0 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -23,6 +23,9 @@ DECLARE_GLOBAL_DATA_PTR; +#define LMB_RGN_OVERLAP 1 +#define LMB_RGN_ADJACENT 2 + /* * The following low level LMB functions must not access the global LMB memory * map since they are also used to manage IOVA memory maps in iommu drivers like @@ -49,8 +52,22 @@ static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1, return 0; } -static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1, - unsigned long r2) +/** + * lmb_regions_check() - Check if the regions overlap, or are adjacent + * @lmb_rgn_lst: List of LMB regions + * @r1: First region to check + * @r2: Second region to check + * + * Check if the two regions with matching flags, r1 and r2 are + * adjacent to each other, or if they overlap. + * + * Return: + * * %LMB_RGN_OVERLAP - Regions overlap + * * %LMB_RGN_ADJACENT - Regions adjacent to each other + * * 0 - Neither of the above, or flags mismatch + */ +static long lmb_regions_check(struct alist *lmb_rgn_lst, unsigned long r1, + unsigned long r2) { struct lmb_region *rgn = lmb_rgn_lst->data; phys_addr_t base1 = rgn[r1].base; @@ -58,19 +75,15 @@ static long lmb_regions_overlap(struct alist *lmb_rgn_lst, unsigned long r1, phys_addr_t base2 = rgn[r2].base; phys_size_t size2 = rgn[r2].size; - return lmb_addrs_overlap(base1, size1, base2, size2); -} + if (rgn[r1].flags != rgn[r2].flags) + return 0; -static long lmb_regions_adjacent(struct alist *lmb_rgn_lst, unsigned long r1, - unsigned long r2) -{ - struct lmb_region *rgn = lmb_rgn_lst->data; - phys_addr_t base1 = rgn[r1].base; - phys_size_t size1 = rgn[r1].size; - phys_addr_t base2 = rgn[r2].base; - phys_size_t size2 = rgn[r2].size; + if (lmb_addrs_overlap(base1, size1, base2, size2)) + return LMB_RGN_OVERLAP; + else if (lmb_addrs_adjacent(base1, size1, base2, size2)) + return LMB_RGN_ADJACENT; - return lmb_addrs_adjacent(base1, size1, base2, size2); + return 0; } static void lmb_remove_region(struct alist *lmb_rgn_lst, unsigned long r) @@ -207,23 +220,21 @@ static long lmb_add_region_flags(struct alist *lmb_rgn_lst, phys_addr_t base, } if (lmb_rgn_lst->count && i < lmb_rgn_lst->count - 1) { - rgn = lmb_rgn_lst->data; - if (rgn[i].flags == rgn[i + 1].flags) { - if (lmb_regions_adjacent(lmb_rgn_lst, i, i + 1)) { - lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); - coalesced++; - } else if (lmb_regions_overlap(lmb_rgn_lst, i, i + 1)) { - /* fix overlapping areas */ - phys_addr_t rgnbase = rgn[i].base; - phys_size_t rgnsize = rgn[i].size; - - ret = lmb_resize_regions(lmb_rgn_lst, i, - rgnbase, rgnsize); - if (ret < 0) - return -1; - - coalesced++; - } + ret = lmb_regions_check(lmb_rgn_lst, i, i + 1); + if (ret == LMB_RGN_ADJACENT) { + lmb_coalesce_regions(lmb_rgn_lst, i, i + 1); + coalesced++; + } else if (ret == LMB_RGN_OVERLAP) { + /* fix overlapping areas */ + phys_addr_t rgnbase = rgn[i].base; + phys_size_t rgnsize = rgn[i].size; + + ret = lmb_resize_regions(lmb_rgn_lst, i, + rgnbase, rgnsize); + if (ret < 0) + return -1; + + coalesced++; } } -- cgit v1.2.3 From 2bf5811e22efffe37bf5dccb8d13529c51fc65dd Mon Sep 17 00:00:00 2001 From: Sughosh Ganu Date: Mon, 3 Mar 2025 19:02:31 +0530 Subject: lmb: optimise the lmb allocation functions The actual logic to allocate a region of memory is in the _lmb_alloc_base() function. The lmb_alloc() API function calls lmb_alloc_base(), which then calls _lmb_alloc_base() to do the allocation. Instead, call the _lmb_alloc_base() directly from both the allocation API's, and move the error message to the _lmb_alloc_base(). Signed-off-by: Sughosh Ganu --- lib/lmb.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'lib/lmb.c') diff --git a/lib/lmb.c b/lib/lmb.c index b42a512f6c0..981ea1b2ca0 100644 --- a/lib/lmb.c +++ b/lib/lmb.c @@ -724,26 +724,22 @@ static phys_addr_t _lmb_alloc_base(phys_size_t size, ulong align, base = ALIGN_DOWN(res_base - size, align); } } + + log_debug("%s: Failed to allocate 0x%lx bytes below 0x%lx\n", + __func__, (ulong)size, (ulong)max_addr); + return 0; } phys_addr_t lmb_alloc(phys_size_t size, ulong align) { - return lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE); + return _lmb_alloc_base(size, align, LMB_ALLOC_ANYWHERE, LMB_NONE); } phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr, uint flags) { - phys_addr_t alloc; - - alloc = _lmb_alloc_base(size, align, max_addr, flags); - - if (alloc == 0) - printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", - (ulong)size, (ulong)max_addr); - - return alloc; + return _lmb_alloc_base(size, align, max_addr, flags); } phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size, u32 flags) -- cgit v1.2.3