x86/unaccepted_memory: Do not accept ranges that are no longer E820 RAM
Subsystems (5)
PATCH v2 1/1 x86/unaccepted_memory: Do not accept ranges that are no longer E820 RAM
The EFI stub builds the unaccepted memory bitmap before the kernel parses its command line. Option memmap=<size>$<addr> removes RAM from the E820 map while its bits are still set in the bitmap.
The removed range, such as a memmap=...$... hole, is then still treated as unaccepted RAM and accept_memory() tries to accept it. The hole is not mapped, so on SEV-SNP the PVALIDATE faults and the guest crashes early in the boot:
BUG: unable to handle page fault for address: ffff8a14bfe00000 RIP: 0010:pvalidate_pages+0xc0/0x250 set_pages_state+0x41a/0x440 snp_accept_memory+0x3e/0x50 accept_memory+0x186/0x260 __free_pages_core+0xd2/0x240 memblock_free_pages+0x1b/0x30 memblock_free_all+0x275/0x2d0
This was seen with memmap=2M$0x1ffe00000, a 2 MiB hole in RAM, on a 19 GiB SNP guest with lazy acceptance.
Accept only whole pages of E820 RAM, and clear the bitmap bits only for the units in which memory was accepted.
---
v1 -> v2:
- Clear the bitmap bits only for the accepted memory
- Round E820 RAM range to page boundaries
v1: https://lore.kernel.org/all/[email protected]/
arch/x86/include/asm/unaccepted_memory.h | 44 +++++++++++++++++++++++- drivers/firmware/efi/unaccepted_memory.c | 4 +-- 2 files changed, 45 insertions(+), 3 deletions(-)
arch/x86/include/asm/unaccepted_memory.h +43 -1
@@ -2,10 +2,11 @@#define _ASM_X86_UNACCEPTED_MEMORY_H#include <linux/efi.h> +#include <asm/e820/api.h> #include <asm/tdx.h> #include <asm/sev.h> -static inline void arch_accept_memory (phys_addr_t start,phys_addr_t end) +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 )) {
@@ -18,6 +19,47 @@ static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) } } +static inline bool e820_ram_pages (struct e820_entry *entry,phys_addr_t start, +phys_addr_t end,phys_addr_t *s,phys_addr_t *e) +{ +if (entry->type !=E820_TYPE_RAM ) +return false; + + *s =PAGE_ALIGN (max_t (phys_addr_t, start, entry->addr)); + *e =PAGE_ALIGN_DOWN (min_t (phys_addr_t, end, entry->addr + entry->size)); +return *s < *e; +} + +static inline void arch_accept_memory (phys_addr_t start,phys_addr_t end) +{ +phys_addr_t s, e; +u32 i; + +for (i =0 ; i < e820_table->nr_entries; i++) { +if (e820_ram_pages (&e820_table->entries[i], start, end, &s, &e)) +__arch_accept_memory (s, e); + } +} + +static inline void arch_clear_accepted (unsigned long *bitmap,phys_addr_t base, +unsigned long unit_size, +phys_addr_t start,phys_addr_t end) +{ +phys_addr_t s, e; +u32 i; + +for (i =0 ; i < e820_table->nr_entries; i++) { +unsigned long first, last; + +if (!e820_ram_pages (&e820_table->entries[i], start, end, &s, &e)) +continue ; + + first = (s - base) / unit_size; + last =DIV_ROUND_UP (e - base, unit_size); +bitmap_clear (bitmap, first, last - first); + } +} +static inline struct efi_unaccepted_memory *efi_get_unaccepted_table (void ) {if (efi.unaccepted ==EFI_INVALID_TABLE_ADDR )
drivers/firmware/efi/unaccepted_memory.c +2 -2
@@ -130,7 +130,6 @@ void accept_memory(phys_addr_t start, unsigned long size)for_each_set_bitrange_from (range_start, range_end, unaccepted->bitmap, range.end) {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;
@@ -150,7 +149,8 @@ void accept_memory(phys_addr_t start, unsigned long size)arch_accept_memory (phys_start, phys_end);spin_lock (&unaccepted_memory_lock); -bitmap_clear (unaccepted->bitmap, range_start, len); +arch_clear_accepted (unaccepted->bitmap, unaccepted->phys_base, + unit_size, phys_start, phys_end); }list_del (&range.list);
Signature · 1 line
2.34.1
Please ignore this patch as sent by mistake.
Thanks,
Pankaj
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] x86/unaccepted_memory: arch_clear_accepted() clears partial units in bitmap - [High]…
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] x86/unaccepted_memory: arch_clear_accepted() clears partial units in bitmap
- [High] x86/unaccepted_memory: e820_table UAF race with free_initmem()
- [Medium] efi/unaccepted: set hole bits break range_contains_unaccepted_memory()
- [Medium] x86/unaccepted_memory: arch_accept_memory() skips hot-added non-E820-RAM
- [Medium] x86/unaccepted_memory: 64-bit PAGE_ALIGN() wraparound in e820_ram_pages()
- [Medium] efi: unaddressed maintainer objection to E820 filtering in accept_memory()
--
commit 9fd60bf8e1c09dbddfc18def62b612b4e1e680e2
Author: Pankaj Gupta <[email protected]>
x86/unaccepted_memory: Do not accept ranges that are no longer E820 RAM
This commit filters x86 memory acceptance in arch_accept_memory() to whole pages covered by E820_TYPE_RAM entries and adds arch_clear_accepted() so accept_memory() only clears bitmap bits for those E820 RAM subranges.
Quoted 4 lines
arch/x86/include/asm/unaccepted_memory.h +0
-0
[ ... ]
Quoted 4 lines
arch/x86/include/asm/unaccepted_memory.h +0
-0@@ -18,6 +19,47 @@ static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) } }
arch/x86/include/asm/unaccepted_memory.h static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) · line 31
Quoted 10 lines
arch/x86/include/asm/unaccepted_memory.h +10
-0+static inline bool e820_ram_pages (struct e820_entry *entry,phys_addr_t start, +phys_addr_t end,phys_addr_t *s,phys_addr_t *e) +{ +if (entry->type !=E820_TYPE_RAM ) +return false; + + *s =PAGE_ALIGN (max_t (phys_addr_t, start, entry->addr)); + *e =PAGE_ALIGN_DOWN (min_t (phys_addr_t, end, entry->addr + entry->size)); +return *s < *e; +}
[Severity: Medium]
Can PAGE_ALIGN() wrap *s to zero for a non-overlapping E820_TYPE_RAM entry near U64_MAX?
An entry such as memmap=0x7ff@0xfffffffffffff800 passes e820__update_table() because addr + size does not overflow. Calling PAGE_ALIGN() before checking overlap wraps *s to 0 while *e equals end, so *s < *e returns true.
arch_accept_memory() then passes [0, end) to __arch_accept_memory(), panicking on TDX or faulting on SEV-SNP, and arch_clear_accepted() underflows (s - base) and (last - first) when base is non-zero, writing out of bounds in bitmap_clear(). Checking overlap before calling PAGE_ALIGN() avoids this.
arch/x86/include/asm/unaccepted_memory.h static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) · line 42
Quoted 11 lines
arch/x86/include/asm/unaccepted_memory.h +11
-0+ +static inline void arch_accept_memory (phys_addr_t start,phys_addr_t end) +{ +phys_addr_t s, e; +u32 i; + +for (i =0 ; i < e820_table->nr_entries; i++) { +if (e820_ram_pages (&e820_table->entries[i], start, end, &s, &e)) +__arch_accept_memory (s, e); + } +}
[Severity: High]
Can arch_accept_memory() and arch_clear_accepted() race with free_initmem() freeing e820_table_init?
During SMP boot, lazy memory acceptance can run accept_memory() on one CPU while kernel_init() runs free_initmem() on another:
CPU0 CPU1kernel_init() accept_memory()
free_initmem() arch_accept_memory()
/* reads e820_table_init */
__arch_accept_memory(...)
e820__reallocate_tables()
e820_table = kmemdup(...)
free_kernel_image_pages(...)
/* poisons/unmaps initmem */
e820_table_init->nr_entries
<-- use-after-freee820__reallocate_tables() swaps e820_table with a plain store and no grace period before free_kernel_image_pages() poisons or unmaps e820_table_init. On AMD systems with X86_FEATURE_INVLPGB, flush_tlb_all() broadcasts hardware invalidations rather than IPIs, so it does not wait for remote IRQ-disabled CPUs executing arch_accept_memory().
A concurrent reader can then observe POISON_FREE_INITMEM (0xcccccccc) as nr_entries or fault on an unmapped init page with interrupts disabled.
[Severity: Medium]
Does filtering on e820_table cause arch_accept_memory() to silently skip hot-added memory?
e820_table is a static boot-time snapshot that arch_add_memory() never updates. If an EFI_UNACCEPTED_MEMORY range is reclassified to E820_TYPE_SOFT_RESERVED via memmap= and later hot-added by dax/kmem (dax_kmem_do_hotplug() -> arch_add_memory() -> online_pages() -> __free_pages_core() -> accept_memory()), e820_ram_pages() rejects it.
__free_pages_core() and __accept_page() then expose the unaccepted pages to the buddy allocator via __free_pages_ok(), causing an unrecoverable EPT violation on TDX or RMP fault on SEV-SNP on first access. Checking direct-map coverage (such as pfn_mapped[] / pfn_range_is_mapped()) instead of boot-time e820_table would cover hot-added memory while still avoiding unmapped holes.
Quoted 9 lines
arch/x86/include/asm/unaccepted_memory.h +9
-0+ +static inline void arch_clear_accepted (unsigned long *bitmap,phys_addr_t base, +unsigned long unit_size, +phys_addr_t start,phys_addr_t end) +{ +phys_addr_t s, e; +u32 i; + +for (i =0 ; i < e820_table->nr_entries; i++) {
arch/x86/include/asm/unaccepted_memory.h static inline void arch_accept_memory(phys_addr_t start, phys_addr_t end) · line 61
Quoted 10 lines
arch/x86/include/asm/unaccepted_memory.h +10
-0+unsigned long first, last; + +if (!e820_ram_pages (&e820_table->entries[i], start, end, &s, &e)) +continue ; + + first = (s - base) / unit_size; + last =DIV_ROUND_UP (e - base, unit_size); +bitmap_clear (bitmap, first, last - first); + } +}
[Severity: High]
Does rounding first down and last up corrupt the persistent unaccepted bitmap when a hole is smaller than or unaligned to unit_size (2 MiB)?
Each bitmap bit tracks a full 2 MiB unit in EFI_ACPI_RECLAIM_MEMORY, which persists across kexec. With a sub-unit hole such as memmap=1M$0x1ffe00000, accepting any E820_TYPE_RAM page in that 2 MiB unit clears the whole unit's bit even though the hole pages remain unaccepted in hardware.
Because e820_table_kexec is snapshotted before parse_early_param() applies memmap=, a kexec'd kernel receives the hole as E820_TYPE_RAM with its bitmap bit already cleared and crashes when accessing it. Additionally, sanity_check_segment_list() in kernel/kexec_core.c calls accept_memory() on kexec segments chosen from e820_table_kexec, which arch_accept_memory() now silently skips if they lie in a memmap=-excluded range.
[Severity: Medium]
What happens in range_contains_unaccepted_memory() when a full unit_size hole leaves its bitmap bit permanently set?
For a 2 MiB-aligned hole such as memmap=2M$0x1ffe00000, arch_clear_accepted() never clears the hole unit's bit. However, range_contains_unaccepted_memory() still extends any unit_size-aligned end by + unit_size for the load_unaligned_zeropad() guard page without consulting e820_table.
As a result, pfn_is_unaccepted_memory() always returns true for the last 4 KiB page of live, accepted RAM immediately preceding the hole, causing unaccepted_memory_vmcore_pfn_is_ram() to omit that page from /proc/vmcore and /proc/kcore to zero it out.
[ ... ]
Quoted 4 lines
drivers/firmware/efi/unaccepted_memory.c +0
-0
[ ... ]
drivers/firmware/efi/unaccepted_memory.c void accept_memory(phys_addr_t start, unsigned long size) · line 154
Quoted 8 lines
drivers/firmware/efi/unaccepted_memory.c +2
-1@@ -150,7 +149,8 @@ void accept_memory(phys_addr_t start, unsigned long size)arch_accept_memory (phys_start, phys_end);spin_lock (&unaccepted_memory_lock); -bitmap_clear (unaccepted->bitmap, range_start, len); +arch_clear_accepted (unaccepted->bitmap, unaccepted->phys_base, + unit_size, phys_start, phys_end); }
[Severity: Medium]
As reported by Ard Biesheuvel on v1 [1], should memmap= instead be disallowed in confidential VMs so the firmware and OS views of memory remain in sync?
Rather than addressing that objection, this version extends the E820-filtering workaround into generic EFI code via arch_clear_accepted(), exposing the mismatch between page-granular memmap= holes and the 2 MiB unit_size EFI bitmap.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1