mm, x86/cc, efi: Implement support for unaccepted memory
Subsystems (10)
v13 cover mm, x86/cc, efi: 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 approach lowers boot time substantially. Boot to shell is ~2.5x faster for 4G TDX VM and ~4x faster for 64G.
TDX-specific code isolated from the core of unaccepted memory support. It supposed to help to plug-in different implementation of unaccepted memory such as SEV-SNP.
-- Fragmentation study --
Vlastimil and Mel were concern about effect of unaccepted memory on fragmentation prevention measures in page allocator. I tried to evaluate it, but it is tricky. As suggested I tried to run multiple parallel kernel builds and follow how often kmem:mm_page_alloc_extfrag gets hit.
See results in the v9 of the patchset[1][2]
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
--
The tree can be found here:
https://github.com/intel/tdx.git guest-unaccepted-memory
v13:
- Fix few boot issues discovered by 0day;
- Simplify tdx_accept_memory(): no need in MAP_GPA hypercall;
- Update commit message for the first patch;
- Add Reviewed-bys from Tom and Ard;
v12:
- Re-initialize 'unaccepted_table' variable from decompressor to cover some boot scenarios;
- Add missing memblock_reserve() for the unaccepted memory configuration table (Mika);
- Add efi.unaccepted into efi_tables (Tom);
- Do not build tdx-shared.o for !TDX (Tom);
- Typo fix (Liam)
- Whitespace fix;
- Reviewed-bys from Liam, Tom and Ard;
v11:
- Restructure the code to make it less x86-specific (suggested by Ard):
+ use EFI configuration table instead of zero-page to pass down bitmap;
+ do not imply 1bit == 2M in bitmap;
+ move bulk of the code under driver/firmware/efi;
- The bitmap only covers unaccpeted memory now. All memory that is not covered by the bitmap assumed accepted;
- Reviewed-by from Ard;
v10:
- Restructure code around zones_with_unaccepted_pages static brach to avoid unnecessary function calls (Suggested by Vlastimil);
- Drop mentions of PageUnaccepted();
- Drop patches that add fake unaccepted memory support and sysfs handle to accept memory manually;
- Add Reviewed-by from Vlastimil;
v9:
- Accept memory up to high watermark when kernel runs out of free memory;
- Treat unaccepted memory as unusable in __zone_watermark_unusable_free();
- Per-zone unaccepted memory accounting;
- All pages on unaccepted list are MAX_ORDER now;
- accept_memory=eager in cmdline to pre-accept memory during the boot;
- Implement fake unaccepted memory;
- Sysfs handle to accept memory manually;
- Drop PageUnaccepted();
- Rename unaccepted_pages static key to zones_with_unaccepted_pages;
v8:
- Rewrite core-mm support for unaccepted memory (patch 02/14);
- s/UnacceptedPages/Unaccepted/ in meminfo;
- Drop arch/x86/boot/compressed/compiler.h;
- Fix build errors;
- Adjust commit messages and comments;
- Reviewed-bys from Dave and Borislav;
- Rebased to tip/master.
v7:
- Rework meminfo counter to use PageUnaccepted() and move to generic code;
- Fix range_contains_unaccepted_memory() on machines without unaccepted memory;
- Add Reviewed-by from David;
v6:
- Fix load_unaligned_zeropad() on machine with unaccepted memory;
- Clear PageUnaccepted() on merged pages, leaving it only on head;
- Clarify error handling in allocate_e820();
- Fix build with CONFIG_UNACCEPTED_MEMORY=y, but without TDX;
- Disable kexec at boottime instead of build conflict;
- Rebased to tip/master;
- Spelling fixes;
- Add Reviewed-by from Mike and David;
v5:
- Updates comments and commit messages;
+ Explain options for unaccepted memory handling;
- Expose amount of unaccepted memory in /proc/meminfo
- Adjust check in page_expected_state();
- Fix error code handling in allocate_e820();
- Centralize __pa()/__va() definitions in the boot stub;
- Avoid includes from the main kernel in the boot stub;
- Use an existing hole in boot_param for unaccepted_memory, instead of adding to the end of the structure;
- Extract allocate_unaccepted_memory() form allocate_e820();
- Complain if there's unaccepted memory, but kernel does not support it;
- Fix vmstat counter;
- Split up few preparatory patches;
- Random readability adjustments;
v4:
- PageBuddyUnaccepted() -> PageUnaccepted;
- Use separate page_type, not shared with offline;
- Rework interface between core-mm and arch code;
- Adjust commit messages;
- Ack from Mike;
Kirill A. Shutemov (9):
mm: Add support for unaccepted memory
efi/x86: Get full memory map in allocate_e820()
efi/libstub: Implement support for unaccepted memory x86/boot/compressed: Handle unaccepted memory
efi: Add unaccepted memory support
efi/unaccepted: Avoid load_unaligned_zeropad() stepping into
unaccepted memory
x86/tdx: Make _tdx_hypercall() and __tdx_module_call() available in
boot stub
x86/tdx: Refactor try_accept_one()
x86/tdx: Add unaccepted memory support
arch/x86/Kconfig | 2 + arch/x86/boot/compressed/Makefile | 3 +- arch/x86/boot/compressed/efi.h | 10 + arch/x86/boot/compressed/error.c | 19 ++ arch/x86/boot/compressed/error.h | 1 + arch/x86/boot/compressed/kaslr.c | 35 ++- arch/x86/boot/compressed/mem.c | 73 ++++++ arch/x86/boot/compressed/misc.c | 7 + arch/x86/boot/compressed/misc.h | 6 + arch/x86/boot/compressed/tdx-shared.c | 2 + arch/x86/coco/tdx/Makefile | 2 +- arch/x86/coco/tdx/tdx-shared.c | 71 ++++++ arch/x86/coco/tdx/tdx.c | 102 +------- arch/x86/include/asm/efi.h | 2 + arch/x86/include/asm/shared/tdx.h | 53 ++++ arch/x86/include/asm/tdx.h | 19 -- arch/x86/include/asm/unaccepted_memory.h | 24 ++ arch/x86/platform/efi/efi.c | 3 + drivers/base/node.c | 7 + drivers/firmware/efi/Kconfig | 14 ++ drivers/firmware/efi/Makefile | 1 + drivers/firmware/efi/efi.c | 26 ++ drivers/firmware/efi/libstub/Makefile | 2 + drivers/firmware/efi/libstub/bitmap.c | 41 +++ drivers/firmware/efi/libstub/efistub.h | 6 + drivers/firmware/efi/libstub/find.c | 43 ++++ .../firmware/efi/libstub/unaccepted_memory.c | 234 ++++++++++++++++++ drivers/firmware/efi/libstub/x86-stub.c | 39 +-- drivers/firmware/efi/unaccepted_memory.c | 138 +++++++++++ fs/proc/meminfo.c | 5 + include/linux/efi.h | 13 +- include/linux/mm.h | 19 ++ include/linux/mmzone.h | 8 + mm/memblock.c | 9 + mm/mm_init.c | 7 + mm/page_alloc.c | 173 +++++++++++++ mm/vmstat.c | 3 + 37 files changed, 1074 insertions(+), 148 deletions(-) create mode 100644 arch/x86/boot/compressed/mem.c create mode 100644 arch/x86/boot/compressed/tdx-shared.c create mode 100644 arch/x86/coco/tdx/tdx-shared.c create mode 100644 arch/x86/include/asm/unaccepted_memory.h create mode 100644 drivers/firmware/efi/libstub/bitmap.c create mode 100644 drivers/firmware/efi/libstub/find.c create mode 100644 drivers/firmware/efi/libstub/unaccepted_memory.c create mode 100644 drivers/firmware/efi/unaccepted_memory.c
Signature · 1 line
2.39.3
PATCH v13 1/9 mm: Add 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, require memory to be accepted before it can be used by the guest. Accepting happens via a protocol specific to the Virtual Machine platform.
There are several ways kernel can deal with unaccepted memory:
1. Accept all the memory during the boot. It is easy to implement and it doesn't have runtime cost once the system is booted. The downside is very long boot time.
Accept can be parallelized to multiple CPUs to keep it manageable (i.e. via DEFERRED_STRUCT_PAGE_INIT), but it tends to saturate memory bandwidth and does not scale beyond the point.
2. Accept a block of memory on the first use. It requires more infrastructure and changes in page allocator to make it work, but it provides good boot time.
On-demand memory accept means latency spikes every time kernel steps onto a new memory block. The spikes will go away once workload data set size gets stabilized or all memory gets accepted.
3. Accept all memory in background. Introduce a thread (or multiple) that gets memory accepted proactively. It will minimize time the system experience latency spikes on memory allocation while keeping low boot time.
This approach cannot function on its own. It is an extension of #2:
background memory acceptance requires functional scheduler, but the page allocator may need to tap into unaccepted memory before that.
The downside of the approach is that these threads also steal CPU cycles and memory bandwidth from the user's workload and may hurt user experience.
Implement #1 and #2 for now. #2 is the default. Some workloads may want to use #1 with accept_memory=eager in kernel command line. #3 can be implemented later based on user's demands.
Support of unaccepted memory requires a few changes in core-mm code:
- memblock accepts memory on allocation. It serves early boot memory allocations and doesn't limit them to pre-accepted pool of memory.
- page allocator accepts memory on the first allocation of the page. When kernel runs out of accepted memory, it accepts memory until the high watermark is reached. It helps to minimize fragmentation.
EFI code will provide two helpers if the platform supports unaccepted memory:
- accept_memory() makes a range of physical addresses accepted.
- range_contains_unaccepted_memory() checks anything within the range of physical addresses requires acceptance.
--- drivers/base/node.c | 7 ++ fs/proc/meminfo.c | 5 ++ include/linux/mm.h | 19 +++++ include/linux/mmzone.h | 8 ++ mm/memblock.c | 9 +++ mm/mm_init.c | 7 ++ mm/page_alloc.c | 173 +++++++++++++++++++++++++++++++++++++++++ mm/vmstat.c | 3 + 8 files changed, 231 insertions(+)
drivers/base/node.c +7 -0
@@ -448,6 +448,9 @@ static ssize_t node_read_meminfo(struct device *dev,"Node %d ShmemPmdMapped: %8lu kB\n" "Node %d FileHugePages: %8lu kB\n" "Node %d FilePmdMapped: %8lu kB\n" +#endif +#ifdef CONFIG_UNACCEPTED_MEMORY +"Node %d Unaccepted: %8lu kB\n" #endif , nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
@@ -477,6 +480,10 @@ static ssize_t node_read_meminfo(struct device *dev, nid,K (node_page_state (pgdat,NR_SHMEM_PMDMAPPED )), nid,K (node_page_state (pgdat,NR_FILE_THPS )), nid, K(node_page_state(pgdat, NR_FILE_PMDMAPPED)) +#endif +#ifdef CONFIG_UNACCEPTED_MEMORY + , + nid,K (sum_zone_node_page_state (nid,NR_UNACCEPTED )) #endif ); len +=hugetlb_report_node_meminfo (buf, len, nid);
fs/proc/meminfo.c +5 -0
@@ -168,6 +168,11 @@ static int meminfo_proc_show(struct seq_file *m, void *v) global_zone_page_state(NR_FREE_CMA_PAGES));#endif +#ifdef CONFIG_UNACCEPTED_MEMORY +show_val_kb (m,"Unaccepted: " , +global_zone_page_state (NR_UNACCEPTED )); +#endif +hugetlb_report_meminfo (m);arch_report_meminfo (m);
include/linux/mm.h +19 -0
@@ -3816,4 +3816,23 @@ madvise_set_anon_name(struct mm_struct *mm, unsigned long start, }#endif +#ifdef CONFIG_UNACCEPTED_MEMORY + +bool range_contains_unaccepted_memory (phys_addr_t start,phys_addr_t end); +void accept_memory (phys_addr_t start,phys_addr_t end); + +#else + +static inline bool range_contains_unaccepted_memory (phys_addr_t start, +phys_addr_t end) +{ +return false; +} + +static inline void accept_memory (phys_addr_t start,phys_addr_t end) +{ +} + +#endif +#endif /* _LINUX_MM_H */
include/linux/mmzone.h +8 -0
@@ -143,6 +143,9 @@ enum zone_stat_item { NR_ZSPAGES, /* allocated in zsmalloc */ #endif NR_FREE_CMA_PAGES, +#ifdef CONFIG_UNACCEPTED_MEMORY + NR_UNACCEPTED, +#endif NR_VM_ZONE_STAT_ITEMS }; enum node_stat_item {
@@ -910,6 +913,11 @@ struct zone {/* free areas of different sizes */ struct free_area free_area[MAX_ORDER +1 ]; +#ifdef CONFIG_UNACCEPTED_MEMORY +/* Pages to be accepted. All pages on the list are MAX_ORDER */ +struct list_head unaccepted_pages; +#endif +/* zone flags, see below */ unsigned long flags;
mm/memblock.c +9 -0
@@ -1436,6 +1436,15 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,*/ kmemleak_alloc_phys (found, size,0 ); +/* +* Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, +* require memory to be accepted before it can be used by the +* guest. +* +* Accept the memory of the allocated buffer. +*/ +accept_memory (found, found + size); +return found; }
mm/mm_init.c +7 -0
@@ -1375,6 +1375,10 @@ static void __meminit zone_init_free_lists(struct zone *zone)INIT_LIST_HEAD (&zone->free_area[order].free_list[t]); zone->free_area[order].nr_free =0 ; } + +#ifdef CONFIG_UNACCEPTED_MEMORY +INIT_LIST_HEAD (&zone->unaccepted_pages); +#endif } void __meminit init_currently_empty_zone(struct zone *zone,
@@ -1960,6 +1964,9 @@ static void __init deferred_free_range(unsigned long pfn,return ; } +/* Accept chunks smaller than MAX_ORDER upfront */ +accept_memory (PFN_PHYS (pfn),PFN_PHYS (pfn + nr_pages)); +for (i =0 ; i < nr_pages; i++, page++, pfn++) {if (pageblock_aligned (pfn))set_pageblock_migratetype (page,MIGRATE_MOVABLE );
mm/page_alloc.c +173 -0
206 lines not on this page · raw message
mm/vmstat.c +3 -0
@@ -1180,6 +1180,9 @@ const char * const vmstat_text[] = {"nr_zspages" , #endif"nr_free_cma" , +#ifdef CONFIG_UNACCEPTED_MEMORY +"nr_unaccepted" , +#endif /* enum numa_stat_item counters */ #ifdef CONFIG_NUMA
Signature · 1 line
2.39.3
PATCH v13 2/9 efi/x86: Get full memory map in allocate_e820()
In reply to Kirill A. ShutemovCurrently allocate_e820() is only interested in the size of map and size of memory descriptor to determine how many e820 entries the kernel needs.
UEFI Specification version 2.9 introduces a new memory type -- unaccepted memory. To track unaccepted memory kernel needs to allocate a bitmap. The size of the bitmap is dependent on the maximum physical address present in the system. A full memory map is required to find the maximum address.
Modify allocate_e820() to get a full memory map.
--- drivers/firmware/efi/libstub/x86-stub.c | 26 +++++++++++-------------- 1 file changed, 11 insertions(+), 15 deletions(-)
drivers/firmware/efi/libstub/x86-stub.c +11 -15
@@ -681,28 +681,24 @@ static efi_status_t allocate_e820(struct boot_params *params,struct setup_data **e820ext, u32 *e820ext_size) { - unsigned long map_size, desc_size, map_key; + struct efi_boot_memmap *map;efi_status_t status; -__u32 nr_desc, desc_version; +__u32 nr_desc; -/* 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 (&map, false); +if (status !=EFI_SUCCESS ) +return status; - 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); + nr_desc = map->map_size / map->desc_size; +if (nr_desc >ARRAY_SIZE (params->e820_table) -EFI_MMAP_NR_SLACK_SLOTS ) { +u32 nr_e820ext = nr_desc -ARRAY_SIZE (params->e820_table) + +EFI_MMAP_NR_SLACK_SLOTS ; status =alloc_e820ext (nr_e820ext, e820ext, e820ext_size); -if (status !=EFI_SUCCESS ) -return status; } -return EFI_SUCCESS ; +efi_bs_call (free_pool, map); +return status; } struct exit_boot_struct {
Signature · 1 line
2.39.3
PATCH v13 3/9 efi/libstub: Implement support for unaccepted memory
In reply to Kirill A. ShutemovUEFI 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 (or whatever the arch uses) has to be modified on every page acceptance. It leads to table fragmentation and 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 a naturally aligned power-2-sized region of address space -- unit.
For x86, unit size is 2MiB: 4k of the bitmap 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 unit_size gets accepted upfront.
The bitmap is allocated and constructed in the EFI stub and passed down to the kernel via EFI configuration table. allocate_e820() allocates the bitmap if unaccepted memory is present, according to the size of unaccepted region.
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/mem.c | 9 + arch/x86/include/asm/efi.h | 2 + drivers/firmware/efi/Kconfig | 14 ++ drivers/firmware/efi/efi.c | 1 + drivers/firmware/efi/libstub/Makefile | 2 + drivers/firmware/efi/libstub/bitmap.c | 41 +++ drivers/firmware/efi/libstub/efistub.h | 6 + drivers/firmware/efi/libstub/find.c | 43 ++++ .../firmware/efi/libstub/unaccepted_memory.c | 234 ++++++++++++++++++ drivers/firmware/efi/libstub/x86-stub.c | 13 + include/linux/efi.h | 12 +- 12 files changed, 377 insertions(+), 1 deletion(-) create mode 100644 arch/x86/boot/compressed/mem.c create mode 100644 drivers/firmware/efi/libstub/bitmap.c create mode 100644 drivers/firmware/efi/libstub/find.c create mode 100644 drivers/firmware/efi/libstub/unaccepted_memory.c
arch/x86/boot/compressed/Makefile +1 -0
@@ -107,6 +107,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o +vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_mixed.o
arch/x86/boot/compressed/mem.c new file +9 -0
@@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "error.h" + +void arch_accept_memory (phys_addr_t start,phys_addr_t end) +{ +/* Platform-specific memory-acceptance call goes here */ +error ("Cannot accept memory" ); +}
arch/x86/include/asm/efi.h +2 -0
@@ -31,6 +31,8 @@ extern unsigned long efi_mixed_mode_stack_pa;#define ARCH_EFI_IRQ_FLAGS_MASK X86_EFLAGS_IF +#define EFI_UNACCEPTED_UNIT_SIZE PMD_SIZE +/* * The EFI services are called through variadic functions in many cases. These * functions are implemented in assembler and support only a fixed number of
drivers/firmware/efi/Kconfig +14 -0
@@ -269,6 +269,20 @@ config EFI_COCO_SECRET virt/coco/efi_secret module to access the secrets, which in turn allows userspace programs to access the injected secrets. +config UNACCEPTED_MEMORY + bool + depends on EFI_STUB + help + Some Virtual Machine platforms, such as Intel TDX, require + some memory to be "accepted" by the guest before it can be used. + This mechanism helps prevent malicious hosts from making changes + to guest memory. + + UEFI specification v2.9 introduced EFI_UNACCEPTED_MEMORY memory type. + + This option adds support for unaccepted memory and makes such memory + usable by the kernel. + config EFI_EMBEDDED_FIRMWARE bool select CRYPTO_LIB_SHA256
drivers/firmware/efi/efi.c +1 -0
@@ -843,6 +843,7 @@ static __initdata char memory_type_name[][13] = {"MMIO Port" ,"PAL Code" ,"Persistent" , + "Unaccepted", }; char * __init efi_md_typeattr_format(char *buf, size_t size,
drivers/firmware/efi/libstub/Makefile +2 -0
@@ -96,6 +96,8 @@ CFLAGS_arm32-stub.o := -DTEXT_OFFSET=$(TEXT_OFFSET) zboot-obj-$(CONFIG_RISCV) := lib-clz_ctz.o lib-ashldi3.o lib-$(CONFIG_EFI_ZBOOT) += zboot.o $(zboot-obj-y) +lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o + extra-y := $(lib-y) lib-y := $(patsubst %.o,%.stub.o,$(lib-y))
drivers/firmware/efi/libstub/bitmap.c new file +41 -0
41 lines not on this page · raw message
drivers/firmware/efi/libstub/efistub.h +6 -0
@@ -1133,4 +1133,10 @@ const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,void efi_remap_image (unsigned long image_base,unsigned alloc_size,unsigned long code_size); +efi_status_t allocate_unaccepted_bitmap (__u32 nr_desc, +struct efi_boot_memmap *map); +void process_unaccepted_memory (u64 start,u64 end); +void accept_memory (phys_addr_t start,phys_addr_t end); +void arch_accept_memory (phys_addr_t start,phys_addr_t end); +#endif
drivers/firmware/efi/libstub/find.c new file +43 -0
43 lines not on this page · raw message
drivers/firmware/efi/libstub/unaccepted_memory.c new file +234 -0
234 lines not on this page · raw message
drivers/firmware/efi/libstub/x86-stub.c +13 -0
@@ -613,6 +613,16 @@ setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_s e820_type =E820_TYPE_PMEM ;break ; +case EFI_UNACCEPTED_MEMORY : +if (!IS_ENABLED (CONFIG_UNACCEPTED_MEMORY )) { +efi_warn_once ( +"The system has unaccepted memory, but kernel does not support it\nConsider enabling CONFIG_UNACCEPTED_MEMORY\n" ); +continue ; + } + e820_type =E820_TYPE_RAM ; +process_unaccepted_memory (d->phys_addr, + d->phys_addr +PAGE_SIZE * d->num_pages); +break ;default :continue ; }
@@ -697,6 +707,9 @@ static efi_status_t allocate_e820(struct boot_params *params, status =alloc_e820ext (nr_e820ext, e820ext, e820ext_size); } +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY ) && status ==EFI_SUCCESS ) + status =allocate_unaccepted_bitmap (nr_desc, map); +efi_bs_call (free_pool, map);return status; }
include/linux/efi.h +11 -1
@@ -108,7 +108,8 @@ typedef struct {#define EFI_MEMORY_MAPPED_IO_PORT_SPACE 12#define EFI_PAL_CODE 13#define EFI_PERSISTENT_MEMORY 14 -#define EFI_MAX_MEMORY_TYPE 15 +#define EFI_UNACCEPTED_MEMORY 15 +#define EFI_MAX_MEMORY_TYPE 16/* Attribute values: */ #define EFI_MEMORY_UC ((u64)0x0000000000000001ULL)/* uncached */
@@ -417,6 +418,7 @@ void efi_native_runtime_setup(void);#define LINUX_EFI_MOK_VARIABLE_TABLE_GUID EFI_GUID(0xc451ed2b, 0x9694, 0x45d3, 0xba, 0xba, 0xed, 0x9f, 0x89, 0x88, 0xa3, 0x89)#define LINUX_EFI_COCO_SECRET_AREA_GUID EFI_GUID(0xadf956ad, 0xe98c, 0x484c, 0xae, 0x11, 0xb5, 0x1c, 0x7d, 0x33, 0x64, 0x47)#define LINUX_EFI_BOOT_MEMMAP_GUID EFI_GUID(0x800f683f, 0xd08b, 0x423a, 0xa2, 0x93, 0x96, 0x5c, 0x3c, 0x6f, 0xe2, 0xb4) +#define LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID EFI_GUID(0xd5d1de3c, 0x105c, 0x44f9, 0x9e, 0xa9, 0xbc, 0xef, 0x98, 0x12, 0x00, 0x31)#define RISCV_EFI_BOOT_PROTOCOL_GUID EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf)
@@ -534,6 +536,14 @@ struct efi_boot_memmap {efi_memory_desc_t map[]; }; +struct efi_unaccepted_memory { +u32 version; +u32 unit_size; +u64 phys_base; +u64 size; +unsigned long bitmap[]; +}; +/* * Architecture independent structure for describing a memory map for the * benefit of efi_memmap_init_early(), and for passing context between
Signature · 1 line
2.39.3
Borislav Petkov
maintainerRe: [PATCHv13 3/9] efi/libstub: Implement support for unaccepted memory
drivers/firmware/efi/Kconfig config EFI_COCO_SECRET · line 272
Quoted 9 lines · Kirill A. Shutemov
drivers/firmware/efi/Kconfig +1
-0@@ -269,6 +269,20 @@ config EFI_COCO_SECRET virt/coco/efi_secret module to access the secrets, which in turn allows userspace programs to access the injected secrets. +config UNACCEPTED_MEMORY
Why is this Kconfig symbol in this file?
I'm thinking this needs to be somewhere generic, like in mm/Kconfig or so...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCHv13 3/9] efi/libstub: Implement support for unaccepted memory
Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Kirill A. Shutemov
drivers/firmware/efi/Kconfig +0
-0@@ -269,6 +269,20 @@ config EFI_COCO_SECRET virt/coco/efi_secret module to access the secrets, which in turn allows userspace programs to access the injected secrets.
Quoted 6 lines
drivers/firmware/efi/Kconfig config EFI_COCO_SECRET · line 272
Quoted 1 line
drivers/firmware/efi/Kconfig +1
-0+config UNACCEPTED_MEMORYWhy is this
Kconfigsymbol in this file?I'm thinking this needs to be somewhere generic, like in mm/Kconfig or so...
Unaccepted memory is an EFI feature. We can move it somewhere else, if other firmware/platform would support anything like it.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v13 4/9 x86/boot/compressed: Handle unaccepted memory
In reply to Kirill A. ShutemovThe firmware will pre-accept the memory used to run the stub. But, the stub is responsible for accepting the memory into which it decompresses the main kernel. Accept memory just before decompression starts.
The stub is also responsible for choosing a physical address in which to place the decompressed kernel image. The KASLR mechanism will randomize this physical address. Since the accepted memory region is relatively small, KASLR would be quite ineffective if it only used the pre-accepted area (EFI_CONVENTIONAL_MEMORY). Ensure that KASLR randomizes among the entire physical address space by also including EFI_UNACCEPTED_MEMORY.
--- arch/x86/boot/compressed/efi.h | 10 +++++++++ arch/x86/boot/compressed/kaslr.c | 35 +++++++++++++++++++++----------- arch/x86/boot/compressed/mem.c | 31 ++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.c | 7 +++++++ arch/x86/boot/compressed/misc.h | 6 ++++++ 5 files changed, 77 insertions(+), 12 deletions(-)
arch/x86/boot/compressed/efi.h +10 -0
@@ -16,6 +16,7 @@ typedef guid_t efi_guid_t __aligned(__alignof__(u32));#define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)#define ACPI_20_TABLE_GUID EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81)#define EFI_CC_BLOB_GUID EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42) +#define LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID EFI_GUID(0xd5d1de3c, 0x105c, 0x44f9, 0x9e, 0xa9, 0xbc, 0xef, 0x98, 0x12, 0x00, 0x31)#define EFI32_LOADER_SIGNATURE "EL32"#define EFI64_LOADER_SIGNATURE "EL64"
@@ -32,6 +33,7 @@ typedef struct { } efi_table_hdr_t;#define EFI_CONVENTIONAL_MEMORY 7 +#define EFI_UNACCEPTED_MEMORY 15#define EFI_MEMORY_MORE_RELIABLE \ ((u64)0x0000000000010000ULL)/* higher reliability */
@@ -104,6 +106,14 @@ struct efi_setup_data {u64 reserved[8 ]; }; +struct efi_unaccepted_memory { +u32 version; +u32 unit_size; +u64 phys_base; +u64 size; +unsigned long bitmap[]; +}; +static inline int efi_guidcmp (efi_guid_t left,efi_guid_t right) {return memcmp (&left, &right,sizeof (efi_guid_t));
arch/x86/boot/compressed/kaslr.c +23 -12
@@ -672,6 +672,28 @@ static bool process_mem_region(struct mem_vector *region, }#ifdef CONFIG_EFI + +/* +* Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are +* guaranteed to be free. +* +* It is more conservative in picking free memory than the EFI spec allows: +* +* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory +* and thus available to place the kernel image into, but in practice there's +* firmware where using that memory leads to crashes. +*/ +static inline bool memory_type_is_free (efi_memory_desc_t *md) +{ +if (md->type ==EFI_CONVENTIONAL_MEMORY ) +return true; + +if (md->type ==EFI_UNACCEPTED_MEMORY ) +return IS_ENABLED (CONFIG_UNACCEPTED_MEMORY ); + +return false; +} +/* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available.
@@ -716,18 +738,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)for (i =0 ; i < nr_desc; i++) { md =efi_early_memdesc_ptr (pmap, e->efi_memdesc_size, i); -/* -* Here we are more conservative in picking free memory than -* the EFI spec allows: -* -* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also -* free memory and thus available to place the kernel image into, -* but in practice there's firmware where using that memory leads -* to crashes. -* -* Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. -*/ -if (md->type !=EFI_CONVENTIONAL_MEMORY ) +if (!memory_type_is_free (md))continue ; if (efi_soft_reserve_enabled() &&
arch/x86/boot/compressed/mem.c +31 -0
@@ -1,9 +1,40 @@// SPDX-License-Identifier: GPL-2.0-only #include "error.h" +#include "misc.h" void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ error ("Cannot accept memory" ); } + +void init_unaccepted_memory (void ) +{ +guid_t guid =LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID ; +struct efi_unaccepted_memory *unaccepted_table; +unsigned long cfg_table_pa; +unsigned int cfg_table_len; +enum efi_type et; +int ret; + + et =efi_get_type (boot_params); +if (et ==EFI_TYPE_NONE ) +return ; + + ret =efi_get_conf_table (boot_params, &cfg_table_pa, &cfg_table_len); +if (ret) +error ("EFI config table not found." ); + + unaccepted_table = (void *)efi_find_vendor_table (boot_params, + cfg_table_pa, + cfg_table_len, + guid); +if (!unaccepted_table) +return ; + +if (unaccepted_table->version !=1 ) +error ("Unknown version of unaccepted memory table\n" ); + +set_unaccepted_table (unaccepted_table); +}
arch/x86/boot/compressed/misc.c +7 -0
@@ -455,6 +455,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,#endif debug_putstr ("\nDecompressing Linux... " ); + +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY )) { +debug_putstr ("Accepting memory... " ); +init_unaccepted_memory (); +accept_memory (__pa (output),__pa (output) + needed_size); + } +__decompress (input_data, input_len,NULL ,NULL , output, output_len,NULL , error); entry_offset =parse_elf (output);
arch/x86/boot/compressed/misc.h +6 -0
@@ -247,4 +247,10 @@ static inline unsigned long efi_find_vendor_table(struct boot_params *bp, }#endif /* CONFIG_EFI */ +void init_unaccepted_memory (void ); + +/* Implemented in EFI stub */ +void set_unaccepted_table (struct efi_unaccepted_memory *table); +void accept_memory (phys_addr_t start,phys_addr_t end); +#endif /* BOOT_COMPRESSED_MISC_H */
Signature · 1 line
2.39.3
Borislav Petkov
maintainerRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
Quoted 5 lines · Kirill A. Shutemov
arch/x86/boot/compressed/kaslr.c +0
-0
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 680
Quoted 9 lines
arch/x86/boot/compressed/kaslr.c +6
-0@@ -672,6 +672,28 @@ static bool process_mem_region(struct mem_vector *region, }#ifdef CONFIG_EFI + +/* +* Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are +* guaranteed to be free. +* +* It is more conservative in picking free memory than the EFI spec allows:
"Pick free memory more conservatively than the EFI spec allows:EFI_BOOT_SERVICES_ ..."
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 684
Quoted 4 lines
arch/x86/boot/compressed/kaslr.c +4
-0+* +* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory +* and thus available to place the kernel image into, but in practice there's +* firmware where using that memory leads to crashes.
... because that firmware still scratches into that memory or why?
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 692
Quoted 8 lines
arch/x86/boot/compressed/kaslr.c +8
-0+*/ +static inline bool memory_type_is_free (efi_memory_desc_t *md) +{ +if (md->type ==EFI_CONVENTIONAL_MEMORY ) +return true; + +if (md->type ==EFI_UNACCEPTED_MEMORY ) +return IS_ENABLED (CONFIG_UNACCEPTED_MEMORY );
Make it plan and simple:
if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && md->type == EFI_UNACCEPTED_MEMORY) return true;
Quoted 36 lines
arch/x86/boot/compressed/kaslr.c +5
-12+ +return false; +} +/* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available. @@ -716,18 +738,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)for (i =0 ; i < nr_desc; i++) { md =efi_early_memdesc_ptr (pmap, e->efi_memdesc_size, i); -/* -* Here we are more conservative in picking free memory than -* the EFI spec allows: -* -* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also -* free memory and thus available to place the kernel image into, -* but in practice there's firmware where using that memory leads -* to crashes. -* -* Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. -*/ -if (md->type !=EFI_CONVENTIONAL_MEMORY ) +if (!memory_type_is_free (md))continue ; if (efi_soft_reserve_enabled() &&arch/x86/boot/compressed/mem.c +1
-0@@ -1,9 +1,40 @@// SPDX-License-Identifier: GPL-2.0-only #include "error.h" +#include "misc.h"
arch/x86/boot/compressed/mem.c line 14
Quoted 10 lines
arch/x86/boot/compressed/mem.c +4
-0void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ error ("Cannot accept memory" ); } + +void init_unaccepted_memory (void ) +{ +guid_t guid =LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID ;
An additional space after the "=".
arch/x86/boot/compressed/mem.c line 27
Quoted 13 lines
arch/x86/boot/compressed/mem.c +13
-0+struct efi_unaccepted_memory *unaccepted_table; +unsigned long cfg_table_pa; +unsigned int cfg_table_len; +enum efi_type et; +int ret; + + et =efi_get_type (boot_params); +if (et ==EFI_TYPE_NONE ) +return ; + + ret =efi_get_conf_table (boot_params, &cfg_table_pa, &cfg_table_len); +if (ret) +error ("EFI config table not found." );
What's the point in erroring out here?
arch/x86/boot/compressed/mem.c line 39
Quoted 11 lines
arch/x86/boot/compressed/mem.c +11
-0+ unaccepted_table = (void *)efi_find_vendor_table (boot_params, + cfg_table_pa, + cfg_table_len, + guid); +if (!unaccepted_table) +return ; + +if (unaccepted_table->version !=1 ) +error ("Unknown version of unaccepted memory table\n" ); + +set_unaccepted_table (unaccepted_table);
Why is this a function at all and not a simple assignment?
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 460
Quoted 11 lines
arch/x86/boot/compressed/misc.c +3
-0@@ -455,6 +455,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,#endif debug_putstr ("\nDecompressing Linux... " ); + +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY )) { +debug_putstr ("Accepting memory... " );
This needs to happen...
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 461
Quoted 1 line
arch/x86/boot/compressed/misc.c +1
-0+init_unaccepted_memory ();
... after the init, after the init has parsed the config table and has found unaccepted memory.
If not, you don't need to issue anything as that would be wrong.
Quoted 6 lines
arch/x86/boot/compressed/misc.c +3
-0+accept_memory (__pa (output),__pa (output) + needed_size); + } +__decompress (input_data, input_len,NULL ,NULL , output, output_len,NULL , error); entry_offset =parse_elf (output);
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
Quoted 8 lines · Borislav Petkov
Quoted 7 lines · Kirill A. Shutemov
arch/x86/boot/compressed/kaslr.c +0
-0@@ -672,6 +672,28 @@ static bool process_mem_region(struct mem_vector *region, }
Quoted 10 lines
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 680
Quoted 7 lines
arch/x86/boot/compressed/kaslr.c +6
-0#ifdef CONFIG_EFI + +/* +* Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are +* guaranteed to be free. +* +* It is more conservative in picking free memory than the EFI spec allows: "Pick free memory more conservatively than the EFI spec allows:
EFI_BOOT_SERVICES_..."
Okay.
Quoted 6 lines
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 684
Quoted 4 lines
arch/x86/boot/compressed/kaslr.c +4
-0+* +* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory +* and thus available to place the kernel image into, but in practice there's +* firmware where using that memory leads to crashes. ... because that firmware still scratches into that memory or why?
I moved the existing comment. I don't have have any context beyond that.
Relevant commit: 0982adc74673 ("x86/boot/KASLR: Work around firmware bugs by excluding EFI_BOOT_SERVICES_* and EFI_LOADER_* from KASLR's choice")
Ard, do you have anything to add here?
Quoted 11 lines
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 692
Quoted 8 lines
arch/x86/boot/compressed/kaslr.c +8
-0+*/ +static inline bool memory_type_is_free (efi_memory_desc_t *md) +{ +if (md->type ==EFI_CONVENTIONAL_MEMORY ) +return true; + +if (md->type ==EFI_UNACCEPTED_MEMORY ) +return IS_ENABLED (CONFIG_UNACCEPTED_MEMORY );Make it plan and simple:
Quoted 3 lines
if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && md->type == EFI_UNACCEPTED_MEMORY) return true;
I don't see why it is simpler. It looks unnecessary noisy to me.
But okay.
Quoted 37 lines
Quoted 37 lines
arch/x86/boot/compressed/kaslr.c +5
-12+ +return false; +} +/* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available. @@ -716,18 +738,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)for (i =0 ; i < nr_desc; i++) { md =efi_early_memdesc_ptr (pmap, e->efi_memdesc_size, i); -/* -* Here we are more conservative in picking free memory than -* the EFI spec allows: -* -* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also -* free memory and thus available to place the kernel image into, -* but in practice there's firmware where using that memory leads -* to crashes. -* -* Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. -*/ -if (md->type !=EFI_CONVENTIONAL_MEMORY ) +if (!memory_type_is_free (md))continue ; if (efi_soft_reserve_enabled() &&arch/x86/boot/compressed/mem.c +1
-0@@ -1,9 +1,40 @@// SPDX-License-Identifier: GPL-2.0-only #include "error.h" +#include "misc.h"
Quoted 11 lines
arch/x86/boot/compressed/mem.c line 14
Quoted 9 lines
arch/x86/boot/compressed/mem.c +4
-0void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ error ("Cannot accept memory" ); } + +void init_unaccepted_memory (void ) +{ +guid_t guid =LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID ;An additional space after the "=".
Okay.
Quoted 7 lines
Quoted 7 lines
arch/x86/boot/compressed/mem.c +7
-0+struct efi_unaccepted_memory *unaccepted_table; +unsigned long cfg_table_pa; +unsigned int cfg_table_len; +enum efi_type et; +int ret; + + et =efi_get_type (boot_params);
Quoted 8 lines
arch/x86/boot/compressed/mem.c line 27
Quoted 6 lines
arch/x86/boot/compressed/mem.c +6
-0+if (et ==EFI_TYPE_NONE ) +return ; + + ret =efi_get_conf_table (boot_params, &cfg_table_pa, &cfg_table_len); +if (ret) +error ("EFI config table not found." );What's the point in erroring out here?
Configuration table suppose to be present, even if unaccepted memory is not supported. Something is very wrong if it is missing.
I will downgrade it warn().
Quoted 5 lines
Quoted 5 lines
arch/x86/boot/compressed/mem.c +5
-0+ unaccepted_table = (void *)efi_find_vendor_table (boot_params, + cfg_table_pa, + cfg_table_len, + guid); +if (!unaccepted_table)
Quoted 8 lines
arch/x86/boot/compressed/mem.c line 39
Quoted 6 lines
arch/x86/boot/compressed/mem.c +6
-0+return ; + +if (unaccepted_table->version !=1 ) +error ("Unknown version of unaccepted memory table\n" ); + +set_unaccepted_table (unaccepted_table);Why is this a function at all and not a simple assignment?
I wanted to keep unaccepted_table private to the libstub/unaccepted_memory.c. The setter provides a good spot for documentation to guide unaccepted memory enablers for other archs.
Still want replace it with direct assignment?
Quoted 15 lines
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 460
Quoted 12 lines
arch/x86/boot/compressed/misc.c +3
-0@@ -455,6 +455,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,#endif debug_putstr ("\nDecompressing Linux... " ); + +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY )) { +debug_putstr ("Accepting memory... " );This needs to happen...
Quoted 6 lines
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 461
Quoted 1 line
arch/x86/boot/compressed/misc.c +1
-0+init_unaccepted_memory ();... after the init, after the init has parsed the config table and has found unaccepted memory.
If not, you don't need to issue anything as that would be wrong.
Okay, I will make init_unaccepted_memory() return true if unaccepted memory is present and hide defined it always-false for !UNACCEPTED_MEMORY. So this hunk will look this way:
if (init_unaccepted_memory()) {
debug_putstr("Accepting memory... ");
accept_memory(__pa(output), __pa(output) + needed_size);
}Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Ard Biesheuvel
maintainerRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
Quoted 28 lines · Kirill A. Shutemov
Quoted 8 lines · Borislav Petkov
Quoted 7 lines · Kirill A. Shutemov
arch/x86/boot/compressed/kaslr.c +0
-0@@ -672,6 +672,28 @@ static bool process_mem_region(struct mem_vector *region, }Quoted 10 lines
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 680
Quoted 7 lines
arch/x86/boot/compressed/kaslr.c +6
-0#ifdef CONFIG_EFI + +/* +* Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are +* guaranteed to be free. +* +* It is more conservative in picking free memory than the EFI spec allows: "Pick free memory more conservatively than the EFI spec allows:
EFI_BOOT_SERVICES_..."Okay.
Quoted 5 lines
Quoted 4 lines
arch/x86/boot/compressed/kaslr.c +4
-0+* +* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory +* and thus available to place the kernel image into, but in practice there's +* firmware where using that memory leads to crashes.
Quoted 9 lines
Quoted 1 line
... because that firmware still scratches into that memory or why?
I moved the existing comment. I don't have have any context beyond that.
Relevant commit:
0982adc74673("x86/boot/KASLR: Work around firmware bugs by excluding EFI_BOOT_SERVICES_* and EFI_LOADER_* from KASLR's choice")Ard, do you have anything to add here?
The problem is that on x86, there is buggy vendor/OEM EFI code that registers for internal events that trigger when SetVirtualAddressMap() is called, and assume that at that point, EfiBootServicesData memory regions have not been touched by the loader yet, which is probably true if you are booting Windows.
So on x86, the kernel proper also preserves these regions until after it calls SetVirtualAddressMap() (efi_free_boot_services() in arch/x86/platform/efi/quirks.c)
So for the same reason, this code needs to disregard those regions as well.
Quoted 144 lines
Quoted 11 lines
arch/x86/boot/compressed/kaslr.c static bool process_mem_region(struct mem_vector *region, · line 692
Quoted 8 lines
arch/x86/boot/compressed/kaslr.c +8
-0+*/ +static inline bool memory_type_is_free (efi_memory_desc_t *md) +{ +if (md->type ==EFI_CONVENTIONAL_MEMORY ) +return true; + +if (md->type ==EFI_UNACCEPTED_MEMORY ) +return IS_ENABLED (CONFIG_UNACCEPTED_MEMORY );Make it plan and simple:
Quoted 3 lines
if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && md->type == EFI_UNACCEPTED_MEMORY) return true;I don't see why it is simpler. It looks unnecessary noisy to me.
But okay.
Quoted 37 lines
Quoted 37 lines
arch/x86/boot/compressed/kaslr.c +5
-12+ +return false; +} +/* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available. @@ -716,18 +738,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)for (i =0 ; i < nr_desc; i++) { md =efi_early_memdesc_ptr (pmap, e->efi_memdesc_size, i); -/* -* Here we are more conservative in picking free memory than -* the EFI spec allows: -* -* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also -* free memory and thus available to place the kernel image into, -* but in practice there's firmware where using that memory leads -* to crashes. -* -* Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. -*/ -if (md->type !=EFI_CONVENTIONAL_MEMORY ) +if (!memory_type_is_free (md))continue ; if (efi_soft_reserve_enabled() &&arch/x86/boot/compressed/mem.c +1
-0@@ -1,9 +1,40 @@// SPDX-License-Identifier: GPL-2.0-only #include "error.h" +#include "misc.h" Quoted 11 lines
arch/x86/boot/compressed/mem.c line 14
Quoted 9 lines
arch/x86/boot/compressed/mem.c +4
-0void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ error ("Cannot accept memory" ); } + +void init_unaccepted_memory (void ) +{ +guid_t guid =LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID ;An additional space after the "=".
Okay.
Quoted 7 lines
Quoted 7 lines
arch/x86/boot/compressed/mem.c +7
-0+struct efi_unaccepted_memory *unaccepted_table; +unsigned long cfg_table_pa; +unsigned int cfg_table_len; +enum efi_type et; +int ret; + + et =efi_get_type (boot_params);Quoted 8 lines
arch/x86/boot/compressed/mem.c line 27
Quoted 6 lines
arch/x86/boot/compressed/mem.c +6
-0+if (et ==EFI_TYPE_NONE ) +return ; + + ret =efi_get_conf_table (boot_params, &cfg_table_pa, &cfg_table_len); +if (ret) +error ("EFI config table not found." );What's the point in erroring out here?
Configuration table suppose to be present, even if unaccepted memory is not supported. Something is very wrong if it is missing.
I will downgrade it
warn().Quoted 5 lines
Quoted 5 lines
arch/x86/boot/compressed/mem.c +5
-0+ unaccepted_table = (void *)efi_find_vendor_table (boot_params, + cfg_table_pa, + cfg_table_len, + guid); +if (!unaccepted_table)Quoted 8 lines
arch/x86/boot/compressed/mem.c line 39
Quoted 6 lines
arch/x86/boot/compressed/mem.c +6
-0+return ; + +if (unaccepted_table->version !=1 ) +error ("Unknown version of unaccepted memory table\n" ); + +set_unaccepted_table (unaccepted_table);Why is this a function at all and not a simple assignment?
I wanted to keep
unaccepted_tableprivate to thelibstub/unaccepted_memory.c. The setter provides a good spot for documentation to guide unaccepted memory enablers for other archs.Still want replace it with direct assignment?
Quoted 15 lines
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 460
Quoted 12 lines
arch/x86/boot/compressed/misc.c +3
-0@@ -455,6 +455,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,#endif debug_putstr ("\nDecompressing Linux... " ); + +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY )) { +debug_putstr ("Accepting memory... " );This needs to happen...
Quoted 6 lines
arch/x86/boot/compressed/misc.c asmlinkage __visible void *extract_kernel(void *rmode, memptr heap, · line 461
Quoted 1 line
arch/x86/boot/compressed/misc.c +1
-0+init_unaccepted_memory ();... after the init, after the init has parsed the config table and has found unaccepted memory.
If not, you don't need to issue anything as that would be wrong.
Okay, I will make
init_unaccepted_memory()return true if unaccepted memory is present and hide defined it always-false for !UNACCEPTED_MEMORY. So this hunk will look this way:if (init_unaccepted_memory()) { debug_putstr("Accepting memory... "); accept_memory(__pa(output), __pa(output) + needed_size); }--
Kiryl Shutsemau / Kirill A. Shutemov
Borislav Petkov
maintainerRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
Quoted 11 lines · Ard Biesheuvel
The problem is that on x86, there is buggy vendor/OEM EFI code that registers for internal events that trigger when
SetVirtualAddressMap()is called, and assume that at that point, EfiBootServicesData memory regions have not been touched by the loader yet, which is probably true if you are booting Windows.So on x86, the kernel proper also preserves these regions until after it calls
SetVirtualAddressMap()(efi_free_boot_services()inarch/x86/platform/efi/quirks.c)So for the same reason, this code needs to disregard those regions as well.
I'd like for us to have this explanation in the comment since it is being touched anyway.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
In reply to Kirill A. ShutemovQuoted 1 line · Kirill A. Shutemov
I don't see why it is simpler. It looks unnecessary noisy to me.
Noisy?
I have no clue what you mean.
It is regular:
if (bla && flu)
vs
if (bla) return flu();
It is about having regular patterns which can be recognized at a quick glance by those who get to stare at that code constantly.
Quoted 2 lines
Configuration table suppose to be present, even if unaccepted memory is not supported. Something is very wrong if it is missing.
I am not sure if it is the decompressor's job to do such validation
- I guess this is something the EFI code should do.
Quoted 1 line
I will downgrade it
warn().
Yes, or simply return here without accepting memory - plain and simple.
Quoted 5 lines
I wanted to keep
unaccepted_tableprivate to thelibstub/unaccepted_memory.c. The setter provides a good spot for documentation to guide unaccepted memory enablers for other archs.Still want replace it with direct assignment?
No clue. Why would you want to keep a variable in the libstub private which is not even in kernel proper, AFAICT?
Quoted 8 lines
Okay, I will make
init_unaccepted_memory()return true if unaccepted memory is present and hide defined it always-false for !UNACCEPTED_MEMORY. So this hunk will look this way:if (init_unaccepted_memory()) { debug_putstr("Accepting memory... "); accept_memory(__pa(output), __pa(output) + needed_size); }
Yap, thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteArd Biesheuvel
maintainerRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
Quoted 2 lines · Borislav Petkov
On Fri, Jun 02, 2023 at 06:36:44PM +0300, Kirill A. Shutemov wrote:
..
Quoted 6 lines
Quoted 2 lines
Configuration table suppose to be present, even if unaccepted memory is not supported. Something is very wrong if it is missing.
I am not sure if it is the decompressor's job to do such validation
- I guess this is something the EFI code should do.
'EFI code' is ambiguous here.
Most of the decompressor code is constructed in a way that permits
- booting 'native EFI' via the EFI stub
- booting 'pseudo-EFI' where GRUB or another Linux/x86 specific bootloader populates boot_params with all the EFI specific information (system table, memory map, etc)
This distinction has been abstracted away here, and so we might be dealing with the second case, and booting from a GRUB that does not understand accepted memory, but simply copied the EFI memory map (including unaccepted regions) as it normally does. (Note that the second case also covers kexec boot, so we do need to support it)
Borislav Petkov
maintainerRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
Quoted 8 lines · Ard Biesheuvel
'EFI code' is ambiguous here.
Most of the decompressor code is constructed in a way that permits
- booting 'native EFI' via the EFI stub
- booting 'pseudo-EFI' where GRUB or another Linux/x86 specific bootloader populatesboot_paramswith all the EFI specific information (system table, memory map, etc)
Quoted 5 lines
This distinction has been abstracted away here, and so we might be dealing with the second case, and booting from a GRUB that does not understand accepted memory, but simply copied the EFI memory map (including unaccepted regions) as it normally does. (Note that the second case also covers kexec boot, so we do need to support it)
Right, I was hoping there to be some glue which sanity-checks boot_params.efi_info instead relying on users to do so and thus have a bunch of duplicated code.
So, yes, right after populating the boot_params pointer...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCHv13 4/9] x86/boot/compressed: Handle unaccepted memory
In reply to Borislav PetkovHi Borislav,
Below is updated patch that suppose to address all your concerns.
I also adjusted 3/9 to remove the setter and make the variable globally visible.
All changes, including fixup for 9/9 has been pushed to the branch
https://github.com/intel/tdx.git guest-unaccepted-memory
Let me know if you see more problems with the patchset.
From ddb1c92d33217b5acd2ec3278f7ed751afd15bd1 Mon Sep 17 00:00:00 2001
From: "Kirill A. Shutemov" <[email protected]>
Date: Wed, 25 Aug 2021 21:42:51 +0300
Subject: [PATCH] x86/boot/compressed: Handle unaccepted memory
The firmware will pre-accept the memory used to run the stub. But, the stub is responsible for accepting the memory into which it decompresses the main kernel. Accept memory just before decompression starts.
The stub is also responsible for choosing a physical address in which to place the decompressed kernel image. The KASLR mechanism will randomize this physical address. Since the accepted memory region is relatively small, KASLR would be quite ineffective if it only used the pre-accepted area (EFI_CONVENTIONAL_MEMORY). Ensure that KASLR randomizes among the entire physical address space by also including EFI_UNACCEPTED_MEMORY.
--- arch/x86/boot/compressed/efi.h | 10 ++++++++ arch/x86/boot/compressed/kaslr.c | 40 +++++++++++++++++++++---------- arch/x86/boot/compressed/mem.c | 41 ++++++++++++++++++++++++++++++++ arch/x86/boot/compressed/misc.c | 6 +++++ arch/x86/boot/compressed/misc.h | 10 ++++++++ 5 files changed, 95 insertions(+), 12 deletions(-)
arch/x86/boot/compressed/efi.h +10 -0
@@ -16,6 +16,7 @@ typedef guid_t efi_guid_t __aligned(__alignof__(u32));#define ACPI_TABLE_GUID EFI_GUID(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)#define ACPI_20_TABLE_GUID EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81)#define EFI_CC_BLOB_GUID EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42) +#define LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID EFI_GUID(0xd5d1de3c, 0x105c, 0x44f9, 0x9e, 0xa9, 0xbc, 0xef, 0x98, 0x12, 0x00, 0x31)#define EFI32_LOADER_SIGNATURE "EL32"#define EFI64_LOADER_SIGNATURE "EL64"
@@ -32,6 +33,7 @@ typedef struct { } efi_table_hdr_t;#define EFI_CONVENTIONAL_MEMORY 7 +#define EFI_UNACCEPTED_MEMORY 15#define EFI_MEMORY_MORE_RELIABLE \ ((u64)0x0000000000010000ULL)/* higher reliability */
@@ -104,6 +106,14 @@ struct efi_setup_data {u64 reserved[8 ]; }; +struct efi_unaccepted_memory { +u32 version; +u32 unit_size; +u64 phys_base; +u64 size; +unsigned long bitmap[]; +}; +static inline int efi_guidcmp (efi_guid_t left,efi_guid_t right) {return memcmp (&left, &right,sizeof (efi_guid_t));
arch/x86/boot/compressed/kaslr.c +28 -12
@@ -672,6 +672,33 @@ static bool process_mem_region(struct mem_vector *region, }#ifdef CONFIG_EFI + +/* +* Only EFI_CONVENTIONAL_MEMORY and EFI_UNACCEPTED_MEMORY (if supported) are +* guaranteed to be free. +* +* Pick free memory more conservatively than the EFI spec allows: according to +* the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also free memory and thus +* available to place the kernel image into, but in practice there's firmware +* where using that memory leads to crashes. Buggy vendor EFI code registers +* for an event that triggers on SetVirtualAddressMap(). The handler assumes +* that EFI_BOOT_SERVICES_DATA memory has not been touched by loader yet, which +* is probably true for Windows. +* +* Preserve EFI_BOOT_SERVICES_* regions until after SetVirtualAddressMap(). +*/ +static inline bool memory_type_is_free (efi_memory_desc_t *md) +{ +if (md->type ==EFI_CONVENTIONAL_MEMORY ) +return true; + +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY ) && + md->type ==EFI_UNACCEPTED_MEMORY ) +return true; + +return false; +} +/* * Returns true if we processed the EFI memmap, which we prefer over the E820 * table if it is available.
@@ -716,18 +743,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)for (i =0 ; i < nr_desc; i++) { md =efi_early_memdesc_ptr (pmap, e->efi_memdesc_size, i); -/* -* Here we are more conservative in picking free memory than -* the EFI spec allows: -* -* According to the spec, EFI_BOOT_SERVICES_{CODE|DATA} are also -* free memory and thus available to place the kernel image into, -* but in practice there's firmware where using that memory leads -* to crashes. -* -* Only EFI_CONVENTIONAL_MEMORY is guaranteed to be free. -*/ -if (md->type !=EFI_CONVENTIONAL_MEMORY ) +if (!memory_type_is_free (md))continue ; if (efi_soft_reserve_enabled() &&
arch/x86/boot/compressed/mem.c +41 -0
50 lines not on this page · raw message
arch/x86/boot/compressed/misc.c +6 -0
@@ -455,6 +455,12 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,#endif debug_putstr ("\nDecompressing Linux... " ); + +if (init_unaccepted_memory ()) { +debug_putstr ("Accepting memory... " ); +accept_memory (__pa (output),__pa (output) + needed_size); + } +__decompress (input_data, input_len,NULL ,NULL , output, output_len,NULL , error); entry_offset =parse_elf (output);
arch/x86/boot/compressed/misc.h +10 -0
@@ -247,4 +247,14 @@ static inline unsigned long efi_find_vendor_table(struct boot_params *bp, }#endif /* CONFIG_EFI */ +#ifdef CONFIG_UNACCEPTED_MEMORY +bool init_unaccepted_memory (void ); +#else +static inline bool init_unaccepted_memory (void ) {return false; } +#endif + +/* Defined in EFI stub */ +extern struct efi_unaccepted_memory *unaccepted_table; +void accept_memory (phys_addr_t start,phys_addr_t end); +#endif /* BOOT_COMPRESSED_MISC_H */
Signature · 4 lines
2.39.3 -- Kiryl Shutsemau / Kirill A. Shutemov
PATCH v13 5/9 efi: Add unaccepted memory support
In reply to Kirill A. Shutemovefi_config_parse_tables() reserves memory that holds unaccepted memory configuration table so it won't be reused by page allocator.
Core-mm requires few helpers to support unaccepted memory:
- accept_memory() checks the range of addresses against the bitmap and accept memory if needed.
- range_contains_unaccepted_memory() checks if anything within the range requires acceptance.
Architectural code has to provide efi_get_unaccepted_table() that returns pointer to the unaccepted memory configuration table.
arch_accept_memory() handles arch-specific part of memory acceptance.
--- arch/x86/platform/efi/efi.c | 3 + drivers/firmware/efi/Makefile | 1 + drivers/firmware/efi/efi.c | 25 ++++++ drivers/firmware/efi/unaccepted_memory.c | 103 +++++++++++++++++++++++ include/linux/efi.h | 1 + 5 files changed, 133 insertions(+) create mode 100644 drivers/firmware/efi/unaccepted_memory.c
arch/x86/platform/efi/efi.c +3 -0
@@ -96,6 +96,9 @@ static const unsigned long * const efi_tables[] = {#ifdef CONFIG_EFI_COCO_SECRET &efi.coco_secret,#endif +#ifdef CONFIG_UNACCEPTED_MEMORY + &efi.unaccepted, +#endif };u64 efi_setup;/* efi setup_data physical address */
drivers/firmware/efi/Makefile +1 -0
@@ -41,3 +41,4 @@ obj-$(CONFIG_EFI_CAPSULE_LOADER) += capsule-loader.o obj-$(CONFIG_EFI_EARLYCON) += earlycon.o obj-$(CONFIG_UEFI_CPER_ARM) += cper-arm.o obj-$(CONFIG_UEFI_CPER_X86) += cper-x86.o +obj-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o
drivers/firmware/efi/efi.c +25 -0
@@ -50,6 +50,9 @@ struct efi __read_mostly efi = {#ifdef CONFIG_EFI_COCO_SECRET .coco_secret = EFI_INVALID_TABLE_ADDR,#endif +#ifdef CONFIG_UNACCEPTED_MEMORY + .unaccepted = EFI_INVALID_TABLE_ADDR, +#endif };EXPORT_SYMBOL (efi);
@@ -605,6 +608,9 @@ static const efi_config_table_type_t common_tables[] __initconst = { #ifdef CONFIG_EFI_COCO_SECRET {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" }, #endif +#ifdef CONFIG_UNACCEPTED_MEMORY + {LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" }, +#endif #ifdef CONFIG_EFI_GENERIC_STUB {LINUX_EFI_SCREEN_INFO_TABLE_GUID, &screen_info_table }, #endif
@@ -759,6 +765,25 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables, } } +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY ) && + efi.unaccepted !=EFI_INVALID_TABLE_ADDR ) { +struct efi_unaccepted_memory *unaccepted; + + unaccepted =early_memremap (efi.unaccepted,sizeof (*unaccepted)); +if (unaccepted) { +unsigned long size; + +if (unaccepted->version ==1 ) { + size =sizeof (*unaccepted) + unaccepted->size; +memblock_reserve (efi.unaccepted, size); + }else { + efi.unaccepted =EFI_INVALID_TABLE_ADDR ; + } + +early_memunmap (unaccepted,sizeof (*unaccepted)); + } + } +return 0 ; }
drivers/firmware/efi/unaccepted_memory.c new file +103 -0
103 lines not on this page · raw message
include/linux/efi.h +1 -0
@@ -646,6 +646,7 @@ extern struct efi {unsigned long tpm_final_log;/* TPM2 Final Events Log table */ unsigned long mokvar_table;/* MOK variable config table */ unsigned long coco_secret;/* Confidential computing secret table */ +unsigned long unaccepted;/* Unaccepted memory table */ efi_get_time_t *get_time;efi_set_time_t *set_time;
Signature · 1 line
2.39.3
Borislav Petkov
maintainerRe: [PATCHv13 5/9] efi: Add unaccepted memory support
drivers/firmware/efi/unaccepted_memory.c line 19
Quoted 9 lines · Kirill A. Shutemov
drivers/firmware/efi/unaccepted_memory.c new file +9
-0+void accept_memory (phys_addr_t start,phys_addr_t end) +{ +struct efi_unaccepted_memory *unaccepted; +unsigned long range_start, range_end; +unsigned long flags; +u64 unit_size; + +if (efi.unaccepted ==EFI_INVALID_TABLE_ADDR ) +return ;
efi_get_unaccepted_table() already does this test.
drivers/firmware/efi/unaccepted_memory.c line 23
Quoted 3 lines
drivers/firmware/efi/unaccepted_memory.c new file +3
-0+ unaccepted =efi_get_unaccepted_table (); +if (!unaccepted) +return ;
So this looks weird: callers can call accept_memory() and that function can fail. But they can't know whether it failed or not because it returns void.
drivers/firmware/efi/unaccepted_memory.c line 32
Quoted 8 lines
drivers/firmware/efi/unaccepted_memory.c new file +8
-0+ unit_size = unaccepted->unit_size; + +/* +* Only care for the part of the range that is represented +* in the bitmap. +*/ +if (start < unaccepted->phys_base) + start = unaccepted->phys_base;
So this silently trims start...
drivers/firmware/efi/unaccepted_memory.c line 34
Quoted 2 lines
drivers/firmware/efi/unaccepted_memory.c new file +2
-0+if (end < unaccepted->phys_base) +return ;
But fails only when end is outside of range.
I'd warn here at least. And return an error so that the callers know.
drivers/firmware/efi/unaccepted_memory.c line 42
Quoted 7 lines
drivers/firmware/efi/unaccepted_memory.c new file +7
-0+/* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted->phys_base; + end -= unaccepted->phys_base; + +/* Make sure not to overrun the bitmap */ +if (end > unaccepted->size * unit_size *BITS_PER_BYTE ) + end = unaccepted->size * unit_size *BITS_PER_BYTE ;
How is all that trimming not important to the caller?
It would assume that its memory got accepted but not really.
Quoted 26 lines
drivers/firmware/efi/unaccepted_memory.c new file +26
-0+ range_start = start / unit_size; + +spin_lock_irqsave (&unaccepted_memory_lock, flags); +for_each_set_bitrange_from (range_start, range_end, unaccepted->bitmap, +DIV_ROUND_UP (end, unit_size)) { +unsigned long phys_start, phys_end; +unsigned long len = range_end - range_start; + + phys_start = range_start * unit_size + unaccepted->phys_base; + phys_end = range_end * unit_size + unaccepted->phys_base; + +arch_accept_memory (phys_start, phys_end); +bitmap_clear (unaccepted->bitmap, range_start, len); + } +spin_unlock_irqrestore (&unaccepted_memory_lock, flags); +} + +bool range_contains_unaccepted_memory (phys_addr_t start,phys_addr_t end) +{ +struct efi_unaccepted_memory *unaccepted; +unsigned long flags; +bool ret = false; +u64 unit_size; + + unaccepted =efi_get_unaccepted_table (); +if (!unaccepted)
drivers/firmware/efi/unaccepted_memory.c line 79
Quoted 10 lines
drivers/firmware/efi/unaccepted_memory.c new file +10
-0+return false; + + unit_size = unaccepted->unit_size; + +/* +* Only care for the part of the range that is represented +* in the bitmap. +*/ +if (start < unaccepted->phys_base) + start = unaccepted->phys_base;
Same comment as above. Trimming start is fine?
drivers/firmware/efi/unaccepted_memory.c line 85
Quoted 6 lines
drivers/firmware/efi/unaccepted_memory.c new file +6
-0+if (end < unaccepted->phys_base) +return false; + +/* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted->phys_base; + end -= unaccepted->phys_base;
Ditto as above.
drivers/firmware/efi/unaccepted_memory.c line 42
Quoted 4 lines
drivers/firmware/efi/unaccepted_memory.c new file +4
-0+ +/* Make sure not to overrun the bitmap */ +if (end > unaccepted->size * unit_size *BITS_PER_BYTE ) + end = unaccepted->size * unit_size *BITS_PER_BYTE ;
Ditto.
drivers/firmware/efi/unaccepted_memory.c line 95
Quoted 5 lines
drivers/firmware/efi/unaccepted_memory.c new file +5
-0+spin_lock_irqsave (&unaccepted_memory_lock, flags); +while (start < end) { +if (test_bit (start / unit_size, unaccepted->bitmap)) { + ret = true; +break ;
I have a faint memory we've had this before but you need to check *every* bit in the unaccepted bitmap before returning true. Doh.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCHv13 5/9] efi: Add unaccepted memory support
Quoted 12 lines · Borislav Petkov
drivers/firmware/efi/unaccepted_memory.c line 19
Quoted 9 lines · Kirill A. Shutemov
drivers/firmware/efi/unaccepted_memory.c new file +9
-0+void accept_memory (phys_addr_t start,phys_addr_t end) +{ +struct efi_unaccepted_memory *unaccepted; +unsigned long range_start, range_end; +unsigned long flags; +u64 unit_size; + +if (efi.unaccepted ==EFI_INVALID_TABLE_ADDR ) +return ;
efi_get_unaccepted_table()already does this test.
Okay.
Quoted 7 lines
drivers/firmware/efi/unaccepted_memory.c line 23
Quoted 3 lines
drivers/firmware/efi/unaccepted_memory.c new file +3
-0+ unaccepted =efi_get_unaccepted_table (); +if (!unaccepted) +return ;So this looks weird: callers can call
accept_memory()and that function can fail. But they can't know whether it failed or not because it returns void.
It is not a failure here. If there's no unaccepted memory in the system accept_memory() always succeeds.
Quoted 11 lines
drivers/firmware/efi/unaccepted_memory.c line 32
Quoted 8 lines
drivers/firmware/efi/unaccepted_memory.c new file +8
-0+ unit_size = unaccepted->unit_size; + +/* +* Only care for the part of the range that is represented +* in the bitmap. +*/ +if (start < unaccepted->phys_base) + start = unaccepted->phys_base;So this silently trims start...
Quoted 6 lines
drivers/firmware/efi/unaccepted_memory.c line 34
Quoted 2 lines
drivers/firmware/efi/unaccepted_memory.c new file +2
-0+if (end < unaccepted->phys_base) +return ;But fails only when end is outside of range.
I'd warn here at least. And return an error so that the callers know.
There's nothing to warn about. The range (or part of it) is not represented in the bitmap because it is not unaccepted. We only allocate bitmap for the range that has unaccepted memory. It can reduce memory overhead on the bitmap if the unaccepted memory starts very high or ends early, but there's something else very high in physical addresss space.
Quoted 11 lines
drivers/firmware/efi/unaccepted_memory.c line 42
Quoted 7 lines
drivers/firmware/efi/unaccepted_memory.c new file +7
-0+/* Translate to offsets from the beginning of the bitmap */ + start -= unaccepted->phys_base; + end -= unaccepted->phys_base; + +/* Make sure not to overrun the bitmap */ +if (end > unaccepted->size * unit_size *BITS_PER_BYTE ) + end = unaccepted->size * unit_size *BITS_PER_BYTE ;How is all that trimming not important to the caller?
It would assume that its memory got accepted but not really.
See above: not represented in the bitmap means pre-accepted.
...
Quoted 8 lines
drivers/firmware/efi/unaccepted_memory.c line 95
Quoted 5 lines
drivers/firmware/efi/unaccepted_memory.c new file +5
-0+spin_lock_irqsave (&unaccepted_memory_lock, flags); +while (start < end) { +if (test_bit (start / unit_size, unaccepted->bitmap)) { + ret = true; +break ;I have a faint memory we've had this before but you need to check *every* bit in the unaccepted bitmap before returning true. Doh.
Yes, it was discussed before. Here's context:
https://lore.kernel.org/all/Ynt8vDY78/[email protected]
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Borislav Petkov
maintainerRe: [PATCHv13 5/9] efi: Add unaccepted memory support
Quoted 2 lines · Kirill A. Shutemov
There's nothing to warn about. The range (or part of it) is not represented in the bitmap because it is not unaccepted.
Sorry but how am I supposed to know that?!
I've read the whole patchset up until now and all text talks like *all* *memory* needs to be accepted and before that has happeend, it is unaccepted.
So how about you explain that explicitly somewhere, perhaps in a comment above accept_memory(), that the unaccepted range is not the whole memory but only, well, what is unaccepted and the rest is implicitly accepted?
And I went and looked at the final result - we error() if we fail accepting.
I guess that's the only action we can do anyway...
Quoted 3 lines
Yes, it was discussed before. Here's context:
You should try those before you paste them - it says "Not found" because of the '/' in the Message-ID and it needs to be escaped.
This works:
https://lore.kernel.org/all/Ynt8vDY78%[email protected]/
Now I remember.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCHv13 5/9] efi: Add unaccepted memory support
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Kirill A. Shutemov
There's nothing to warn about. The range (or part of it) is not represented in the bitmap because it is not unaccepted.
Sorry but how am I supposed to know that?!
Quoted 7 lines
I've read the whole patchset up until now and all text talks like *all* *memory* needs to be accepted and before that has happeend, it is unaccepted.
So how about you explain that explicitly somewhere, perhaps in a comment above
accept_memory(), that the unaccepted range is not the whole memory but only, well, what is unaccepted and the rest is implicitly accepted?
Okay, will do.
Quoted 4 lines
And I went and looked at the final result - we
error()if we fail accepting.I guess that's the only action we can do anyway...
Right, there's no recovery from the error.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCHv13 5/9] efi: Add unaccepted memory support
In reply to Borislav PetkovQuoted 3 lines · Borislav Petkov
So how about you explain that explicitly somewhere, perhaps in a comment above
accept_memory(), that the unaccepted range is not the whole memory but only, well, what is unaccepted and the rest is implicitly accepted?
Does it look okay to you?
/*
* accept_memory() -- Consult bitmap and accept the memory if needed. *
* Only memory that explicitly marked as unaccepted in the bitmap requires
* an action.
*
* No need to accept:
* - anything if the system has no unaccepted table; * - memory that is below phys_base; * - memory that is above the memory that addressable by the bitmap;
*/
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Borislav Petkov
maintainerRe: [PATCHv13 5/9] efi: Add unaccepted memory support
Quoted 6 lines · Kirill A. Shutemov
Does it look okay to you?
/*
*accept_memory()-- Consult bitmap and accept the memory if needed. *
* Only memory that explicitly marked as unaccepted in the bitmap requires
... that is ...
Quoted 1 line
* an action.
And let's add an additional sentence stating it all clearly:
"All the remaining memory is implicitly accepted and doesn't need acceptance."
Quoted 5 lines
*
* No need to accept:* - anything if the system has no unaccepted table; * - memory that is below phys_base; * - memory that is above the memory that addressable by the bitmap;
And this is an additional clarification.
Good, thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v13 6/9 efi/unaccepted: Avoid load_unaligned_zeropad() stepping into unaccepted memory
In reply to Kirill A. Shutemovload_unaligned_zeropad() can lead to unwanted loads across page boundaries. The unwanted loads are typically harmless. But, they might be made to totally unrelated or even unmapped memory. load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now #VE) to recover from these unwanted loads.
But, this approach does not work for unaccepted memory. For TDX, a load from unaccepted memory will not lead to a recoverable exception within the guest. The guest will exit to the VMM where the only recourse is to terminate the guest.
There are two parts to fix this issue and comprehensively avoid access to unaccepted memory. Together these ensure that an extra "guard" page is accepted in addition to the memory that needs to be used.
1. Implicitly extend the range_contains_unaccepted_memory(start, end) checks up to end+unit_size if 'end' is aligned on a unit_size boundary.
2. Implicitly extend accept_memory(start, end) to end+unit_size if 'end' is aligned on a unit_size boundary.
Side note: This leads to something strange. Pages which were accepted
at boot, marked by the firmware as accepted and will never _need_ to be accepted might be on unaccepted_pages list This is a cue to ensure that the next page is accepted before 'page' can be used.
This is an actual, real-world problem which was discovered during TDX testing.
--- drivers/firmware/efi/unaccepted_memory.c | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
drivers/firmware/efi/unaccepted_memory.c +35 -0
@@ -37,6 +37,34 @@ void accept_memory(phys_addr_t start, phys_addr_t end) start -= unaccepted->phys_base; end -= unaccepted->phys_base; +/* +* load_unaligned_zeropad() can lead to unwanted loads across page +* boundaries. The unwanted loads are typically harmless. But, they +* might be made to totally unrelated or even unmapped memory. +* load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now +* #VE) to recover from these unwanted loads. +* +* But, this approach does not work for unaccepted memory. For TDX, a +* load from unaccepted memory will not lead to a recoverable exception +* within the guest. The guest will exit to the VMM where the only +* recourse is to terminate the guest. +* +* There are two parts to fix this issue and comprehensively avoid +* access to unaccepted memory. Together these ensure that an extra +* "guard" page is accepted in addition to the memory that needs to be +* used: +* +* 1. Implicitly extend the range_contains_unaccepted_memory(start, end) +* checks up to end+unit_size if 'end' is aligned on a unit_size +* boundary. +* +* 2. Implicitly extend accept_memory(start, end) to end+unit_size if +* 'end' is aligned on a unit_size boundary. (immediately following +* this comment) +*/ +if (!(end % unit_size)) + end += unit_size; +/* Make sure not to overrun the bitmap */ if (end > unaccepted->size * unit_size *BITS_PER_BYTE ) end = unaccepted->size * unit_size *BITS_PER_BYTE ;
@@ -84,6 +112,13 @@ bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end) start -= unaccepted->phys_base; end -= unaccepted->phys_base; +/* +* Also consider the unaccepted state of the *next* page. See fix #1 in +* the comment on load_unaligned_zeropad() in accept_memory(). +*/ +if (!(end % unit_size)) + end += unit_size; +/* Make sure not to overrun the bitmap */ if (end > unaccepted->size * unit_size *BITS_PER_BYTE ) end = unaccepted->size * unit_size *BITS_PER_BYTE ;
Signature · 1 line
2.39.3
PATCH v13 7/9 x86/tdx: Make _tdx_hypercall() and __tdx_module_call() available in boot stub
In reply to Kirill A. ShutemovMemory acceptance requires a hypercall and one or multiple module calls.
Make helpers for the calls available in boot stub. It has to accept memory where kernel image and initrd are placed.
--- arch/x86/coco/tdx/tdx.c | 32 ------------------- arch/x86/include/asm/shared/tdx.h | 51 +++++++++++++++++++++++++++++++ arch/x86/include/asm/tdx.h | 19 ------------ 3 files changed, 51 insertions(+), 51 deletions(-)
arch/x86/coco/tdx/tdx.c +0 -32
@@ -14,20 +14,6 @@#include <asm/insn-eval.h> #include <asm/pgtable.h> -/* TDX module Call Leaf IDs */ -#define TDX_GET_INFO 1 -#define TDX_GET_VEINFO 3 -#define TDX_GET_REPORT 4 -#define TDX_ACCEPT_PAGE 6 -#define TDX_WR 8 - -/* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */ -#define TDCS_NOTIFY_ENABLES 0x9100000000000010 - -/* TDX hypercall Leaf IDs */ -#define TDVMCALL_MAP_GPA 0x10001 -#define TDVMCALL_REPORT_FATAL_ERROR 0x10003 -/* MMIO direction */ #define EPT_READ 0#define EPT_WRITE 1
@@ -51,24 +37,6 @@#define TDREPORT_SUBTYPE_0 0 -/* -* Wrapper for standard use of __tdx_hypercall with no output aside from -* return code. -*/ -static inline u64 _tdx_hypercall (u64 fn,u64 r12,u64 r13,u64 r14,u64 r15) -{ -struct tdx_hypercall_args args = { - .r10 =TDX_HYPERCALL_STANDARD , - .r11 = fn, - .r12 = r12, - .r13 = r13, - .r14 = r14, - .r15 = r15, - }; - -return __tdx_hypercall (&args); -} -/* Called from __tdx_hypercall() for unrecoverable failure */ noinstr void __tdx_hypercall_failed(void) {
arch/x86/include/asm/shared/tdx.h +51 -0
65 lines not on this page · raw message
arch/x86/include/asm/tdx.h +0 -19
@@ -20,21 +20,6 @@#ifndef __ASSEMBLY__ -/* -* Used to gather the output registers values of the TDCALL and SEAMCALL -* instructions when requesting services from the TDX module. -* -* This is a software only structure and not part of the TDX module/VMM ABI. -*/ -struct tdx_module_output { -u64 rcx; -u64 rdx; -u64 r8; -u64 r9; -u64 r10; -u64 r11; -}; -/* * Used by the #VE exception handler to gather the #VE exception * info from the TDX module. This is a software only structure
@@ -55,10 +40,6 @@ struct ve_info {void __init tdx_early_init (void ); -/* Used to communicate with the TDX module */ -u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, -struct tdx_module_output *out); -void tdx_get_ve_info (struct ve_info *ve);bool tdx_handle_virt_exception (struct pt_regs *regs,struct ve_info *ve);
Signature · 1 line
2.39.3
PATCH v13 8/9 x86/tdx: Refactor try_accept_one()
In reply to Kirill A. ShutemovRework try_accept_one() to return accepted size instead of modifying 'start' inside the helper. It makes 'start' in-only argument and streamlines code on the caller side.
--- arch/x86/coco/tdx/tdx.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-)
arch/x86/coco/tdx/tdx.c +19 -19
@@ -713,18 +713,18 @@ static bool tdx_cache_flush_required(void)return true; } -static bool try_accept_one (phys_addr_t *start,unsigned long len, -enum pg_level pg_level) +static unsigned long try_accept_one (phys_addr_t start,unsigned long len, +enum pg_level pg_level) {unsigned long accept_size =page_level_size (pg_level);u64 tdcall_rcx;u8 page_size; -if (!IS_ALIGNED (*start, accept_size)) -return false; +if (!IS_ALIGNED (start, accept_size)) +return 0 ;if (len < accept_size) -return false; +return 0 ;/* * Pass the page physical address to the TDX module to accept the
@@ -743,15 +743,14 @@ static bool try_accept_one(phys_addr_t *start, unsigned long len, page_size =2 ;break ;default : -return false; +return 0 ; } - tdcall_rcx = *start | page_size; + tdcall_rcx = start | page_size;if (__tdx_module_call (TDX_ACCEPT_PAGE , tdcall_rcx,0 ,0 ,0 ,NULL )) -return false; +return 0 ; - *start += accept_size; -return true; +return accept_size; }/*
@@ -788,21 +787,22 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)*/ while (start < end) {unsigned long len = end - start; +unsigned long accept_size;/* * Try larger accepts first. It gives chance to VMM to keep -* 1G/2M SEPT entries where possible and speeds up process by -* cutting number of hypercalls (if successful). +* 1G/2M Secure EPT entries where possible and speeds up +* process by cutting number of hypercalls (if successful). */ -if (try_accept_one (&start, len,PG_LEVEL_1G )) -continue ; - -if (try_accept_one (&start, len,PG_LEVEL_2M )) -continue ; - -if (!try_accept_one (&start, len,PG_LEVEL_4K )) + accept_size =try_accept_one (start, len,PG_LEVEL_1G ); +if (!accept_size) + accept_size =try_accept_one (start, len,PG_LEVEL_2M ); +if (!accept_size) + accept_size =try_accept_one (start, len,PG_LEVEL_4K ); +if (!accept_size)return false; + start += accept_size; }return true;
Signature · 1 line
2.39.3
PATCH v13 9/9 x86/tdx: Add unaccepted memory support
In reply to Kirill A. ShutemovHookup TDX-specific code to accept memory.
Accepting the memory is done with ACCEPT_PAGE module call on every page in the range. MAP_GPA hypercall is not required as the unaccepted memory is considered private already.
Extract the part of tdx_enc_status_changed() that does memory acceptance in a new helper. Move the helper tdx-shared.c. It is going to be used by both main kernel and decompressor.
--- arch/x86/Kconfig | 2 + arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/error.c | 19 +++++++ arch/x86/boot/compressed/error.h | 1 + arch/x86/boot/compressed/mem.c | 35 +++++++++++- arch/x86/boot/compressed/tdx-shared.c | 2 + arch/x86/coco/tdx/Makefile | 2 +- arch/x86/coco/tdx/tdx-shared.c | 71 ++++++++++++++++++++++++ arch/x86/coco/tdx/tdx.c | 70 +---------------------- arch/x86/include/asm/shared/tdx.h | 2 + arch/x86/include/asm/unaccepted_memory.h | 24 ++++++++ 11 files changed, 160 insertions(+), 70 deletions(-) create mode 100644 arch/x86/boot/compressed/tdx-shared.c create mode 100644 arch/x86/coco/tdx/tdx-shared.c create mode 100644 arch/x86/include/asm/unaccepted_memory.h
arch/x86/Kconfig +2 -0
@@ -884,9 +884,11 @@ config INTEL_TDX_GUEST bool "Intel TDX (Trust Domain Extensions) - Guest Support" depends on X86_64 && CPU_SUP_INTEL depends on X86_X2APIC + depends on EFI_STUB select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE + select UNACCEPTED_MEMORY help Support running as a guest under Intel TDX. Without this support, the guest kernel can not boot or run under TDX.
arch/x86/boot/compressed/Makefile +1 -1
@@ -106,7 +106,7 @@ ifdef CONFIG_X86_64 endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o -vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o $(obj)/tdx-shared.o vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o
arch/x86/boot/compressed/error.c +19 -0
@@ -22,3 +22,22 @@ void error(char *m)while (1 ) asm("hlt" ); } + +/* EFI libstub provides vsnprintf() */ +#ifdef CONFIG_EFI_STUB +void panic (const char *fmt, ...) +{ +static char buf[1024 ]; +va_list args; +int len; + +va_start (args, fmt); + len =vsnprintf (buf,sizeof (buf), fmt, args); +va_end (args); + +if (len && buf[len -1 ] =='\n' ) + buf[len -1 ] ='\0' ; + +error (buf); +} +#endif
arch/x86/boot/compressed/error.h +1 -0
@@ -6,5 +6,6 @@void warn (char *m);void error (char *m)__noreturn ; +void panic (const char *fmt, ...)__noreturn __cold ;#endif /* BOOT_COMPRESSED_ERROR_H */
arch/x86/boot/compressed/mem.c +34 -1
@@ -2,11 +2,44 @@#include "error.h" #include "misc.h" +#include "tdx.h" +#include <asm/shared/tdx.h> + +/* +* accept_memory() and process_unaccepted_memory() called from EFI stub which +* runs before decompresser and its early_tdx_detect(). +* +* Enumerate TDX directly from the early users. +*/ +static bool early_is_tdx_guest (void ) +{ +static bool once; +static bool is_tdx; + +if (!IS_ENABLED (CONFIG_INTEL_TDX_GUEST )) +return false; + +if (!once) { +u32 eax, sig[3 ]; + +cpuid_count (TDX_CPUID_LEAF_ID ,0 , &eax, + &sig[0 ], &sig[2 ], &sig[1 ]); + is_tdx = !memcmp (TDX_IDENT , sig,sizeof (sig)); + once = true; + } + +return is_tdx; +}void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ -error ("Cannot accept memory" ); +if (early_is_tdx_guest ()) { +if (tdx_accept_memory (start, end)) +return ; + } + +error ("Cannot accept memory: unknown platform\n" ); }void init_unaccepted_memory (void )
arch/x86/boot/compressed/tdx-shared.c new file +2 -0
@@ -0,0 +1,2 @@ +#include "error.h" +#include "../../coco/tdx/tdx-shared.c"
arch/x86/coco/tdx/Makefile +1 -1
@@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += tdx.o tdcall.o +obj-y += tdx.o tdx-shared.o tdcall.o
arch/x86/coco/tdx/tdx-shared.c new file +71 -0
71 lines not on this page · raw message
arch/x86/coco/tdx/tdx.c +3 -67
82 lines not on this page · raw message
arch/x86/include/asm/shared/tdx.h +2 -0
@@ -91,5 +91,7 @@ struct tdx_module_output {u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_output *out); +bool tdx_accept_memory (phys_addr_t start,phys_addr_t end); +#endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_SHARED_TDX_H */
arch/x86/include/asm/unaccepted_memory.h new file +24 -0
@@ -0,0 +1,24 @@ +#ifndef _ASM_X86_UNACCEPTED_MEMORY_H +#define _ASM_X86_UNACCEPTED_MEMORY_H + +#include <linux/efi.h> +#include <asm/tdx.h> + +static inline void arch_accept_memory (phys_addr_t start,phys_addr_t end) +{ +/* Platform-specific memory-acceptance call goes here */ +if (cpu_feature_enabled (X86_FEATURE_TDX_GUEST )) { +if (tdx_accept_memory (start, end)) +return ; + } + +panic ("Cannot accept memory: unknown platform\n" ); +} + +static inline struct efi_unaccepted_memory *efi_get_unaccepted_table (void ) +{ +if (efi.unaccepted ==EFI_INVALID_TABLE_ADDR ) +return NULL ; +return __va (efi.unaccepted); +} +#endif
Signature · 1 line
2.39.3
Re: [PATCHv13 9/9] x86/tdx: Add unaccepted memory support
Quoted 132 lines · Kirill A. Shutemov
Hookup TDX-specific code to accept memory.
Accepting the memory is done with
ACCEPT_PAGEmodule call on every page in the range.MAP_GPAhypercall is not required as the unaccepted memory is considered private already.Extract the part of
tdx_enc_status_changed()that does memory acceptance in a new helper. Move the helpertdx-shared.c. It is going to be used by both main kernel and decompressor.Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/Kconfig | 2 + arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/error.c | 19 +++++++ arch/x86/boot/compressed/error.h | 1 + arch/x86/boot/compressed/mem.c | 35 +++++++++++- arch/x86/boot/compressed/tdx-shared.c | 2 + arch/x86/coco/tdx/Makefile | 2 +- arch/x86/coco/tdx/tdx-shared.c | 71 ++++++++++++++++++++++++ arch/x86/coco/tdx/tdx.c | 70 +---------------------- arch/x86/include/asm/shared/tdx.h | 2 + arch/x86/include/asm/unaccepted_memory.h | 24 ++++++++ 11 files changed, 160 insertions(+), 70 deletions(-)create mode 100644
arch/x86/boot/compressed/tdx-shared.ccreate mode 100644arch/x86/coco/tdx/tdx-shared.ccreate mode 100644arch/x86/include/asm/unaccepted_memory.harch/x86/Kconfig +2
-0@@ -884,9 +884,11 @@ config INTEL_TDX_GUEST bool "Intel TDX (Trust Domain Extensions) - Guest Support" depends on X86_64 && CPU_SUP_INTEL depends on X86_X2APIC + depends on EFI_STUB select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE + select UNACCEPTED_MEMORY help Support running as a guest under Intel TDX. Without this support, the guest kernel can not boot or run under TDX.arch/x86/boot/compressed/Makefile +1
-1@@ -106,7 +106,7 @@ ifdef CONFIG_X86_64 endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o -vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o $(obj)/tdx-shared.o vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.oarch/x86/boot/compressed/error.c +19
-0@@ -22,3 +22,22 @@ void error(char *m)while (1 ) asm("hlt" ); } + +/* EFI libstub provides vsnprintf() */ +#ifdef CONFIG_EFI_STUB +void panic (const char *fmt, ...) +{ +static char buf[1024 ]; +va_list args; +int len; + +va_start (args, fmt); + len =vsnprintf (buf,sizeof (buf), fmt, args); +va_end (args); + +if (len && buf[len -1 ] =='\n' ) + buf[len -1 ] ='\0' ; + +error (buf); +} +#endif arch/x86/boot/compressed/error.h +1
-0@@ -6,5 +6,6 @@void warn (char *m);void error (char *m)__noreturn ; +void panic (const char *fmt, ...)__noreturn __cold ;#endif /* BOOT_COMPRESSED_ERROR_H */ arch/x86/boot/compressed/mem.c +28
-0@@ -2,11 +2,44 @@#include "error.h" #include "misc.h" +#include "tdx.h" +#include <asm/shared/tdx.h> + +/* +* accept_memory() and process_unaccepted_memory() called from EFI stub which +* runs before decompresser and its early_tdx_detect(). +* +* Enumerate TDX directly from the early users. +*/ +static bool early_is_tdx_guest (void ) +{ +static bool once; +static bool is_tdx; + +if (!IS_ENABLED (CONFIG_INTEL_TDX_GUEST )) +return false; + +if (!once) { +u32 eax, sig[3 ]; + +cpuid_count (TDX_CPUID_LEAF_ID ,0 , &eax, + &sig[0 ], &sig[2 ], &sig[1 ]); + is_tdx = !memcmp (TDX_IDENT , sig,sizeof (sig)); + once = true; + } + +return is_tdx; +}
arch/x86/boot/compressed/mem.c line 42
Quoted 10 lines
arch/x86/boot/compressed/mem.c +6
-1void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ -error ("Cannot accept memory" ); +if (early_is_tdx_guest ()) { +if (tdx_accept_memory (start, end)) +return ; + } + +error ("Cannot accept memory: unknown platform\n" );
So this is a change in this version. If tdx_accept_memory() fails, you'll report unknown platform. Wouldn't it be better to have an error message that indicates a failure in the accept path?
Thanks,
Tom
Quoted 227 lines
arch/x86/boot/compressed/mem.c +0
-0}void init_unaccepted_memory (void )arch/x86/boot/compressed/tdx-shared.c new file +2
-0@@ -0,0 +1,2 @@ +#include "error.h" +#include "../../coco/tdx/tdx-shared.c" arch/x86/coco/tdx/Makefile +1
-1@@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += tdx.o tdcall.o +obj-y += tdx.o tdx-shared.o tdcall.oarch/x86/coco/tdx/tdx-shared.c new file +71
-0@@ -0,0 +1,71 @@ +#include <asm/tdx.h> +#include <asm/pgtable.h> + +static unsigned long try_accept_one (phys_addr_t start,unsigned long len, +enum pg_level pg_level) +{ +unsigned long accept_size =page_level_size (pg_level); +u64 tdcall_rcx; +u8 page_size; + +if (!IS_ALIGNED (start, accept_size)) +return 0 ; + +if (len < accept_size) +return 0 ; + +/* +* Pass the page physical address to the TDX module to accept the +* pending, private page. +* +* Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G. +*/ +switch (pg_level) { +case PG_LEVEL_4K : + page_size =0 ; +break ; +case PG_LEVEL_2M : + page_size =1 ; +break ; +case PG_LEVEL_1G : + page_size =2 ; +break ; +default : +return 0 ; + } + + tdcall_rcx = start | page_size; +if (__tdx_module_call (TDX_ACCEPT_PAGE , tdcall_rcx,0 ,0 ,0 ,NULL )) +return 0 ; + +return accept_size; +} + +bool tdx_accept_memory (phys_addr_t start,phys_addr_t end) +{ +/* +* For shared->private conversion, accept the page using +* TDX_ACCEPT_PAGE TDX module call. +*/ +while (start < end) { +unsigned long len = end - start; +unsigned long accept_size; + +/* +* Try larger accepts first. It gives chance to VMM to keep +* 1G/2M Secure EPT entries where possible and speeds up +* process by cutting number of hypercalls (if successful). +*/ + + accept_size =try_accept_one (start, len,PG_LEVEL_1G ); +if (!accept_size) + accept_size =try_accept_one (start, len,PG_LEVEL_2M ); +if (!accept_size) + accept_size =try_accept_one (start, len,PG_LEVEL_4K ); +if (!accept_size) +return false; + start += accept_size; + } + +return true; +}arch/x86/coco/tdx/tdx.c +3
-67@@ -713,46 +713,6 @@ static bool tdx_cache_flush_required(void)return true; } -static unsigned long try_accept_one (phys_addr_t start,unsigned long len, -enum pg_level pg_level) -{ -unsigned long accept_size =page_level_size (pg_level); -u64 tdcall_rcx; -u8 page_size; - -if (!IS_ALIGNED (start, accept_size)) -return 0 ; - -if (len < accept_size) -return 0 ; - -/* -* Pass the page physical address to the TDX module to accept the -* pending, private page. -* -* Bits 2:0 of RCX encode page size: 0 - 4K, 1 - 2M, 2 - 1G. -*/ -switch (pg_level) { -case PG_LEVEL_4K : - page_size =0 ; -break ; -case PG_LEVEL_2M : - page_size =1 ; -break ; -case PG_LEVEL_1G : - page_size =2 ; -break ; -default : -return 0 ; - } - - tdcall_rcx = start | page_size; -if (__tdx_module_call (TDX_ACCEPT_PAGE , tdcall_rcx,0 ,0 ,0 ,NULL )) -return 0 ; - -return accept_size; -} -/* * Inform the VMM of the guest's intent for this physical page: shared with * the VMM or private to the guest. The VMM is expected to change its mapping @@ -777,33 +737,9 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)if (_tdx_hypercall (TDVMCALL_MAP_GPA , start, end - start,0 ,0 ))return false; -/* private->shared conversion requires only MapGPA call */ -if (!enc) -return true; - -/* -* For shared->private conversion, accept the page using -* TDX_ACCEPT_PAGE TDX module call. -*/ -while (start < end) { -unsigned long len = end - start; -unsigned long accept_size; - -/* -* Try larger accepts first. It gives chance to VMM to keep -* 1G/2M Secure EPT entries where possible and speeds up -* process by cutting number of hypercalls (if successful). -*/ - - accept_size =try_accept_one (start, len,PG_LEVEL_1G ); -if (!accept_size) - accept_size =try_accept_one (start, len,PG_LEVEL_2M ); -if (!accept_size) - accept_size =try_accept_one (start, len,PG_LEVEL_4K ); -if (!accept_size) -return false; - start += accept_size; - } +/* shared->private conversion requires memory to be accepted before use */ +if (enc) +return tdx_accept_memory (start, end);return true; }arch/x86/include/asm/shared/tdx.h +2
-0@@ -91,5 +91,7 @@ struct tdx_module_output {u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9,struct tdx_module_output *out); +bool tdx_accept_memory (phys_addr_t start,phys_addr_t end); +#endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_SHARED_TDX_H */ arch/x86/include/asm/unaccepted_memory.h new file +24
-0@@ -0,0 +1,24 @@ +#ifndef _ASM_X86_UNACCEPTED_MEMORY_H +#define _ASM_X86_UNACCEPTED_MEMORY_H + +#include <linux/efi.h> +#include <asm/tdx.h> + +static inline void arch_accept_memory (phys_addr_t start,phys_addr_t end) +{ +/* Platform-specific memory-acceptance call goes here */ +if (cpu_feature_enabled (X86_FEATURE_TDX_GUEST )) { +if (tdx_accept_memory (start, end)) +return ; + } + +panic ("Cannot accept memory: unknown platform\n" ); +} + +static inline struct efi_unaccepted_memory *efi_get_unaccepted_table (void ) +{ +if (efi.unaccepted ==EFI_INVALID_TABLE_ADDR ) +return NULL ; +return __va (efi.unaccepted); +} +#endif
Re: [PATCHv13 9/9] x86/tdx: Add unaccepted memory support
Quoted 70 lines · Tom Lendacky
Quoted 69 lines · Kirill A. Shutemov
Hookup TDX-specific code to accept memory.
Accepting the memory is done with
ACCEPT_PAGEmodule call on every page in the range.MAP_GPAhypercall is not required as the unaccepted memory is considered private already.Extract the part of
tdx_enc_status_changed()that does memory acceptance in a new helper. Move the helpertdx-shared.c. It is going to be used by both main kernel and decompressor.Signed-off-by Kirill A. Shutemov <[email protected]>---
arch/x86/Kconfig | 2 + arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/error.c | 19 +++++++ arch/x86/boot/compressed/error.h | 1 + arch/x86/boot/compressed/mem.c | 35 +++++++++++- arch/x86/boot/compressed/tdx-shared.c | 2 + arch/x86/coco/tdx/Makefile | 2 +- arch/x86/coco/tdx/tdx-shared.c | 71 ++++++++++++++++++++++++ arch/x86/coco/tdx/tdx.c | 70 +---------------------- arch/x86/include/asm/shared/tdx.h | 2 +arch/x86/include/asm/unaccepted_memory.h| 24 ++++++++11 files changed, 160 insertions(+), 70 deletions(-)create mode 100644
arch/x86/boot/compressed/tdx-shared.ccreate mode 100644arch/x86/coco/tdx/tdx-shared.ccreate mode 100644arch/x86/include/asm/unaccepted_memory.harch/x86/boot/compressed/mem.c +5
-0@@ -2,11 +2,44 @@ #include "error.h" #include "misc.h" +#include "tdx.h" +#include <asm/shared/tdx.h> + +/* +* accept_memory() and process_unaccepted_memory() called from EFI stub which
+ * runs before decompresser and itsearly_tdx_detect().
+ *
+ * Enumerate TDX directly from the early users.
+ */arch/x86/boot/compressed/mem.c +19
-0+static bool early_is_tdx_guest (void ) +{ + static bool once; + static bool is_tdx; + + if (!IS_ENABLED(CONFIG_INTEL_TDX_GUEST)) + return false; + + if (!once) { + u32 eax, sig[3]; + + cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, + &sig[0], &sig[2], &sig[1]); + is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig)); + once = true; + } + + return is_tdx; +}void
arch_accept_memory(phys_addr_t start, phys_addr_t end){
/* Platform-specific memory-acceptance call goes here */ -error("Cannot accept memory");
Quoted 9 lines
Quoted 6 lines
+ if (
early_is_tdx_guest()) {
+ if (tdx_accept_memory(start, end))
+ return;
+ }
+
+error("Cannot accept memory: unknown platform\n");So this is a change in this version. If
tdx_accept_memory()fails, you'll report unknown platform. Wouldn't it be better to have an error message that indicates a failure in the accept path?
Maybe you can keep it similar to the v12 version with just a new error message, something like:
if (early_is_tdx_guest()) {
if (!tdx_accept_memory(start, end))
error("TDX error accepting memory\n");
} else {
error("Cannot accept memory: unknown platform\n");
}And similar in arch/x86/include/asm/unaccepted_memory.h.
Thanks,
Tom
Quoted 5 lines
Thanks,
TomQuoted 2 lines
}
voidinit_unaccepted_memory(void)
Re: [PATCHv13 9/9] x86/tdx: Add unaccepted memory support
Quoted 8 lines · Tom Lendacky
Quoted 4 lines
So this is a change in this version. If
tdx_accept_memory()fails, you'll report unknown platform. Wouldn't it be better to have an error message that indicates a failure in the accept path?Maybe you can keep it similar to the v12 version with just a new error message, something like:
Quoted 6 lines
if (
early_is_tdx_guest()) {
if (!tdx_accept_memory(start, end))error("TDX error accepting memory\n"); } else {
error("Cannot accept memory: unknown platform\n"); }
In the end, these errors aren't plumbed out to the page allocator. They *need* to succeed or we are dead anyway. Should we just send a fatal error up to the TDX module when we fail to accept memory? It's _slightly_ less opaque than plowing into an unaccepted page.
Re: [PATCHv13 9/9] x86/tdx: Add unaccepted memory support
Quoted 16 lines · Dave Hansen
Quoted 8 lines · Tom Lendacky
Quoted 4 lines
So this is a change in this version. If
tdx_accept_memory()fails, you'll report unknown platform. Wouldn't it be better to have an error message that indicates a failure in the accept path?Maybe you can keep it similar to the v12 version with just a new error message, something like:
Quoted 6 lines
if (
early_is_tdx_guest()) {
if (!tdx_accept_memory(start, end))error("TDX error accepting memory\n"); } else {
error("Cannot accept memory: unknown platform\n"); }
Quoted 4 lines
In the end, these errors aren't plumbed out to the page allocator. They *need* to succeed or we are dead anyway. Should we just send a fatal error up to the TDX module when we fail to accept memory? It's _slightly_ less opaque than plowing into an unaccepted page.
This is decompressor's error()s which are fatal.
arch_accept_memory() in the main kernel uses panic() in the same spot.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCHv13 9/9] x86/tdx: Add unaccepted memory support
In reply to Tom LendackyQuoted 133 lines · Tom Lendacky
Quoted 132 lines · Kirill A. Shutemov
Hookup TDX-specific code to accept memory.
Accepting the memory is done with
ACCEPT_PAGEmodule call on every page in the range.MAP_GPAhypercall is not required as the unaccepted memory is considered private already.Extract the part of
tdx_enc_status_changed()that does memory acceptance in a new helper. Move the helpertdx-shared.c. It is going to be used by both main kernel and decompressor.Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/Kconfig | 2 + arch/x86/boot/compressed/Makefile | 2 +- arch/x86/boot/compressed/error.c | 19 +++++++ arch/x86/boot/compressed/error.h | 1 + arch/x86/boot/compressed/mem.c | 35 +++++++++++- arch/x86/boot/compressed/tdx-shared.c | 2 + arch/x86/coco/tdx/Makefile | 2 +- arch/x86/coco/tdx/tdx-shared.c | 71 ++++++++++++++++++++++++ arch/x86/coco/tdx/tdx.c | 70 +---------------------- arch/x86/include/asm/shared/tdx.h | 2 + arch/x86/include/asm/unaccepted_memory.h | 24 ++++++++ 11 files changed, 160 insertions(+), 70 deletions(-)create mode 100644
arch/x86/boot/compressed/tdx-shared.ccreate mode 100644arch/x86/coco/tdx/tdx-shared.ccreate mode 100644arch/x86/include/asm/unaccepted_memory.harch/x86/Kconfig +2
-0@@ -884,9 +884,11 @@ config INTEL_TDX_GUEST bool "Intel TDX (Trust Domain Extensions) - Guest Support" depends on X86_64 && CPU_SUP_INTEL depends on X86_X2APIC + depends on EFI_STUB select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE + select UNACCEPTED_MEMORY help Support running as a guest under Intel TDX. Without this support, the guest kernel can not boot or run under TDX.arch/x86/boot/compressed/Makefile +1
-1@@ -106,7 +106,7 @@ ifdef CONFIG_X86_64 endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o -vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o +vmlinux-objs-$(CONFIG_INTEL_TDX_GUEST) += $(obj)/tdx.o $(obj)/tdcall.o $(obj)/tdx-shared.o vmlinux-objs-$(CONFIG_UNACCEPTED_MEMORY) += $(obj)/mem.o vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.oarch/x86/boot/compressed/error.c +19
-0@@ -22,3 +22,22 @@ void error(char *m)while (1 ) asm("hlt" ); } + +/* EFI libstub provides vsnprintf() */ +#ifdef CONFIG_EFI_STUB +void panic (const char *fmt, ...) +{ +static char buf[1024 ]; +va_list args; +int len; + +va_start (args, fmt); + len =vsnprintf (buf,sizeof (buf), fmt, args); +va_end (args); + +if (len && buf[len -1 ] =='\n' ) + buf[len -1 ] ='\0' ; + +error (buf); +} +#endif arch/x86/boot/compressed/error.h +1
-0@@ -6,5 +6,6 @@void warn (char *m);void error (char *m)__noreturn ; +void panic (const char *fmt, ...)__noreturn __cold ;#endif /* BOOT_COMPRESSED_ERROR_H */ arch/x86/boot/compressed/mem.c +30
-1@@ -2,11 +2,44 @@#include "error.h" #include "misc.h" +#include "tdx.h" +#include <asm/shared/tdx.h> + +/* +* accept_memory() and process_unaccepted_memory() called from EFI stub which +* runs before decompresser and its early_tdx_detect(). +* +* Enumerate TDX directly from the early users. +*/ +static bool early_is_tdx_guest (void ) +{ +static bool once; +static bool is_tdx; + +if (!IS_ENABLED (CONFIG_INTEL_TDX_GUEST )) +return false; + +if (!once) { +u32 eax, sig[3 ]; + +cpuid_count (TDX_CPUID_LEAF_ID ,0 , &eax, + &sig[0 ], &sig[2 ], &sig[1 ]); + is_tdx = !memcmp (TDX_IDENT , sig,sizeof (sig)); + once = true; + } + +return is_tdx; +}void arch_accept_memory (phys_addr_t start,phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ -error ("Cannot accept memory" ); +if (early_is_tdx_guest ()) { +if (tdx_accept_memory (start, end))
Quoted 8 lines
arch/x86/boot/compressed/mem.c line 40
Quoted 4 lines
arch/x86/boot/compressed/mem.c +4
-0+return ; + } + +error ("Cannot accept memory: unknown platform\n" );So this is a change in this version. If
tdx_accept_memory()fails, you'll report unknown platform. Wouldn't it be better to have an error message that indicates a failure in the accept path?
Urgh.. Didn't read the error message on the rework.
arch/x86/boot/compressed/mem.c +4 -4
@@ -35,11 +35,11 @@ void arch_accept_memory(phys_addr_t start, phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ if (early_is_tdx_guest ()) { -if (tdx_accept_memory (start, end)) -return ; +if (!tdx_accept_memory (start, end)) +panic ("TDX: Failed to accept memory\n" ); + }else { +error ("Cannot accept memory: unknown platform\n" ); } - -error ("Cannot accept memory: unknown platform\n" ); }void init_unaccepted_memory (void )
arch/x86/include/asm/unaccepted_memory.h +4 -4
@@ -8,11 +8,11 @@ static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) {/* Platform-specific memory-acceptance call goes here */ if (cpu_feature_enabled (X86_FEATURE_TDX_GUEST )) { -if (tdx_accept_memory (start, end)) -return ; +if (!tdx_accept_memory (start, end)) +panic ("TDX: Failed to accept memory\n" ); + }else { +panic ("Cannot accept memory: unknown platform\n" ); } - -panic ("Cannot accept memory: unknown platform\n" ); }static inline struct efi_unaccepted_memory *efi_get_unaccepted_table (void )
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov