From 760d3062d0ec028e65b05036299e16b9058d5abd Mon Sep 17 00:00:00 2001 From: Ming Wang Date: Wed, 3 Dec 2025 09:49:42 +0800 Subject: [PATCH] anolis: LoongArch: efi: avoid double calling set_virtual_map() ANBZ: #31063 Since UEFI firmware allows SVAM to be called only once per boot, calling it again in the main kernel (after the EFI Stub has already run) results in failure, disabling EFI runtime services. This patch implements a reliable detection mechanism by inspecting the EFI memory map data directly. We check if the virt_addr field of memory descriptors has already been populated with a valid kernel virtual address (matching TO_CACHE/TO_UNCACHE). If a match is found, it indicates that the mapping has already been established (e.g., by the EFI Stub). In this case, set_virtual_map() returns immediately to avoid the erroneous second call. Signed-off-by: Ming Wang Signed-off-by: Juxin Gao --- arch/loongarch/kernel/efi.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/loongarch/kernel/efi.c b/arch/loongarch/kernel/efi.c index 08a1d6fc84b7..0c07a715448f 100644 --- a/arch/loongarch/kernel/efi.c +++ b/arch/loongarch/kernel/efi.c @@ -195,6 +195,7 @@ static int __init set_virtual_map(void) efi_runtime_services_t *rt; efi_set_virtual_address_map_t *svam; efi_memory_desc_t *in, runtime_map[32]; + unsigned long target_virt; if (efi_bp) return EFI_SUCCESS; @@ -207,9 +208,16 @@ static int __init set_virtual_map(void) continue; if (attr & (EFI_MEMORY_WB | EFI_MEMORY_WT)) - in->virt_addr = TO_CACHE(in->phys_addr); + target_virt = TO_CACHE(in->phys_addr); else - in->virt_addr = TO_UNCACHE(in->phys_addr); + target_virt = TO_UNCACHE(in->phys_addr); + + if (in->virt_addr == target_virt) { + pr_info("EFI: Virtual mapping already established. Skipping SVAM.\n"); + return EFI_SUCCESS; + } + + in->virt_addr = target_virt; memcpy(&runtime_map[count++], in, size); } -- Gitee