Linux Power Management development
 help / color / mirror / Atom feed
* [PATCH] x86/efi: Map firmware runtime regions before boot services regions
@ 2026-12-03 15:30 Alvaro Parker
  2026-12-05 12:07 ` dev12124
  0 siblings, 1 reply; 2+ messages in thread
From: Alvaro Parker @ 2026-12-03 15:30 UTC (permalink / raw)
  To: Ard Biesheuvel, Ilias Apalodimas, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin
  Cc: linux-pm, Rafael J . Wysocki, Chun-Yi Lee, Alvaro Parker,
	linux-efi, linux-kernel

efi_map_regions() gives every EFI region its virtual address in one walk
over the memory map. Regions are placed top-down from EFI_VA_START and
each keeps its offset within a 2M page, so the address of a region
depends on the size and position of every region mapped before it. The
walk covers the runtime regions and also the boot services regions,
which are mapped because some firmware touches them after
ExitBootServices().

Hibernation needs two boots to arrive at the same layout. The kernel
that loads the image calls SetVirtualAddressMap() with the layout it
computed, and the firmware relocates its runtime code and its runtime
services table to match. The restored kernel then goes on using the EFI
page tables it built when it booted. Nothing checks that the two layouts
agree.

Firmware keeps its runtime regions in place from one boot to the next,
but not its boot services regions. On a Lenovo ThinkPad P14s Gen 5
(21G3, BIOS R2DET41W 1.26) the firmware allocates less boot services
code when it wakes from S4:

                  normal boot               S4 wake
  BSCode    0x6f954000, 2987 pages    0x6f973000, 2956 pages
  BSCode    0x6f4fb000, 1106 pages    0x6f521000, 1068 pages
  BSData    0x68cbf000, 22592 pages   0x68cc6000, 22585 pages

That is enough to move the 88M boot services data region, and every
region mapped after it, up by one 2M step:

                  normal boot           S4 wake
  BSData    0xfffffffee94bf000    0xfffffffee96c6000
  RTCode    0xfffffffee8eff000    0xfffffffee90ff000
  RTData    0xfffffffee66ff000    0xfffffffee68ff000

After a resume, the function pointers in the runtime services table are
2M above the addresses at which the restored kernel mapped the runtime
code. The first runtime service call faults, and EFI runtime services
stay disabled until the next boot:

  [Firmware Bug]: Page fault caused by firmware at PA: 0xfffffffee9264460
  WARNING: arch/x86/platform/efi/quirks.c:797 at efi_crash_gracefully_on_page_fault+0x55/0xc0
  Workqueue: efi_runtime efi_call_rts
  efi: Froze efi_rts_wq and disabled EFI Runtime Services

Map the firmware's runtime regions first, and the boot services regions
after them in a second pass, so that the addresses of the runtime
regions depend on the runtime regions alone. Boot services regions that
the kernel marked EFI_MEMORY_RUNTIME to keep them reserved, see
efi_arch_mem_reserve(), go into the second pass: they move with the
other boot services regions. The order within each pass is unchanged,
which keeps the relative offsets that EFI_PROPERTIES_TABLE needs, and
the same set of regions is mapped as before.

With this change both memory maps above put RTCode at
0xfffffffeefcff000 and RTData at 0xfffffffeed4ff000, and that machine
resumes from S4 with runtime services working.

The cause was found and the patch was written with the help of an AI
coding assistant (Claude Fable 5.2 in Claude Code), working from memory
maps and logs taken on that machine.

Fixes: d2f7cbe7b26a ("x86/efi: Runtime services virtual mapping")
Signed-off-by: Alvaro Parker <alparkerdf@gmail.com>
---

Notes:
    Tested with 7.3.6 (Arch config) on the machine above: one S4 cycle with
    the patch. The wake boot's memory map was recovered from RAM afterwards
    and was the "S4 wake" variant, so the unpatched layout would have
    shifted on that resume. The unpatched kernel has failed on every such
    resume here. On current mainline the patch is only compile-tested
    (x86_64 defconfig, arch/x86/platform/efi/efi.o).
    
    Not addressed by this patch:
    
     - Nothing detects a mismatch. After a resume the kernel could compare
       the firmware's systab->runtime with efi.runtime and disable runtime
       services before the first call, instead of faulting in it.
    
     - An image written by a kernel without this patch and loaded by a
       kernel with it, or the reverse, now always mismatches. x86 accepts
       images across kernel versions, so this may call for a RESTORE_MAGIC
       bump.
    
    The same failure was reported in 2015, with a patch that gave boot and
    runtime regions separate VA ranges. It was not merged:
    https://lore.kernel.org/r/1438230757-30840-1-git-send-email-jlee@suse.com
    
    A recent report with the same signature on another Lenovo machine:
    https://bbs.archlinux.org/viewtopic.php?id=312435
    
    I can send the full memory maps of both boots.

 arch/x86/platform/efi/efi.c | 72 ++++++++++++++++++++++++++++---------
 1 file changed, 56 insertions(+), 16 deletions(-)

diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
index f569d9a7c5ab..31af1b0876d4 100644
--- a/arch/x86/platform/efi/efi.c
+++ b/arch/x86/platform/efi/efi.c
@@ -703,6 +703,21 @@ static bool should_map_region(efi_memory_desc_t *md)
 	return false;
 }
 
+/*
+ * Regions the firmware itself uses at run time. Boot services regions do
+ * not count, even when the kernel tagged them with EFI_MEMORY_RUNTIME to
+ * keep them reserved (efi_arch_mem_reserve(), efi_reserve_boot_services()):
+ * the firmware is free to place those differently on every boot.
+ */
+static bool efi_is_firmware_runtime_region(const efi_memory_desc_t *md)
+{
+	if (md->type == EFI_BOOT_SERVICES_CODE ||
+	    md->type == EFI_BOOT_SERVICES_DATA)
+		return false;
+
+	return md->attribute & EFI_MEMORY_RUNTIME;
+}
+
 /*
  * Map the efi memory ranges of the runtime services and update new_mmap with
  * virtual addresses.
@@ -713,31 +728,56 @@ static void * __init efi_map_regions(int *count, int *pg_shift)
 	unsigned long left = 0;
 	unsigned long desc_size;
 	efi_memory_desc_t *md;
+	int pass;
 
 	desc_size = efi.memmap.desc_size;
 
-	p = NULL;
-	while ((p = efi_map_next_entry(p))) {
-		md = p;
+	/*
+	 * Virtual addresses are handed out top-down, in the order the regions
+	 * are mapped. Map the firmware's runtime regions first and the boot
+	 * services regions after them, so that the addresses of the runtime
+	 * regions depend on the runtime regions alone.
+	 *
+	 * Hibernation relies on that. The kernel that loads the image calls
+	 * SetVirtualAddressMap() with its own layout, and the restored kernel
+	 * goes on using the EFI page tables it built when it booted. Firmware
+	 * keeps its runtime regions in place from one boot to the next, but
+	 * its boot services regions can differ, for instance when it wakes
+	 * from ACPI S4. With both kinds mapped in one walk, such a difference
+	 * moved the runtime regions by a multiple of 2M and the first runtime
+	 * call after resume jumped to an address the restored kernel had not
+	 * mapped.
+	 *
+	 * Within each pass the regions keep the order efi_map_next_entry()
+	 * gives them, which is what EFI_PROPERTIES_TABLE needs.
+	 */
+	for (pass = 0; pass < 2; pass++) {
+		p = NULL;
+		while ((p = efi_map_next_entry(p))) {
+			md = p;
 
-		if (!should_map_region(md))
-			continue;
+			if (!should_map_region(md))
+				continue;
 
-		efi_map_region(md);
+			if (efi_is_firmware_runtime_region(md) != (pass == 0))
+				continue;
 
-		if (left < desc_size) {
-			new_memmap = realloc_pages(new_memmap, *pg_shift);
-			if (!new_memmap)
-				return NULL;
+			efi_map_region(md);
 
-			left += PAGE_SIZE << *pg_shift;
-			(*pg_shift)++;
-		}
+			if (left < desc_size) {
+				new_memmap = realloc_pages(new_memmap, *pg_shift);
+				if (!new_memmap)
+					return NULL;
+
+				left += PAGE_SIZE << *pg_shift;
+				(*pg_shift)++;
+			}
 
-		memcpy(new_memmap + (*count * desc_size), md, desc_size);
+			memcpy(new_memmap + (*count * desc_size), md, desc_size);
 
-		left -= desc_size;
-		(*count)++;
+			left -= desc_size;
+			(*count)++;
+		}
 	}
 
 	return new_memmap;

base-commit: d06ce6f1eb5eeffd4ebcfde345dc34a98131d106
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] x86/efi: Map firmware runtime regions before boot services regions
  2026-12-03 15:30 [PATCH] x86/efi: Map firmware runtime regions before boot services regions Alvaro Parker
@ 2026-12-05 12:07 ` dev12124
  0 siblings, 0 replies; 2+ messages in thread
