Skip to contentSkip to conversation

LoongArch: add KHO support and selftests

PATCH In review George Guo 4 participants 15 messages May 25 – May 28

Subsystems (5)
lore
Share & tools
lore

George Guo

author
#0 lore raw

cover LoongArch: add KHO support and selftests

From: George Guo <[email protected]>>

This series adds Kexec Handover (KHO) support for LoongArch64 and extends the KHO selftest infrastructure to run on LoongArch under QEMU.

KHO support (patches 1-2)
--------------------------
Patch 1 wires up the three pieces needed for KHO on LoongArch:
- Kconfig: select ARCH_SUPPORTS_KEXEC_HANDOVER for 64BIT
- machine_kexec_file: call cmdline_add_kho() to pass the KHO FDT and scratch buffer addresses via "kho_handover=" on the kernel cmdline
- setup: parse "kho_handover=" early and call kho_populate()

Patch 2 fixes a duplicate-parameter bug when kexec is invoked with --reuse-cmdline: the running kernel's cmdline already contains a stale kho_handover= from the previous load. load_other_segments() prepends a fresh entry but appends the original cmdline verbatim, so early_param() calls kho_populate() twice and the second call overwrites the first with a stale FDT pointer, panicking early in mm_core_init().

Bug fixes (patches 3-4)
------------------------
Patch 3 fixes a build failure with CONFIG_KFENCE=y: virt_to_phys() and phys_to_virt() in asm/io.h expand to use offset_in_page() and page_address(), which require <linux/mm.h> that was not included.

Patch 4 moves KEXEC_CONTROL_CODE from 0x100000 to 0x180000. QEMU places its machine FDT at 0x100000 when booting with '-kernel'; the previous placement silently overwrote the FDT with the relocation trampoline, breaking earlycon in the kexec'd kernel.

Selftest support (patches 5-7)
-------------------------------
Patch 5 adds loongarch.conf and extends vmtest.sh to recognise loongarch64 as a build target. QEMU's LoongArch virt machine is FDT-only (no ACPI), so 'earlycon' must appear on the kernel cmdline or the console UART is never discovered and the test log stays empty.

Patch 6 disables SERIO_I8042 and its dependents in the QEMU_KCONFIG fragment. QEMU's LoongArch virt machine has no i8042 controller; the fallback port probe hits a page fault on the memory-mapped I/O range and panics before reaching userspace.

Patch 7 polls the serial output for the test verdict and kills QEMU once it appears. QEMU provides no EFI runtime services on LoongArch, so machine_restart() falls through to an infinite idle loop and QEMU never exits after kexec.

George Guo (7):
LoongArch: Add KHO basic support
LoongArch: kho: strip stale kho_handover= from reused cmdline
LoongArch: Add missing linux/mm.h include in asm/io.h
LoongArch: kexec: avoid overwriting QEMU's machine FDT at 0x100000 selftests/kho: add LoongArch vmtest support selftests/kho: LoongArch: disable PS/2 input devices for QEMU virt selftests/kho: handle QEMU not exiting after kexec on LoongArch

 arch/loongarch/Kconfig                     |  3 ++
 arch/loongarch/include/asm/io.h            |  1 +
 arch/loongarch/kernel/machine_kexec.c      |  9 +++-
 arch/loongarch/kernel/machine_kexec_file.c | 57 ++++++++++++++++++++++
 arch/loongarch/kernel/setup.c              | 27 ++++++++++
 tools/testing/selftests/kho/loongarch.conf | 10 ++++
 tools/testing/selftests/kho/vmtest.sh      | 38 ++++++++++++---
 7 files changed, 135 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/kho/loongarch.conf
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/7 LoongArch: Add KHO basic support

From: George Guo <[email protected]>

Enable Kexec Handover on LoongArch64:

- Kconfig: select ARCH_SUPPORTS_KEXEC_HANDOVER for 64BIT
- machine_kexec_file: add cmdline_add_kho() to pass the KHO FDT and scratch buffer addresses to the next kernel via the "kho_handover=" command-line parameter
- setup: parse "kho_handover=" early and call kho_populate() to hand memory regions to the KHO core

---
 arch/loongarch/Kconfig                     |  3 +++
 arch/loongarch/kernel/machine_kexec_file.c | 22 ++++++++++++++++++
 arch/loongarch/kernel/setup.c              | 27 ++++++++++++++++++++++
 3 files changed, 52 insertions(+)
arch/loongarch/Kconfig +3 -0
index 606597da46b8..d494418545f5 100644
@@ -684,6 +684,9 @@ config ARCH_SUPPORTS_KEXEC
 config ARCH_SUPPORTS_KEXEC_FILE
 	def_bool 64BIT
 
