Skip to contentSkip to conversation

LoongArch: Add kexec/kdump support

PATCH Superseded Youling Tang 2 participants 6 messages Sep 15, 2022 – Sep 22, 2022

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover LoongArch: Add kexec/kdump support

This patch series to support kexec/kdump (only 64bit).

Kexec is a system call that enables you to load and boot into another kernel from the currently running kernel. This is useful for kernel developers or other people who need to reboot very quickly without waiting for the whole BIOS boot process to finish.

Kdump uses kexec to quickly boot to a dump-capture kernel whenever a dump of the system kernel's memory needs to be taken (for example, when the system panics). The system kernel's memory image is preserved across the reboot and is accessible to the dump-capture kernel.

For details, see Documentation/admin-guide/kdump/kdump.rst.

User tools kexec-tools see link [1].

TODO:
Currently kdump does not support the same binary image, the production kernel and the capture kernel will be generated with different configurations. I will support kernel relocation support in the near future. Then will go to implement the same binary support based on kernel relocation support.

[1] Link: https://github.com/tangyouling/kexec-tools

Changes in v3:
- Adjust the PE header (note that kexec-tools needs to be updated).
- Add ibar in kexec_reboot().
- boot_flag is replaced by efi_boot.
- Adjust do_kexec parameter passing order.
- Adjust the order of static variables to be consistent with do_kexec.
- Remove a-series register save.
- Some comments and register usage modification.
- Add the opening and closing of the cpu core state.
- Add a call to cpu_device_up to turn it on when the cpu core state is offline.

Changes in v2:
- Add ibar.
- Access via IOCSR.
- Remove the settings of the tp, sp registers.
- Remove the crash.c file and merge the relevant code into machine_kexec.c.
- Adjust the use of CONFIG_SMP macro to avoid build errors under !SMP configuration.
- Change the default value of PHYSICAL_START of the capture kernel to 0x90000000a0000000.

Youling Tang (3):
LoongArch: Add kexec support
LoongArch: Add kdump support
LoongArch: Enable CONFIG_KEXEC

 arch/loongarch/Kconfig                     |  33 +++
 arch/loongarch/Makefile                    |   4 +
 arch/loongarch/configs/loongson3_defconfig |   1 +
 arch/loongarch/include/asm/kexec.h         |  58 ++++
 arch/loongarch/kernel/Makefile             |   3 +
 arch/loongarch/kernel/crash_dump.c         |  19 ++
 arch/loongarch/kernel/head.S               |   6 +-
 arch/loongarch/kernel/machine_kexec.c      | 295 +++++++++++++++++++++
 arch/loongarch/kernel/mem.c                |   6 +
 arch/loongarch/kernel/relocate_kernel.S    | 111 ++++++++
 arch/loongarch/kernel/setup.c              |  49 ++++
 arch/loongarch/kernel/traps.c              |   4 +
 12 files changed, 588 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/crash_dump.c
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S
Signature · 1 line
2.36.0
#1 lore raw

PATCH v3 1/3 LoongArch: Add kexec support

Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to the LoongArch architecture that add support for the kexec re-boot mechanis (CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in PE format.

I tested this on LoongArch 3A5000 machine and works as expected,

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e
---
 arch/loongarch/Kconfig                  |  11 ++
 arch/loongarch/include/asm/kexec.h      |  58 +++++++
 arch/loongarch/kernel/Makefile          |   2 +
 arch/loongarch/kernel/head.S            |   6 +-
 arch/loongarch/kernel/machine_kexec.c   | 209 ++++++++++++++++++++++++
 arch/loongarch/kernel/relocate_kernel.S | 105 ++++++++++++
 6 files changed, 390 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S
arch/loongarch/Kconfig +11 -0
index 9b1f2ab878e9..08e063aaf847 100644
@@ -422,6 +422,17 @@ config ARCH_IOREMAP
 	  protection support. However, you can enable LoongArch DMW-based
 	  ioremap() for better performance.
 
+config KEXEC
+	bool "Kexec system call"
+	select KEXEC_CORE
+	help
+	  kexec is a system call that implements the ability to shutdown your
+	  current kernel, and to start another kernel.  It is like a reboot
+	  but it is independent of the system firmware.   And like a reboot
+	  you can start any kernel with it, not just Linux.
+
+	  The name comes from the similarity to the exec system call.
+
 config SECCOMP
 	bool "Enable seccomp to safely compute untrusted bytecode"
 	depends on PROC_FS
arch/loongarch/include/asm/kexec.h new file +58 -0

58 lines not on this page · raw message

arch/loongarch/kernel/Makefile +2 -0
index 7225916dd378..17dc8ce6b5ce 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU)	+= fpu.o
 obj-$(CONFIG_MODULES)		+= module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
 
+obj-$(CONFIG_KEXEC)             += machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)		+= proc.o
 
 obj-$(CONFIG_SMP)		+= smp.o
arch/loongarch/kernel/head.S +5 -1
index eb3f641d5915..c0d61b29fcaa 100644
@@ -20,7 +20,11 @@
 
 _head:
 	.word	MZ_MAGIC		/* "MZ", MS-DOS header */