From: dev12124 @ 2026-12-05 12:07 UTC (permalink / raw)
  To: Alvaro Parker
  Cc: ardb, linux-efi, ilias.apalodimas, tglx, mingo, bp, dave.hansen,
	x86, hpa, linux-pm, rafael, jlee, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2428 bytes --]

Hi,

Analyzing the code, have a few points:

I. The good side

 	[ x ] - Why it would break during S4 (Hibernation)
 			During the resumption from hibernation, the new kernel must map the EFI Runtime Services regions (RTCode and RTData)
                		to the exact
 			same virtual addresses used in the previous session, ensuring that
 			`SetVirtualAddressMap()` and the firmware pointers remain valid.
         [ x  ] - The failure of the old algorithm
 			Since efi_map_regions() traversed the memory table from top to bottom, mapping everything in a single pass,
                         any change in the size of Boot Services regions (BSCode/BSData)—which the firmware might
                         allocate differently between a normal boot and an S4 wake—would shift all subsequent virtual addresses
                         of the Runtime regions, causing an immediate page fault in efi_call_rts().
 	[ x ] - The two-pass correction (pass 0 and pass 1)
 			Separating the loop to first map all pure runtime regions (pass == 0)
 			and only subsequently mapping the reserved boot services regions (pass == 1)
 			isolates the virtual layout of the runtime services.
  II. The bad side
 	[   ] - Confusing and hard-to-read Boolean condition in the loop (pass == 0)
 			In line:

 				if (efi_is_firmware_runtime_region(md) != (pass == 0))
     					continue;

                         Comparing the return value of a `bool` function—or
 			an equality expression like `(pass == 0)`—using `!=` works in C (since both evaluate to 0 or 1), but it makes the code unnecessarily dense
 			and prone to errors during future maintenance. Using a clear `enum` (e.g., `PASS_RUNTIME`, `PASS_BOOT_SERVICES`)
 			or a direct check makes the intent
 			immediately obvious.
 	[   ] - Incompatibility of hibernation layout without a bump in RESTORE_MAGIC (Risk of silent regression)
 			Regarding your notes: why shouldn't the x86 `RESTORE_MAGIC` be incremented, or, alternatively, shouldn't a consistency
 			check be performed before calling `efi.runtime`?
 	[   ] - Check the impact of ordering when EFI_PROPERTIES_TABLE is present
 			Is this validated on firmwares that expose the EFI_PROPERTIES_TABLE? Given that the original interleaving between BS
 			(Boot Services) and RT (Runtime Services) can alter the 2M page alignment(EFI_VAR_START).

This is a good solution (but, with a `small bugs`).

Cheers,
     João.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 12:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 15:30 [PATCH] x86/efi: Map firmware runtime regions before boot services regions Alvaro Parker
2026-12-05 12:07 ` dev12124

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox