mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Chali Anis <chalianis1@gmail.com>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	 BAREBOX <barebox@lists.infradead.org>
Cc: Chali Anis <chalianis1@gmail.com>
Subject: [PATCH 13/15] efi: payload: earlymem: allocate only the barebox needs in term of memory.
Date: Fri, 19 Sep 2025 23:03:22 -0400	[thread overview]
Message-ID: <20250919-efi-loader-v1-13-dd8cdafb9067@gmail.com> (raw)
In-Reply-To: <20250919-efi-loader-v1-0-dd8cdafb9067@gmail.com>

Since an EFI malloc callback is available to provide additional memory
on demand, limit early allocations to what barebox strictly requires.
This ensures more deterministic behavior and reduces the risk of OOM
conditions.

Signed-off-by: Chali Anis <chalianis1@gmail.com>
---
 efi/payload/early-mem.c    | 18 ++++--------------
 efi/payload/entry-multi.c  |  5 ++---
 efi/payload/entry-single.c |  5 ++---
 include/efi/efi-payload.h  |  2 +-
 4 files changed, 9 insertions(+), 21 deletions(-)

diff --git a/efi/payload/early-mem.c b/efi/payload/early-mem.c
index 150a2a595afcdb69026f98c53f9a3bf3425da999..c5e94b33a756f3b8612eb261de27b99b46b0abba 100644
--- a/efi/payload/early-mem.c
+++ b/efi/payload/early-mem.c
@@ -6,27 +6,17 @@
 #include <efi/efi-payload.h>
 
 void *efi_earlymem_alloc(const struct efi_system_table *sys_table,
-			 size_t *memsize, enum efi_memory_type mem_type)
+			 size_t memsize, enum efi_memory_type mem_type)
 {
 	struct efi_boot_services *bs = sys_table->boottime;
 	efi_physical_addr_t mem;
 	efi_status_t efiret;
-	size_t m_sz;
 
-	if (IS_ENABLED(CONFIG_X86))
-		m_sz = SZ_512M;
-	else
-		m_sz = SZ_256M;
-
-	for (*memsize = m_sz; *memsize >= SZ_8M; *memsize /= 2) {
-		efiret  = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES, mem_type,
-					     *memsize / EFI_PAGE_SIZE, &mem);
-		if (!EFI_ERROR(efiret) || efiret != EFI_OUT_OF_RESOURCES)
-			break;
-	}
+	efiret  = bs->allocate_pages(EFI_ALLOCATE_ANY_PAGES, mem_type,
+					     memsize / EFI_PAGE_SIZE, &mem);
 	if (EFI_ERROR(efiret))
 		panic("failed to allocate %zu byte memory pool: 0x%lx\n",
-		      *memsize, efiret);
+		      memsize, efiret);
 
 	return efi_phys_to_virt(mem);
 }
diff --git a/efi/payload/entry-multi.c b/efi/payload/entry-multi.c
index d5d54cdf70a141428edf35a5fcde45ff7b59a61e..82f3dfffe8709f9a4275c7f9ec246be979e605cd 100644
--- a/efi/payload/entry-multi.c
+++ b/efi/payload/entry-multi.c
@@ -23,7 +23,6 @@ static void efi_putc(void *ctx, int ch)
 
 void __efistub_efi_pe_entry(void *image, struct efi_system_table *sys_table)
 {
-	size_t memsize;
 	void *mem;
 	static struct barebox_efi_data efidata;
 
@@ -37,7 +36,7 @@ void __efistub_efi_pe_entry(void *image, struct efi_system_table *sys_table)
 
 	handoff_data_add(HANDOFF_DATA_EFI, &efidata, sizeof(efidata));
 
-	mem = efi_earlymem_alloc(sys_table, &memsize, EFI_LOADER_CODE);
+	mem = efi_earlymem_alloc(sys_table, SZ_16M, EFI_BOOT_SERVICES_CODE);
 
-	barebox_pbl_entry((uintptr_t)mem, memsize, NULL);
+	barebox_pbl_entry((uintptr_t)mem, SZ_16M, NULL);
 }
diff --git a/efi/payload/entry-single.c b/efi/payload/entry-single.c
index 8600bd845c49719da3ad62c5f5bf8d0b547aed74..f481d0942ba5dc96b2f97079c4e7575fc986e538 100644
--- a/efi/payload/entry-single.c
+++ b/efi/payload/entry-single.c
@@ -17,7 +17,6 @@
 void efi_main(efi_handle_t image, struct efi_system_table *sys_table)
 {
 	efi_status_t efiret;
-	size_t memsize;
 	void *mem;
 
 #ifdef DEBUG
@@ -37,9 +36,9 @@ void efi_main(efi_handle_t image, struct efi_system_table *sys_table)
 		BS->handle_protocol(efi_loaded_image->device_handle,
 				&efi_device_path_protocol_guid, (void **)&efi_device_path);
 
-	mem = efi_earlymem_alloc(sys_table, &memsize, EFI_LOADER_DATA);
+	mem = efi_earlymem_alloc(sys_table, SZ_16M, EFI_BOOT_SERVICES_DATA);
 
-	mem_malloc_init(mem, mem + memsize - 1);
+	mem_malloc_init(mem, mem + SZ_16M - 1);
 
 	start_barebox();
 }
diff --git a/include/efi/efi-payload.h b/include/efi/efi-payload.h
index d8e66a187a870fcae9288d9c65984896e5196c31..337471117d5acecccfe8526ef470bb84429d31aa 100644
--- a/include/efi/efi-payload.h
+++ b/include/efi/efi-payload.h
@@ -32,7 +32,7 @@ int efi_set_variable(char *name, efi_guid_t *vendor, uint32_t attributes,
 int efi_set_variable_usec(char *name, efi_guid_t *vendor, uint64_t usec);
 
 void *efi_earlymem_alloc(const struct efi_system_table *sys_table,
-			 size_t *memsize, enum efi_memory_type mem_type);
+			 size_t memsize, enum efi_memory_type mem_type);
 
 int efi_initrd_register(void *initrd, size_t initrd_size);
 void efi_initrd_unregister(void);

-- 
2.34.1




  parent reply	other threads:[~2025-09-20  3:04 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-20  3:03 [PATCH 00/15] Implement efi loader to run barebox as efi payload app and then load efi stubed application/kernels chalianis1
2025-09-20  3:03 ` [PATCH 01/15] efi: payload: split image handling from legacy handover boot support chalianis1
2025-09-20  3:03 ` [PATCH 02/15] efi: payload: add support for efi stub boot chalianis1
2025-09-20  3:03 ` [PATCH 03/15] efi: payload: add support for fit image chalianis1
2025-09-20  3:03 ` [PATCH 04/15] efi: payload: make selectable without COMPILE_TEST chalianis1
2025-09-20  3:03 ` [PATCH 05/15] arm: efi: add a generic defconfig for v8 efi payload, Chali Anis
2025-09-20  3:03 ` [PATCH 06/15] efi: payload: initrd: implement efi initrd media protocol Chali Anis
2025-09-20  3:03 ` [PATCH 07/15] common: filetype: add x86 linux filetype Chali Anis
2025-09-20  3:03 ` [PATCH 08/15] efi: payload: early-mem: helps to correctly boot x86 linux Chali Anis
2025-09-20  3:03 ` [PATCH 09/15] efi: payload: bootm: add x86 efi stub boot support Chali Anis
2025-09-20  3:03 ` [PATCH 10/15] efi: payload: x86: enable the possibility to efi stub bootm for x86 Chali Anis
2025-09-20  3:03 ` [PATCH 11/15] malloc: tlsf: efi: add a fallback for allocate more memory when we are in efi Chali Anis
2025-09-20  3:03 ` [PATCH 12/15] configs: efi: arm64: x86: enable the use of efi malloc fallback Chali Anis
2025-09-20  3:03 ` Chali Anis [this message]
2025-09-20  3:03 ` [PATCH 14/15] ARM: cpu: allow selecting CPU_V7/CPU_V8 directly chalianis1
2025-09-20  3:03 ` [PATCH 15/15] efi: payload: bootm: remove redundant reallocations in image loader Chali Anis
2025-09-22  7:37 ` [PATCH 00/15] Implement efi loader to run barebox as efi payload app and then load efi stubed application/kernels Ahmad Fatoum

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=20250919-efi-loader-v1-13-dd8cdafb9067@gmail.com \
    --to=chalianis1@gmail.com \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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