All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 00/10] Fix and improve the LoongArch implementation
@ 2025-09-16  1:46 Youling Tang
  2025-09-16  1:46 ` [PATCH v2 01/10] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

- Add kexec_file support for LoongArch.
- Fix and improve the LoongArch implementation.
- Fixed the handling of crashkernel parameters when using --reuse-cmdline.

Kernel support for kexec_file was introduced by the patch detailed in Link [1].

Link[1]:
https://lore.kernel.org/loongarch/20250903030100.196744-1-youling.tang@linux.dev/T/#t

Changelog:
v2:
 - Add kexec_file support for LoongArch.
 - Correct email address and Signed-off-by.

Youling Tang (10):
  kexec: Handle removal of multiple 'crashkernel' parameters
  LoongArch: Fix comments
  LoongArch: Terminate the cmdline string using '\0'
  LoongArch: Enforce relocatable kernel check for crash dump
  LoongArch: Change initrd allocation to top-down
  LoongArch: Fix the use of loongarch_image_header in ELF format
  LoongArch: Add pe_hdr->machine check for pei format images
  LoongArch/pez: Fix kernel_fd handling when kexec_file is supported
  LoongArch: Add kexec_file_load syscall
  LoongArch: Remove 'kexec_file' cmdline parameters when using
    --reuse-cmdline option

 kexec/arch/loongarch/image-header.h        |  5 ++-
 kexec/arch/loongarch/kexec-elf-loongarch.c | 47 ++++++++++++++++++----
 kexec/arch/loongarch/kexec-loongarch.c     | 44 +++++++++++++++++++-
 kexec/arch/loongarch/kexec-loongarch.h     |  1 +
 kexec/arch/loongarch/kexec-pei-loongarch.c | 27 ++++++++++++-
 kexec/arch/loongarch/kexec-pez-loongarch.c | 14 ++++---
 kexec/kexec-syscall.h                      |  5 ++-
 kexec/kexec.c                              |  6 +++
 8 files changed, 130 insertions(+), 19 deletions(-)

-- 
2.34.1



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

* [PATCH v2 01/10] kexec: Handle removal of multiple 'crashkernel' parameters
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 02/10] LoongArch: Fix comments Youling Tang
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

When the kernel command line contains multiple 'crashkernel' parameters
(e.g., `crashkernel=1G,high crashkernel=256M,low`), the original
`remove_parameter()` function only removed the first instance. This
left residual parameters that caused conflicts during kexec operations.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/kexec.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index 6bf12d7..c9e4bcb 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -1153,6 +1153,7 @@ void remove_parameter(char *line, const char *param_name)
 	if (!start)
 		return;
 
+again:
 	/*
 	 * check if that's really the start of a parameter and not in
 	 * the middle of the word
@@ -1167,6 +1168,11 @@ void remove_parameter(char *line, const char *param_name)
 		memmove(start, end+1, strlen(end));
 		*(end + strlen(end)) = 0;
 	}
+
+	/* There may be multiple 'crashkernel' parameters, such as low and high */
+	start = strstr(line, param_name);
+	if (start)
+		goto again;
 }
 
 static ssize_t _read(int fd, void *buf, size_t count)
-- 
2.34.1



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

* [PATCH v2 02/10] LoongArch: Fix comments
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
  2025-09-16  1:46 ` [PATCH v2 01/10] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 03/10] LoongArch: Terminate the cmdline string using '\0' Youling Tang
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

loongarch_load_other_segments() mainly loads initrd and cmdline. Fix this
annotation.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-pei-loongarch.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kexec/arch/loongarch/kexec-pei-loongarch.c b/kexec/arch/loongarch/kexec-pei-loongarch.c
index 1a11103..f0e0d09 100644
--- a/kexec/arch/loongarch/kexec-pei-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pei-loongarch.c
@@ -102,10 +102,10 @@ int pei_loongarch_load(int argc, char **argv, const char *buf,
 	/* Load the kernel */
 	add_segment(info, buf, len, kernel_segment, loongarch_mem.image_size);
 
-	/* Prepare and load dtb and initrd data */
+	/* Prepare and load initrd and cmdline data */
 	result = loongarch_load_other_segments(info, hole_min);
 	if (result) {
-		fprintf(stderr, "kexec: Load dtb and initrd segments failed.\n");
+		fprintf(stderr, "kexec: Load initrd and cmdline segments failed.\n");
 		goto exit;
 	}
 
-- 
2.34.1



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

* [PATCH v2 03/10] LoongArch: Terminate the cmdline string using '\0'
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
  2025-09-16  1:46 ` [PATCH v2 01/10] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
  2025-09-16  1:46 ` [PATCH v2 02/10] LoongArch: Fix comments Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 04/10] LoongArch: Enforce relocatable kernel check for crash dump Youling Tang
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

