Fix and improve the LoongArch implementation
Subsystems (2)
cover Fix and improve the LoongArch implementation
From: Youling Tang <[email protected]>
- 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(-)
Signature · 1 line
2.34.1
PATCH 1/7 kexec: Handle removal of multiple 'crashkernel' parameters
From: Youling Tang <[email protected]>
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.
--- kexec/kexec.c | 6 ++++++ 1 file changed, 6 insertions(+)
kexec/kexec.c +6 -0
@@ -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)
Signature · 1 line
2.34.1
PATCH 2/7 LoongArch: Fix comments
In reply to Youling TangFrom: Youling Tang <[email protected]>
loongarch_load_other_segments() mainly loads initrd and cmdline. Fix this annotation.
--- kexec/arch/loongarch/kexec-pei-loongarch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kexec/arch/loongarch/kexec-pei-loongarch.c +2 -2
@@ -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; }
Signature · 1 line
2.34.1
PATCH 3/7 LoongArch: Terminate the cmdline string using '\0'
In reply to Youling TangFrom: Youling Tang <[email protected]>
Terminate the cmdline string using '\0' instead of 0, improve code readability.
--- kexec/arch/loongarch/kexec-loongarch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kexec/arch/loongarch/kexec-loongarch.c +1 -1
@@ -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 );
Signature · 1 line
2.34.1
PATCH 4/7 LoongArch: Enforce relocatable kernel check for crash dump
In reply to Youling TangFrom: Youling Tang <[email protected]>
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 RELOCATABLEThe 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.
--- kexec/arch/loongarch/kexec-elf-loongarch.c | 32 ++++++++++++++++++++++ kexec/arch/loongarch/kexec-pei-loongarch.c | 3 ++ 2 files changed, 35 insertions(+)
kexec/arch/loongarch/kexec-elf-loongarch.c +32 -0
@@ -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];
kexec/arch/loongarch/kexec-pei-loongarch.c +3 -0
@@ -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);
Signature · 1 line
2.34.1
PATCH 5/7 LoongArch: Change initrd allocation to top-down
In reply to Youling TangFrom: Youling Tang <[email protected]>
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.
--- kexec/arch/loongarch/kexec-loongarch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kexec/arch/loongarch/kexec-loongarch.c +1 -1
@@ -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);
Signature · 1 line
2.34.1
PATCH 6/7 LoongArch: Fix the use of loongarch_image_header in ELF format
In reply to Youling TangFrom: Youling Tang <[email protected]>
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.
--- kexec/arch/loongarch/image-header.h | 2 +- kexec/arch/loongarch/kexec-elf-loongarch.c | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-)
kexec/arch/loongarch/image-header.h +1 -1
@@ -1,5 +1,5 @@/* -* LoongArch binary image header. +* LoongArch binary image header. (for pei/pez format) */ #if !defined(__LOONGARCH_IMAGE_HEADER_H)
kexec/arch/loongarch/kexec-elf-loongarch.c +4 -8
@@ -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; }
Signature · 1 line
2.34.1
PATCH 7/7 LoongArch: Add pe_hdr->machine check for pei format images
In reply to Youling TangFrom: Youling Tang <[email protected]>
Add the pe_hdr->machine check to ensure that the kernel image is of the 64-bit LoongArch architecture.
--- kexec/arch/loongarch/image-header.h | 3 +++ kexec/arch/loongarch/kexec-pei-loongarch.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+)
kexec/arch/loongarch/image-header.h +3 -0
@@ -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. *
kexec/arch/loongarch/kexec-pei-loongarch.c +17 -0
@@ -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 ; }
Signature · 1 line
2.34.1
My oversight, email and Singed-off-by error will be updated to "Youling Tang <[email protected]>" in the next version.
Youling.
Quoted 21 lines · Youling Tang
From: Youling Tang <[email protected]>
- 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 ofloongarch_image_headerin ELF format
LoongArch: Addpe_hdr->machinecheck for pei format imageskexec/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(-)