mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Marco Martins <marcomartins86@gmail.com>
To: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] MIPS: kexec: Sync the instruction cache with the copied kernel
Date: Mon, 16 Nov 2026 01:42:07 +0000	[thread overview]
Message-ID: <20261116014207.3112156-1-marcomartins86@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-16  1:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116014207.3112156-1-marcomartins86@gmail.com \
    --to=marcomartins86@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=tsbogend@alpha.franken.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®