efi/libstub/x86: Avoid EFI map buffer alloc in allocate_e820()

In allocate_e820(), call the EFI get_memory_map() service directly
instead of indirectly via efi_get_memory_map(). This avoids allocation
of a buffer and return of the full EFI memory map, which is not needed
here and would otherwise need to be freed.

Routine allocate_e820() only needs to know how many EFI memory
descriptors there are in the map to allocate an adequately sized
e820ext buffer, if it's needed. Note that since efi_get_memory_map()
returns a memory map buffer sized with extra headroom, allocate_e820()
now needs to explicitly factor that into the e820ext size calculation.

Signed-off-by: Lenny Szubowicz <lszubowi@redhat.com>
Suggested-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
This commit is contained in:
Lenny Szubowicz 2020-05-07 14:33:32 -04:00 committed by Ard Biesheuvel
parent 8f592ada59
commit fd62619598
3 changed files with 22 additions and 17 deletions

View File

@ -92,6 +92,19 @@ extern __pure efi_system_table_t *efi_system_table(void);
#define EFI_LOCATE_BY_REGISTER_NOTIFY 1
#define EFI_LOCATE_BY_PROTOCOL 2
/*
* An efi_boot_memmap is used by efi_get_memory_map() to return the
* EFI memory map in a dynamically allocated buffer.
*
* The buffer allocated for the EFI memory map includes extra room for
* a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
* This facilitates the reuse of the EFI memory map buffer when a second
* call to ExitBootServices() is needed because of intervening changes to
* the EFI memory map. Other related structures, e.g. x86 e820ext, need
* to factor in this headroom requirement as well.
*/
#define EFI_MMAP_NR_SLACK_SLOTS 8
struct efi_boot_memmap {
efi_memory_desc_t **map;
unsigned long *map_size;

View File

@ -5,8 +5,6 @@
#include "efistub.h"
#define EFI_MMAP_NR_SLACK_SLOTS 8
static inline bool mmap_has_headroom(unsigned long buff_size,
unsigned long map_size,
unsigned long desc_size)

View File

@ -606,24 +606,18 @@ static efi_status_t allocate_e820(struct boot_params *params,
struct setup_data **e820ext,
u32 *e820ext_size)
{
unsigned long map_size, desc_size, buff_size;
struct efi_boot_memmap boot_map;
efi_memory_desc_t *map;
unsigned long map_size, desc_size, map_key;
efi_status_t status;
__u32 nr_desc;
__u32 nr_desc, desc_version;
boot_map.map = &map;
boot_map.map_size = &map_size;
boot_map.desc_size = &desc_size;
boot_map.desc_ver = NULL;
boot_map.key_ptr = NULL;
boot_map.buff_size = &buff_size;
/* Only need the size of the mem map and size of each mem descriptor */
map_size = 0;
status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
&desc_size, &desc_version);
if (status != EFI_BUFFER_TOO_SMALL)
return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
status = efi_get_memory_map(&boot_map);
if (status != EFI_SUCCESS)
return status;
nr_desc = buff_size / desc_size;
nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
if (nr_desc > ARRAY_SIZE(params->e820_table)) {
u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);