+config ARCH_SUPPORTS_KEXEC_HANDOVER
+	def_bool 64BIT
+
 config ARCH_SELECTS_KEXEC_FILE
 	def_bool 64BIT
 	depends on KEXEC_FILE
arch/loongarch/kernel/machine_kexec_file.c +22 -0
index 5584b798ba46..ddf4d0e0e7fd 100644
@@ -55,6 +55,24 @@ static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmpl
 	*cmdline_tmplen += initrd_strlen;
 }
 
+#ifdef CONFIG_KEXEC_HANDOVER
+/* Add "kho_handover=<fdt_size>@<fdt_addr>,<scratch_size>@<scratch_addr>" to cmdline. */
+static void cmdline_add_kho(struct kimage *image, unsigned long *cmdline_tmplen,
+			    char *modified_cmdline)
+{
+	int n;
+
+	if (!image->kho.fdt || !image->kho.scratch)
+		return;
+
+	n = sprintf(modified_cmdline + *cmdline_tmplen,
+	    "kho_handover=0x%llx@0x%llx,0x%lx@0x%llx ",
+	    (u64)PAGE_SIZE,               image->kho.fdt,
+	    image->kho.scratch->bufsz,    (u64)image->kho.scratch->mem);
+	*cmdline_tmplen += n;
+}
+#endif
+
 #ifdef CONFIG_CRASH_DUMP
 
 static int prepare_elf_headers(void **addr, unsigned long *sz)
@@ -220,6 +238,10 @@ int load_other_segments(struct kimage *image,
 		cmdline_add_initrd(image, &cmdline_tmplen, modified_cmdline, initrd_load_addr);
 	}
 
+#ifdef CONFIG_KEXEC_HANDOVER
+	cmdline_add_kho(image, &cmdline_tmplen, modified_cmdline);
+#endif
+
 	if (cmdline_len + cmdline_tmplen > COMMAND_LINE_SIZE) {
 		pr_err("Appending command line exceeds COMMAND_LINE_SIZE\n");
 		ret = -EINVAL;
arch/loongarch/kernel/setup.c +27 -0
index 839b23edee87..5934ba6f13e3 100644
@@ -48,6 +48,7 @@
 #include <asm/setup.h>
 #include <asm/time.h>
 #include <asm/unwind.h>
+#include <linux/kexec_handover.h>
 
 #define SMBIOS_BIOSSIZE_OFFSET		0x09
 #define SMBIOS_BIOSEXTERN_OFFSET	0x13
@@ -227,6 +228,32 @@ static int __init early_parse_mem(char *p)
 }
 early_param("mem", early_parse_mem);
 
+#ifdef CONFIG_KEXEC_HANDOVER
+static int __init early_parse_kho(char *p)
+{
+	phys_addr_t fdt_addr, scratch_addr;
+	u64 fdt_size, scratch_size;
+
+	if (!p)
+		return -EINVAL;
+
+	fdt_size = memparse(p, &p);
+	if (*p++ != '@')
+		return -EINVAL;
+	fdt_addr = memparse(p, &p);
+	if (*p++ != ',')
+		return -EINVAL;
+	scratch_size = memparse(p, &p);
+	if (*p++ != '@')
+		return -EINVAL;
+	scratch_addr = memparse(p, &p);
+
+	kho_populate(fdt_addr, fdt_size, scratch_addr, scratch_size);
+	return 0;
+}
+early_param("kho_handover", early_parse_kho);
+#endif
+
 static void __init arch_reserve_vmcore(void)
 {
 #ifdef CONFIG_PROC_VMCORE
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH 1/7] LoongArch: Add KHO basic support

Quoted 20 lines · George Guo

From: George Guo <[email protected]>

Enable Kexec Handover on LoongArch64:

- Kconfig: select ARCH_SUPPORTS_KEXEC_HANDOVER for 64BIT
- machine_kexec_file: add cmdline_add_kho() to pass the KHO FDT and scratch buffer addresses to the next kernel via the "kho_handover=" command-line parameter
- setup: parse "kho_handover=" early and call kho_populate() to hand memory regions to the KHO core

---
 arch/loongarch/Kconfig                     |  3 +++
 arch/loongarch/kernel/machine_kexec_file.c | 22 ++++++++++++++++++
 arch/loongarch/kernel/setup.c              | 27 ++++++++++++++++++++++
 3 files changed, 52 insertions(+)

[...]

Quoted 13 lines
arch/loongarch/kernel/machine_kexec_file.c +5 -0
index 5584b798ba46..ddf4d0e0e7fd 100644
@@ -55,6 +55,24 @@ static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmpl
 	*cmdline_tmplen += initrd_strlen;
 }
 