Terminate the cmdline string using '\0' instead of 0, improve code readability.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-loongarch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kexec/arch/loongarch/kexec-loongarch.c b/kexec/arch/loongarch/kexec-loongarch.c
index ea0c316..c335e0b 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -334,7 +334,7 @@ int loongarch_load_other_segments(struct kexec_info *info, unsigned long hole_mi
 		}
 	}
 
-	cmdline[sizeof(cmdline) - 1] = 0;
+	cmdline[sizeof(cmdline) - 1] = '\0';
 	add_buffer(info, cmdline, sizeof(cmdline), sizeof(cmdline),
 		sizeof(void *), _ALIGN_UP(hole_min, getpagesize()),
 		hole_max, 1);
-- 
2.34.1



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

* [PATCH v2 04/10] LoongArch: Enforce relocatable kernel check for crash dump
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (2 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 03/10] LoongArch: Terminate the cmdline string using '\0' Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 05/10] LoongArch: Change initrd allocation to top-down Youling Tang
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

Without enabling the RELOCATABLE configuration, LoongArch is a non-PIE kernel
and cannot be loaded to run at any appropriate address. So the CRASH_DUMP
feature depends on RELOCATABLE.

$ cat arch/loongarch/Kconfig
config ARCH_SELECTS_CRASH_DUMP
        def_bool y
        depends on CRASH_DUMP
        select RELOCATABLE

The relocatable kernel is determined by checking if there is a la_abs section.
Currently, only the elf format has been checked, while pei/pez is in the FIXME
state.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-elf-loongarch.c | 32 ++++++++++++++++++++++
 kexec/arch/loongarch/kexec-pei-loongarch.c |  3 ++
 2 files changed, 35 insertions(+)

diff --git a/kexec/arch/loongarch/kexec-elf-loongarch.c b/kexec/arch/loongarch/kexec-elf-loongarch.c
index c87f022..44b63a9 100644
--- a/kexec/arch/loongarch/kexec-elf-loongarch.c
+++ b/kexec/arch/loongarch/kexec-elf-loongarch.c
@@ -13,6 +13,7 @@
 #include <limits.h>
 #include <errno.h>
 #include <elf.h>
+#include <stdbool.h>
 
 #include "kexec.h"
 #include "kexec-elf.h"
@@ -47,6 +48,27 @@ out:
 	return result;
 }
 
+/*
+ * To determine whether it is a relocatable kernel based on the ".la_abs "section,
+ * the CRASH_DUMP feature depends on CONFIG_RELOCATABLE in LoongArch.
+ */
+static bool laabs_section(const struct mem_ehdr *ehdr)
+{
+	struct mem_shdr *shdr, *shdr_end;
+	unsigned char *strtab;
+
+	strtab = (unsigned char *)ehdr->e_shdr[ehdr->e_shstrndx].sh_data;
+	shdr_end = &ehdr->e_shdr[ehdr->e_shnum];
+	for (shdr = ehdr->e_shdr; shdr != shdr_end; shdr++) {
+		if (shdr->sh_size &&
+			strcmp((char *)&strtab[shdr->sh_name], ".la_abs") == 0) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
 int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
 	off_t kernel_size, struct kexec_info *info)
 {
@@ -63,6 +85,16 @@ int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
 		goto exit;
 	}
 
+	if (info->kexec_flags & KEXEC_ON_CRASH) {
+		bool is_relocatable_kernel = laabs_section(&ehdr);
+		if (!is_relocatable_kernel) {
+			dbgprintf("%s: The non-relocation kernel cannot be loaded, "
+				   "CONFIG_RELOCATABLE needs to be enabled\n", __func__);
+			result = EFAILED;
+			goto exit;
+		}
+	}
+
 	/* Find and process the loongarch image header. */
 	for (i = 0; i < ehdr.e_phnum; i++) {
 		struct mem_phdr *phdr = &ehdr.e_phdr[i];
diff --git a/kexec/arch/loongarch/kexec-pei-loongarch.c b/kexec/arch/loongarch/kexec-pei-loongarch.c
index f0e0d09..1a19a39 100644
--- a/kexec/arch/loongarch/kexec-pei-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pei-loongarch.c
@@ -99,6 +99,9 @@ int pei_loongarch_load(int argc, char **argv, const char *buf,
 		}
 	}
 
+	/* Fixme: Loading a non-relocation kernel will cause the second kernel to fail
+	   to start in KEXEC_ON_CRASH */
+
 	/* Load the kernel */
 	add_segment(info, buf, len, kernel_segment, loongarch_mem.image_size);
 
-- 
2.34.1



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

* [PATCH v2 05/10] LoongArch: Change initrd allocation to top-down
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (3 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 04/10] LoongArch: Enforce relocatable kernel check for crash dump Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 06/10] LoongArch: Fix the use of loongarch_image_header in ELF format Youling Tang
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman
  Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang,
	Chenghao Duan

