Skip to content

Commit 3ed642e

Browse files
ardbiesheuvelgregkh
authored andcommitted
efi: Avoid cold plugged memory for placing the kernel
commit ba69e07 upstream. UEFI 2.11 introduced EFI_MEMORY_HOT_PLUGGABLE to annotate system memory regions that are 'cold plugged' at boot, i.e., hot pluggable memory that is available from early boot, and described as system RAM by the firmware. Existing loaders and EFI applications running in the boot context will happily use this memory for allocating data structures that cannot be freed or moved at runtime, and this prevents the memory from being unplugged. Going forward, the new EFI_MEMORY_HOT_PLUGGABLE attribute should be tested, and memory annotated as such should be avoided for such allocations. In the EFI stub, there are a couple of occurrences where, instead of the high-level AllocatePages() UEFI boot service, a low-level code sequence is used that traverses the EFI memory map and carves out the requested number of pages from a free region. This is needed, e.g., for allocating as low as possible, or for allocating pages at random. While AllocatePages() should presumably avoid special purpose memory and cold plugged regions, this manual approach needs to incorporate this logic itself, in order to prevent the kernel itself from ending up in a hot unpluggable region, preventing it from being unplugged. So add the EFI_MEMORY_HOTPLUGGABLE macro definition, and check for it where appropriate. Cc: [email protected] Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3d041fb commit 3ed642e

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

drivers/firmware/efi/efi.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -922,13 +922,15 @@ char * __init efi_md_typeattr_format(char *buf, size_t size,
922922
EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
923923
EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
924924
EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
925-
EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
925+
EFI_MEMORY_MORE_RELIABLE | EFI_MEMORY_HOT_PLUGGABLE |
926+
EFI_MEMORY_RUNTIME))
926927
snprintf(pos, size, "|attr=0x%016llx]",
927928
(unsigned long long)attr);
928929
else
929930
snprintf(pos, size,
930-
"|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
931+
"|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
931932
attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
933+
attr & EFI_MEMORY_HOT_PLUGGABLE ? "HP" : "",
932934
attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
933935
attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "",
934936
attr & EFI_MEMORY_SP ? "SP" : "",

drivers/firmware/efi/libstub/randomalloc.c

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ static unsigned long get_entry_num_slots(efi_memory_desc_t *md,
2525
if (md->type != EFI_CONVENTIONAL_MEMORY)
2626
return 0;
2727

28+
if (md->attribute & EFI_MEMORY_HOT_PLUGGABLE)
29+
return 0;
30+
2831
if (efi_soft_reserve_enabled() &&
2932
(md->attribute & EFI_MEMORY_SP))
3033
return 0;

drivers/firmware/efi/libstub/relocate.c

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
5353
if (desc->type != EFI_CONVENTIONAL_MEMORY)
5454
continue;
5555

56+
if (desc->attribute & EFI_MEMORY_HOT_PLUGGABLE)
57+
continue;
58+
5659
if (efi_soft_reserve_enabled() &&
5760
(desc->attribute & EFI_MEMORY_SP))
5861
continue;

include/linux/efi.h

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ typedef struct {
128128
#define EFI_MEMORY_RO ((u64)0x0000000000020000ULL) /* read-only */
129129
#define EFI_MEMORY_SP ((u64)0x0000000000040000ULL) /* soft reserved */
130130
#define EFI_MEMORY_CPU_CRYPTO ((u64)0x0000000000080000ULL) /* supports encryption */
131+
#define EFI_MEMORY_HOT_PLUGGABLE BIT_ULL(20) /* supports unplugging at runtime */
131132
#define EFI_MEMORY_RUNTIME ((u64)0x8000000000000000ULL) /* range requires runtime mapping */
132133
#define EFI_MEMORY_DESCRIPTOR_VERSION 1
133134

0 commit comments

Comments
 (0)