From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796311837; b=TgFW7ug3hWlBxjT93QXZiw48l5DaalC4O2jBu/oiQgM0zZ3wRPxuE5B+JJ+bUtn4fihR D1UZiwL2dSHvfOz4ehFxM14+QpSS9hdJe98B+rkEDqbpv18v6zWqbIX1Ra/RiboWKO8sZ ZROUQKQ187Mdq9T8/Xc8sfmWc/Fe0FyFvw5o0wgW38SmNmIMZ6QZcNErHp4gH5NccmaPy 2N18KLe+t2JdexyaVj72hCLzMWVF4nKufQ33xJVTvfHDgnfOp0c/AMxCvAadhTGG8xi6Q fqJDZ8Tol1xQhCgEFBp8145zpDHDO3vOtNrAarSVQbT6xngKI69X2Dj7ucKZwTYsSng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796311837; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=cSQCxNdPUopRKelDz2jTKVDSQP9hHAwcTQUKd93uTDY=; b=X8iV6wALQGorI+n4nl5ktN8+QptU6wsVDZMuiR+6PjLrWicUw7XSkRFkd86nX4oifH0E 21UTxcucMDqYpeVygofTT3vvHnD/x0wWx+JgEFMmsulxp5H4nb7krJeUEaB6biqQyrNkU KFPwnMeuLF95S6q8+09lQAtMRA9IGAx2G2UIVavMiIcS1qxJCaM8O8uH7bNIZM3AZ1eEs MUrwen+l+zPmuwYmTYsleI9OdGPMKIz9khDAJ4CAXJy9Ktnt5W9VzR8PVIALe6+6TMGF7 1UMCtHxOlDUD1VEVIPzVVaMjXTFi8ZRfx3Dy3ZzrULPjOroF6q/cYhzgmiv9yT5zk3w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=HV9S0X5s; arc=none smtp.remote-ip=74.125.229.139 Received: from mail-dl2-f11.google.com (mail-dl2-f11.google.com [74.125.229.139]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 116E8606A33A for ; Thu, 3 Dec 2026 15:30:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 116E8606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=HV9S0X5s Received: by mail-dl2-f11.google.com with SMTP id a92af1059eb24-1b26e33645fso366946c88.0 for ; Thu, 03 Dec 2026 07:30:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796311835; x=1796916635; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=cSQCxNdPUopRKelDz2jTKVDSQP9hHAwcTQUKd93uTDY=; b=HV9S0X5sGcQaPsKDGP0O0NWUDHpsjX0YsFLXaAIwC9q4f8U2RbiGN99/hIjn2xVR5T Em6IcqZ0AefpReiZXuM8SfM5HAVpTV2psoX2dBqj0/Z09uQV1xoLYZ5hj6fpQj6NrPrX 88d43gM+NYSvMz3t6c/ZLu6uZK/bSDajqliz8MnBaOyslpN+cIqSPfTv8ANx+T62W49m 1Yf/pv3yUbQ6yxNpELKZdD3q+iMkQSR9U/WLYSIoK1vrQuaeOFliSszXgECOZTc0dCsp gAjPl04BETYbFurN8Z/j4MLm/UNXT7dS6lvfWIq4xuWCwGpSRLV0fskzARP6gd8gQ/sf Vusg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796311835; x=1796916635; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cSQCxNdPUopRKelDz2jTKVDSQP9hHAwcTQUKd93uTDY=; b=xjfUzQPEf7Dxyta8aGrHSLwbn1qUhGAtWbmzhhIHMe+5mBfh7mkqOFuDI+/u+zTsRv SRSDlNUCDzqr09VEdi1MA/7Y5aj+kYlH35JrkSXSSfZLiarxQxgqzFFQT1T4dDOjMC1o WFxQgtD4sYMX1kKkGsg1eV+GUmye+Cq/KzZaQYRfrgXt0vF9lm+Jmt742C7PY+mm9dte VVs4Xb8o6olm1WD171umbXkDYpteqtE9AAee8DaBU/MG3UgxzrSkPuA7B7Mbtg8lMKcf fE5abqaigtingLdGElfBt4GC6A/XRihPmJq+XcsjG6/v3YufjpXqrVV8qnvE5cT2O2Jd oBcg== X-Forwarded-Encrypted: i=1; AL+hErG88OrBjYhJVpH/qCztZPU9PydPfPaj1FkPLOG175QzDuWHJVFMcmF8w09hNzt+VR5Sv6hWXTHBeCk=@vger.kernel.org X-Gm-Message-State: APuU4Z+mgxNs3iArrdxLyVyVQHMSL/Zkqwp4eZoXU6NB4ynCKlO2FTto P4Xz/fFJmR41AIlf1wC+f+gZ1YsJvp3vVpwVAFt+T2VpKyne5RpVeyST X-Gm-Gg: AZFdTZcuI5IPFOkjQ+TEe2SQNm5+Wxx75foDCfN0cYGE5o3thEIs+NbV6t5HhjuYle1 YRs8j/beTtJ/ViGJMBm2MHC8q5TMtBQE1fBYVgghmv8U5JE2F3tZjrS2p/AGkNRUUf9PAwkq/xk Ilve1CZD+omwiC4GK13a/SKaE4g7ni9JEYgqg5pjuSrOsIbafRY63XhEv9U6YbNM0xr6j1YSW1L qhSHWLwnDWrfvIoH1/CBiZBQhrFuZw5GJt5SjFkVlKQ8WOwHBz7HEFBfTvKdJmOqnucxHDE1xK8 0QyFTZzZqWfmbNkFivPC0W+s454PlV8hiidy9e/67vmeQ7i3YtGGlRRDOY1ef+y74TJxad1SIIt YbmCpmr0+uodefzzUcfkC8tuz/kJ5j3C8G/cfhVHMKdoRk3t6ynq5RIUSYoN1SzCsZC7oXw/J5U rQ8bBLY+U1wET+HpxDwUjEcMYhvDcX5p1XyWtPvxa084Okgm+dYNFv6onIGDt18s8HkbyvT6UVy cDJRoktNg5yl5no+6rvuOZiI/LfAMGadYYTxv6upwjOOO5T92MJyvHQOFK9HvnWli0H2gvQz9Ae DTHm2CKnMuSh1hIXtpGpWBjUaTqTOBPzzbxE9vxnL5rFKYKsiZndGITxUA== X-Received: by 2002:a05:7022:b1da:20b0:1b5:c305:2f72 with SMTP id a92af1059eb24-1b5ceba2877mr1973219c88.0.1796311834644; Thu, 03 Dec 2026 07:30:34 -0800 (PST) Received: from arch-thinkpad.tail5a6161.ts.net ([181.173.162.145]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b56ef9fd16sm100901752c88.4.2026.12.03.07.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 07:30:34 -0800 (PST) From: Alvaro Parker To: Ard Biesheuvel , Ilias Apalodimas , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: linux-pm@vger.kernel.org, "Rafael J . Wysocki" , Chun-Yi Lee , Alvaro Parker , linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] x86/efi: Map firmware runtime regions before boot services regions Date: Thu, 3 Dec 2026 12:30:17 -0300 Message-ID: <20261203153018.26852-1-alparkerdf@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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