From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796135904; b=u7EcHcx7yMg2Xf69cmfLHx4++cXhKTHJtTj9Gbp5i7E/8knrq1vmHKqUjiuKffaX8gf6 b0h/IV0RDNa1a+NpkO0oVbn3fLgNpx3/Cwq/iOyEW7GuKg4MpIpzD+a1UhUlQ2bdAF17Z lA5Jjj+zuTKoe2VmdJ6kPoySsM6wWyV8HanA5X73xCZO2/2rfpErWx/4W4mFrSaw8ZIk1 tbbeDfG+ml9VaULB7Ecv1ygObWzm4OHtHcNNvRF9DP0ChRDEfwaPt2NzyqNQqVJ5X8HDN m+IYp3KrukKvWxwsmlpYHZA3v3dWcBZbHRnEjiZex9zZzD3WU54Jr/FdZGXOr4iUE7g== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796135904; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=A2/1UtylqyQyBcGyGjwjHUC4m4bpZZplqA8ebSIBGQI=; b=h9XoYtkA5v2U6l46CvInpzSbAmWljoNBCR1CW7b8UkUnN3CW1sj1WUdFaI8z1LbUZQoe igJKK/FsriOkA+eavpMX74Q3IWpC1COULKLUSwAaWiqwFHWEl4Ig0nw5BT3vRD2AT45Wb en8t3sWZHBh4naWe+kDnPxpLSM3wVoKZCXfP6YYEdk5ZetnLzM/fuIimPv9oPSaKf3iVa yIv4tcr39gDCZjzlpS9f/slM35PU+gfgvAi+bEI1yG7fajT3Vs2Ou+i9NWUTLfhHRevFs bbQ1tyT7jGFLUg2fVIadv4MU5RoaILDfFnN6i9ozoTPsHJKkd2ZglAp1SCEDAa3U+ZQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=baSnEI2b; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E97F4606A322 for ; Tue, 1 Dec 2026 14:38:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=baSnEI2b Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796135903; b=TE8VJmyQAzcy7i0Z1BMH6/zYHL3Gqmav4ELTl3Ga5cEyA+gqF9xnWmEpGkuBBFrPRqas 8zWurc0oiQqOOm0M+t9vfGefMpIXYGAuZsOSVxXXBiw/yuYUiqq3rDp0aZF7+z8BpHEHb qBBEtb9tqAFfDYE3Qr+Wurv2yfwqopvlky70+mHq4UloBcEjJpBghziWol3o+P7dsUJ1L sjirccnT0uG7/zEneIOIFYK2ky2FjAHBqlchBMwET1Gm3t/TD4jCMOi0kfHRUHW8GG70r 9LTIAG7hdojoyfUw7ivBiY6ax9YGjzd1mLFhw694diWd4x/DXIKPsEAXAQRkwXmvAZw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796135903; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=A2/1UtylqyQyBcGyGjwjHUC4m4bpZZplqA8ebSIBGQI=; b=SMQKB1ibfLJ6dngxyXnRc4qX4iAkrZX4CeewwbRPqSR3+Voy/U4XeUUe5sxomNpQNSw4 hDFEEr3NkaQK0b/tAnCwiKTbuQv/ilbN/aoaInN+576vo3WyZVq3DFmfCAwTSi+jTh1wo ecilt6sPw/AnKQadvhlxUBReh1y53mtb6uOJ8skMwdXZ8eN9XyGpvebUczinpxwNodOjK hR9MTf1Ad/Yb4W9X3QqmtScRkSjSyqf+RNAtuqzGmTIGFFRu2IMIfXU/bRD2yjKJUrq2f m3NVQyA9ti89L5DzQBvZGt0k+ifMeuUqJOaCVBIJyL8ka7aoaYvxVKgnvex6/CvXKyg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 590A41F0089B; Tue, 1 Dec 2026 14:38:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796135903; bh=A2/1UtylqyQyBcGyGjwjHUC4m4bpZZplqA8ebSIBGQI=; h=From:Date:Subject:To:Cc:Reply-To; b=baSnEI2bb+7DyKXLrAjC3dYh0uUfVmULDKC1JjicG6+RJl+JhqbsSsCei/rdkKIof IGrRRGSfS9fMgvnPcRCfJ5j5ZvbROMz1Wm2pn9LclR//NpeYwyJ2SvzCvQobM4E9yM vWLk2+sdR4Xp9hQQRJQLNz795V50v+j+rNt2oVyDXwMOcHbl6WxxTaNpl2tleftZ/U NmorClz9G40TXvj4BEMMa9ZbAfc/LElVz7s060IkhzBw8mB/A9xCHEmazyqt4rRWn8 0PQ2D79g+ZeJAeM9BOWDfC1hcgxFBBkQvKUG1xY84d+EA5I2RiWqoaIvKG8AcVo6In Z94AxxqIprH7A== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id A54504EE3A; Tue, 1 Dec 2026 14:38:12 +0000 (UTC) From: Jens Taprogge via B4 Relay Date: Tue, 01 Dec 2026 15:37:51 +0100 Subject: [PATCH] ARM: patch: flush the I-cache before releasing patch_lock X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261201-arm-patch-icache-v1-1-07fac14f3f98@molex.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAA/yXMQQqEMAxA0atI1gbaSgW9yuCixmgj6EirIhTvP h1dvsX/CSIH4QhtkSDwKVG+a4YuCyDv1olRhmwwytTaKI0uLLi5nTwKOfKMVFtFQ1PZxo6Qsy3 wKNez/HSv49HPTPv/A/f9A/8Zs4F0AAAA X-Change-ID: 20261201-arm-patch-icache-c650cd93595f To: Nicolas Pitre , Rabin Vincent , Russell King Cc: linux-arm-kernel@lists.infradead.org, Robert Nelson , linux-kernel@vger.kernel.org, Peter Zijlstra , Kees Cook X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4813; i=jens.taprogge@molex.com; h=from:subject:message-id; bh=v5QgiveRlJYVnuRHhcC8SEhBqvEMDyYZiBmzNx0/xCk=; b=owEBiQJ2/ZANAwAIAbR25DuIDAGhAcsmYgBrDtvS3exeNIYw8UDts52n8CxXLlWDbP0IC+1H9 QN9+ziBgpWJAk8EAAEIADkWIQSQpsttUfvLrbH2hOa0duQ7iAwBoQUCaw7b0hsUgAAAAAAEAA5t YW51MiwyLjUrMS4xMiwwLDMACgkQtHbkO4gMAaF0VQ//ZLjTrksXoR/QvaZoBMqfkecu8NOGYXJ vJ8YO7NT3j2nW85ypspMafl1REVcerRYFep69b4xTzZd4rQIiYY9T8OHkhhKWWAsdKjnqCNOc2s RfusyiGUsqeFPSHFEdWhUAw+BI8E/tTxYDgCHfXEUA76P8l9Fxcafu/pb9w+x6fnRmdAnzNwLbk vZfLj5UksYfoH5yZP9lpe+hCTjpQ+k7Y7Xr3PmpqmxcAbsbRAsCw3abGsV/Um/VlhbvfFYoM8ro U1MLGtOJQj9iRRW8Vl+JmMl0E/qnKbjZeSRivJraaTHQHIQSYFpFkjAXOb+t4c0MGEFegxYNOv7 ZdVRP59CfyxSLuGabkY53oocf2u79rYgF3QzIp8dCY8Y47iTPnrPwqIb4JLjOvyraVe2XnQnax0 5OaZnuiiDDSx8jjis+XBYr/hLhCe1nFk4mUfFVGNXUG4UZ5hBUlhlTb5Poo6yOB52ErSbmPKaxA F4uYHwQZy7z1WCFMXq7aXwWwQ59lOG3L6a/ov+EkKYEIXRuTFnvKG9J/+HveNMgIR1n2Iz4siAT ZotG3m4Jz+R8Jxu6dmdaP4GgSeleHXMAHMfn+WDqUYRKIvanFhDioacFf5wjP2/6jCUV0vC1Yjp 1m+yUvf/TtDqgWGNMWJr9zBSqqqh36stm5UM0JxDzopxEeX48woM= X-Developer-Key: i=jens.taprogge@molex.com; a=openpgp; fpr=90A6CB6D51FBCBADB1F684E6B476E43B880C01A1 X-Endpoint-Received: by B4 Relay for jens.taprogge@molex.com/default with auth_id=1711 X-Original-From: Jens Taprogge Reply-To: jens.taprogge@molex.com Content-Transfer-Encoding: 7bit From: Jens Taprogge __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 --- 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