-	.org	0x3c			/* 0x04 ~ 0x3b reserved */
+	.org	0x8
+	.dword	kernel_entry		/* Kernel entry point */
+	.dword	_end - _text		/* Effective size of kernel image */
+	.quad	0			/* Image load offset from start of RAM */
+	.org	0x3c			/* 0x20 ~ 0x3b reserved */
 	.long	pe_header - _head	/* Offset to the PE header */
 
 pe_header:
arch/loongarch/kernel/machine_kexec.c new file +209 -0

209 lines not on this page · raw message

arch/loongarch/kernel/relocate_kernel.S new file +105 -0

105 lines not on this page · raw message

Signature · 7 lines
2.36.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#2 lore raw

Re: [PATCH v3 1/3] LoongArch: Add kexec support

arch/loongarch/kernel/machine_kexec.c line 64

Quoted 10 lines · Youling Tang
arch/loongarch/kernel/machine_kexec.c new file +10 -0
+int machine_kexec_prepare(struct kimage *kimage)
+{
+       int i;
+       void *dtb = (void *)KEXEC_BLOB_ADDR;
+
+       kexec_image_info(kimage);
+
+       /* Find the Flattened Device Tree */
+       for (i = 0; i < kimage->nr_segments; i++) {
+               if (!fdt_check_header(kimage->segment[i].buf)) {

Hi Youling,

When I build Huacai's tree
(https://github.com/loongson/linux/commits/loongarch-next), there is a strange error linking vmlinux:

ld: arch/loongarch/kernel/machine_kexec.o: in function `machine_kexec_prepare':
/home/xry111/git-repos/linux/arch/loongarch/kernel/machine_kexec.c:70: undefined reference to `fdt_check_header'

Not sure why...

Signature · 2 lines
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University
#3 lore raw

Re: [PATCH v3 1/3] LoongArch: Add kexec support

Hi, Ruoyao

Quoted 14 lines · Xi Ruoyao

arch/loongarch/kernel/machine_kexec.c line 64

Quoted 10 lines · Youling Tang
arch/loongarch/kernel/machine_kexec.c new file +10 -0
+int machine_kexec_prepare(struct kimage *kimage)
+{
+       int i;
+       void *dtb = (void *)KEXEC_BLOB_ADDR;
+
+       kexec_image_info(kimage);
+
+       /* Find the Flattened Device Tree */
+       for (i = 0; i < kimage->nr_segments; i++) {
+               if (!fdt_check_header(kimage->segment[i].buf)) {

Hi Youling,

Quoted 8 lines

When I build Huacai's tree
(https://github.com/loongson/linux/commits/loongarch-next), there is a strange error linking vmlinux:

ld: arch/loongarch/kernel/machine_kexec.o: in function `machine_kexec_prepare':
/home/xry111/git-repos/linux/arch/loongarch/kernel/machine_kexec.c:70: undefined reference to `fdt_check_header'

Not sure why...

This is because of the problem after efistub removes the fdt modification, I will reimplement kexec/kdump. This build error can be avoided by turning off CONFIG_KEXEC temporarily, tomorrow I will submit a new kexec/kdump implementation.

Thanks,
Youling.

Quoted 1 line

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#4 lore raw

PATCH v3 2/3 LoongArch: Add kdump support

In reply to Youling Tang

This patch adds support for kdump, the kernel will reserve a region for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a separate region for the core's ELF header within crash kdump kernel memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new device-tree property, "linux,elfcorehdr", and crash dump kernel preserves the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the crashkernel area via a new device-tree property, "linux, usable-memory-range", so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory with copy_oldmem_page().

I tested this on LoongArch 3A5000 machine and works as expected (Suggest crashkernel parameter is "crashkernel=512M@2560M"), you may test it by triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger
---
 arch/loongarch/Kconfig                  | 22 ++++++
 arch/loongarch/Makefile                 |  4 +
 arch/loongarch/kernel/Makefile          |  1 +
 arch/loongarch/kernel/crash_dump.c      | 19 +++++
 arch/loongarch/kernel/machine_kexec.c   | 98 +++++++++++++++++++++++--
 arch/loongarch/kernel/mem.c             |  6 ++
 arch/loongarch/kernel/relocate_kernel.S |  6 ++
 arch/loongarch/kernel/setup.c           | 49 +++++++++++++
 arch/loongarch/kernel/traps.c           |  4 +
 9 files changed, 203 insertions(+), 6 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash_dump.c
arch/loongarch/Kconfig +22 -0
index 08e063aaf847..4eeeebf888c1 100644
@@ -433,6 +433,28 @@ config KEXEC
 
 	  The name comes from the similarity to the exec system call.
 
+config CRASH_DUMP
+	bool "Build kdump crash kernel"
+	help
+	  Generate crash dump after being started by kexec. This should
+	  be normally only set in special crash dump kernels which are
+	  loaded in the main kernel with kexec-tools into a specially
+	  reserved region and then later executed after a crash by
+	  kdump/kexec.
+
+	  For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+	hex "Physical address where the kernel is loaded"
+	default "0x90000000a0000000" if 64BIT
+	depends on CRASH_DUMP
+	help
+	  This gives the XKPRANGE address where the kernel is loaded.
+	  If you plan to use kernel for capturing the crash dump change
+	  this value to start of the reserved region (the "X" value as
+	  specified in the "crashkernel=YM@XM" command line boot parameter
+	  passed to the panic-ed kernel).
+
 config SECCOMP
 	bool "Enable seccomp to safely compute untrusted bytecode"
 	depends on PROC_FS
arch/loongarch/Makefile +4 -0
index 69b39ba3a09d..224274c1644e 100644
@@ -66,7 +66,11 @@ endif
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)
 
+ifdef CONFIG_PHYSICAL_START
+load-y		= $(CONFIG_PHYSICAL_START)
+else
 load-y		= 0x9000000000200000
+endif
 bootvars-y	= VMLINUX_LOAD_ADDRESS=$(load-y)
 
 drivers-$(CONFIG_PCI)		+= arch/loongarch/pci/
arch/loongarch/kernel/Makefile +1 -0
index 17dc8ce6b5ce..79eee7db1414 100644
@@ -18,6 +18,7 @@ obj-$(CONFIG_MODULES)		+= module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
 
 obj-$(CONFIG_KEXEC)             += machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_CRASH_DUMP)        += crash_dump.o
 
 obj-$(CONFIG_PROC_FS)		+= proc.o
 
arch/loongarch/kernel/crash_dump.c new file +19 -0
new file mode 100644
index 000000000000..13e5d2f7870d
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/highmem.h>
+#include <linux/crash_dump.h>
+#include <linux/io.h>
+
+ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
+			 size_t csize, unsigned long offset)
+{
+	void  *vaddr;
+
+	if (!csize)
+		return 0;
+
+	vaddr = kmap_local_pfn(pfn);
+	csize = copy_to_iter(vaddr + offset, csize, iter);
+	kunmap_local(vaddr);
+
+	return csize;
+}
arch/loongarch/kernel/machine_kexec.c +92 -6

140 lines not on this page · raw message

arch/loongarch/kernel/mem.c +6 -0
index 7423361b0ebc..c6def6ff81c8 100644
@@ -5,6 +5,7 @@
 #include <linux/efi.h>
 #include <linux/initrd.h>
 #include <linux/memblock.h>
+#include <linux/of_fdt.h>
 
 #include <asm/bootinfo.h>
 #include <asm/loongson.h>
@@ -61,4 +62,9 @@ void __init memblock_init(void)
 
 	/* Reserve the initrd */
 	reserve_initrd_mem();
+
+	/* Mainly reserved memory for the elf core head */
+	early_init_fdt_scan_reserved_mem();
+	/* Parse linux,usable-memory-range is for crash dump kernel */
+	early_init_dt_check_for_usable_mem_range();
 }
arch/loongarch/kernel/relocate_kernel.S +6 -0
index 0ea8b29242e6..a4176f2ae402 100644
@@ -24,6 +24,12 @@ SYM_CODE_START(relocate_new_kernel)
 
 	move		s0, a3
 
+	/*
+	 * In case of a kdump/crash kernel, the indirection page is not
+	 * populated as the kernel is directly copied to a reserved location
+	 */
+	beqz		s0, done
+
 process_entry:
 	PTR_L		s1, s0, 0
 	PTR_ADDI	s0, s0, SZREG
arch/loongarch/kernel/setup.c +49 -0

73 lines not on this page · raw message

arch/loongarch/kernel/traps.c +4 -0
index aa1c95aaf595..0e610872f3f4 100644
@@ -10,6 +10,7 @@
 #include <linux/entry-common.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
+#include <linux/kexec.h>
 #include <linux/module.h>
 #include <linux/extable.h>
 #include <linux/mm.h>
@@ -246,6 +247,9 @@ void __noreturn die(const char *str, struct pt_regs *regs)
 
 	oops_exit();
 
+	if (regs && kexec_should_crash(current))
+		crash_kexec(regs);
+
 	if (in_interrupt())
 		panic("Fatal exception in interrupt");
 
Signature · 7 lines
2.36.0


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#5 lore raw

PATCH v3 3/3 LoongArch: Enable CONFIG_KEXEC

In reply to Youling Tang

Defaults enable CONFIG_KEXEC to convenient kexec operations.

---
 arch/loongarch/configs/loongson3_defconfig | 1 +
 1 file changed, 1 insertion(+)
arch/loongarch/configs/loongson3_defconfig +1 -0
index 573f7a41f735..1ae85e797858 100644
@@ -46,6 +46,7 @@ CONFIG_SMP=y
 CONFIG_HOTPLUG_CPU=y
 CONFIG_NR_CPUS=64
 CONFIG_NUMA=y
+CONFIG_KEXEC=y
 CONFIG_PAGE_SIZE_16KB=y
 CONFIG_HZ_250=y
 CONFIG_ACPI=y
Signature · 1 line
2.36.0