+#ifdef CONFIG_KEXEC_HANDOVER
+/* Add "kho_handover=<fdt_size>@<fdt_addr>,<scratch_size>@<scratch_addr>" to cmdline. */
+static void cmdline_add_kho(struct kimage *image, unsigned long *cmdline_tmplen,
+			    char *modified_cmdline)
+{

arch/loongarch/kernel/machine_kexec_file.c static void cmdline_add_initrd(struct kimage *image, unsigned long *cmdline_tmpl · line 72

Quoted 10 lines
arch/loongarch/kernel/machine_kexec_file.c +10 -0
+	int n;
+
+	if (!image->kho.fdt || !image->kho.scratch)
+		return;
+
+	n = sprintf(modified_cmdline + *cmdline_tmplen,
+	    "kho_handover=0x%llx@0x%llx,0x%lx@0x%llx ",
+	    (u64)PAGE_SIZE,               image->kho.fdt,
+	    image->kho.scratch->bufsz,    (u64)image->kho.scratch->mem);
+	*cmdline_tmplen += n;

Do you have nothing else for a bootloader to pass data to the kernel? Passing this via the command line seems crazy... All these addresses are now available directly to unprivileged userspace. Can't you use the device tree like we do on ARM64?

Also, this commandline parameter isn't documented anywhere.

Quoted 34 lines
arch/loongarch/kernel/machine_kexec_file.c +7 -0
+}
+#endif
+
 #ifdef CONFIG_CRASH_DUMP
 
 static int prepare_elf_headers(void **addr, unsigned long *sz)
@@ -220,6 +238,10 @@ int load_other_segments(struct kimage *image,
 		cmdline_add_initrd(image, &cmdline_tmplen, modified_cmdline, initrd_load_addr);
 	}
 
+#ifdef CONFIG_KEXEC_HANDOVER
+	cmdline_add_kho(image, &cmdline_tmplen, modified_cmdline);
+#endif
+
 	if (cmdline_len + cmdline_tmplen > COMMAND_LINE_SIZE) {
 		pr_err("Appending command line exceeds COMMAND_LINE_SIZE\n");
 		ret = -EINVAL;
arch/loongarch/kernel/setup.c +2 -0
index 839b23edee87..5934ba6f13e3 100644
@@ -48,6 +48,7 @@
 #include <asm/setup.h>
 #include <asm/time.h>
 #include <asm/unwind.h>
+#include <linux/kexec_handover.h>
 
 #define SMBIOS_BIOSSIZE_OFFSET		0x09
 #define SMBIOS_BIOSEXTERN_OFFSET	0x13
@@ -227,6 +228,32 @@ static int __init early_parse_mem(char *p)
 }
 early_param("mem", early_parse_mem);
 
+#ifdef CONFIG_KEXEC_HANDOVER

arch/loongarch/kernel/setup.c static int __init early_parse_mem(char *p) · line 241

Quoted 10 lines
arch/loongarch/kernel/setup.c +10 -0
+static int __init early_parse_kho(char *p)
+{
+	phys_addr_t fdt_addr, scratch_addr;
+	u64 fdt_size, scratch_size;
+
+	if (!p)
+		return -EINVAL;
+
+	fdt_size = memparse(p, &p);
+	if (*p++ != '@')

Can you please wrap these in parenthesis? I am too dumb to remember all the precedence rules and this immediately makes me question whether we are doing (*p)++ or *(p++).

Quoted 18 lines
arch/loongarch/kernel/setup.c +15 -0
+		return -EINVAL;
+	fdt_addr = memparse(p, &p);
+	if (*p++ != ',')
+		return -EINVAL;
+	scratch_size = memparse(p, &p);
+	if (*p++ != '@')
+		return -EINVAL;
+	scratch_addr = memparse(p, &p);
+
+	kho_populate(fdt_addr, fdt_size, scratch_addr, scratch_size);
+	return 0;
+}
+early_param("kho_handover", early_parse_kho);
+#endif
+
 static void __init arch_reserve_vmcore(void)
 {
 #ifdef CONFIG_PROC_VMCORE
Signature · 2 lines
Regards,
Pratyush Yadav
author
#3 lore raw

Re: [PATCH 1/7] LoongArch: Add KHO basic support

On Mon, May 25 2026, Pratyush Yadav wrote:

Hi Pratyush,

Quoted 6 lines

Do you have nothing else for a bootloader to pass data to the kernel? Passing this via the command line seems crazy... All these addresses are now available directly to unprivileged userspace. Can't you use the device tree like we do on ARM64?

Also, this commandline parameter isn't documented anywhere.

I have tried the FDT /chosen approach, storing the KHO metadata as linux,kho-fdt and linux,kho-scratch properties (the same properties that drivers/of/kexec.c:kho_add_chosen() writes
and drivers/of/fdt.c:early_init_dt_check_kho() reads).

This passes the KHO selftest when the system boots with FDT.

I am currently debugging the ACPI boot case.

Thanks for your advice!

--
Regards,
George Guo

author
#4 lore raw

PATCH 2/7 LoongArch: kho: strip stale kho_handover= from reused cmdline

In reply to George Guo

From: George Guo <[email protected]>

When kexec is invoked with --reuse-cmdline after a liveupdate boot, the running kernel's command line already contains a kho_handover= parameter from the previous kexec load. load_other_segments() prepends a fresh kho_handover= for the new handover FDT but then appends the original cmdline verbatim, resulting in two kho_handover= entries:

kho_handover=0x4000@<new_fdt>,... kho_handover=0x4000@<stale_fdt>,...

early_param() calls early_parse_kho() for each occurrence in order, so kho_populate() is invoked twice and the second call overwrites the first with the stale FDT address. The stale address no longer holds a valid KHO FDT, causing __kho_radix_walk_tree() to dereference a garbage pointer and panic early in mm_core_init().

Fix this by adding the new kho_handover= to the cmdline prefix first, then stripping any stale kho_handover= tokens from the appended original cmdline portion, so only the freshly generated entry survives.

---
 arch/loongarch/kernel/machine_kexec_file.c | 35 ++++++++++++++++++++++
 1 file changed, 35 insertions(+)
arch/loongarch/kernel/machine_kexec_file.c +35 -0
index ddf4d0e0e7fd..ffaedd055e62 100644
@@ -71,6 +71,37 @@ static void cmdline_add_kho(struct kimage *image, unsigned long *cmdline_tmplen,
 	    image->kho.scratch->bufsz,    (u64)image->kho.scratch->mem);
 	*cmdline_tmplen += n;
 }
+
+/*
+ * Remove all "kho_handover=..." tokens from cmdline.  Needed when
+ * --reuse-cmdline is used: the running kernel's cmdline already carries a
+ * stale kho_handover= from the previous kexec load; without removal the new
+ * kernel sees two entries and kho_populate() ends up using the wrong (stale)
+ * FDT address.
+ */
+static void cmdline_remove_kho(char *cmdline)
+{
+	const char *key = "kho_handover=";
+	size_t key_len = strlen(key);
+	char *p = cmdline;
+
+	while ((p = strstr(p, key)) != NULL) {
+		char *start = p;
+		char *end;
+
+		/* Only match at a token boundary */
+		if (start != cmdline && *(start - 1) != ' ') {
+			p += key_len;
+			continue;
+		}
+		end = start + key_len;
+		while (*end && *end != ' ')
+			end++;
+		while (*end == ' ')
+			end++;
+		memmove(start, end, strlen(end) + 1);
+	}
+}
 #endif
 
 #ifdef CONFIG_CRASH_DUMP
@@ -249,6 +280,10 @@ int load_other_segments(struct kimage *image,
 	}
 
 	memcpy(modified_cmdline + cmdline_tmplen, cmdline, cmdline_len);
+#ifdef CONFIG_KEXEC_HANDOVER
+	/* Strip stale kho_handover= that --reuse-cmdline may have carried over */
+	cmdline_remove_kho(modified_cmdline + cmdline_tmplen);
+#endif
 	cmdline = modified_cmdline;
 	image->arch.cmdline_ptr = (unsigned long)cmdline;
 
Signature · 1 line
2.25.1
maintainer
#5 lore raw

Re: [PATCH 2/7] LoongArch: kho: strip stale kho_handover= from reused cmdline

Quoted 54 lines · George Guo

From: George Guo <[email protected]>

When kexec is invoked with --reuse-cmdline after a liveupdate boot, the running kernel's command line already contains a kho_handover= parameter from the previous kexec load. load_other_segments() prepends a fresh kho_handover= for the new handover FDT but then appends the original cmdline verbatim, resulting in two kho_handover= entries:

kho_handover=0x4000@<new_fdt>,... kho_handover=0x4000@<stale_fdt>,...

early_param() calls early_parse_kho() for each occurrence in order, so kho_populate() is invoked twice and the second call overwrites the first with the stale FDT address. The stale address no longer holds a valid KHO FDT, causing __kho_radix_walk_tree() to dereference a garbage pointer and panic early in mm_core_init().

Fix this by adding the new kho_handover= to the cmdline prefix first, then stripping any stale kho_handover= tokens from the appended original cmdline portion, so only the freshly generated entry survives.

---
 arch/loongarch/kernel/machine_kexec_file.c | 35 ++++++++++++++++++++++
 1 file changed, 35 insertions(+)
arch/loongarch/kernel/machine_kexec_file.c +21 -0
index ddf4d0e0e7fd..ffaedd055e62 100644
@@ -71,6 +71,37 @@ static void cmdline_add_kho(struct kimage *image, unsigned long *cmdline_tmplen,
 	    image->kho.scratch->bufsz,    (u64)image->kho.scratch->mem);
 	*cmdline_tmplen += n;
 }
