MIPS: kexec: Sync the instruction cache with the copied kernel
PATCH 1/1 MIPS: kexec: Sync the instruction cache with the copied 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.
---
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(-)
arch/mips/kernel/relocate_kernel.S +22 -1
@@ -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
Signature · 1 line
2.53.0