efi/libstub: Add API function to allocate aligned memory

Break out the code to create an aligned page allocation from mem.c
and move it into a function efi_allocate_pages_aligned() in alignedmem.c.
Update efi_allocate_pages() to invoke it unless the minimum alignment
equals the EFI page size (4 KB), in which case the ordinary page
allocator is sufficient. This way, efi_allocate_pages_aligned() will
only be pulled into the build if it is actually being used (which will
be on arm64 only in the immediate future)

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
This commit is contained in:
Ard Biesheuvel
2020-03-27 16:09:40 +01:00
parent 5d12da9dd6
commit 43b1df0e01
4 changed files with 71 additions and 17 deletions

View File

@@ -93,31 +93,24 @@ fail:
efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
unsigned long max)
{
efi_physical_addr_t alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
int slack = EFI_ALLOC_ALIGN / EFI_PAGE_SIZE - 1;
efi_physical_addr_t alloc_addr;
efi_status_t status;
size = round_up(size, EFI_ALLOC_ALIGN);
if (EFI_ALLOC_ALIGN > EFI_PAGE_SIZE)
return efi_allocate_pages_aligned(size, addr, max,
EFI_ALLOC_ALIGN);
alloc_addr = ALIGN_DOWN(max + 1, EFI_ALLOC_ALIGN) - 1;
status = efi_bs_call(allocate_pages, EFI_ALLOCATE_MAX_ADDRESS,
EFI_LOADER_DATA, size / EFI_PAGE_SIZE + slack,
EFI_LOADER_DATA, DIV_ROUND_UP(size, EFI_PAGE_SIZE),
&alloc_addr);
if (status != EFI_SUCCESS)
return status;
*addr = ALIGN((unsigned long)alloc_addr, EFI_ALLOC_ALIGN);
if (slack > 0) {
int l = (alloc_addr % EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
if (l) {
efi_bs_call(free_pages, alloc_addr, slack - l + 1);
slack = l - 1;
}
if (slack)
efi_bs_call(free_pages, *addr + size, slack);
}
*addr = alloc_addr;
return EFI_SUCCESS;
}
/**
* efi_low_alloc_above() - allocate pages at or above given address
* @size: size of the memory area to allocate