From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796276438; b=sVhklmpcTQz6UWvLhThLU3P1UI20Ydk2Ouv0Un/bgMwXL3u4Eq29S0F1rIooc1m2GIxR KqEuHc4sYsRNnXxA2wr2/WZ6npiswi4DDWW08vZeAx3ULZJdbJOMzNGGcwRcbEd2aVmsL 3h5tesN2uNZhyhzLrpBF8f4OyV9yz8NeAMYRI9WTdEpkGbH3Af9eGEPG5a93Y7QiKDDH1 0wbw3aHC7VrSZkEFwhlyrJczCeKq6rvnGGFnEwxUFlEct2HcUWcyHKcV4f5fAr9KNzrWt TFIHCCt9p7LxOFv/uh1KQtSh8AGQPEDYD6doWap4cxVulbJvxNuaGYiwVS0ggPnVdRw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796276438; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=nq7W/J9yG/6ek6qVCimB064n/xdDFDS0WKL7TpJS1dU=; b=DFqNNDAuPVf22MKprAKweYEqS15hG2AbCh7joJCa57iQpCkjF8iw+/mDtGbzn3hfDLxk Y6OfkFORsBgqnPmjw4LLC0La7xNCp+G5X4Q/AJeDWWefZYwCgrBeS36um1CGBq3OTE/5m iaKkcEMjy71TaZAK6LLYabyDlRzzjfHpg9OlvM3+KRF+L7leGPoY0mm8+ajz8GAVqyBm3 ngnfIAYq6JHDPTj2YfJcC6tJ7jjQTZMT/pGq8pHTwrXf4vL3XgbihFkssEgq5Wuwd0RyL 0aEAtZV4NV1yEDheFgipxcRkpC1yqRweXIRfv3NmlYQjcMnd+Ik/33u1FKW9oGQ8mow== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZA2Z0nbA; arc=none smtp.remote-ip=91.218.175.199 Received: from mta0.migadu.com (out-199.mta0.migadu.com [91.218.175.199]) (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 AE0D2606A33B for ; Thu, 3 Dec 2026 05:40:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AE0D2606A33B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ZA2Z0nbA X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=PjXRS+ORLjKbTCpOjMhV9zMlZC7SFgAZfoUrND/LQkY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796276434; v=1; x=1796881234; b=ZA2Z0nbAxZYvF5Amib8YgiINwaXmCUfS0JzVcddyt0Yq53R/NP51a/8yNYfREyfvLfwtVb02 q1Q6Nr1ePbs4G6+5BJdl4irIhnIGO/J5UsYqNzgg5QqwiC6ux8sTfbov+pbFK1HPMqFAjf1Rleq kCFW6iZCeLI1J5YxvHTI2T2k= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 32e11377880bf619; Thu, 03 Dec 2026 05:40:28 +0000 X-Mizu-Trace-ID: 32e11377880bf619 X-Migadu-Flow: FLOW_OUT From: George Guo To: ardb@kernel.org Cc: chenhuacai@kernel.org, pratyush@kernel.org, rppt@kernel.org, pasha.tatashin@soleen.com, shuah@kernel.org, ilias.apalodimas@linaro.org, akpm@linux-foundation.org, baoquan.he@linux.dev, ruirui.yang@linux.dev, guodongtai@kylinos.cn, kernel@xen0n.name, graf@amazon.com, liukexin@kylinos.cn, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-efi@vger.kernel.org Subject: Re: [PATCH v6 1/8] efi: Pass KHO state to the next kernel in a configuration table Date: Wed, 2 Dec 2026 21:40:07 -0800 Message-ID: <20261203054007.84080-1-dongtai.guo@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <976f1220-a259-4bad-9eb4-ae1cc78a79a7@app.fastmail.com> References: <976f1220-a259-4bad-9eb4-ae1cc78a79a7@app.fastmail.com> X-Mailing-List: linux-kernel@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 Hi Ard, On Fri, Nov 27, 2026 at 04:44:51PM +0100, Ard Biesheuvel wrote: >> So I would like to keep the table common in v7. > > Fair enough. Thanks. >> Would you prefer the >> x86 patches in the same series, so that the generic code comes with a >> second user, or as a separate series on top? > > I'd like to see both tbh, but only if it is not too much of a hassle. Pratyush asked to keep x86 out of v7, so the LoongArch series does not wait on x86 review. v7 stays LoongArch only, and I will post the x86 patches as a separate series on top of it, with the vmtest changes that boot the x86 guest with OVMF. Below is the x86 EFI patch, for the record, so you can see the second user now. It applies on top of this series. Thanks, George -- >8 -- From: George Guo Subject: x86/efi: Pass KHO state to the next kernel in the EFI configuration table x86 passes the KHO FDT and bootmem addresses to the next kernel in a SETUP_KEXEC_KHO setup_data record. When the next kernel boots with EFI, pass them in the Linux EFI configuration table instead, as LoongArch does. - Move install_kho_table() from efi-stub.c to efi-stub-helper.c, since the x86 stub does not link efi-stub.o, and call it from exit_boot() in the x86 stub. - In setup_kho(), skip the SETUP_KEXEC_KHO record when the table was found and the next kernel gets efi_info, so kho_populate() runs only once. Without EFI, for example on a legacy BIOS boot or with noefi, keep using setup_data. - The decompressor's KASLR code must not place the kernel on preserved memory. Without a SETUP_KEXEC_KHO record, look up the bootmem areas in the table. - setup_kho() runs at kexec load time, so efi_kho and efi_kho_table_found() can no longer be __init. Tested in QEMU on x86_64. With OVMF, the KHO vmtest and luo_kexec_simple pass, and the next kernel finds the KHO state through the table. With SeaBIOS, the KHO vmtest passes through setup_data. Co-developed-by: Kexin Liu Signed-off-by: Kexin Liu Signed-off-by: George Guo --- arch/x86/boot/compressed/efi.h | 9 +++ arch/x86/boot/compressed/kaslr.c | 63 +++++++++++++++---- arch/x86/kernel/kexec-bzimage64.c | 19 ++++-- drivers/firmware/efi/efi.c | 4 +- .../firmware/efi/libstub/efi-stub-helper.c | 24 +++++++ drivers/firmware/efi/libstub/efi-stub.c | 22 ------- drivers/firmware/efi/libstub/x86-stub.c | 2 + 7 files changed, 104 insertions(+), 39 deletions(-) diff --git a/arch/x86/boot/compressed/efi.h b/arch/x86/boot/compressed/efi.h index b22300970f97..569dd5964e3b 100644 --- a/arch/x86/boot/compressed/efi.h +++ b/arch/x86/boot/compressed/efi.h @@ -17,6 +17,7 @@ typedef guid_t efi_guid_t __aligned(__alignof__(u32)); #define ACPI_20_TABLE_GUID EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81) #define EFI_CC_BLOB_GUID EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42) #define LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID EFI_GUID(0xd5d1de3c, 0x105c, 0x44f9, 0x9e, 0xa9, 0xbc, 0xef, 0x98, 0x12, 0x00, 0x31) +#define LINUX_EFI_KEXEC_HANDOVER_GUID EFI_GUID(0xc941b6c7, 0x7b3f, 0x4af6, 0x9e, 0x50, 0xfc, 0xb3, 0xa8, 0x86, 0x8a, 0x17) #define EFI32_LOADER_SIGNATURE "EL32" #define EFI64_LOADER_SIGNATURE "EL64" @@ -105,6 +106,14 @@ struct efi_unaccepted_memory { unsigned long bitmap[]; }; +/* Same layout as struct linux_efi_kho_data in include/linux/efi.h */ +struct efi_kho_data { + u64 fdt_addr; + u64 fdt_size; + u64 bootmem_addr; + u64 bootmem_size; +}; + static inline int efi_guidcmp (efi_guid_t left, efi_guid_t right) { return memcmp(&left, &right, sizeof (efi_guid_t)); diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 290153818d0f..2bf38f637e14 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -763,32 +763,73 @@ static void process_e820_entries(unsigned long minimum, } } +/* Find the KHO bootmem areas in the EFI config table, if it has any. */ +static bool efi_kho_bootmem(u64 *addr, u64 *size) +{ + struct efi_kho_data *kho; + unsigned long cfg_table_pa; + unsigned int cfg_table_len; + + if (efi_get_type(boot_params_ptr) == EFI_TYPE_NONE) + return false; + + if (efi_get_conf_table(boot_params_ptr, &cfg_table_pa, &cfg_table_len)) + return false; + + kho = (void *)efi_find_vendor_table(boot_params_ptr, cfg_table_pa, + cfg_table_len, + LINUX_EFI_KEXEC_HANDOVER_GUID); + /* A cold boot installs the table zeroed. */ + if (!kho || !kho->fdt_addr) + return false; + + *addr = kho->bootmem_addr; + *size = kho->bootmem_size; + return true; +} + /* - * If KHO is active, only process its bootmem areas to ensure we are not - * stepping onto preserved memory. + * Find the KHO bootmem areas in the SETUP_KEXEC_KHO record or, if the + * previous kernel passed the KHO state through EFI, in the config table. */ -static bool process_kho_entries(unsigned long minimum, unsigned long image_size) +static bool find_kho_bootmem(u64 *addr, u64 *size) { - struct kho_bootmem *kho_bootmem; struct setup_data *ptr; struct kho_data *kho; - int i, nr_areas = 0; - - if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER)) - return false; ptr = (struct setup_data *)(unsigned long)boot_params_ptr->hdr.setup_data; while (ptr) { if (ptr->type == SETUP_KEXEC_KHO) { kho = (struct kho_data *)(unsigned long)ptr->data; - kho_bootmem = (void *)(unsigned long)kho->bootmem_addr; - nr_areas = kho->bootmem_size / sizeof(*kho_bootmem); - break; + *addr = kho->bootmem_addr; + *size = kho->bootmem_size; + return true; } ptr = (struct setup_data *)(unsigned long)ptr->next; } + return efi_kho_bootmem(addr, size); +} + +/* + * If KHO is active, only process its bootmem areas to ensure we are not + * stepping onto preserved memory. + */ +static bool process_kho_entries(unsigned long minimum, unsigned long image_size) +{ + struct kho_bootmem *kho_bootmem; + u64 bootmem_addr, bootmem_size; + int i, nr_areas; + + if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER)) + return false; + + if (!find_kho_bootmem(&bootmem_addr, &bootmem_size)) + return false; + + kho_bootmem = (void *)(unsigned long)bootmem_addr; + nr_areas = bootmem_size / sizeof(*kho_bootmem); if (!nr_areas) return false; diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c index 3aa77006adab..57fca805e7ac 100644 --- a/arch/x86/kernel/kexec-bzimage64.c +++ b/arch/x86/kernel/kexec-bzimage64.c @@ -139,6 +139,13 @@ setup_rng_seed(struct boot_params *params, unsigned long params_load_addr, params->hdr.setup_data = setup_data_phys; } +/* Will the next kernel get efi_info, and so see the EFI config tables? */ +static bool kexec_passes_efi_state(void) +{ + return efi_enabled(EFI_RUNTIME_SERVICES) && + boot_params.efi_info.efi_memmap_size; +} + #ifdef CONFIG_EFI static int setup_efi_info_memmap(struct boot_params *params, unsigned long params_load_addr, @@ -200,10 +207,7 @@ setup_efi_state(struct boot_params *params, unsigned long params_load_addr, params->acpi_rsdp_addr = efi.acpi; } - if (!efi_enabled(EFI_RUNTIME_SERVICES)) - return 0; - - if (!current_ei->efi_memmap_size) + if (!kexec_passes_efi_state()) return 0; params->secure_boot = boot_params.secure_boot; @@ -281,6 +285,13 @@ static void setup_kho(const struct kimage *image, struct boot_params *params, if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER)) return; + /* + * The KHO state goes through the EFI config table when the next + * kernel can see it, and through setup_data otherwise. + */ + if (efi_kho_table_found() && kexec_passes_efi_state()) + return; + sd->type = SETUP_KEXEC_KHO; sd->len = sizeof(struct kho_data); diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c index f5293ce4ed33..4730eee430e7 100644 --- a/drivers/firmware/efi/efi.c +++ b/drivers/firmware/efi/efi.c @@ -57,7 +57,7 @@ EXPORT_SYMBOL(efi); unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR; static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR; static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR; -static unsigned long efi_kho __initdata = EFI_INVALID_TABLE_ADDR; +static unsigned long efi_kho __ro_after_init = EFI_INVALID_TABLE_ADDR; extern unsigned long primary_display_table; @@ -1223,7 +1223,7 @@ early_initcall(efi_memreserve_root_init); static struct linux_efi_kho_data *efi_kho_table __ro_after_init; /* Did the previous kernel or the EFI stub pass a KHO table? */ -bool __init efi_kho_table_found(void) +bool efi_kho_table_found(void) { return efi_kho != EFI_INVALID_TABLE_ADDR; } diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 8e43eb3f418b..0ca390e9064e 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -483,6 +483,30 @@ void *get_efi_config_table(efi_guid_t guid) return NULL; } +void install_kho_table(void) +{ + struct linux_efi_kho_data *kho; + efi_guid_t kho_table_guid = LINUX_EFI_KEXEC_HANDOVER_GUID; + efi_status_t status; + + if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER)) + return; + + status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*kho), (void **)&kho); + if (status != EFI_SUCCESS) { + efi_err("Failed to allocate KHO config table!\n"); + return; + } + + *kho = (struct linux_efi_kho_data){}; + + status = efi_bs_call(install_configuration_table, &kho_table_guid, kho); + if (status != EFI_SUCCESS) { + efi_err("Failed to install KHO config table!\n"); + efi_bs_call(free_pool, kho); + } +} + /* * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way * for the firmware or bootloader to expose the initrd data directly to the stub diff --git a/drivers/firmware/efi/libstub/efi-stub.c b/drivers/firmware/efi/libstub/efi-stub.c index abb7c418ba80..6db10590f89b 100644 --- a/drivers/firmware/efi/libstub/efi-stub.c +++ b/drivers/firmware/efi/libstub/efi-stub.c @@ -108,28 +108,6 @@ static void install_memreserve_table(void) efi_err("Failed to install memreserve config table!\n"); } -void install_kho_table(void) -{ - struct linux_efi_kho_data *kho; - efi_guid_t kho_table_guid = LINUX_EFI_KEXEC_HANDOVER_GUID; - efi_status_t status; - - if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER)) - return; - - status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*kho), (void **)&kho); - if (status != EFI_SUCCESS) { - efi_err("Failed to allocate KHO config table!\n"); - return; - } - - *kho = (struct linux_efi_kho_data){}; - - status = efi_bs_call(install_configuration_table, &kho_table_guid, kho); - if (status != EFI_SUCCESS) - efi_err("Failed to install KHO config table!\n"); -} - static u32 get_supported_rt_services(void) { const efi_rt_properties_table_t *rt_prop_table; diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c index 253dd3ba8fab..188780e807e4 100644 --- a/drivers/firmware/efi/libstub/x86-stub.c +++ b/drivers/firmware/efi/libstub/x86-stub.c @@ -767,6 +767,8 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) priv.boot_params = boot_params; priv.efi = &boot_params->efi_info; + install_kho_table(); + status = allocate_e820(boot_params, &e820ext, &e820ext_size); if (status != EFI_SUCCESS) return status;