+
+/*
+ * Remove all "kho_handover=..." tokens from cmdline.  Needed when
+ * --reuse-cmdline is used: the running kernel's cmdline already carries a
+ * stale kho_handover= from the previous kexec load; without removal the new
+ * kernel sees two entries and kho_populate() ends up using the wrong (stale)
+ * FDT address.
+ */
+static void cmdline_remove_kho(char *cmdline)
+{
+	const char *key = "kho_handover=";
+	size_t key_len = strlen(key);
+	char *p = cmdline;
+
+	while ((p = strstr(p, key)) != NULL) {
+		char *start = p;
+		char *end;
+
+		/* Only match at a token boundary */
+		if (start != cmdline && *(start - 1) != ' ') {
+			p += key_len;

arch/loongarch/kernel/machine_kexec_file.c static void cmdline_add_kho(struct kimage *image, unsigned long *cmdline_tmplen, · line 104

Quoted 10 lines
arch/loongarch/kernel/machine_kexec_file.c +10 -0
+			continue;
+		}
+		end = start + key_len;
+		while (*end && *end != ' ')
+			end++;
+		while (*end == ' ')
+			end++;
+		memmove(start, end, strlen(end) + 1);
+	}
+}

Ugh, modifying the commandline supplied by userspace feels odd... Can you at all do this via device tree?

If not, would it make more sense to reject kexec_load if command line has kho_handover= and teach kexec-tools to strip it on its side?

Quoted 13 lines
arch/loongarch/kernel/machine_kexec_file.c +4 -0
 #endif
 
 #ifdef CONFIG_CRASH_DUMP
@@ -249,6 +280,10 @@ int load_other_segments(struct kimage *image,
 	}
 
 	memcpy(modified_cmdline + cmdline_tmplen, cmdline, cmdline_len);
+#ifdef CONFIG_KEXEC_HANDOVER
+	/* Strip stale kho_handover= that --reuse-cmdline may have carried over */
+	cmdline_remove_kho(modified_cmdline + cmdline_tmplen);
+#endif
 	cmdline = modified_cmdline;
 	image->arch.cmdline_ptr = (unsigned long)cmdline;
Signature · 2 lines
Regards,
Pratyush Yadav
author
#6 lore raw

PATCH 3/7 LoongArch: Add missing linux/mm.h include in asm/io.h

In reply to George Guo

From: George Guo <[email protected]>

When CONFIG_KFENCE is enabled, the virt_to_phys() and phys_to_virt() macros in arch/loongarch/include/asm/io.h expand to use offset_in_page() and page_address(), both of which are declared in <linux/mm.h>.

Without this include, any translation unit that includes <asm/io.h> and expands these macros with CONFIG_KFENCE=y will fail to build. This was observed when building kernel/liveupdate/luo_session.c with CONFIG_KFENCE=y:

asm/io.h: error: implicit declaration of function 'offset_in_page' asm/io.h: error: implicit declaration of function 'page_address'

Add the missing include to fix these build errors.

Fixes 0ca84aeaee15 ("LoongArch: Make {virt, phys, page, pfn} translation work with KFENCE")
---
 arch/loongarch/include/asm/io.h | 1 +
 1 file changed, 1 insertion(+)
arch/loongarch/include/asm/io.h +1 -0
index 0130185e0349..7ff4aa72a34a 100644
@@ -13,6 +13,7 @@
 #include <asm/page.h>
 #include <asm/pgtable-bits.h>
 #include <asm/string.h>
+#include <linux/mm.h>
 
 extern void __init __iomem *early_ioremap(phys_addr_t phys_addr, unsigned long size);
 extern void __init early_iounmap(void __iomem *addr, unsigned long size);
Signature · 1 line
2.25.1
author
#7 lore raw

PATCH 4/7 LoongArch: kexec: avoid overwriting QEMU's machine FDT at 0x100000

In reply to George Guo

From: George Guo <[email protected]>

QEMU places its machine FDT at physical address 0x100000 when booting with '-kernel'. KEXEC_CONTROL_CODE was defined at the same address, so machine_kexec_prepare() overwrites the FDT with the relocation trampoline before jumping to the new kernel.

The kexec'd kernel's fdt_setup() reads the FDT pointer from the EFI config table (FDTPTR = 0x100000) and finds trampoline code instead of a valid FDT, so earlycon auto-detection fails and the second kernel boots silently with no console output.

Move KEXEC_CONTROL_CODE to 0x180000, which is still within the first 2MB reserved by memblock_init() and does not conflict with the QEMU FDT.

---
 arch/loongarch/kernel/machine_kexec.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/loongarch/kernel/machine_kexec.c +7 -2
index d7fafda1d541..44df7dbd3de5 100644
@@ -21,8 +21,13 @@
 #include <asm/cacheflush.h>
 #include <asm/page.h>
 
-/* 0x100000 ~ 0x200000 is safe */
-#define KEXEC_CONTROL_CODE	TO_CACHE(0x100000UL)
+/*
+ * Both addresses are within the first 2MB which is always reserved by
+ * memblock_init().  Avoid 0x100000 because QEMU places its machine FDT
+ * there when using '-kernel'; overwriting it silences earlycon in the
+ * kexec'd kernel.
+ */
+#define KEXEC_CONTROL_CODE	TO_CACHE(0x180000UL)
 #define KEXEC_CMDLINE_ADDR	TO_CACHE(0x108000UL)
 
 static unsigned long reboot_code_buffer;
Signature · 1 line
2.25.1
author
#8 lore raw

PATCH 5/7 selftests/kho: add LoongArch vmtest support

In reply to George Guo

From: George Guo <[email protected]>

Add loongarch.conf to configure QEMU's LoongArch virt machine with a la464 CPU, enable the 8250 serial console, and set the kernel image to vmlinux.efi. Extend vmtest.sh to recognize loongarch64 as a supported target and map it to the 'loongarch' kernel arch name.

QEMU's LoongArch virt machine provides no ACPI tables and relies on FDT to describe hardware. Without 'earlycon' on the kernel command line, the FDT is not scanned for a console UART, no output reaches the console, and vmtest.sh's console log stays empty causing the test to always fail. Add 'earlycon' to KERNEL_CMDLINE in loongarch.conf to fix this.

---
 tools/testing/selftests/kho/loongarch.conf | 7 +++++++
 tools/testing/selftests/kho/vmtest.sh      | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kho/loongarch.conf
tools/testing/selftests/kho/loongarch.conf new file +7 -0
new file mode 100644
index 000000000000..b0551e0c2d77
@@ -0,0 +1,7 @@
+QEMU_CMD="qemu-system-loongarch64 -M virt -cpu la464"
+QEMU_KCONFIG="
+CONFIG_SERIAL_8250=y
+CONFIG_SERIAL_8250_CONSOLE=y
+"
+KERNEL_IMAGE="vmlinux.efi"
+KERNEL_CMDLINE="console=ttyS0 earlycon"
tools/testing/selftests/kho/vmtest.sh +2 -1
index 49fdac8e8b15..a6ae9ac09595 100755
@@ -21,7 +21,7 @@ Options:
 	-d)	path to the kernel build directory
 	-j)	number of jobs for compilation, similar to -j in make
 	-t)	run test for target_arch, requires CROSS_COMPILE set
