Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
efi/x86: Implement support for unaccepted memory
UEFI Specification version 2.9 introduces the concept of memory acceptance: Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, requiring memory to be accepted before it can be used by the guest. Accepting happens via a protocol specific for the Virtual Machine platform. Accepting memory is costly and it makes VMM allocate memory for the accepted guest physical address range. It's better to postpone memory acceptance until memory is needed. It lowers boot time and reduces memory overhead. The kernel needs to know what memory has been accepted. Firmware communicates this information via memory map: a new memory type -- EFI_UNACCEPTED_MEMORY -- indicates such memory. Range-based tracking works fine for firmware, but it gets bulky for the kernel: e820 has to be modified on every page acceptance. It leads to table fragmentation, but there's a limited number of entries in the e820 table Another option is to mark such memory as usable in e820 and track if the range has been accepted in a bitmap. One bit in the bitmap represents 2MiB in the address space: one 4k page is enough to track 64GiB or physical address space. In the worst-case scenario -- a huge hole in the middle of the address space -- It needs 256MiB to handle 4PiB of the address space. Any unaccepted memory that is not aligned to 2M gets accepted upfront. The bitmap is allocated and constructed in the EFI stub and passed down to the kernel via boot_params. allocate_e820() allocates the bitmap if unaccepted memory is present, according to the maximum address in the memory map. The same boot_params.unaccepted_memory can be used to pass the bitmap between two kernels on kexec, but the use-case is not yet implemented. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
- Loading branch information
Showing
10 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-only | ||
| /* Taken from lib/string.c */ | ||
|
|
||
| #include <linux/bitmap.h> | ||
|
|
||
| void __bitmap_set(unsigned long *map, unsigned int start, int len) | ||
| { | ||
| unsigned long *p = map + BIT_WORD(start); | ||
| const unsigned int size = start + len; | ||
| int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); | ||
| unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); | ||
|
|
||
| while (len - bits_to_set >= 0) { | ||
| *p |= mask_to_set; | ||
| len -= bits_to_set; | ||
| bits_to_set = BITS_PER_LONG; | ||
| mask_to_set = ~0UL; | ||
| p++; | ||
| } | ||
| if (len) { | ||
| mask_to_set &= BITMAP_LAST_WORD_MASK(size); | ||
| *p |= mask_to_set; | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-only | ||
|
|
||
| #include "error.h" | ||
| #include "misc.h" | ||
|
|
||
| static inline void __accept_memory(phys_addr_t start, phys_addr_t end) | ||
| { | ||
| /* Platform-specific memory-acceptance call goes here */ | ||
| error("Cannot accept memory"); | ||
| } | ||
|
|
||
| void mark_unaccepted(struct boot_params *params, u64 start, u64 end) | ||
| { | ||
| /* | ||
| * The accepted memory bitmap only works at PMD_SIZE granularity. | ||
| * If a request comes in to mark memory as unaccepted which is not | ||
| * PMD_SIZE-aligned, simply accept the memory now since it can not be | ||
| * *marked* as unaccepted. | ||
| */ | ||
|
|
||
| /* Immediately accept whole range if it is within a PMD_SIZE block: */ | ||
| if ((start & PMD_MASK) == (end & PMD_MASK)) { | ||
| npages = (end - start) / PAGE_SIZE; | ||
| __accept_memory(start, start + npages * PAGE_SIZE); | ||
| return; | ||
| } | ||
|
|
||
| /* Immediately accept a <PMD_SIZE piece at the start: */ | ||
| if (start & ~PMD_MASK) { | ||
| __accept_memory(start, round_up(start, PMD_SIZE)); | ||
| start = round_up(start, PMD_SIZE); | ||
| } | ||
|
|
||
| /* Immediately accept a <PMD_SIZE piece at the end: */ | ||
| if (end & ~PMD_MASK) { | ||
| __accept_memory(round_down(end, PMD_SIZE), end); | ||
| end = round_down(end, PMD_SIZE); | ||
| } | ||
|
|
||
| if (start == end) | ||
| return; | ||
|
|
||
| bitmap_set((unsigned long *)params->unaccepted_memory, | ||
| start / PMD_SIZE, (end - start) / PMD_SIZE); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /* SPDX-License-Identifier: GPL-2.0 */ | ||
| /* Copyright (C) 2020 Intel Corporation */ | ||
| #ifndef _ASM_X86_UNACCEPTED_MEMORY_H | ||
| #define _ASM_X86_UNACCEPTED_MEMORY_H | ||
|
|
||
| #include <linux/types.h> | ||
|
|
||
| struct boot_params; | ||
|
|
||
| void mark_unaccepted(struct boot_params *params, u64 start, u64 num); | ||
|
|
||
| #endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters