All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pankaj Gupta" <pankaj.gupta@amd.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	rdunlap@infradead.org
Cc: "Arnd Bergmann" <arnd@arndb.de>,
	"Dapeng Mi" <dapeng1.mi@linux.intel.com>,
	"Marco Elver" <elver@google.com>,
	"H . Peter Anvin" <hpa@zytor.com>,
	"Tom Lendacky" <thomas.lendacky@amd.com>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Kiryl Shutsemau" <kas@kernel.org>,
	"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	linux-mm@kvack.org, linux-efi@vger.kernel.org,
	Pankaj.gupta.linux@gmail.com,
	"Ethan Nelson-Moore" <enelsonmoore@gmail.com>,
	"Karl Mehltretter" <kmehltretter@gmail.com>
Subject: Re: [PATCH v2] x86/unaccepted_memory: Do not accept ranges that are no longer E820 RAM
Date: Fri, 04 Dec 2026 11:35:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-249694@kernel.org> (raw)
In-Reply-To: <20261203113224.250498-1-pankaj.gupta@amd.com>

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 <pankaj.gupta@amd.com>

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.

> diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/asm/unaccepted_memory.h
> index f5937e9866ac1..9e27a9953dd83 100644
> --- a/arch/x86/include/asm/unaccepted_memory.h
> +++ b/arch/x86/include/asm/unaccepted_memory.h

[ ... ]

> @@ -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;
> +}

[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.

> +
> +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                                CPU1
kernel_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-free

e820__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.

> +
> +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);
> +	}
> +}

[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.

[ ... ]

> diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c
> index 4a8ec8d6a571e..b8a0c0d1d2891 100644
> --- a/drivers/firmware/efi/unaccepted_memory.c
> +++ b/drivers/firmware/efi/unaccepted_memory.c

[ ... ]

> @@ -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/bd56cfa0-6fdc-4e83-b571-f2abf647afa2@app.fastmail.com

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203113224.250498-1-pankaj.gupta@amd.com?part=1

  parent reply	other threads:[~2026-12-04 11:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:32 [PATCH v2] x86/unaccepted_memory: Do not accept ranges that are no longer E820 RAM Pankaj Gupta
2026-12-03 11:35 ` Gupta, Pankaj
2026-12-04 11:35 ` sashiko-bot [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-11-13 10:55 Pankaj Gupta
2026-11-13 12:55 ` Kiryl Shutsemau
2026-11-13 14:10   ` Gupta, Pankaj
2026-11-14 11:33 ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-249694@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Pankaj.gupta.linux@gmail.com \
    --cc=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=elver@google.com \
    --cc=enelsonmoore@gmail.com \
    --cc=hpa@zytor.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=kas@kernel.org \
    --cc=kmehltretter@gmail.com \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=pankaj.gupta@amd.com \
    --cc=rdunlap@infradead.org \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.