-		supported targets: aarch64, x86_64
+		supported targets: aarch64, x86_64, loongarch64
 	-h)	display this help
 EOF
 }
@@ -123,6 +123,7 @@ function target_to_arch() {
 	case $target in
 	     aarch64) echo "arm64" ;;
 	     x86_64) echo "x86" ;;
+	     loongarch64) echo "loongarch" ;;
 	     *) skip "architecture $target is not supported"
 	esac
 }
Signature · 1 line
2.25.1
author
#9 lore raw

PATCH 6/7 selftests/kho: LoongArch: disable PS/2 input devices for QEMU virt

In reply to George Guo

From: George Guo <[email protected]>

QEMU's LoongArch virt machine has no i8042 PS/2 controller. When PNP detection finds nothing, i8042_init() falls back to probing the ports directly. On LoongArch the I/O ports are memory-mapped, and the i8042 port addresses are not backed by any device on the virt machine, so i8042_flush() takes a page fault and the kernel panics:

i8042: PNP: No PS/2 controller found.
i8042: Probing ports directly.
CPU 0 Unable to handle kernel paging request at virtual address ffff800000008064
ERA: i8042_flush+0x50/0x198
RA: i8042_init+0x2a8/0x35c

  Kernel panic - not syncing: Attempted to kill init!

