Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: "Alexandre Ghiti" <alex@ghiti.fr>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Catalin Marinas" <catalin.marinas@arm.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
	"Justin Stitt" <justinstitt@google.com>,
	linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org,
	linux-efi@vger.kernel.org, linux-riscv@lists.infradead.org,
	llvm@lists.linux.dev, "Mark Rutland" <mark.rutland@arm.com>,
	"Bill Wendling" <morbo@google.com>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nick Desaulniers" <ndesaulniers@google.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Paul Walmsley" <pjw@kernel.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Will Deacon" <will@kernel.org>,
	linux-integrity@vger.kernel.org, patches@lists.linux.dev,
	"Piotr Król" <piotr.krol@3mdeb.com>,
	"Ross Philipson" <ross.philipson@gmail.com>
Subject: Re: [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image
Date: Wed, 2 Dec 2026 11:41:38 -0800	[thread overview]
Message-ID: <20261202114138.000076af@oss.qualcomm.com> (raw)
In-Reply-To: <2-v3-22d1a1eb7d2e+17de-arm64_drtm_jgg@nvidia.com>

On Fri, 20 Nov 2026 13:51:55 -0400
Jason Gunthorpe <jgg@nvidia.com> wrote:

> The only use for these values is to trigger a memory free if the boot is
> aborted by efi_pe_entry(), make it clear through the call chain.
> 
> This cleanly allows them to be used to describe something that is not the
> image in the next patch.
> 
> Reorganize efi_kaslr_relocate_kernel() with a efi_kaslr_copy_kernel()
> helper as clang isn't able to grok the original control flow once
> image_addr is only set by allocation.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

One small thing inline.  I think the code flow is a little missleading
with the update of *free_addr and *free_size inside a function that
wasn't responsible for the allocation.

With that changed.

Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>



> diff --git a/drivers/firmware/efi/libstub/kaslr.c b/drivers/firmware/efi/libstub/kaslr.c
> index 7cf68a6123946a..2f1f3e19583def 100644
> --- a/drivers/firmware/efi/libstub/kaslr.c
> +++ b/drivers/firmware/efi/libstub/kaslr.c
> @@ -80,10 +80,30 @@ static bool check_image_region(u64 base, u64 size)
>  	return false;
>  }
>  
> +static void efi_kaslr_copy_kernel(unsigned long image_addr,
> +				  unsigned long *free_addr,
> +				  unsigned long *free_size,
> +				  const struct efi_image_info **image_info)
> +{
> +	unsigned long kernel_base	= efistub_kernel_base(*image_info);
> +	unsigned long kernel_size	= efistub_kernel_size(*image_info);
> +	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
> +	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
> +
> +	memcpy((void *)image_addr, (void *)kernel_base, kernel_size);
> +	*free_addr = image_addr;
> +	*free_size = kernel_memsize;

This feels like it's in slightly the wrong place.  The allocation
was what made these necessary to free not the copy.

Could we set those two just before calling this copy function?
Means duplicating these two lines which isn't ideal.

Or does that not avoid the llvm issue?

> +
> +	efi_icache_sync(image_addr, image_addr + kernel_codesize);
> +	efi_remap_image(image_addr, kernel_memsize, kernel_codesize);
> +
> +	*(unsigned long *)image_info += image_addr - kernel_base;
> +}
> +
>  /**
>   * efi_kaslr_relocate_kernel() - Relocate the kernel (random if KASLR enabled)
> - * @image_addr:		Pointer to variable carrying the image base address
> - * @alloc_size:		Pointer to variable carrying the allocated size
> + * @free_addr:		Pointer to variable carrying the address to free
> + * @free_size:		Pointer to variable carrying the size to free
>   * @image_info:		Pointer to struct efi_image_info pointer
>   * @phys_seed:		Random seed used for the relocation
>   *
> @@ -93,16 +113,15 @@ 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 *alloc_size,
> +efi_status_t efi_kaslr_relocate_kernel(unsigned long *free_addr,
> +				       unsigned long *free_size,
>  				       const struct efi_image_info **image_info,
>  				       u32 phys_seed)
>  {
>  	unsigned long kernel_base	= efistub_kernel_base(*image_info);
> -	unsigned long kernel_size	= efistub_kernel_size(*image_info);
> -	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
>  	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
>  	u64 min_kimg_align		= efi_get_kimg_min_align();
> +	unsigned long image_addr;
>  	efi_status_t status;
>  
>  	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && phys_seed != 0) {
> @@ -111,43 +130,36 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
>  		 * locate the kernel at a randomized offset in physical memory.
>  		 */
>  		status = efi_random_alloc(kernel_memsize, min_kimg_align,
> -					  image_addr, phys_seed,
> +					  &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);
> -	} else {
> -		status = EFI_OUT_OF_RESOURCES;
> -	}
> -
> -	if (status != EFI_SUCCESS) {
> -		if (!check_image_region(kernel_base, kernel_memsize)) {
> -			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
> -		} 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.
> -			 */
> +		if (status == EFI_SUCCESS) {
That means setting free_addr and free_size here...
> +			efi_kaslr_copy_kernel(image_addr, free_addr, free_size,
> +					      image_info);
>  			return EFI_SUCCESS;
>  		}
> -
> -		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");
> -			return status;
> -		}
> +		efi_warn("efi_random_alloc() failed: 0x%lx\n", status);
>  	}
>  
> -	memcpy((void *)*image_addr, (void *)kernel_base, kernel_size);
> -	*alloc_size = kernel_memsize;
> +	/* Try to use the image in place */
> +	if (!check_image_region(kernel_base, kernel_memsize)) {
> +		efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
> +	} 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.
> +		 */
> +		return EFI_SUCCESS;
> +	}
>  
> -	efi_icache_sync(*image_addr, *image_addr + kernel_codesize);
> -	efi_remap_image(*image_addr, kernel_memsize, kernel_codesize);
> +	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");
> +		return status;
> +	}
>  
> -	*(unsigned long *)image_info += *image_addr - kernel_base;
> -
> -	return status;
and here...
> +	efi_kaslr_copy_kernel(image_addr, free_addr, free_size, image_info);
> +	return EFI_SUCCESS;
>  }


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2026-12-02 19:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 17:51 [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 01/11] efi/libstub: Add generic arch callbacks for DRTM Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image Jason Gunthorpe
2026-12-02 19:41   ` Jonathan Cameron [this message]
2026-11-20 17:51 ` [PATCH v3 03/11] efi/libstub: Have efi_kaslr_relocate_kernel() handle extra_size Jason Gunthorpe
2026-11-20 23:33   ` Ard Biesheuvel
2026-11-22 14:41     ` Jason Gunthorpe
2026-12-02 19:48   ` Jonathan Cameron
2026-11-20 17:51 ` [PATCH v3 04/11] efi: Add a __efi_data_handoff section annotation Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 05/11] efi/libstub: Put the stub's writable data in unique sections for DRTM Jason Gunthorpe
2026-11-20 17:51 ` [PATCH v3 06/11] arm64: drtm: Add macro definitions for DEN0113 Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 07/11] arm64: drtm: Update the linker script for EFI_STUB_DRTM Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 08/11] arm64: drtm: Add drtm_entry point to head.S Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 09/11] arm64: drtm: Call UNPROTECT_MEMORY Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 10/11] efi/arm64: Add a way to detect if SMCC 1.1 can be used Jason Gunthorpe
2026-11-20 17:52 ` [PATCH v3 11/11] efi/arm64: Implement ARM64 DRTM in the stub Jason Gunthorpe
2026-12-02 19:33 ` [PATCH v3 00/11] arm64: DRTM, boot portion Jason Gunthorpe

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=20261202114138.000076af@oss.qualcomm.com \
    --to=jonathan.cameron@oss.qualcomm.com \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=corbet@lwn.net \
    --cc=ilias.apalodimas@linaro.org \
    --cc=jgg@nvidia.com \
    --cc=justinstitt@google.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=palmer@dabbelt.com \
    --cc=patches@lists.linux.dev \
    --cc=piotr.krol@3mdeb.com \
    --cc=pjw@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=ross.philipson@gmail.com \
    --cc=skhan@linuxfoundation.org \
    --cc=will@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