mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pankaj Gupta <pankaj.gupta@amd.com>
To: 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>
Cc: "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>,
	<linux-kernel@vger.kernel.org>, Mike Rapoport <rppt@kernel.org>,
	<linux-mm@kvack.org>, <linux-efi@vger.kernel.org>,
	<Pankaj.gupta.linux@gmail.com>,
	Pankaj Gupta <pankaj.gupta@amd.com>, <stable@vger.kernel.org>
Subject: [PATCH v2] x86/unaccepted_memory: Do not accept ranges that are no longer E820 RAM
Date: Fri, 13 Nov 2026 04:55:54 -0600	[thread overview]
Message-ID: <20261113105554.199912-1-pankaj.gupta@amd.com> (raw)

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.

Fixes: 2053bc57f367 ("efi: Add unaccepted memory support")
Cc: stable@vger.kernel.org
Signed-off-by: Pankaj Gupta <pankaj.gupta@amd.com>
---
v1 -> v2:
- Clear the bitmap bits only for the accepted memory
- Round E820 RAM range to page boundaries

v1: https://lore.kernel.org/all/20261102113127.161823-1-pankaj.gupta@amd.com/

 arch/x86/include/asm/unaccepted_memory.h | 44 +++++++++++++++++++++++-
 drivers/firmware/efi/unaccepted_memory.c |  4 +--
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/unaccepted_memory.h b/arch/x86/include/asm/unaccepted_memory.h
index f5937e9866ac..9e27a9953dd8 100644
--- a/arch/x86/include/asm/unaccepted_memory.h
+++ b/arch/x86/include/asm/unaccepted_memory.h
@@ -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)
diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c
index 4a8ec8d6a571..b8a0c0d1d289 100644
--- a/drivers/firmware/efi/unaccepted_memory.c
+++ b/drivers/firmware/efi/unaccepted_memory.c
@@ -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);
-- 
2.34.1


                 reply	other threads:[~2026-11-13 11:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261113105554.199912-1-pankaj.gupta@amd.com \
    --to=pankaj.gupta@amd.com \
    --cc=Pankaj.gupta.linux@gmail.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=ilias.apalodimas@linaro.org \
    --cc=kas@kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=rppt@kernel.org \
    --cc=stable@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®