Disable SERIO_I8042 and its dependents (KEYBOARD_ATKBD, MOUSE_PS2) in the QEMU_KCONFIG fragment to prevent the driver from being built.

---
 tools/testing/selftests/kho/loongarch.conf | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/kho/loongarch.conf +3 -0
index b0551e0c2d77..0145cb49e5b2 100644
@@ -2,6 +2,9 @@ QEMU_CMD="qemu-system-loongarch64 -M virt -cpu la464"
 QEMU_KCONFIG="
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
+# CONFIG_KEYBOARD_ATKBD is not set
+# CONFIG_MOUSE_PS2 is not set
+# CONFIG_SERIO_I8042 is not set
 "
 KERNEL_IMAGE="vmlinux.efi"
 KERNEL_CMDLINE="console=ttyS0 earlycon"
Signature · 1 line
2.25.1
author
#10 lore raw

PATCH 7/7 selftests/kho: handle QEMU not exiting after kexec on LoongArch

In reply to George Guo

From: George Guo <[email protected]>

On LoongArch, QEMU provides only a minimal EFI stub with no runtime services and no ACPI tables, so machine_restart() falls through to its infinite idle loop and QEMU never exits after kexec.

Poll the serial output for the test verdict and kill QEMU once it appears, rather than waiting for QEMU to exit naturally.

