From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: Simon Glass <sjg@chromium.org>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Sughosh Ganu <sughosh.ganu@linaro.org>,
Tom Rini <trini@konsulko.com>,
U-Boot Mailing List <u-boot@lists.denx.de>
Subject: Re: [PATCH 5/6] efi: Use malloc() for the EFI pool
Date: Thu, 25 Jul 2024 17:49:49 +0200 [thread overview]
Message-ID: <72e02ed6-f8c3-4caa-8ae6-2cef00a2ddd2@gmx.de> (raw)
In-Reply-To: <20240725135629.3505072-6-sjg@chromium.org>
On 25.07.24 15:56, Simon Glass wrote:
> This API call is intended for allocating small amounts of memory,
> similar to malloc(). The current implementation rounds up to whole pages
> which can waste large amounts of memory. It also implements its own
> malloc()-style header on each block.
>
> Use U-Boot's built-in malloc() instead, to avoid these problems:
>
> - it should normally be large enough for pool allocations
> - if it isn't we can enforce a minimum size for boards which use
> EFI_LOADER
> - the existing mechanism may create an unwatned entry in the memory map
> - it is used for most EFI allocations already
>
> One side effect is that this seems to be showing up some bugs in the
> EFI code, since the malloc() pool becomes corrupted with some tests.
> This has likely crept in due to the very large gaps between allocations
> (around 4KB), which provides a lot of leeway when the allocation size is
> too small. Work around this by increasing the size for now, until these
> (presumed) bugs are located.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> lib/efi_loader/efi_memory.c | 93 ++++++++-----------------------------
> 1 file changed, 19 insertions(+), 74 deletions(-)
>
> diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c
> index 2945f5648c7..fabe9e3a87a 100644
> --- a/lib/efi_loader/efi_memory.c
> +++ b/lib/efi_loader/efi_memory.c
> @@ -80,45 +80,6 @@ static LIST_HEAD(efi_mem);
> void *efi_bounce_buffer;
> #endif
>
> -/**
> - * struct efi_pool_allocation - memory block allocated from pool
> - *
> - * @num_pages: number of pages allocated
> - * @checksum: checksum
> - * @data: allocated pool memory
> - *
> - * U-Boot services each UEFI AllocatePool() request as a separate
> - * (multiple) page allocation. We have to track the number of pages
> - * to be able to free the correct amount later.
> - *
> - * The checksum calculated in function checksum() is used in FreePool() to avoid
> - * freeing memory not allocated by AllocatePool() and duplicate freeing.
> - *
> - * EFI requires 8 byte alignment for pool allocations, so we can
> - * prepend each allocation with these header fields.
> - */
> -struct efi_pool_allocation {
> - u64 num_pages;
> - u64 checksum;
> - char data[] __aligned(ARCH_DMA_MINALIGN);
> -};
> -
> -/**
> - * checksum() - calculate checksum for memory allocated from pool
> - *
> - * @alloc: allocation header
> - * Return: checksum, always non-zero
> - */
> -static u64 checksum(struct efi_pool_allocation *alloc)
> -{
> - u64 addr = (uintptr_t)alloc;
> - u64 ret = (addr >> 32) ^ (addr << 32) ^ alloc->num_pages ^
> - EFI_ALLOC_POOL_MAGIC;
> - if (!ret)
> - ++ret;
> - return ret;
> -}
> -
> /**
> * efi_mem_cmp() - comparator function for sorting memory map
> *
> @@ -681,13 +642,10 @@ void *efi_alloc_aligned_pages(u64 len, int memory_type, size_t align)
> * @buffer: allocated memory
> * Return: status code
> */
> -efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size, void **buffer)
> +efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size,
> + void **buffer)
> {
> - efi_status_t r;
> - u64 addr;
> - struct efi_pool_allocation *alloc;
> - u64 num_pages = efi_size_in_pages(size +
> - sizeof(struct efi_pool_allocation));
> + void *ptr;
>
> if (!check_allowed())
> return EFI_UNSUPPORTED;
> @@ -700,16 +658,21 @@ efi_status_t efi_allocate_pool(enum efi_memory_type pool_type, efi_uintn_t size,
> return EFI_SUCCESS;
> }
Unfortunately this patch does not match the UEFI specification:
Two different memory types cannot reside in the same memory page. You
have to keep track of the memory type of each allocated memory page and
report it in GetMemoryMap().
The specification is available https://uefi.org/specifications.
>
> - r = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, pool_type, num_pages,
> - &addr);
> - if (r == EFI_SUCCESS) {
> - alloc = (struct efi_pool_allocation *)(uintptr_t)addr;
> - alloc->num_pages = num_pages;
> - alloc->checksum = checksum(alloc);
> - *buffer = alloc->data;
> - }
> + /*
> + * Some tests crash on qemu_arm etc. if the correct size is allocated.
> + * Adding 0x10 seems to fix test_efi_selftest_device_tree
> + * Increasing it to 0x20 seems to fix test_efi_selftest_base except
> + * for riscv64 (in CI only). But 0x100 fixes CI too.
> + *
> + * This workaround can be dropped once these problems are resolved
> + */
> + ptr = memalign(8, size + 0x100);
We were using ARCH_DMA_MINALIGN before this patch.
If we are overwriting allocated memory, we must fix that instead of
increasing size. Do you have a git tag showing the problem?
Best regards
Heinrich
> + if (!ptr)
> + return EFI_OUT_OF_RESOURCES;
> +
> + *buffer = ptr;
>
> - return r;
> + return EFI_SUCCESS;
> }
>
> /**
> @@ -742,30 +705,12 @@ void *efi_alloc(size_t size)
> */
> efi_status_t efi_free_pool(void *buffer)
> {
> - efi_status_t ret;
> - struct efi_pool_allocation *alloc;
> -
> if (!buffer)
> return EFI_INVALID_PARAMETER;
>
> - ret = efi_check_allocated((uintptr_t)buffer, true);
> - if (ret != EFI_SUCCESS)
> - return ret;
> -
> - alloc = container_of(buffer, struct efi_pool_allocation, data);
> -
> - /* Check that this memory was allocated by efi_allocate_pool() */
> - if (((uintptr_t)alloc & EFI_PAGE_MASK) ||
> - alloc->checksum != checksum(alloc)) {
> - printf("%s: illegal free 0x%p\n", __func__, buffer);
> - return EFI_INVALID_PARAMETER;
> - }
> - /* Avoid double free */
> - alloc->checksum = 0;
> -
> - ret = efi_free_pages((uintptr_t)alloc, alloc->num_pages);
> + free(buffer);
>
> - return ret;
> + return EFI_SUCCESS;
> }
>
> /**
next prev parent reply other threads:[~2024-07-25 15:50 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-25 13:56 [PATCH 0/6] efi: Start tidying up memory management Simon Glass
2024-07-25 13:56 ` [PATCH 1/6] doc: Show more information in efi index Simon Glass
2024-07-25 16:02 ` Heinrich Schuchardt
2024-07-25 13:56 ` [PATCH 2/6] efi: Convert device_path allocation to use malloc() Simon Glass
2024-07-25 16:13 ` Heinrich Schuchardt
2024-07-25 23:32 ` Simon Glass
2024-07-25 13:56 ` [PATCH 3/6] efi: Allow monitoring of page allocations Simon Glass
2024-07-25 16:20 ` Heinrich Schuchardt
2024-07-25 23:33 ` Simon Glass
2024-07-25 13:56 ` [PATCH 4/6] efi: Avoid pool allocation in efi_get_memory_map_alloc() Simon Glass
2024-07-25 16:38 ` Heinrich Schuchardt
2024-07-31 14:39 ` Simon Glass
2024-07-25 13:56 ` [PATCH 5/6] efi: Use malloc() for the EFI pool Simon Glass
2024-07-25 15:49 ` Heinrich Schuchardt [this message]
2024-07-25 23:32 ` Simon Glass
2024-07-26 8:31 ` Ilias Apalodimas
2024-07-26 14:54 ` Simon Glass
2024-07-29 10:01 ` Ilias Apalodimas
2024-07-29 15:28 ` Simon Glass
2024-07-30 8:19 ` Ilias Apalodimas
2024-07-30 14:38 ` Simon Glass
2024-07-30 14:53 ` Mark Kettenis
2024-07-30 15:18 ` Simon Glass
2024-07-30 14:53 ` Ilias Apalodimas
2024-07-30 15:18 ` Simon Glass
2024-07-30 15:24 ` Tom Rini
2024-07-30 19:42 ` Simon Glass
2024-07-30 19:49 ` Tom Rini
2024-07-30 20:05 ` Simon Glass
2024-07-30 20:11 ` Tom Rini
2024-07-30 20:52 ` Simon Glass
2024-07-30 21:20 ` Tom Rini
2024-07-31 14:39 ` Simon Glass
2024-07-31 17:17 ` Tom Rini
2024-08-01 10:14 ` Ilias Apalodimas
2024-08-01 16:14 ` Simon Glass
2024-08-01 16:22 ` Ilias Apalodimas
2024-08-01 17:43 ` Simon Glass
2024-07-31 7:39 ` Ilias Apalodimas
2024-07-31 14:39 ` Simon Glass
2024-07-25 13:56 ` [PATCH 6/6] efi: Show the location of the bounce buffer Simon Glass
2024-07-25 16:31 ` Heinrich Schuchardt
2024-07-31 14:39 ` Simon Glass
2024-07-25 14:25 ` [PATCH 0/6] efi: Start tidying up memory management Ilias Apalodimas
2024-07-25 15:58 ` Simon Glass
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=72e02ed6-f8c3-4caa-8ae6-2cef00a2ddd2@gmx.de \
--to=xypron.glpk@gmx.de \
--cc=ilias.apalodimas@linaro.org \
--cc=sjg@chromium.org \
--cc=sughosh.ganu@linaro.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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