mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] MIPS: kexec: Sync the instruction cache with the copied kernel
@ 2026-11-16  1:42 Marco Martins
  0 siblings, 0 replies; only message in thread
From: Marco Martins @ 2026-11-16  1:42 UTC (permalink / raw)
  To: Thomas Bogendoerfer; +Cc: linux-mips, linux-kernel

machine_kexec() writes back and invalidates the caches with
__flush_cache_all() and then calls kexec_reboot(), which still runs
set_cpu_online() and local_flush_icache_range() before it jumps to the
relocation code, so their lines stay valid in the instruction cache.
relocate_new_kernel() then copies the new kernel over the old one
through the data cache and jumps to it without writing the copy back
or invalidating the instruction cache, except on Octeon. Where both
kernels have code at the same address, the new kernel can run stale
instructions of the old one.

The new kernel calls set_cpu_online() in boot_cpu_init(), before it
prints anything, and hangs there when its copy sits at a different
address than the old kernel's. On an MT7620A (24KEc), a kexec from a
kernel with set_cpu_online() at 0x8002bb5c into one with it at
0x8002bb7c hung in all 5 tries; with this change in the old kernel,
the new one booted in all 4.

On R2 and later, SYNCI each copied page, which writes it back from the
data cache and drops it from the instruction cache, and jump to the new
kernel with JR.HB. Octeon keeps its own SYNCI of the whole instruction
cache.

The bug was found, and this patch and its description written, with an
AI coding assistant (Claude Code, model Claude Opus 5.5), asked to find
why images started with kexec on an MT7620A board sometimes hung. It
bisected the new kernel's boot with reset probes patched into the
image, and ran the tests on the board.

Fixes: 62cac480f33f ("MIPS: kexec: Make a framework for both jumping and halting on nonboot CPUs")
Cc: stable@vger.kernel.org
Signed-off-by: Marco Martins <marcomartins86@gmail.com>
---
Tested on a TP-Link TL-WPA7510 v2 (MT7620A, MIPS 24KEc, single core)
with OpenWrt's 6.18.60 kernel. relocate_kernel.S and machine_kexec.c are
the same in 6.18.60 and v7.4-rc3, but OpenWrt adds a patch of its own to
both (command line arguments for the new kernel). The board runs this
change on top of that patch, in a form that assembles to the same
machine code. The 5 hangs and 4 boots above are from November 13;
without a serial console, a hang showed as the watchdog resetting the
board, as the kernel was booted with watchdog.stop_on_reboot=0. Since
then the board has run the change, and on November 15 alone kexec
started kernels laid out differently from the running one more than 30
times, all of which booted.

Built only in that 6.18.60 tree, with the board's configuration. Not
tested: SMP, where the other CPUs also jump into the new kernel, from
kexec_smp_wait; 64-bit; R6; Octeon.

 arch/mips/kernel/relocate_kernel.S | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/arch/mips/kernel/relocate_kernel.S b/arch/mips/kernel/relocate_kernel.S
index de894a0..e3cae10 100644
--- a/arch/mips/kernel/relocate_kernel.S
+++ b/arch/mips/kernel/relocate_kernel.S
@@ -64,8 +64,25 @@ copy_word:
 	PTR_ADDIU	s4, s4, SZREG
 	PTR_ADDIU	s2, s2, SZREG
 	LONG_ADDIU	s6, s6, -1
-	beq		s6, zero, process_entry
+	beq		s6, zero, 2f
 	b		copy_word
+2:
+#if MIPS_ISA_REV >= 2 && !defined(CONFIG_CPU_CAVIUM_OCTEON)
+	/*
+	 * The old kernel's code may still be in the I-cache at this page's
+	 * address: write the page back from the D-cache and drop it from
+	 * the I-cache.
+	 */
+	rdhwr		t0, $1
+	beqz		t0, process_entry
+	li		t1, (1 << _PAGE_SHIFT)
+	PTR_SUBU	t1, s4, t1
+3:	synci		0(t1)
+	PTR_ADDU	t1, t1, t0
+	sltu		t2, t1, s4
+	bnez		t2, 3b
+#endif
+	b		process_entry
 
 done:
 #ifdef CONFIG_SMP
@@ -96,7 +113,11 @@ done:
 	sync
 #endif
 	/* jump to kexec_start_address */
+#if MIPS_ISA_REV >= 2
+	jr.hb		s1
+#else
 	j		s1
+#endif
 	END(relocate_new_kernel)
 
 #ifdef CONFIG_SMP

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16  1:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16  1:42 [PATCH] MIPS: kexec: Sync the instruction cache with the copied kernel Marco Martins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®