From: Youling Tang <tangyouling@kylinos.cn>

When loading a secondary kernel with KASLR enabled, the randomized relocation
of the kernel image may overlap the initrd memory region, causing data corruption
and boot failure. This occurs because KASLR dynamically shifts the kernel's load
address, which could accidentally target the initrd's pre-allocated space.

Modify the initrd allocation strategy from bottom-up to top-down within the
reserved memory region. By placing the initrd at higher addresses first, we
minimize the risk of the relocated kernel overwriting it.

Reported-by: Chenghao Duan <duanchenghao@kylinos.cn>
Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-loongarch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kexec/arch/loongarch/kexec-loongarch.c b/kexec/arch/loongarch/kexec-loongarch.c
index c335e0b..32cd484 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -317,7 +317,7 @@ int loongarch_load_other_segments(struct kexec_info *info, unsigned long hole_mi
 		initrd_base = add_buffer(info, initrd_buf, initrd_size,
 					initrd_size, sizeof(void *),
 					_ALIGN_UP(initrd_min,
-						pagesize), hole_max, 1);
+						pagesize), hole_max, -1);
 		dbgprintf("initrd_base: %lx, initrd_size: %lx\n", initrd_base, initrd_size);
 
 		cmdline_add_initrd(cmdline, initrd_base, initrd_size);
-- 
2.34.1



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

* [PATCH v2 06/10] LoongArch: Fix the use of loongarch_image_header in ELF format
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (4 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 05/10] LoongArch: Change initrd allocation to top-down Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 07/10] LoongArch: Add pe_hdr->machine check for pei format images Youling Tang
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

loongarch_image_header should not be used in the ELF format. When the CONFIG_EFI_STUB
configuration is turned off, the ELF kernel image will not add loongarch_image_heade
processing, as can be seen from the kernel code arch/loongarch/kernel/head.S.

Thus, use the elf header content to fix this issue.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/image-header.h        |  2 +-
 kexec/arch/loongarch/kexec-elf-loongarch.c | 12 ++++--------
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/kexec/arch/loongarch/image-header.h b/kexec/arch/loongarch/image-header.h
index 223d81f..0db7615 100644
--- a/kexec/arch/loongarch/image-header.h
+++ b/kexec/arch/loongarch/image-header.h
@@ -1,5 +1,5 @@
 /*
- * LoongArch binary image header.
+ * LoongArch binary image header. (for pei/pez format)
  */
 
 #if !defined(__LOONGARCH_IMAGE_HEADER_H)
diff --git a/kexec/arch/loongarch/kexec-elf-loongarch.c b/kexec/arch/loongarch/kexec-elf-loongarch.c
index 44b63a9..92fa9f8 100644
--- a/kexec/arch/loongarch/kexec-elf-loongarch.c
+++ b/kexec/arch/loongarch/kexec-elf-loongarch.c
@@ -72,7 +72,6 @@ static bool laabs_section(const struct mem_ehdr *ehdr)
 int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
 	off_t kernel_size, struct kexec_info *info)
 {
-	const struct loongarch_image_header *header = NULL;
 	unsigned long kernel_segment;
 	struct mem_ehdr ehdr;
 	int result;
@@ -95,22 +94,19 @@ int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
 		}
 	}
 
