* [PATCH 1/7] kexec: Handle removal of multiple 'crashkernel' parameters
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:10 ` [PATCH 2/7] LoongArch: Fix comments Youling Tang
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, youling.tang, Youling Tang
From: Youling Tang <tangyouling@kylinios.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@kylinios.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] 9+ messages in thread* [PATCH 2/7] LoongArch: Fix comments
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
2025-08-29 9:10 ` [PATCH 1/7] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:10 ` [PATCH 3/7] LoongArch: Terminate the cmdline string using '\0' Youling Tang
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, youling.tang, Youling Tang
From: Youling Tang <tangyouling@kylinios.cn>
loongarch_load_other_segments() mainly loads initrd and cmdline. Fix this
annotation.
Signed-off-by: Youling Tang <tangyouling@kylinios.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] 9+ messages in thread* [PATCH 3/7] LoongArch: Terminate the cmdline string using '\0'
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
2025-08-29 9:10 ` [PATCH 1/7] kexec: Handle removal of multiple 'crashkernel' parameters Youling Tang
2025-08-29 9:10 ` [PATCH 2/7] LoongArch: Fix comments Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:10 ` [PATCH 4/7] LoongArch: Enforce relocatable kernel check for crash dump Youling Tang
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, youling.tang, Youling Tang
From: Youling Tang <tangyouling@kylinios.cn>
Terminate the cmdline string using '\0' instead of 0, improve code readability.
Signed-off-by: Youling Tang <tangyouling@kylinios.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] 9+ messages in thread* [PATCH 4/7] LoongArch: Enforce relocatable kernel check for crash dump
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
` (2 preceding siblings ...)
2025-08-29 9:10 ` [PATCH 3/7] LoongArch: Terminate the cmdline string using '\0' Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:10 ` [PATCH 5/7] LoongArch: Change initrd allocation to top-down Youling Tang
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, youling.tang, Youling Tang
From: Youling Tang <tangyouling@kylinios.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@kylinios.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] 9+ messages in thread* [PATCH 5/7] LoongArch: Change initrd allocation to top-down
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
` (3 preceding siblings ...)
2025-08-29 9:10 ` [PATCH 4/7] LoongArch: Enforce relocatable kernel check for crash dump Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:10 ` [PATCH 6/7] LoongArch: Fix the use of loongarch_image_header in ELF format Youling Tang
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman
Cc: kexec, Huacai Chen, youling.tang, Youling Tang, Chenghao Duan
From: Youling Tang <tangyouling@kylinios.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@kylinios.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] 9+ messages in thread* [PATCH 6/7] LoongArch: Fix the use of loongarch_image_header in ELF format
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
` (4 preceding siblings ...)
2025-08-29 9:10 ` [PATCH 5/7] LoongArch: Change initrd allocation to top-down Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:10 ` [PATCH 7/7] LoongArch: Add pe_hdr->machine check for pei format images Youling Tang
2025-08-29 9:19 ` [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, youling.tang, Youling Tang
From: Youling Tang <tangyouling@kylinios.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@kylinios.cn>
---
| 2 +-
kexec/arch/loongarch/kexec-elf-loongarch.c | 12 ++++--------
2 files changed, 5 insertions(+), 9 deletions(-)
--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] 9+ messages in thread* [PATCH 7/7] LoongArch: Add pe_hdr->machine check for pei format images
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
` (5 preceding siblings ...)
2025-08-29 9:10 ` [PATCH 6/7] LoongArch: Fix the use of loongarch_image_header in ELF format Youling Tang
@ 2025-08-29 9:10 ` Youling Tang
2025-08-29 9:19 ` [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:10 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, youling.tang, Youling Tang
From: Youling Tang <tangyouling@kylinios.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@kylinios.cn>
---
| 3 +++
kexec/arch/loongarch/kexec-pei-loongarch.c | 17 +++++++++++++++++
2 files changed, 20 insertions(+)
--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] 9+ messages in thread* Re: [PATCH 0/7] Fix and improve the LoongArch implementation
2025-08-29 9:10 [PATCH 0/7] Fix and improve the LoongArch implementation Youling Tang
` (6 preceding siblings ...)
2025-08-29 9:10 ` [PATCH 7/7] LoongArch: Add pe_hdr->machine check for pei format images Youling Tang
@ 2025-08-29 9:19 ` Youling Tang
7 siblings, 0 replies; 9+ messages in thread
From: Youling Tang @ 2025-08-29 9:19 UTC (permalink / raw)
To: Simon Horman; +Cc: kexec, Huacai Chen, Youling Tang
My oversight, email and Singed-off-by error will be updated to
"Youling Tang <tangyouling@kylinos.cn>" in the next version.
Youling.
On 2025/8/29 17:10, Youling Tang wrote:
> From: Youling Tang <tangyouling@kylinos.cn>
>
> - Fix and improve the LoongArch implementation.
> - Fixed the handling of crashkernel parameters when using --reuse-cmdline.
>
> Youling Tang (7):
> 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
>
> kexec/arch/loongarch/image-header.h | 5 ++-
> kexec/arch/loongarch/kexec-elf-loongarch.c | 44 ++++++++++++++++++----
> kexec/arch/loongarch/kexec-loongarch.c | 4 +-
> kexec/arch/loongarch/kexec-pei-loongarch.c | 24 +++++++++++-
> kexec/kexec.c | 6 +++
> 5 files changed, 70 insertions(+), 13 deletions(-)
>
^ permalink raw reply [flat|nested] 9+ messages in thread