mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] memory: keep memory_banks sorted
Date: Fri, 28 Nov 2025 18:21:46 +0100	[thread overview]
Message-ID: <20251128172149.36116-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251128172149.36116-1-a.fatoum@pengutronix.de>

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@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




  reply	other threads:[~2025-11-28 17:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-28 17:21 [PATCH 1/2] memory: drop superfluous struct memory_bank::start/size parameters Ahmad Fatoum
2025-11-28 17:21 ` Ahmad Fatoum [this message]
2025-12-01  9:10 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251128172149.36116-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox