From: Ard Biesheuvel <ardb+git@google.com>
To: linux-efi@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org,
Ard Biesheuvel <ardb@kernel.org>,
Jason Gunthorpe <jgg@nvidia.com>, Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Huacai Chen <chenhuacai@kernel.org>,
loongarch@lists.linux.dev, linux-riscv@lists.infradead.org
Subject: [PATCH v4 06/19] efi/libstub: arm: Size the decompressed kernel allocation precisely
Date: Wed, 18 Nov 2026 19:21:12 +0100 [thread overview]
Message-ID: <20261118182105.1633603-27-ardb+git@google.com> (raw)
In-Reply-To: <20261118182105.1633603-21-ardb+git@google.com>
From: Ard Biesheuvel <ardb@kernel.org>
The ARM EFI stub reserves a window for the decompressed kernel, as the
decompressor writes it to memory without regard for other allocations
made while running under the firmware. Currently, this window is
MAX_UNCOMP_KERNEL_SIZE (32 MiB) in size, but it starts 5 pages below the
kernel's start address, to cover the initial page tables. This leaves
32 MiB minus 5 pages for the kernel itself, and nothing checks whether
that is sufficient. Large kernels (e.g., multiplatform builds with debug
options enabled) can exceed this, in which case the decompressed image
and its BSS overrun the reservation into memory that the firmware
regards as free, and which may have been handed out for the FDT, the
initrd or the EFI memory map.
So instead, size the window based on the actual memory footprint of the
kernel: the uncompressed image size, which is stored in the last 4 bytes
of the compressed payload, plus the size of BSS, which is recorded in
the zImage tag table, plus the size of the initial page tables, which is
4 pages, or 5 pages when LPAE is enabled.
While at it, drop the requirement that the window is aligned to a power
of two that is at least TEXT_OFFSET, which means 4 MiB when TEXT_OFFSET
is 0x208000 or 0x308000. The only actual requirement is that the
physical start of the kernel minus TEXT_OFFSET, which becomes
PHYS_OFFSET, is a multiple of 2 MiB, as required by the phys-to-virt
patching code. The reason for the larger alignment was that the existing
logic breaks down when the slack below the kernel exceeds the alignment,
and so this is fixed by making the additional space that is allocated to
accommodate the alignment the larger of 2 MiB and the size of the slack.
This also ensures that PHYS_OFFSET never ends up below the 2 MiB aligned
block containing the start of the allocation, which only requires DRAM
to be 2 MiB aligned rather than 4 MiB aligned.
Finally, free the unused parts at either end of the allocation without
updating alloc_base and alloc_size in place, so that both frees are
derived directly from the original allocation, and return the reserved
window to the caller as is.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
arch/arm/include/asm/efi.h | 15 ++--------
drivers/firmware/efi/libstub/arm32-stub.c | 31 ++++++++++++--------
2 files changed, 20 insertions(+), 26 deletions(-)
diff --git a/arch/arm/include/asm/efi.h b/arch/arm/include/asm/efi.h
index e408399d5f0e..c28508c6e107 100644
--- a/arch/arm/include/asm/efi.h
+++ b/arch/arm/include/asm/efi.h
@@ -56,22 +56,11 @@ void efi_virtmap_unload(void);
/* arch specific definitions used by the stub code */
-/*
- * A reasonable upper bound for the uncompressed kernel size is 32 MBytes,
- * so we will reserve that amount of memory. We have no easy way to tell what
- * the actuall size of code + data the uncompressed kernel will use.
- * If this is insufficient, the decompressor will relocate itself out of the
- * way before performing the decompression.
- */
-#define MAX_UNCOMP_KERNEL_SIZE SZ_32M
-
/*
* phys-to-virt patching requires that the physical to virtual offset is a
- * multiple of 2 MiB. However, using an alignment smaller than TEXT_OFFSET
- * here throws off the memory allocation logic, so let's use the lowest power
- * of two greater than 2 MiB and greater than TEXT_OFFSET.
+ * multiple of 2 MiB.
*/
-#define EFI_PHYS_ALIGN max(UL(SZ_2M), roundup_pow_of_two(TEXT_OFFSET))
+#define EFI_PHYS_ALIGN SZ_2M
/* on ARM, the initrd should be loaded in a lowmem region */
static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
diff --git a/drivers/firmware/efi/libstub/arm32-stub.c b/drivers/firmware/efi/libstub/arm32-stub.c
index 1073dd947516..1ab15d66e709 100644
--- a/drivers/firmware/efi/libstub/arm32-stub.c
+++ b/drivers/firmware/efi/libstub/arm32-stub.c
@@ -76,6 +76,12 @@ void efi_handle_post_ebs_state(void)
&efi_entry_state->sctlr_after_ebs);
}
+/* last unaligned u32 in the image carries the uncompressed size */
+extern u32 input_data_end[] __aligned(1);
+
+/* entry #3 in the zimage magic table carries the kernel's BSS size */
+extern u32 zimage_magic_table[] asm("_table_start");
+
efi_status_t handle_kernel_image(unsigned long *image_addr,
unsigned long *image_size,
unsigned long *reserve_addr,
@@ -83,18 +89,22 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
efi_loaded_image_t *image,
efi_handle_t image_handle)
{
- const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
- int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;
+ unsigned long kernel_memsize = input_data_end[-1] + zimage_magic_table[3];
+ const int pgdir_size = (4 + IS_ENABLED(CONFIG_ARM_LPAE)) * PAGE_SIZE;
+ int alloc_size = pgdir_size + ALIGN(kernel_memsize, EFI_PAGE_SIZE);
+ const int slack = TEXT_OFFSET - pgdir_size;
+ const unsigned long margin = max(EFI_PHYS_ALIGN, slack);
unsigned long alloc_base, kernel_base;
efi_status_t status;
/*
* Allocate space for the decompressed kernel as low as possible.
- * The region should be 16 MiB aligned, but the first 'slack' bytes
+ * The region should be 2 MiB aligned, but the first 'slack' bytes
* are not used by Linux, so we allow those to be occupied by the
* firmware.
*/
- status = efi_low_alloc_above(alloc_size, EFI_PAGE_SIZE, &alloc_base, 0x0);
+ status = efi_low_alloc_above(alloc_size + margin, EFI_PAGE_SIZE,
+ &alloc_base, 0x0);
if (status != EFI_SUCCESS) {
efi_err("Unable to allocate memory for uncompressed kernel.\n");
return status;
@@ -103,7 +113,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
if ((alloc_base % EFI_PHYS_ALIGN) > slack) {
/*
* More than 'slack' bytes are already occupied at the base of
- * the allocation, so we need to advance to the next 16 MiB block.
+ * the allocation, so we need to advance to the next 2 MiB block.
*/
kernel_base = round_up(alloc_base, EFI_PHYS_ALIGN);
efi_info("Free memory starts at 0x%lx, setting kernel_base to 0x%lx\n",
@@ -113,16 +123,11 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
}
*reserve_addr = kernel_base + slack;
- *reserve_size = MAX_UNCOMP_KERNEL_SIZE;
+ *reserve_size = alloc_size;
/* now free the parts that we will not use */
- if (*reserve_addr > alloc_base) {
- efi_bs_call(free_pages, alloc_base,
- (*reserve_addr - alloc_base) / EFI_PAGE_SIZE);
- alloc_size -= *reserve_addr - alloc_base;
- }
- efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
- (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
+ efi_free(*reserve_addr - alloc_base, alloc_base); /* no-op if 0 */
+ efi_free(alloc_base + margin - *reserve_addr, *reserve_addr + alloc_size);
*image_addr = kernel_base + TEXT_OFFSET;
*image_size = 0;
--
2.56.1.708.g77570a6a1d-goog
next prev parent reply other threads:[~2026-11-18 18:23 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 18:21 [PATCH v4 00/19] efi: Expose loaded image properties via info struct Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 01/19] efi/zboot: arm64: Add missing barrier after cache maintenance Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 02/19] riscv: Use _stext not _text to denote the start of kernel code Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 03/19] riscv: Make _text point to the start of the kernel image Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 04/19] efi/riscv: Rename __init_text_end to __inittext_end like other archs Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 05/19] efi/libstub: Avoid _end section marker in library code Ard Biesheuvel
2026-11-18 18:21 ` Ard Biesheuvel [this message]
2026-11-18 18:21 ` [PATCH v4 07/19] efi/libstub: Avoid reserve_addr/_size in efi_kaslr_relocate_kernel() Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 08/19] efi/libstub: Capture loaded image information in struct efi_image_info Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 09/19] efi/libstub: Pass kernel entrypoint down to efi_enter_kernel() Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 10/19] efi/libstub: arm: Simplify handle_kernel_image() Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 11/19] efi/libstub: Drop reserve_addr/_size params from handle_kernel_image() Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 12/19] efi/libstub: arm64: Use struct efi_image_info to access image properties Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 13/19] efi/libstub: loongarch: " Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 14/19] efi/libstub: riscv: " Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 15/19] efi/libstub: Make efi_remap_image() return efi_status_t Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 16/19] efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image() Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 17/19] efi/libstub: arm64: Drop zboot code_size hack Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 18/19] efi: Make config table based primary display handover ARM-only again Ard Biesheuvel
2026-11-18 18:21 ` [PATCH v4 19/19] Revert "efi: earlycon: Reprobe after parsing config tables" Ard Biesheuvel
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=20261118182105.1633603-27-ardb+git@google.com \
--to=ardb+git@google.com \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=chenhuacai@kernel.org \
--cc=jgg@nvidia.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-efi@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=loongarch@lists.linux.dev \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.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