[PATCH v3 02/11] efi/libstub: Use free_addr/size as the arg to handle_kernel_image

Jonathan Cameron jonathan.cameron at oss.qualcomm.com
Wed Dec 2 11:41:38 PST 2026


On Fri, 20 Nov 2026 13:51:55 -0400
Jason Gunthorpe <jgg at 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 at 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 at 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;
>  }




More information about the linux-arm-kernel mailing list