---
 tools/testing/selftests/kho/vmtest.sh | 35 +++++++++++++++++++++------
 1 file changed, 28 insertions(+), 7 deletions(-)
tools/testing/selftests/kho/vmtest.sh +28 -7
index a6ae9ac09595..3cb9674e4475 100755
@@ -103,16 +103,37 @@ function run_qemu() {
 	local qemu_cmd=$1
 	local cmdline=$2
 	local kernel=$3
+	local arch=$4
 	local serial="$tmp_dir/qemu.serial"
 
 	cmdline="$cmdline kho=on panic=-1"
 
-	$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \
-		  -accel kvm -accel hvf -accel tcg  \
-		  -serial file:"$serial" \
-		  -append "$cmdline" \
-		  -kernel "$kernel" \
-		  -initrd "$initrd"
+	local qemu_args=(
+		-m 1G -smp 2 -no-reboot -nographic -nodefaults
+		-accel kvm -accel hvf -accel tcg
+		-serial file:"$serial"
+		-append "$cmdline"
+		-kernel "$kernel"
+		-initrd "$initrd"
+	)
+
+	# On LoongArch, EFI runtime services are unavailable after kexec so
+	# machine_restart() hangs and QEMU never exits on its own.  Run QEMU
+	# in the background, poll for the test result, then kill it.
+	if [[ "$arch" == "loongarch" ]]; then
+		$qemu_cmd "${qemu_args[@]}" &
+		local qemu_pid=$!
+		local timeout=100
+		while ((timeout-- > 0)); do
+			grep -q "KHO restore succeeded\|KHO restore failed" \
+				"$serial" 2>/dev/null && break
+			sleep 1
+		done
+		kill "$qemu_pid" 2>/dev/null
+		wait "$qemu_pid" 2>/dev/null || true
+	else
+		$qemu_cmd "${qemu_args[@]}"
+	fi
 
 	grep "KHO restore succeeded" "$serial" &> /dev/null || fail "KHO failed"
 }
@@ -179,7 +200,7 @@ function main() {
 	local kernel="$build_dir/arch/$arch/boot/$KERNEL_IMAGE"
 	mkinitrd "$kernel"
 
-	run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$kernel"
+	run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$kernel" "$arch"
 
 	ktap_test_pass "KHO succeeded"
 }
Signature · 1 line
2.25.1
maintainer
#11 lore raw

Re: [PATCH 7/7] selftests/kho: handle QEMU not exiting after kexec on LoongArch

Quoted 5 lines · George Guo
tools/testing/selftests/kho/vmtest.sh +0 -0
index a6ae9ac09595..3cb9674e4475 100755

