Skip to content

Commit fd62619

Browse files
LennySzubowiczardbiesheuvel
authored andcommitted
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 <[email protected]> Suggested-by: Ard Biesheuvel <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 8f592ad commit fd62619

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

drivers/firmware/efi/libstub/efistub.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ extern __pure efi_system_table_t *efi_system_table(void);
9292
#define EFI_LOCATE_BY_REGISTER_NOTIFY 1
9393
#define EFI_LOCATE_BY_PROTOCOL 2
9494

95+
/*
96+
* An efi_boot_memmap is used by efi_get_memory_map() to return the
97+
* EFI memory map in a dynamically allocated buffer.
98+
*
99+
* The buffer allocated for the EFI memory map includes extra room for
100+
* a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors.
101+
* This facilitates the reuse of the EFI memory map buffer when a second
102+
* call to ExitBootServices() is needed because of intervening changes to
103+
* the EFI memory map. Other related structures, e.g. x86 e820ext, need
104+
* to factor in this headroom requirement as well.
105+
*/
106+
#define EFI_MMAP_NR_SLACK_SLOTS 8
107+
95108
struct efi_boot_memmap {
96109
efi_memory_desc_t **map;
97110
unsigned long *map_size;

drivers/firmware/efi/libstub/mem.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#include "efistub.h"
77

8-
#define EFI_MMAP_NR_SLACK_SLOTS 8
9-
108
static inline bool mmap_has_headroom(unsigned long buff_size,
119
unsigned long map_size,
1210
unsigned long desc_size)

drivers/firmware/efi/libstub/x86-stub.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -606,24 +606,18 @@ static efi_status_t allocate_e820(struct boot_params *params,
606606
struct setup_data **e820ext,
607607
u32 *e820ext_size)
608608
{
609-
unsigned long map_size, desc_size, buff_size;
610-
struct efi_boot_memmap boot_map;
611-
efi_memory_desc_t *map;
609+
unsigned long map_size, desc_size, map_key;
612610
efi_status_t status;
613-
__u32 nr_desc;
611+
__u32 nr_desc, desc_version;
614612

615-
boot_map.map = &map;
616-
boot_map.map_size = &map_size;
617-
boot_map.desc_size = &desc_size;
618-
boot_map.desc_ver = NULL;
619-
boot_map.key_ptr = NULL;
620-
boot_map.buff_size = &buff_size;
613+
/* Only need the size of the mem map and size of each mem descriptor */
614+
map_size = 0;
615+
status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
616+
&desc_size, &desc_version);
617+
if (status != EFI_BUFFER_TOO_SMALL)
618+
return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
621619

622-
status = efi_get_memory_map(&boot_map);
623-
if (status != EFI_SUCCESS)
624-
return status;
625-
626-
nr_desc = buff_size / desc_size;
620+
nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
627621

628622
if (nr_desc > ARRAY_SIZE(params->e820_table)) {
629623
u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);

0 commit comments

Comments
 (0)