Skip to content

Commit ad69b0b

Browse files
committed
efi/libstub: Use cleanup helpers for freeing copies of the memory map
The EFI stub may obtain the memory map from the firmware numerous times, and this involves doing a EFI pool allocation first, which needs to be freed after use. Streamline this using a cleanup helper, which makes the code easier to follow. Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 90534e6 commit ad69b0b

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

drivers/firmware/efi/libstub/kaslr.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle)
5757
*/
5858
static bool check_image_region(u64 base, u64 size)
5959
{
60-
struct efi_boot_memmap *map;
60+
struct efi_boot_memmap *map __free(efi_pool) = NULL;
6161
efi_status_t status;
6262
bool ret = false;
6363
int map_offset;
@@ -80,8 +80,6 @@ static bool check_image_region(u64 base, u64 size)
8080
}
8181
}
8282

83-
efi_bs_call(free_pool, map);
84-
8583
return ret;
8684
}
8785

drivers/firmware/efi/libstub/mem.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
2121
bool install_cfg_tbl)
2222
{
23+
struct efi_boot_memmap tmp, *m __free(efi_pool) = NULL;
2324
int memtype = install_cfg_tbl ? EFI_ACPI_RECLAIM_MEMORY
2425
: EFI_LOADER_DATA;
2526
efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
26-
struct efi_boot_memmap *m, tmp;
2727
efi_status_t status;
2828
unsigned long size;
2929

@@ -48,24 +48,20 @@ efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,
4848
*/
4949
status = efi_bs_call(install_configuration_table, &tbl_guid, m);
5050
if (status != EFI_SUCCESS)
51-
goto free_map;
51+
return status;
5252
}
5353

5454
m->buff_size = m->map_size = size;
5555
status = efi_bs_call(get_memory_map, &m->map_size, m->map, &m->map_key,
5656
&m->desc_size, &m->desc_ver);
57-
if (status != EFI_SUCCESS)
58-
goto uninstall_table;
57+
if (status != EFI_SUCCESS) {
58+
if (install_cfg_tbl)
59+
efi_bs_call(install_configuration_table, &tbl_guid, NULL);
60+
return status;
61+
}
5962

60-
*map = m;
63+
*map = no_free_ptr(m);
6164
return EFI_SUCCESS;
62-
63-
uninstall_table:
64-
if (install_cfg_tbl)
65-
efi_bs_call(install_configuration_table, &tbl_guid, NULL);
66-
free_map:
67-
efi_bs_call(free_pool, m);
68-
return status;
6965
}
7066

7167
/**

drivers/firmware/efi/libstub/randomalloc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ efi_status_t efi_random_alloc(unsigned long size,
5959
unsigned long alloc_min,
6060
unsigned long alloc_max)
6161
{
62+
struct efi_boot_memmap *map __free(efi_pool) = NULL;
6263
unsigned long total_slots = 0, target_slot;
6364
unsigned long total_mirrored_slots = 0;
64-
struct efi_boot_memmap *map;
6565
efi_status_t status;
6666
int map_offset;
6767

@@ -130,7 +130,5 @@ efi_status_t efi_random_alloc(unsigned long size,
130130
break;
131131
}
132132

133-
efi_bs_call(free_pool, map);
134-
135133
return status;
136134
}

drivers/firmware/efi/libstub/relocate.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
2424
unsigned long *addr, unsigned long min)
2525
{
26-
struct efi_boot_memmap *map;
26+
struct efi_boot_memmap *map __free(efi_pool) = NULL;
2727
efi_status_t status;
2828
unsigned long nr_pages;
2929
int i;
3030

3131
status = efi_get_memory_map(&map, false);
3232
if (status != EFI_SUCCESS)
33-
goto fail;
33+
return status;
3434

3535
/*
3636
* Enforce minimum alignment that EFI or Linux requires when
@@ -79,11 +79,9 @@ efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
7979
}
8080

8181
if (i == map->map_size / map->desc_size)
82-
status = EFI_NOT_FOUND;
82+
return EFI_NOT_FOUND;
8383

84-
efi_bs_call(free_pool, map);
85-
fail:
86-
return status;
84+
return EFI_SUCCESS;
8785
}
8886

8987
/**

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ static efi_status_t allocate_e820(struct boot_params *params,
616616
struct setup_data **e820ext,
617617
u32 *e820ext_size)
618618
{
619-
struct efi_boot_memmap *map;
619+
struct efi_boot_memmap *map __free(efi_pool) = NULL;
620620
efi_status_t status;
621621
__u32 nr_desc;
622622

@@ -630,13 +630,14 @@ static efi_status_t allocate_e820(struct boot_params *params,
630630
EFI_MMAP_NR_SLACK_SLOTS;
631631

632632
status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
633+
if (status != EFI_SUCCESS)
634+
return status;
633635
}
634636

635-
if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
636-
status = allocate_unaccepted_bitmap(nr_desc, map);
637+
if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
638+
return allocate_unaccepted_bitmap(nr_desc, map);
637639

638-
efi_bs_call(free_pool, map);
639-
return status;
640+
return EFI_SUCCESS;
640641
}
641642

642643
struct exit_boot_struct {

0 commit comments

Comments
 (0)