tools/testing/selftests/kho/vmtest.sh function run_qemu() { · line 110

Quoted 9 lines
tools/testing/selftests/kho/vmtest.sh +1 -1
@@ -103,16 +103,37 @@ function run_qemu() {
 	local qemu_cmd=$1
 	local cmdline=$2
 	local kernel=$3
+	local arch=$4
 	local serial="$tmp_dir/qemu.serial"
 
 	cmdline="$cmdline kho=on panic=-1"
 
-	$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \

Just make it

	timeout 100 $qemu_cmd ...

for all architectures.

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#12 lore raw
In reply to George Guo

On Mon, 25 May 2026 14:28:03 +0800, George Guo <[email protected]> wrote:

Hi,

Quoted 7 lines

[...]

Bug fixes (patches 3-4)
------------------------
Patch 3 fixes a build failure with CONFIG_KFENCE=y: virt_to_phys() and phys_to_virt() in asm/io.h expand to use offset_in_page() and page_address(), which require <linux/mm.h> that was not included.

This fix looks unrelated to KHO, it makes sense to send it as a separate patch.

Quoted 4 lines

Patch 4 moves KEXEC_CONTROL_CODE from 0x100000 to 0x180000. QEMU places its machine FDT at 0x100000 when booting with '-kernel'; the previous placement silently overwrote the FDT with the relocation

And I believe that patch 2 and patch 4 can be folded into patch 1 so that addition of KHO support will be a single patch.

Quoted 9 lines

trampoline, breaking earlycon in the kexec'd kernel.

Selftest support (patches 5-7)
-------------------------------
Patch 5 adds loongarch.conf and extends vmtest.sh to recognise loongarch64 as a build target. QEMU's LoongArch virt machine is FDT-only (no ACPI), so 'earlycon' must appear on the kernel cmdline or the console UART is never discovered and the test log stays empty.

Quoted 4 lines

Patch 6 disables SERIO_I8042 and its dependents in the QEMU_KCONFIG fragment. QEMU's LoongArch virt machine has no i8042 controller; the fallback port probe hits a page fault on the memory-mapped I/O range and panics before reaching userspace.

This should be folded into patch 5 for sure.

Quoted 5 lines

Patch 7 polls the serial output for the test verdict and kills QEMU once it appears. QEMU provides no EFI runtime services on LoongArch, so machine_restart() falls through to an infinite idle loop and QEMU never exits after kexec.

And since without this selftest would hang on loongarch, the timeout handing should be folded into patch 5.

In general, the rationale when adding new features is to make sure that after each patch there are no regressions and the new functionality either works or cannot be enabled until completely implemented.

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#13 lore raw

Hi, Mike,

Quoted 7 lines · Mike Rapoport

On Mon, 25 May 2026 14:28:03 +0800, George Guo <[email protected]> wrote:

Hi,

Quoted 2 lines

[...]

Quoted 8 lines
Quoted 5 lines

Bug fixes (patches 3-4)
------------------------
Patch 3 fixes a build failure with CONFIG_KFENCE=y: virt_to_phys() and phys_to_virt() in asm/io.h expand to use offset_in_page() and page_address(), which require <linux/mm.h> that was not included.

This fix looks unrelated to KHO, it makes sense to send it as a separate patch.

He tried, but it causes new problems, but I agree this patch has nothing to do with KHO.
https://lore.kernel.org/loongarch/[email protected]/T/#t

Quoted 8 lines
Quoted 5 lines

Patch 4 moves KEXEC_CONTROL_CODE from 0x100000 to 0x180000. QEMU places its machine FDT at 0x100000 when booting with '-kernel'; the previous placement silently overwrote the FDT with the relocation

And I believe that patch 2 and patch 4 can be folded into patch 1 so that addition of KHO support will be a single patch.

Patch 2 should be folded, but Patch 4 can be separate, because it should be backported.

Quoted 10 lines
Quoted 10 lines

trampoline, breaking earlycon in the kexec'd kernel.

Selftest support (patches 5-7)
-------------------------------
Patch 5 adds loongarch.conf and extends vmtest.sh to recognise loongarch64 as a build target. QEMU's LoongArch virt machine is FDT-only (no ACPI), so 'earlycon' must appear on the kernel cmdline or the console UART is never discovered and the test log stays empty.

Quoted 6 lines
Quoted 4 lines

Patch 6 disables SERIO_I8042 and its dependents in the QEMU_KCONFIG fragment. QEMU's LoongArch virt machine has no i8042 controller; the fallback port probe hits a page fault on the memory-mapped I/O range and panics before reaching userspace.

This should be folded into patch 5 for sure.

I completely don't know why Patch 6 is needed, for normal use I8042 doesn't cause problems in virtual machines.

Huacai

Quoted 18 lines
Quoted 6 lines

Patch 7 polls the serial output for the test verdict and kills QEMU once it appears. QEMU provides no EFI runtime services on LoongArch, so machine_restart() falls through to an infinite idle loop and QEMU never exits after kexec.

And since without this selftest would hang on loongarch, the timeout handing should be folded into patch 5.

In general, the rationale when adding new features is to make sure that after each patch there are no regressions and the new functionality either works or cannot be enabled until completely implemented.

--
Sincerely yours,
Mike.

maintainer
#14 lore raw

Hi,

Quoted 13 lines · Huacai Chen

Hi, Mike,

Quoted 4 lines · Mike Rapoport

On Mon, 25 May 2026 14:28:03 +0800, George Guo <[email protected]> wrote:

This fix looks unrelated to KHO, it makes sense to send it as a separate patch.

He tried, but it causes new problems, but I agree this patch has nothing to do with KHO.
https://lore.kernel.org/loongarch/[email protected]/T/#t

Quoted 5 lines
Quoted 2 lines

And I believe that patch 2 and patch 4 can be folded into patch 1 so that addition of KHO support will be a single patch.

Patch 2 should be folded, but Patch 4 can be separate, because it should be backported.

Fair enough.
I'd suggest to move the fixes to be the first patches in the series, but presuming this is going via loongarch tree it's up to you :)

Quoted 6 lines
Quoted 1 line

This should be folded into patch 5 for sure.

I completely don't know why Patch 6 is needed, for normal use I8042 doesn't cause problems in virtual machines.

Huacai