Linux EFI development
 help / color / mirror / Atom feed
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 07/19] efi/libstub: Avoid reserve_addr/_size in efi_kaslr_relocate_kernel()
Date: Wed, 18 Nov 2026 19:21:13 +0100	[thread overview]
Message-ID: <20261118182105.1633603-28-ardb+git@google.com> (raw)
In-Reply-To: <20261118182105.1633603-21-ardb+git@google.com>

From: Ard Biesheuvel <ardb@kernel.org>

Avoid reserve_addr and reserve_size to carry the information about the
allocated space for the relocated kernel, and instead, use a single
alloc_size output parameter that is wired to image_size.

While at it, don't use the alloc_size as an in/out parameter, but only
use it to return the allocated size (and leave it as 0x0 if the image
is going to execute in place)

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/libstub/arm64-stub.c |  3 +-
 drivers/firmware/efi/libstub/efistub.h    |  3 +-
 drivers/firmware/efi/libstub/kaslr.c      | 30 +++++++++-----------
 drivers/firmware/efi/libstub/riscv-stub.c |  4 +--
 4 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/drivers/firmware/efi/libstub/arm64-stub.c b/drivers/firmware/efi/libstub/arm64-stub.c
index 2c3869356147..fe0deae78e5b 100644
--- a/drivers/firmware/efi/libstub/arm64-stub.c
+++ b/drivers/firmware/efi/libstub/arm64-stub.c
@@ -35,10 +35,9 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	kernel_size = _edata - _text;
 	kernel_codesize = __inittext_end - _text;
 	kernel_memsize = kernel_size + (_end - _edata);
-	*reserve_size = kernel_memsize;
 	*image_addr = (unsigned long)_text;
 
-	return efi_kaslr_relocate_kernel(image_addr, reserve_addr, reserve_size,
+	return efi_kaslr_relocate_kernel(image_addr, image_size,
 					 kernel_size, kernel_codesize, kernel_memsize,
 					 efi_kaslr_get_phys_seed(image_handle));
 }
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index f5eec8c12894..5c10815d9172 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -1244,8 +1244,7 @@ const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,
 void efi_remap_image(unsigned long image_base, unsigned alloc_size,
 		     unsigned long code_size);
 efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
-				       unsigned long *reserve_addr,
-				       unsigned long *reserve_size,
+				       unsigned long *alloc_size,
 				       unsigned long kernel_size,
 				       unsigned long kernel_codesize,
 				       unsigned long kernel_memsize,
diff --git a/drivers/firmware/efi/libstub/kaslr.c b/drivers/firmware/efi/libstub/kaslr.c
index b34e4599f0f6..49b05d0c0003 100644
--- a/drivers/firmware/efi/libstub/kaslr.c
+++ b/drivers/firmware/efi/libstub/kaslr.c
@@ -82,9 +82,8 @@ static bool check_image_region(u64 base, u64 size)
 
 /**
  * efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled)
- * @image_addr:		Pointer to the current kernel location
- * @reserve_addr:	Pointer to the relocated kernel location
- * @reserve_size:	Size of the relocated kernel
+ * @image_addr:		Pointer to variable carrying the image base address
+ * @alloc_size:		Pointer to variable carrying the allocated size
  * @kernel_size:	Size of the text + data
  * @kernel_codesize:	Size of the text
  * @kernel_memsize:	Size of the text + data + bss
@@ -97,13 +96,13 @@ static bool check_image_region(u64 base, u64 size)
  * Return:	status code, EFI_SUCCESS if relocation is successful
  */
 efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
-				       unsigned long *reserve_addr,
-				       unsigned long *reserve_size,
+				       unsigned long *alloc_size,
 				       unsigned long kernel_size,
 				       unsigned long kernel_codesize,
 				       unsigned long kernel_memsize,
 				       u32 phys_seed)
 {
+	unsigned long kernel_base = *image_addr;
 	efi_status_t status;
 	u64 min_kimg_align = efi_get_kimg_min_align();
 
@@ -112,8 +111,8 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
 		 * If KASLR is enabled, and we have some randomness available,
 		 * locate the kernel at a randomized offset in physical memory.
 		 */
-		status = efi_random_alloc(*reserve_size, min_kimg_align,
-					  reserve_addr, phys_seed,
+		status = efi_random_alloc(kernel_memsize, min_kimg_align,
+					  image_addr, phys_seed,
 					  EFI_LOADER_CODE, 0, EFI_ALLOC_LIMIT);
 		if (status != EFI_SUCCESS)
 			efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
@@ -122,33 +121,32 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
 	}
 
 	if (status != EFI_SUCCESS) {
-		if (!check_image_region(*image_addr, kernel_memsize)) {
+		if (!check_image_region(kernel_base, kernel_memsize)) {
 			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
-		} else if (IS_ALIGNED(*image_addr, min_kimg_align) &&
-			   (*image_addr + kernel_memsize) < EFI_ALLOC_LIMIT) {
+		} else if (IS_ALIGNED(kernel_base, min_kimg_align) &&
+			   (kernel_base + kernel_memsize) < EFI_ALLOC_LIMIT) {
 			/*
 			 * Just execute from wherever we were loaded by the
 			 * UEFI PE/COFF loader if the placement is suitable.
 			 */
-			*reserve_size = 0;
 			return EFI_SUCCESS;
 		}
 
-		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
+		status = efi_allocate_pages_aligned(kernel_memsize, image_addr,
 						    ULONG_MAX, min_kimg_align,
 						    EFI_LOADER_CODE);
 
 		if (status != EFI_SUCCESS) {
 			efi_err("Failed to relocate kernel\n");
-			*reserve_size = 0;
 			return status;
 		}
 	}
 
-	memcpy((void *)*reserve_addr, (void *)*image_addr, kernel_size);
-	*image_addr = *reserve_addr;
+	memcpy((void *)*image_addr, (void *)kernel_base, kernel_size);
+	*alloc_size = kernel_memsize;
+
 	efi_icache_sync(*image_addr, *image_addr + kernel_codesize);
-	efi_remap_image(*image_addr, *reserve_size, kernel_codesize);
+	efi_remap_image(*image_addr, kernel_memsize, kernel_codesize);
 
 	return status;
 }
diff --git a/drivers/firmware/efi/libstub/riscv-stub.c b/drivers/firmware/efi/libstub/riscv-stub.c
index 5cbc50a814e1..d0fc04d21b56 100644
--- a/drivers/firmware/efi/libstub/riscv-stub.c
+++ b/drivers/firmware/efi/libstub/riscv-stub.c
@@ -37,10 +37,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	kernel_codesize = __inittext_end - _start;
 	kernel_memsize = kernel_size + (_end - _edata);
 	*image_addr = (unsigned long)_start;
-	*reserve_size = kernel_memsize;
 
-	status = efi_kaslr_relocate_kernel(image_addr,
-					   reserve_addr, reserve_size,
+	status = efi_kaslr_relocate_kernel(image_addr, image_size,
 					   kernel_size, kernel_codesize, kernel_memsize,
 					   efi_kaslr_get_phys_seed(image_handle));
 	if (status != EFI_SUCCESS)
-- 
2.56.1.708.g77570a6a1d-goog


  parent reply	other threads:[~2026-11-18 18:22 UTC|newest]

Thread overview: 22+ 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:28   ` sashiko-bot
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 ` [PATCH v4 06/19] efi/libstub: arm: Size the decompressed kernel allocation precisely Ard Biesheuvel
2026-11-18 18:41   ` sashiko-bot
2026-11-18 18:21 ` Ard Biesheuvel [this message]
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-28-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