Re: [PATCHv11 3/9] efi/libstub: Implement support for unaccepted memory

From: Ard Biesheuvel
Date: Tue May 16 2023 - 14:01:58 EST


On Sun, 14 May 2023 at 23:13, Kirill A. Shutemov <kirill@xxxxxxxxxxxxx> wrote:
>
> On Sun, May 14, 2023 at 08:08:07AM +0300, Mika Penttilä wrote:
> > > + status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
> > > + sizeof(*unaccepted_table) + bitmap_size,
> > > + (void **)&unaccepted_table);
> >
> >
> > Wonder if EFI_LOADER_DATA guarantees bitmap not to be freed, or should some
> > more persistent type be used. If EFI_LOADER_DATA is enough, maybe a comment
> > why it is safe could be added.
>
> Ughh.. I've lost the hunk that reserves the memory explicitly while
> folding in the patch we discussed with Ard. See below.
>
> But the question is solid.
>
> Ard, do we want to allocate the memory as EFI_RUNTIME_SERVICES_DATA (or
> something else?) that got reserved automatically without additional steps?
>


EFI loader data should be fine here, as long as we reserve it.

EFI runtime services data is intended for allocations that have
significance to the firmware itself, so it gets mapped into the EFI
runtime page tables and on some architectures, it gets removed from
the direct map as well.

The unaccepted bitmap is only accessed by the OS itself, so runtime
services data is really not the right choice. We just have to ensure
the bitmap gets reserved in memblock sufficiently early.

> diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
> index e15a2005ed93..d817e7afd266 100644
> --- a/drivers/firmware/efi/efi.c
> +++ b/drivers/firmware/efi/efi.c
> @@ -765,6 +765,25 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
> }
> }
>
> + if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) &&
> + efi.unaccepted != EFI_INVALID_TABLE_ADDR) {
> + struct efi_unaccepted_memory *unaccepted;
> +
> + unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted));
> + if (unaccepted) {
> + unsigned long size;
> +
> + if (unaccepted->version == 1) {
> + size = sizeof(*unaccepted) + unaccepted->size;
> + memblock_reserve(efi.unaccepted, size);
> + } else {
> + efi.unaccepted = EFI_INVALID_TABLE_ADDR;
> + }
> +
> + early_memunmap(unaccepted, sizeof(*unaccepted));
> + }
> + }
> +
> return 0;
> }
>
> --
> Kiryl Shutsemau / Kirill A. Shutemov