[PATCH 2/2] memory: keep memory_banks sorted
Ahmad Fatoum
a.fatoum at pengutronix.de
Fri Nov 28 09:21:46 PST 2025
Failing to sort the memory banks can lead to surprising behavior, e.g.
kernel ending up beyond the lower 4G.
Keep the memory banks sorted, also to support more optimal lookups in
future.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
common/memory.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/common/memory.c b/common/memory.c
index f179a3243ff1..2b1523c5d0bf 100644
--- a/common/memory.c
+++ b/common/memory.c
@@ -202,6 +202,17 @@ static int barebox_grow_memory_bank(struct memory_bank *bank, const char *name,
return 0;
}
+static int sort_memory_banks(struct list_head *a, struct list_head *b)
+{
+ struct resource *res_a = list_entry(a, struct memory_bank, list)->res;
+ struct resource *res_b = list_entry(b, struct memory_bank, list)->res;
+
+ /* banks are requested from iomem_resource, so they can't overlap
+ * and thus comparing just the start is sufficient
+ */
+ return compare3(res_a->start, res_b->start);
+}
+
int barebox_add_memory_bank(const char *name, resource_size_t start,
resource_size_t size)
{
@@ -230,7 +241,7 @@ int barebox_add_memory_bank(const char *name, resource_size_t start,
bank->res = res;
- list_add_tail(&bank->list, &memory_banks);
+ list_add_sort(&bank->list, &memory_banks, sort_memory_banks);
return 0;
}
--
2.47.3
More information about the barebox
mailing list