From: Sebastian Mayr <me@smayr.name>
To: Ard Biesheuvel <ardb@kernel.org>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
x86@kernel.org, Kees Cook <kees@kernel.org>,
linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org,
Sebastian Mayr <me@smayr.name>
Subject: [PATCH] x86/efistub: Avoid placing the kernel in MTRR-uncached RAM
Date: Sat, 28 Nov 2026 18:16:50 -0500 [thread overview]
Message-ID: <20261128231650.20941-1-me@smayr.name> (raw)
On systems whose firmware leaves part of RAM uncovered by variable MTRRs
(with a UC default type), physical KASLR in the EFI stub can decompress
the kernel into that uncached range, causing a silent early-boot hang
when mtrr_trim_uncached_memory() later trims it out of the direct map.
For example, on a Dell Wyse 5070 with 32 GiB, 0x800000000-0x87fffffff
(the RAM remapped from under the PCI hole) is left uncovered (/proc/mtrr):
reg00: base=0x000000000 ( 0MB), size= 2048MB, count=1: write-back
reg01: base=0x07a000000 ( 1952MB), size= 32MB, count=1: uncachable
reg02: base=0x07c000000 ( 1984MB), size= 64MB, count=1: uncachable
reg03: base=0x100000000 ( 4096MB), size= 4096MB, count=1: write-back
reg04: base=0x200000000 ( 8192MB), size= 8192MB, count=1: write-back
reg05: base=0x400000000 (16384MB), size=16384MB, count=1: write-back
WARNING: BIOS bug: CPU MTRRs don't cover all of memory, losing 2048MB of RAM.
The EFI stub's physical KASLR considers every EFI_CONVENTIONAL_MEMORY
slot, so it sometimes decompresses the kernel into that range (about 6%
of boots on that machine). mtrr_trim_uncached_memory() then turns the
range the kernel is running from into E820_TYPE_RESERVED, so
init_mem_mapping() doesn't direct-map the kernel image, and the first
__va() of a kernel page after that faults. In practice that is
set_pte_vaddr() while early_acpi_boot_init() sets the APIC fixmap:
BUG: unable to handle page fault for address: ff4896d3e8e32ff8
RIP: 0010:fill_pud+0x6/0xc0
set_pte_vaddr_p4d+0x28/0x50
native_set_fixmap+0x46/0xa0
apic_set_fixmap+0x35/0x80
early_acpi_boot_init+0xbf/0x150
setup_arch+0x7c3/0xcc0
This happens before any console is up, so the machine just hangs.
Setting mem= allows working around this issue by implicitly disabling
physical KASLR. This ensures the kernel can never end up in an area
which is trimmed later and also silences the BIOS bug warning.
Apply the same test as mtrr_trim_uncached_memory() before randomizing:
if MTRRs are enabled, the default type is UC and the variable ranges are
WB/UC only, cap the allocation at the highest WB-covered address. If
nothing fits below that cap, fall back to the old limit, so no system
that booted before can fail to boot now.
Known limitations:
- AMD/Hygon are skipped, since TOP_MEM2 can make RAM WB without any
variable MTRR.
- Only the top of RAM is handled; holes, or a UC range carved out of
the top of a WB range, are not.
- This does not fix the same issue in the legacy boot path
(arch/x86/boot/compressed/kaslr.c).
Fixes: a1b87d54f4e4 ("x86/efistub: Avoid legacy decompressor when doing EFI boot")
Signed-off-by: Sebastian Mayr <me@smayr.name>
---
Not tagged for stable; happy to add Cc: stable if you think it qualifies.
Testing:
I started this off by creating a QEMU-based repro. No changes on QEMU/kernel,
just a small EFI app which sets up the UC-default MTRR layout and leaves the top
2 GiB of a 6 GiB guest uncovered, then chainloads bzImage. It's available at
https://gist.github.com/Sebmaster/fd494b4cf65eec9e1fd27dc419112e3f.
forced placement into the uncovered range: hangs every time
normal KASLR, unpatched: 4 of 12 boots hang
normal KASLR, patched: all boots OK
forced placement, patched: falls back to the old limit
(no WB RAM left free), so hangs
I also applied this patch to the Fedora kernel on the Dell Wyse 5070 and
ran 20 consecutive reboots without hangs (vs. ~6% hang rate unpatched).
Open questions:
- I'd love to keep this initial patch small, but this same issue looks to be
present in the boot/compressed x86 codepath as well. If you think this should
be done in a single series, I can give that a shot, but would love some
guidance on where to put shared helpers.
- I've tried to keep this codepath very defensive (Intel-only, TDX check), but
are there other environments which could cause any issues here?
This is my first kernel submission; would appreciate any feedback on the
approach and process.
Disclosure:
The repros and an initial draft of the patch were developed with Opus/Neon. I
refined the code, ran the repros myself and verified the fixes on real hardware.
drivers/firmware/efi/libstub/x86-stub.c | 144 ++++++++++++++++++++----
1 file changed, 123 insertions(+), 21 deletions(-)
diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c
index 253dd3ba8fab..705f75e7ffe6 100644
--- a/drivers/firmware/efi/libstub/x86-stub.c
+++ b/drivers/firmware/efi/libstub/x86-stub.c
@@ -16,7 +16,11 @@
#include <asm/setup.h>
#include <asm/desc.h>
#include <asm/boot.h>
+#include <asm/cpufeatures.h>
#include <asm/kaslr.h>
+#include <asm/msr-index.h>
+#include <asm/mtrr.h>
+#include <asm/shared/msr.h>
#include <asm/sev.h>
#include <asm/shared/tdx.h>
@@ -835,13 +839,117 @@ static efi_status_t parse_options(const char *cmdline)
return efi_parse_options(cmdline);
}
+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 = TDX_CPUID_LEAF_ID, sig[3] = {};
+
+ native_cpuid(&eax, &sig[0], &sig[2], &sig[1]);
+ is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig));
+ once = true;
+ }
+
+ return is_tdx;
+}
+
+/*
+ * If the variable MTRRs leave the top of RAM uncached (default type UC and
+ * no WB range covering it), mtrr_trim_uncached_memory() will later turn that
+ * memory into E820_TYPE_RESERVED. A kernel image placed there loses its
+ * direct mapping and dies in early boot, so keep the randomized placement
+ * below the highest address covered by a WB variable MTRR, applying the
+ * same preconditions as the trim code.
+ */
+static u64 efi_mtrr_wb_limit(void)
+{
+ u32 eax = 0, vendor[3] = {};
+ u64 highest = 0;
+ struct msr m;
+ int i, nvar;
+
+ if (!IS_ENABLED(CONFIG_MTRR))
+ return U64_MAX;
+
+ /*
+ * Only handle Intel: on AMD/Hygon, TOP_MEM2 may make RAM above 4 GiB
+ * WB regardless of the variable MTRRs, and that cannot be checked
+ * safely from here.
+ */
+ native_cpuid(&eax, &vendor[0], &vendor[2], &vendor[1]);
+ if (memcmp(vendor, "GenuineIntel", 12))
+ return U64_MAX;
+
+ if (!(native_cpuid_edx(1) & BIT(X86_FEATURE_MTRR & 31)))
+ return U64_MAX;
+
+ /*
+ * TDX guests read the MTRRs from the untrusted VMM (via #VE), and
+ * the kernel replaces them with WB and never trims, so skip them.
+ */
+ if (early_is_tdx_guest())
+ return U64_MAX;
+
+ raw_rdmsr(MSR_MTRRdefType, &m);
+ if (!(m.q & MTRR_DEF_TYPE_E) ||
+ (m.q & MTRR_DEF_TYPE_TYPE) != MTRR_TYPE_UNCACHABLE)
+ return U64_MAX;
+
+ raw_rdmsr(MSR_MTRRcap, &m);
+ nvar = m.q & MTRR_CAP_VCNT;
+
+ for (i = 0; i < nvar; i++) {
+ struct msr base, mask;
+ u64 addr_mask, size, top;
+
+ raw_rdmsr(MTRRphysMask_MSR(i), &mask);
+ if (!(mask.q & MTRR_PHYSMASK_V))
+ continue;
+ raw_rdmsr(MTRRphysBase_MSR(i), &base);
+
+ switch (base.q & MTRR_PHYSBASE_TYPE) {
+ case MTRR_TYPE_WRBACK:
+ /* size is the lowest set bit of the (contiguous) mask */
+ addr_mask = mask.q & PAGE_MASK;
+ size = addr_mask & -addr_mask;
+ /*
+ * Empty or non-contiguous mask: don't guess. A contiguous
+ * mask plus its lowest bit is a single bit (open-coded,
+ * as is_power_of_2() takes an unsigned long).
+ */
+ top = addr_mask + size;
+ if (!top || (top & (top - 1)))
+ return U64_MAX;
+ highest = max(highest, (base.q & addr_mask) + size);
+ break;
+ case MTRR_TYPE_UNCACHABLE:
+ break;
+ default:
+ /* the trim code only acts on WB/UC-only layouts */
+ return U64_MAX;
+ }
+ }
+
+ if (!highest)
+ return U64_MAX;
+
+ return highest - 1;
+}
+
static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry,
struct boot_params *boot_params)
{
unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
unsigned long addr, alloc_size, entry;
efi_status_t status;
+ unsigned long alloc_max = EFI_X86_KERNEL_ALLOC_LIMIT;
u32 seed[2] = {};
+ u64 wb_limit;
boot_params_ptr = boot_params;
@@ -878,10 +986,23 @@ static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry,
boot_params->hdr.loadflags |= KASLR_FLAG;
}
+ wb_limit = efi_mtrr_wb_limit();
+ if (wb_limit < alloc_max) {
+ efi_debug("Limiting kernel placement to the MTRR WB range below 0x%llx\n",
+ wb_limit + 1);
+ alloc_max = wb_limit;
+ }
+
status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
seed[0], EFI_LOADER_CODE,
- LOAD_PHYSICAL_ADDR,
- EFI_X86_KERNEL_ALLOC_LIMIT);
+ LOAD_PHYSICAL_ADDR, alloc_max);
+ if (status != EFI_SUCCESS && alloc_max != EFI_X86_KERNEL_ALLOC_LIMIT) {
+ efi_warn("No room below the MTRR WB limit - ignoring it\n");
+ status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
+ seed[0], EFI_LOADER_CODE,
+ LOAD_PHYSICAL_ADDR,
+ EFI_X86_KERNEL_ALLOC_LIMIT);
+ }
if (status != EFI_SUCCESS)
return status;
@@ -1082,25 +1203,6 @@ void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
* presence of TDX or SEV-SNP, or emit any kind of output if any error
* conditions are detected.
*/
-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 = TDX_CPUID_LEAF_ID, sig[3] = {};
-
- native_cpuid(&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)
{
if (early_is_tdx_guest()) {
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
reply other threads:[~2026-11-28 23:17 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=20261128231650.20941-1-me@smayr.name \
--to=me@smayr.name \
--cc=ardb@kernel.org \
--cc=ilias.apalodimas@linaro.org \
--cc=kees@kernel.org \
--cc=linux-efi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--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®