Re: [PATCH] efi/loongarch: Reintroduce efi_relocate_kernel() to relocate kernel

From: Huacai Chen
Date: Wed Apr 05 2023 - 05:58:59 EST


Hi, Ard,

On Wed, Apr 5, 2023 at 4:20 PM Ard Biesheuvel <ardb@xxxxxxxxxx> wrote:
>
> On Wed, 5 Apr 2023 at 10:14, Huacai Chen <chenhuacai@xxxxxxxxxx> wrote:
> >
> > Hi, Ard,
> >
> > What is the status of this patch now?
> >
>
> I'd still like to understand how zboot is affected. It has
>
> static unsigned long alloc_preferred_address(unsigned long alloc_size)
> {
> #ifdef EFI_KIMG_PREFERRED_ADDRESS
> efi_physical_addr_t efi_addr = EFI_KIMG_PREFERRED_ADDRESS;
>
> if (efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
> alloc_size / EFI_PAGE_SIZE, &efi_addr) == EFI_SUCCESS)
> return efi_addr;
> #endif
> return ULONG_MAX;
> }
>
> and if I understand you correctly, we never needed this in the first place?
Technically we don't need this, but I think setting a preferred
address as before may be better (keeping the default behavior the same
as before can avoid some unexpected problem).

Huacai
>
> (I understand this is a separate issue, but it would be nice to get
> rid of EFI_KIMG_PREFERRED_ADDRESS entirely if we can.)
>
>
>
> > On Wed, Mar 1, 2023 at 8:06 PM Huacai Chen <chenhuacai@xxxxxxxxxx> wrote:
> > >
> > > Hi, Youling,
> > >
> > > On Wed, Mar 1, 2023 at 6:08 PM Youling Tang <tangyouling@xxxxxxxxxxx> wrote:
> > > >
> > > > Hi, Huacai
> > > >
> > > > On 03/01/2023 05:30 PM, Huacai Chen wrote:
> > > > > Hi, Youling,
> > > > >
> > > > > On Wed, Mar 1, 2023 at 5:21 PM Youling Tang <tangyouling@xxxxxxxxxxx> wrote:
> > > > >>
> > > > >> Hi, Huacai
> > > > >>
> > > > >> On 03/01/2023 04:55 PM, Huacai Chen wrote:
> > > > >>> Since Linux-6.3, LoongArch supports PIE kernel now, so let's reintroduce
> > > > >>> efi_relocate_kernel() to relocate the core kernel.
> > > > >>
> > > > >> If we strongly rely on the PIE feature after the modification, we need
> > > > >> to modify it as follows,
> > > > > There is no strong rely, efi_relocate_kernel() can also handle the
> > > > > non-PIE kernel.
> > > >
> > > > IMHO, if EFI_KIMG_PREFERRED_ADDRESS is no longer defined as
> > > > `PHYSADDR(VMLINUX_LOAD_ADDRESS)` (physical link address), we need to
> > > > relocate through the RELOCATABLE function, so it is recommended to
> > > > select RELOCATABLE if EFI_STUB is enabled
> > > We can select RELOCATABLE when we modify EFI_KIMG_PREFERRED_ADDRESS.
> > > Before that I prefer to give a chance to build a non-PIE kernel.
> > >
> > > Huacai
> > > >
> > > > Youling.
> > > > >
> > > > > Huacai
> > > > >>
> > > > >> --- a/arch/loongarch/Kconfig
> > > > >> +++ b/arch/loongarch/Kconfig
> > > > >> @@ -137,6 +137,7 @@ config LOONGARCH
> > > > >> select PCI_MSI_ARCH_FALLBACKS
> > > > >> select PCI_QUIRKS
> > > > >> select PERF_USE_VMALLOC
> > > > >> + select RELOCATABLE
> > > > >> select RTC_LIB
> > > > >> select SMP
> > > > >> select SPARSE_IRQ
> > > > >>
> > > > >> or:
> > > > >>
> > > > >> --- a/arch/loongarch/Kconfig
> > > > >> +++ b/arch/loongarch/Kconfig
> > > > >> @@ -360,6 +360,7 @@ config EFI_STUB
> > > > >> default y
> > > > >> depends on EFI
> > > > >> select EFI_GENERIC_STUB
> > > > >> + select RELOCATABLE
> > > > >> help
> > > > >>
> > > > >> Youling.
> > > > >>
> > > > >>>
> > > > >>> Signed-off-by: Huacai Chen <chenhuacai@xxxxxxxxxxx>
> > > > >>> ---
> > > > >>> drivers/firmware/efi/libstub/loongarch-stub.c | 24 ++++++-------------
> > > > >>> 1 file changed, 7 insertions(+), 17 deletions(-)
> > > > >>>
> > > > >>> diff --git a/drivers/firmware/efi/libstub/loongarch-stub.c b/drivers/firmware/efi/libstub/loongarch-stub.c
> > > > >>> index eee7ed43cdfb..72c71ae201f0 100644
> > > > >>> --- a/drivers/firmware/efi/libstub/loongarch-stub.c
> > > > >>> +++ b/drivers/firmware/efi/libstub/loongarch-stub.c
> > > > >>> @@ -21,26 +21,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
> > > > >>> efi_loaded_image_t *image,
> > > > >>> efi_handle_t image_handle)
> > > > >>> {
> > > > >>> - int nr_pages = round_up(kernel_asize, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
> > > > >>> - efi_physical_addr_t kernel_addr = EFI_KIMG_PREFERRED_ADDRESS;
> > > > >>> efi_status_t status;
> > > > >>> + unsigned long kernel_addr = 0;
> > > > >>>
> > > > >>> - /*
> > > > >>> - * Allocate space for the kernel image at the preferred offset. This is
> > > > >>> - * the only location in memory from where we can execute the image, so
> > > > >>> - * no point in falling back to another allocation.
> > > > >>> - */
> > > > >>> - status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
> > > > >>> - EFI_LOADER_DATA, nr_pages, &kernel_addr);
> > > > >>> - if (status != EFI_SUCCESS)
> > > > >>> - return status;
> > > > >>> -
> > > > >>> - *image_addr = EFI_KIMG_PREFERRED_ADDRESS;
> > > > >>> - *image_size = kernel_asize;
> > > > >>> + kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
> > > > >>> +
> > > > >>> + status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
> > > > >>> + EFI_KIMG_PREFERRED_ADDRESS, efi_get_kimg_min_align(), 0x0);
> > > > >>>
> > > > >>> - memcpy((void *)EFI_KIMG_PREFERRED_ADDRESS,
> > > > >>> - (void *)&kernel_offset - kernel_offset,
> > > > >>> - kernel_fsize);
> > > > >>> + *image_addr = kernel_addr;
> > > > >>> + *image_size = kernel_asize;
> > > > >>>
> > > > >>> return status;
> > > > >>> }
> > > > >>>
> > > > >>
> > > >
> > > >