From nobody Fri Oct 3 20:58:19 2025 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B95DF288C1E for ; Tue, 26 Aug 2025 01:18:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756171107; cv=none; b=XNXpJf1WPRoE/MC3UD5jck2eYHNro8HDNhSptUE7t2glsMh85DvpPQdU/UVuWdNud1gQvGhIorh7OcVHJ08N6egV0IiqC/vHG6u9bqoDsIcVtN8B6I/zjIOxJezDsyUpEPgUh35Na/Lk4i7GHCeUZktmAlYLV73IM2CNw24PpMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756171107; c=relaxed/simple; bh=cCYKxuJYvPXEpEbngDNnm+hpqpPawWlgJtjggYVuFWw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K8GUVciU1e43xJ0GU9xMnH8NIcXX83jSjFXapMZ36khXUPgfHR9PkyShQ38rZFwqKJy7IkiCkieH10aNQiKj+UjC4EwR3fEddJ63UU/1Z9jQ3zfxNw1HSqzVqra9vsLrygW2mREifVrKrWOVP7JIFkoTxTvOGhcQ7ft1y8d3eps= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Y+pJzeq3; arc=none smtp.client-ip=91.218.175.180 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) header.d=linux.dev header.i=@linux.dev header.b="Y+pJzeq3" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1756171103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NL2qRizv3kWdFUkXtTX2dki08TzfPVi2Nl7IUnUwTHA=; b=Y+pJzeq3PnMBpz5yn+E4SwxrswzSQ90w8jmZ9KnuX0l5Zo17tT+njxnIWcg4kzIsjOEiTm 6coS3Z40k+0SpvgU6BAfnJJ8he2lxTEw93jEfMREgi/y0cJqLVn0D2mQI21rOWKFST0DLf g9Bx/vfSg0ZYvfULuteBJEDlBCo7Fc8= From: Youling Tang To: Huacai Chen Cc: WANG Xuerui , Baoquan He , Yao Zi , kexec@lists.infradead.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, youling.tang@linux.dev, Youling Tang Subject: [PATCH v3 4/6] LoongArch/kexec_file: Support loading ELF binary file Date: Tue, 26 Aug 2025 09:17:20 +0800 Message-Id: <20250826011722.82391-5-youling.tang@linux.dev> In-Reply-To: <20250826011722.82391-1-youling.tang@linux.dev> References: <20250826011722.82391-1-youling.tang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Youling Tang This patch creates kexec_elf_ops to load ELF binary file for kexec_file_load() syscall. However, for `kbuf->memsz` and `kbuf->buf_min`, special handling is require= d, and the generic `kexec_elf_load()` cannot be used directly. $ readelf -l vmlinux ... Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000010000 0x9000000000200000 0x9000000000200000 0x0000000002747a00 0x000000000287a0d8 RWE 0x10000 NOTE 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x0000000000000000 R 0x8 phdr->p_paddr should have been a physical address, but it is a virtual address on the current LoongArch. This will cause kexec_file to fail when loading the kernel and need to be converted to a physical address. From the above MemSiz, it can be seen that 0x287a0d8 has not been aligned. Although kexec_add_buffer() will perform PAGE_SIZE alignment on kbuf->memsz, there is still a stampeding in the loaded kernel space and initrd space. The initrd resolution failed when starting the second kernel. It can be known from the link script vmlinux.lds.S that, BSS_SECTION(0, SZ_64K, 8) . =3D ALIGN(PECOFF_SEGMENT_ALIGN); It needs to be aligned according to the SZ_64K size, so that after alignment, its size is consistent with _kernel_asize. Signed-off-by: Youling Tang --- arch/loongarch/Kconfig | 1 + arch/loongarch/include/asm/kexec.h | 1 + arch/loongarch/kernel/Makefile | 2 +- arch/loongarch/kernel/kexec_elf.c | 105 +++++++++++++++++++++ arch/loongarch/kernel/machine_kexec_file.c | 1 + 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 arch/loongarch/kernel/kexec_elf.c diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index 490dc6eed749..bf2664ed7ce3 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -632,6 +632,7 @@ config ARCH_SELECTS_KEXEC_FILE def_bool y depends on KEXEC_FILE select HAVE_IMA_KEXEC if IMA + select KEXEC_ELF select RELOCATABLE =20 config ARCH_SUPPORTS_CRASH_DUMP diff --git a/arch/loongarch/include/asm/kexec.h b/arch/loongarch/include/as= m/kexec.h index 34799db933fb..fecfb3015abc 100644 --- a/arch/loongarch/include/asm/kexec.h +++ b/arch/loongarch/include/asm/kexec.h @@ -43,6 +43,7 @@ struct kimage_arch { =20 #ifdef CONFIG_KEXEC_FILE extern const struct kexec_file_ops kexec_efi_ops; +extern const struct kexec_file_ops kexec_elf_ops; =20 int arch_kimage_file_post_load_cleanup(struct kimage *image); #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_clea= nup diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile index dd6183f353e6..001924877772 100644 --- a/arch/loongarch/kernel/Makefile +++ b/arch/loongarch/kernel/Makefile @@ -62,7 +62,7 @@ obj-$(CONFIG_MAGIC_SYSRQ) +=3D sysrq.o obj-$(CONFIG_RELOCATABLE) +=3D relocate.o =20 obj-$(CONFIG_KEXEC_CORE) +=3D machine_kexec.o relocate_kernel.o -obj-$(CONFIG_KEXEC_FILE) +=3D machine_kexec_file.o kexec_efi.o +obj-$(CONFIG_KEXEC_FILE) +=3D machine_kexec_file.o kexec_efi.o kexec_elf.o obj-$(CONFIG_CRASH_DUMP) +=3D crash_dump.o =20 obj-$(CONFIG_UNWINDER_GUESS) +=3D unwind_guess.o diff --git a/arch/loongarch/kernel/kexec_elf.c b/arch/loongarch/kernel/kexe= c_elf.c new file mode 100644 index 000000000000..175d8d935e11 --- /dev/null +++ b/arch/loongarch/kernel/kexec_elf.c @@ -0,0 +1,105 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Load ELF vmlinux file for the kexec_file_load syscall. + * + * Author: Youling Tang + * Copyright (C) 2025 KylinSoft Corporation. + */ + +#define pr_fmt(fmt) "kexec_file(ELF): " fmt + +#include +#include +#include +#include +#include +#include + +static int loongarch_kexec_elf_load(struct kimage *image, struct elfhdr *e= hdr, + struct kexec_elf_info *elf_info, + struct kexec_buf *kbuf, + unsigned long *text_offset) +{ + int ret =3D -1; + size_t i; + + /* Read in the PT_LOAD segments. */ + for (i =3D 0; i < ehdr->e_phnum; i++) { + size_t size; + const struct elf_phdr *phdr; + + phdr =3D &elf_info->proghdrs[i]; + if (phdr->p_type !=3D PT_LOAD) + continue; + + size =3D phdr->p_filesz; + if (size > phdr->p_memsz) + size =3D phdr->p_memsz; + + kbuf->buffer =3D (void *)elf_info->buffer + phdr->p_offset; + kbuf->bufsz =3D size; + kbuf->buf_align =3D phdr->p_align; + *text_offset =3D __pa(phdr->p_paddr); + kbuf->buf_min =3D *text_offset; + kbuf->memsz =3D ALIGN(phdr->p_memsz, SZ_64K); + kbuf->mem =3D KEXEC_BUF_MEM_UNKNOWN; + ret =3D kexec_add_buffer(kbuf); + if (ret) + break; + } + + return ret; +} + +static void *elf_kexec_load(struct kimage *image, char *kernel_buf, + unsigned long kernel_len, char *initrd, + unsigned long initrd_len, char *cmdline, + unsigned long cmdline_len) +{ + int ret; + unsigned long text_offset =3D 0, kernel_segment_number; + struct elfhdr ehdr; + struct kexec_elf_info elf_info; + struct kexec_segment *kernel_segment; + struct kexec_buf kbuf; + + ret =3D kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info); + if (ret) + return ERR_PTR(ret); + + /* + * Load the kernel + * FIXME: Non-relocatable kernel rejected for kexec_file (require CONFIG_= RELOCATABLE) + */ + kbuf.image =3D image; + kbuf.buf_max =3D ULONG_MAX; + kbuf.top_down =3D false; + + kernel_segment_number =3D image->nr_segments; + + ret =3D loongarch_kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &text_of= fset); + if (ret) + goto out; + + /* Load additional data */ + kernel_segment =3D &image->segment[kernel_segment_number]; + ret =3D load_other_segments(image, kernel_segment->mem, kernel_segment->m= emsz, + initrd, initrd_len, cmdline, cmdline_len); + if (ret) + goto out; + + /* Make sure the second kernel jumps to the correct "kernel_entry". */ + image->start =3D kernel_segment->mem + __pa(ehdr.e_entry) - text_offset; + + kexec_dprintk("Loaded kernel at 0x%lx bufsz=3D0x%lx memsz=3D0x%lx\n", + kernel_segment->mem, kbuf.bufsz, kernel_segment->memsz); + +out: + kexec_free_elf_info(&elf_info); + return ret ? ERR_PTR(ret) : NULL; +} + +const struct kexec_file_ops kexec_elf_ops =3D { + .probe =3D kexec_elf_probe, + .load =3D elf_kexec_load, +}; diff --git a/arch/loongarch/kernel/machine_kexec_file.c b/arch/loongarch/ke= rnel/machine_kexec_file.c index a713acf32db8..dc6e37457559 100644 --- a/arch/loongarch/kernel/machine_kexec_file.c +++ b/arch/loongarch/kernel/machine_kexec_file.c @@ -22,6 +22,7 @@ =20 const struct kexec_file_ops * const kexec_file_loaders[] =3D { &kexec_efi_ops, + &kexec_elf_ops, NULL }; =20 --=20 2.43.0