-	/* Find and process the loongarch image header. */
 	for (i = 0; i < ehdr.e_phnum; i++) {
 		struct mem_phdr *phdr = &ehdr.e_phdr[i];
 
 		if (phdr->p_type != PT_LOAD)
 			continue;
 
-		header = (const struct loongarch_image_header *)(
-			kernel_buf + phdr->p_offset);
-
-		if (!loongarch_process_image_header(header))
-			break;
+		loongarch_mem.text_offset = virt_to_phys(phdr->p_paddr);
+		loongarch_mem.image_size = _ALIGN_UP(phdr->p_memsz, KiB(64));
+		break;
 	}
 
 	if (i == ehdr.e_phnum) {
-		dbgprintf("%s: Valid loongarch image header not found\n", __func__);
+		dbgprintf("%s: Valid loongarch phdr not found\n", __func__);
 		result = EFAILED;
 		goto exit;
 	}
-- 
2.34.1



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

* [PATCH v2 07/10] LoongArch: Add pe_hdr->machine check for pei format images
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (5 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 06/10] LoongArch: Fix the use of loongarch_image_header in ELF format Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 08/10] LoongArch/pez: Fix kernel_fd handling when kexec_file is supported Youling Tang
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

Add the pe_hdr->machine check to ensure that the kernel image is of the 64-bit
LoongArch architecture.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/image-header.h        |  3 +++
 kexec/arch/loongarch/kexec-pei-loongarch.c | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/kexec/arch/loongarch/image-header.h b/kexec/arch/loongarch/image-header.h
