* [PATCH] ARM: patch: flush the I-cache before releasing patch_lock
@ 2026-12-01 14:37 Jens Taprogge via B4 Relay
0 siblings, 0 replies; only message in thread
From: Jens Taprogge via B4 Relay @ 2026-12-01 14:37 UTC (permalink / raw)
To: Nicolas Pitre, Rabin Vincent, Russell King
Cc: linux-arm-kernel, Robert Nelson, linux-kernel, Peter Zijlstra, Kees Cook
From: Jens Taprogge <jens.taprogge@molex.com>
__patch_text_real() releases patch_lock in patch_unmap() before it
flushes the I-cache for the new instruction. The unlock runs
_raw_spin_unlock_irqrestore(), which may be the very function being
patched: ftrace turns the profiling calls of all functions into NOPs
at boot, including the one in _raw_spin_unlock_irqrestore().
With a Thumb-2 kernel, the 32-bit instruction can cross an I-cache
line. The CPU may then execute the first half from memory and the
second half from the stale line. On an AM335x this combined the first
half of the NOP (add.w sp, sp, #4) with the second half of the former
bl __gnu_mcount_nc into a bl into the kernel's BSS, and the kernel
panicked in ftrace_init() before the console was up:
Unable to handle kernel execution of memory at virtual address c1b4f7b8 when execute
Internal error: Oops: 8000000d [#1] SMP THUMB2
PC is at __log_buf+0x79cc/0x20000
LR is at _raw_spin_unlock_irqrestore+0x7/0x34
Kernel panic - not syncing: Attempted to kill the idle task!
Whether it happens depends only on the code layout: the call site
in _raw_spin_unlock_irqrestore() was at offset 62 of a 64 byte cache
line here, and a build where unrelated changes moved it to offset 30
booted fine.
The order dates back to the introduction of the fixmap, when only
kprobes, jump labels and kgdb patched text this way. Commit
5a735583b764 ("arm/ftrace: Use __patch_text()") in v5.6 made ftrace
use it for the NOPs it writes at every boot.
Flush the I-cache first, and only then unmap the fixmaps and release
the lock. Between the write and the flush only the inline and
assembler cache maintenance functions run.
Fixes: ab0615e2d6fb ("arm: use fixmap for text patching when text is RO")
Cc: stable@vger.kernel.org
Signed-off-by: Jens Taprogge <jens.taprogge@molex.com>
---
How the bug was found and the fix verified:
- An AM335x board (Cortex-A8, Thumb-2 kernel based on 7.3.5) hung right
after "Starting kernel ..." and was reset by the watchdog in a loop.
earlycon printed nothing. The oops quoted above was read from __log_buf
in RAM with U-Boot after the watchdog reset.
- The torn instruction matches the oops exactly: the first halfword of
the new NOP (f10d) followed by the second halfword of the old
bl __gnu_mcount_nc (d6fb) decodes as a bl to 0xc1b4f7b8, the faulting
address.
- With this patch applied, a kernel with the call site at the same
offset (62) in a cache line boots normally on the same board.
- Tested on the 7.3.5 based kernel only. On v7.4-rc5 the patch is build
tested (W=1, sparse, no new warnings), but not runtime tested.
Tool use: an AI coding assistant (Claude Code) helped to analyse the
crash, wrote the fix and drafted this changelog. I have verified the
testing. The changes are outside my area of expertise. They do appear
plausible.
---
arch/arm/kernel/patch.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/arch/arm/kernel/patch.c b/arch/arm/kernel/patch.c
index 4d45e60cd46d..e2b55e73b7f1 100644
--- a/arch/arm/kernel/patch.c
+++ b/arch/arm/kernel/patch.c
@@ -60,6 +60,7 @@ void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap)
bool thumb2 = IS_ENABLED(CONFIG_THUMB2_KERNEL);
unsigned int uintaddr = (uintptr_t) addr;
bool twopage = false;
+ bool unmap_poke1 = false;
unsigned long flags;
void *waddr = addr;
int size;
@@ -85,7 +86,7 @@ void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap)
if (twopage && addrh1 != addr + 2) {
flush_kernel_vmap_range(addrh1, 2);
- patch_unmap(FIX_TEXT_POKE1, NULL);
+ unmap_poke1 = true;
}
size = sizeof(u32);
@@ -99,13 +100,23 @@ void __kprobes __patch_text_real(void *addr, unsigned int insn, bool remap)
size = sizeof(u32);
}
- if (waddr != addr) {
+ if (waddr != addr)
flush_kernel_vmap_range(waddr, twopage ? size / 2 : size);
- patch_unmap(FIX_TEXT_POKE0, &flags);
- }
+ /*
+ * Make the new instruction visible before calling any function:
+ * patch_unmap() releases patch_lock, and the instruction may be in
+ * _raw_spin_unlock_irqrestore() itself, e.g. when ftrace turns its
+ * profiling call into a NOP. A Thumb-2 instruction crossing an
+ * I-cache line could then be executed half old and half new.
+ */
flush_icache_range((uintptr_t)(addr),
(uintptr_t)(addr) + size);
+
+ if (unmap_poke1)
+ patch_unmap(FIX_TEXT_POKE1, NULL);
+ if (waddr != addr)
+ patch_unmap(FIX_TEXT_POKE0, &flags);
}
static int __kprobes patch_text_stop_machine(void *data)
---
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
change-id: 20261201-arm-patch-icache-c650cd93595f
Best regards,
--
Jens Taprogge <jens.taprogge@molex.com>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-12-01 14:38 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 14:37 [PATCH] ARM: patch: flush the I-cache before releasing patch_lock Jens Taprogge via B4 Relay
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®