index 0db7615..fc71ff2 100644
--- a/kexec/arch/loongarch/image-header.h
+++ b/kexec/arch/loongarch/image-header.h
@@ -35,6 +35,9 @@ struct loongarch_image_header {
 static const uint8_t loongarch_image_pe_sig[2] = {'M', 'Z'};
 static const uint8_t loongarch_pe_machtype[6] = {'P','E', 0x0, 0x0, 0x64, 0x62};
 
+#define IMAGE_FILE_MACHINE_LOONGARCH64  0x6264 /* LoongArch 64-bit processor family */
+
+
 /**
  * loongarch_header_check_pe_sig - Helper to check the loongarch image header.
  *
diff --git a/kexec/arch/loongarch/kexec-pei-loongarch.c b/kexec/arch/loongarch/kexec-pei-loongarch.c
index 1a19a39..e0a82b6 100644
--- a/kexec/arch/loongarch/kexec-pei-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pei-loongarch.c
@@ -24,9 +24,20 @@
 #include "kexec-loongarch.h"
 #include "arch/options.h"
 
+#include <pe.h>
+
+static inline int loongarch_pe_check_machine(const struct pe_hdr *pe_hdr)
+{
+	if (!pe_hdr)
+		return 0;
+
+	return (pe_hdr->machine == IMAGE_FILE_MACHINE_LOONGARCH64);
+}
+
 int pei_loongarch_probe(const char *kernel_buf, off_t kernel_size)
 {
 	const struct loongarch_image_header *h;
+	const struct pe_hdr *pe_hdr;
 
 	if (kernel_size < sizeof(struct loongarch_image_header)) {
 		dbgprintf("%s: No loongarch image header.\n", __func__);
@@ -40,6 +51,12 @@ int pei_loongarch_probe(const char *kernel_buf, off_t kernel_size)
 		return -1;
 	}
 
+	pe_hdr = (const struct pe_hdr *)(kernel_buf + get_pehdr_offset(kernel_buf));
+	if (!loongarch_pe_check_machine(pe_hdr)) {
+		dbgprintf("%s: Bad loongarch pe_hdr machine.\n", __func__);
+		return -1;
+	}
+
 	return 0;
 }
 
-- 
2.34.1



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

* [PATCH v2 08/10] LoongArch/pez: Fix kernel_fd handling when kexec_file is supported
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (6 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 07/10] LoongArch: Add pe_hdr->machine check for pei format images Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 09/10] LoongArch: Add kexec_file_load syscall Youling Tang
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

After the current pez kernel image is decompressed and loaded, this kernel_fd
will be released. When kexec_file is added subsequently, the kernel detects
that the content in kernel_fd is empty, causing the kernel loading to fail.
Therefore, kernel_fd is rewritten for processing.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-pez-loongarch.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/kexec/arch/loongarch/kexec-pez-loongarch.c b/kexec/arch/loongarch/kexec-pez-loongarch.c
index 942a47c..e78909c 100644
--- a/kexec/arch/loongarch/kexec-pez-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pez-loongarch.c
@@ -64,13 +64,17 @@ int pez_loongarch_load(int argc, char **argv, const char *buf, off_t len,
 	if (kernel_fd > 0 && decompressed_size > 0) {
 		char *kbuf;
 		off_t nread;
+		int fd;
 
+		if (info->kernel_fd > 0)
+			close(info->kernel_fd);
 		info->kernel_fd = kernel_fd;
-		/*
-		 * slurp_fd will close kernel_fd, but it is safe here
-		 * due to no kexec_file_load support.
-		 */
-		kbuf = slurp_fd(kernel_fd, NULL, decompressed_size, &nread);
+		fd = dup(kernel_fd);
+		if (fd < 0) {
+			dbgprintf("%s: dup fd failed.\n", __func__);
+			return -1;
+		}
+		kbuf = slurp_fd(fd, NULL, decompressed_size, &nread);
 		if (!kbuf || nread != decompressed_size) {
 			dbgprintf("%s: slurp_fd failed.\n", __func__);
 			return -1;
-- 
2.34.1



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

* [PATCH v2 09/10] LoongArch: Add kexec_file_load syscall
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (7 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 08/10] LoongArch/pez: Fix kernel_fd handling when kexec_file is supported Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-16  1:46 ` [PATCH v2 10/10] LoongArch: Remove 'kexec_file' cmdline parameters when using --reuse-cmdline option Youling Tang
  2025-09-23 16:51 ` [PATCH v2 00/10] Fix and improve the LoongArch implementation Simon Horman
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

Create prepare_kexec_file_options() function to prepare the options
to kexec_file_load syscall, and it would be used in pei_loongarch_load().

Currently, pez(vmlinuz.efi), pei(vmlinux.efi) and elf(vmlinux) format
images are supported.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-elf-loongarch.c |  3 ++
 kexec/arch/loongarch/kexec-loongarch.c     | 39 ++++++++++++++++++++++
 kexec/arch/loongarch/kexec-loongarch.h     |  1 +
 kexec/arch/loongarch/kexec-pei-loongarch.c |  3 ++
 kexec/kexec-syscall.h                      |  5 ++-
 5 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/kexec/arch/loongarch/kexec-elf-loongarch.c b/kexec/arch/loongarch/kexec-elf-loongarch.c
index 92fa9f8..1cc6212 100644
--- a/kexec/arch/loongarch/kexec-elf-loongarch.c
+++ b/kexec/arch/loongarch/kexec-elf-loongarch.c
@@ -77,6 +77,9 @@ int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
 	int result;
 	int i;
 
+	if (info->file_mode)
+		return prepare_kexec_file_options(info);
+
 	result = build_elf_exec_info(kernel_buf, kernel_size, &ehdr, 0);
 
 	if (result < 0) {
diff --git a/kexec/arch/loongarch/kexec-loongarch.c b/kexec/arch/loongarch/kexec-loongarch.c
index 32cd484..eb60ef0 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -10,12 +10,14 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
 #include <sys/stat.h>
 #include <linux/elf-em.h>
 #include <elf.h>
@@ -29,6 +31,10 @@
 #include "mem_regions.h"
 #include "arch/options.h"
 
+#ifndef _O_BINARY
+#define _O_BINARY 0
+#endif
+
 #define CMDLINE_PREFIX "kexec "
 static char cmdline[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;
 
@@ -201,6 +207,39 @@ struct arch_options_t arch_options = {
 	.core_header_type = CORE_TYPE_ELF64,
 };
 
+int prepare_kexec_file_options(struct kexec_info *info)
+{
+	int fd;
+	ssize_t result;
+	struct stat stats;
+
+	if (arch_options.command_line) {
+		info->command_line = (char *)arch_options.command_line;
+		info->command_line_len = strlen(info->command_line) + 1;
+	}
+
+	if (!arch_options.initrd_file) {
+		info->initrd_fd = -1;
+		return 0;
+	}
+
+	fd = open(arch_options.initrd_file, O_RDONLY | _O_BINARY);
+	if (fd < 0) {
+		fprintf(stderr, "Cannot open `%s': %s\n", arch_options.initrd_file,
+				strerror(errno));
+		return -EINVAL;
+	}
+	result = fstat(fd, &stats);
+	if (result < 0) {
+		close(fd);
+		fprintf(stderr, "Cannot stat: %s: %s\n", arch_options.initrd_file,
+				strerror(errno));
+		return -EINVAL;
+	}
+	info->initrd_fd = fd;
+	return 0;
+}
+
 int arch_process_options(int argc, char **argv)
 {
 	static const char short_options[] = KEXEC_ARCH_OPT_STR "";
diff --git a/kexec/arch/loongarch/kexec-loongarch.h b/kexec/arch/loongarch/kexec-loongarch.h
index 2c7624f..2bffa47 100644
--- a/kexec/arch/loongarch/kexec-loongarch.h
+++ b/kexec/arch/loongarch/kexec-loongarch.h
@@ -37,6 +37,7 @@ int loongarch_process_image_header(const struct loongarch_image_header *h);
 unsigned long loongarch_locate_kernel_segment(struct kexec_info *info);
 int loongarch_load_other_segments(struct kexec_info *info,
 	unsigned long hole_min);
+int prepare_kexec_file_options(struct kexec_info *info);
 
 struct arch_options_t {
 	char *command_line;
diff --git a/kexec/arch/loongarch/kexec-pei-loongarch.c b/kexec/arch/loongarch/kexec-pei-loongarch.c
index e0a82b6..10f79c1 100644
--- a/kexec/arch/loongarch/kexec-pei-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pei-loongarch.c
@@ -70,6 +70,9 @@ int pei_loongarch_load(int argc, char **argv, const char *buf,
 
 	header = (const struct loongarch_image_header *)(buf);
 
+	if (info->file_mode)
+		return prepare_kexec_file_options(info);
+
 	if (loongarch_process_image_header(header))
 		return EFAILED;
 
diff --git a/kexec/kexec-syscall.h b/kexec/kexec-syscall.h
index 9b17578..e9bb7de 100644
--- a/kexec/kexec-syscall.h
+++ b/kexec/kexec-syscall.h
@@ -59,7 +59,7 @@
 #endif
 #endif /*ifndef __NR_kexec_load*/
 
-#if defined(__arm__) || defined(__loongarch__)
+#if defined(__arm__)
 #undef __NR_kexec_file_load
 #endif
 
@@ -83,6 +83,9 @@
 #if defined(__riscv__) || defined(__riscv)
 #define __NR_kexec_file_load	294
 #endif
+#ifdef __loongarch__
+#define __NR_kexec_file_load	294
+#endif
 
 #ifndef __NR_kexec_file_load
 /* system call not available for the arch */
-- 
2.34.1



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

* [PATCH v2 10/10] LoongArch: Remove 'kexec_file' cmdline parameters when using --reuse-cmdline option
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (8 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 09/10] LoongArch: Add kexec_file_load syscall Youling Tang
@ 2025-09-16  1:46 ` Youling Tang
  2025-09-23 16:51 ` [PATCH v2 00/10] Fix and improve the LoongArch implementation Simon Horman
  10 siblings, 0 replies; 12+ messages in thread
From: Youling Tang @ 2025-09-16  1:46 UTC (permalink / raw)
  To: Simon Horman; +Cc: Simon Horman, Huacai Chen, kexec, youling.tang, Youling Tang

From: Youling Tang <tangyouling@kylinos.cn>

On the LoongArch architecture, when loading the kernel via kexec_file,
the 'kexec_file'prefix is automatically appended to the command line.
Consequently, if the '--reuse-cmdline' option is used to inherit the
current command line, this prefix must be detected and removed.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 kexec/arch/loongarch/kexec-loongarch.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kexec/arch/loongarch/kexec-loongarch.c b/kexec/arch/loongarch/kexec-loongarch.c
index eb60ef0..240202f 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -260,6 +260,7 @@ int arch_process_options(int argc, char **argv)
 		case OPT_REUSE_CMDLINE:
 			cmdline = get_command_line();
 			remove_parameter(cmdline, "kexec");
+			remove_parameter(cmdline, "kexec_file");
 			remove_parameter(cmdline, "initrd");
 			break;
 		case OPT_INITRD:
-- 
2.34.1



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

* Re: [PATCH v2 00/10] Fix and improve the LoongArch implementation
  2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
                   ` (9 preceding siblings ...)
  2025-09-16  1:46 ` [PATCH v2 10/10] LoongArch: Remove 'kexec_file' cmdline parameters when using --reuse-cmdline option Youling Tang
@ 2025-09-23 16:51 ` Simon Horman
  10 siblings, 0 replies; 12+ messages in thread
From: Simon Horman @ 2025-09-23 16:51 UTC (permalink / raw)
  To: Youling Tang; +Cc: Huacai Chen, kexec, Youling Tang

On Tue, Sep 16, 2025 at 09:46:45AM +0800, Youling Tang wrote:
> From: Youling Tang <tangyouling@kylinos.cn>
> 
> - Add kexec_file support for LoongArch.
> - Fix and improve the LoongArch implementation.
> - Fixed the handling of crashkernel parameters when using --reuse-cmdline.
> 
> Kernel support for kexec_file was introduced by the patch detailed in Link [1].
> 
> Link[1]:
> https://lore.kernel.org/loongarch/20250903030100.196744-1-youling.tang@linux.dev/T/#t
> 
> Changelog:
> v2:
>  - Add kexec_file support for LoongArch.
>  - Correct email address and Signed-off-by.
> 
> Youling Tang (10):
>   kexec: Handle removal of multiple 'crashkernel' parameters
>   LoongArch: Fix comments
>   LoongArch: Terminate the cmdline string using '\0'
>   LoongArch: Enforce relocatable kernel check for crash dump
>   LoongArch: Change initrd allocation to top-down
>   LoongArch: Fix the use of loongarch_image_header in ELF format
>   LoongArch: Add pe_hdr->machine check for pei format images
>   LoongArch/pez: Fix kernel_fd handling when kexec_file is supported
>   LoongArch: Add kexec_file_load syscall
>   LoongArch: Remove 'kexec_file' cmdline parameters when using
>     --reuse-cmdline option

Thanks, applied.

- LoongArch: Remove 'kexec_file' cmdline parameters when using --reuse-cmdline option
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=19d1b275c649
- LoongArch: Add kexec_file_load syscall
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=f1d66280a48b
- LoongArch/pez: Fix kernel_fd handling when kexec_file is supported
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=b7c35c42c3db
- LoongArch: Add pe_hdr->machine check for pei format images
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=13e701e72ecf
- LoongArch: Fix the use of loongarch_image_header in ELF format
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=919005b26c8f
- LoongArch: Change initrd allocation to top-down
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=2d3f8185f5d7
- LoongArch: Enforce relocatable kernel check for crash dump
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=024073e74dc7
- LoongArch: Terminate the cmdline string using '\0'
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=d9c82c188646
- LoongArch: Fix comments
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=e2bfd6df147c
- kexec: Handle removal of multiple 'crashkernel' parameters
  https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/commit/?id=39ba2bd288af


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

end of thread, other threads:[~2025-09-23 16:51 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-16  1:46 [PATCH v2 00/10] Fix and improve the LoongArch implementation Youling Tang
2025-09-16  1:46 ` [PATCH v2 01/10] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
2025-09-16  1:46 ` [PATCH v2 02/10] LoongArch: Fix comments Youling Tang
2025-09-16  1:46 ` [PATCH v2 03/10] LoongArch: Terminate the cmdline string using '\0' Youling Tang
2025-09-16  1:46 ` [PATCH v2 04/10] LoongArch: Enforce relocatable kernel check for crash dump Youling Tang
2025-09-16  1:46 ` [PATCH v2 05/10] LoongArch: Change initrd allocation to top-down Youling Tang
2025-09-16  1:46 ` [PATCH v2 06/10] LoongArch: Fix the use of loongarch_image_header in ELF format Youling Tang
2025-09-16  1:46 ` [PATCH v2 07/10] LoongArch: Add pe_hdr->machine check for pei format images Youling Tang
2025-09-16  1:46 ` [PATCH v2 08/10] LoongArch/pez: Fix kernel_fd handling when kexec_file is supported Youling Tang
2025-09-16  1:46 ` [PATCH v2 09/10] LoongArch: Add kexec_file_load syscall Youling Tang
2025-09-16  1:46 ` [PATCH v2 10/10] LoongArch: Remove 'kexec_file' cmdline parameters when using --reuse-cmdline option Youling Tang
2025-09-23 16:51 ` [PATCH v2 00/10] Fix and improve the LoongArch implementation Simon Horman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.