From: "Vítor Hugo Silva" <vitorhg20080@gmail.com>
To: linux-riscv@lists.infradead.org
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Kees Cook <kees@kernel.org>, David Hildenbrand <david@kernel.org>,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM
Date: Wed, 25 Nov 2026 23:30:15 +0000 [thread overview]
Message-ID: <20261125233020.1380594-15-vitorhg20080@gmail.com> (raw)
In-Reply-To: <20261125233020.1380594-1-vitorhg20080@gmail.com>
On ESP32-P4, a load can observe new instruction bytes in the data
cache while the instruction fetch path still sees old bytes. The
runtime constant patching code can then execute stale instructions
and use an invalid address during boot.
Write back the L1 data and L2 caches for the modified PSRAM range,
invalidate the local L1 instruction cache for that range and issue
fence.i before executing the modified instructions. Apply the same
sequence to NOMMU userspace code, including FLAT binaries and the
signal return trampoline.
Split the aligned range into 32 KiB chunks and restore the caller's
IRQ state after each chunk. In instrumented measurements on the
Waveshare board, the longest observed IRQ-disabled window fell from
2.69 ms with 128 KiB chunks to 1.13 ms with 32 KiB chunks.
Bound cache synchronization by a fixed number of reads per command.
After a timeout, stop issuing commands because the controller may
still be busy. Propagate failures through executable loading and
signal setup. Treat a failure to synchronize patched kernel text as
fatal.
The ESP32-P4 board probe observed stale instructions after fence.i
alone. Combining data writeback and instruction cache invalidation
made the modified instructions visible.
Verified boot to the UART shell on a Waveshare ESP32-P4-Module-DEV-KIT
with SDMMC disabled in Linux. The shell handled SIGUSR1 and resumed
execution. With CLIC disabled, the RV32 NOMMU patch.o and signal.o
matched the reference byte for byte.
AI assisted with isolating the cache synchronization change from
the rest of the ESP32-P4 port.
Link: https://docs.espressif.com/projects/esp-idf/en/stable/esp32p4/api-reference/system/mm_sync.html
Signed-off-by: Vítor Hugo Silva <vitorhg20080@gmail.com>
---
arch/riscv/include/asm/cacheflush.h | 6 ++
arch/riscv/kernel/patch.c | 132 +++++++++++++++++++++++++++-
arch/riscv/kernel/signal.c | 6 +-
fs/binfmt_flat.c | 6 +-
fs/exec.c | 10 ++-
include/linux/cacheflush.h | 7 ++
6 files changed, 160 insertions(+), 7 deletions(-)
diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h
index c2b0a2928f06..a5c20d7d1c9d 100644
--- a/arch/riscv/include/asm/cacheflush.h
+++ b/arch/riscv/include/asm/cacheflush.h
@@ -85,6 +85,12 @@ static inline void flush_icache_range(unsigned long start, unsigned long end)
flush_icache_all();
}
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+int esp32p4_sync_user_text(unsigned long start, unsigned long end);
+
+#define flush_icache_user_range_checked esp32p4_sync_user_text
+#endif
+
extern unsigned int riscv_cbom_block_size;
extern unsigned int riscv_cboz_block_size;
extern unsigned int riscv_cbop_block_size;
diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
index 2b5adf01c550..e0687c943358 100644
--- a/arch/riscv/kernel/patch.c
+++ b/arch/riscv/kernel/patch.c
@@ -3,6 +3,13 @@
* Copyright (C) 2020 SiFive
*/
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+#include <linux/align.h>
+#include <linux/io.h>
+#include <linux/irqflags.h>
+#include <linux/panic.h>
+#include <linux/sizes.h>
+#endif
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/memory.h>
@@ -173,9 +180,132 @@ static int __patch_insn_set(void *addr, u8 c, size_t len)
}
NOKPROBE_SYMBOL(__patch_insn_set);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+#define ESP32P4_CACHE_BASE 0x3ff10000
+#define ESP32P4_CACHE_SYNC_CTRL 0x98
+#define ESP32P4_CACHE_SYNC_MAP 0x9c
+#define ESP32P4_CACHE_SYNC_ADDR 0xa0
+#define ESP32P4_CACHE_SYNC_SIZE 0xa4
+
+#define ESP32P4_CACHE_INVALIDATE BIT(0)
+#define ESP32P4_CACHE_WRITEBACK BIT(2)
+#define ESP32P4_CACHE_SYNC_DONE BIT(4)
+#define ESP32P4_CACHE_SYNC_MAX_POLLS 1000000
+
+#define ESP32P4_CACHE_L1_I0 BIT(0)
+#define ESP32P4_CACHE_L1_D BIT(4)
+#define ESP32P4_CACHE_L2 BIT(5)
+
+#define ESP32P4_PSRAM_START 0x48000000UL
+#define ESP32P4_PSRAM_END 0x4c000000UL
+#define ESP32P4_CACHE_MAX_LINE 128
+
+static int esp32p4_cache_sync(u32 map, u32 start, u32 size, u32 op,
+ unsigned int count)
+{
+ /* The caller holds local IRQs disabled on the single supported hart. */
+ static bool sync_failed;
+ void __iomem *base = (void __iomem *)ESP32P4_CACHE_BASE;
+ unsigned int polls;
+
+ if (sync_failed)
+ return -EIO;
+
+ writel(map, base + ESP32P4_CACHE_SYNC_MAP);
+ writel(start, base + ESP32P4_CACHE_SYNC_ADDR);
+ writel(size, base + ESP32P4_CACHE_SYNC_SIZE);
+
+ while (count--) {
+ writel(op, base + ESP32P4_CACHE_SYNC_CTRL);
+ /* Order the sync command before reading its completion status. */
+ mb();
+ for (polls = 0; polls < ESP32P4_CACHE_SYNC_MAX_POLLS; polls++) {
+ if (readl(base + ESP32P4_CACHE_SYNC_CTRL) & ESP32P4_CACHE_SYNC_DONE)
+ break;
+ cpu_relax();
+ }
+ if (polls == ESP32P4_CACHE_SYNC_MAX_POLLS) {
+ sync_failed = true;
+ return -ETIMEDOUT;
+ }
+ }
+
+ return 0;
+}
+
+static int esp32p4_cache_writeback(u32 start, u32 size)
+{
+ int ret;
+
+ /* Repeat writeback to avoid losing a sync with conflicting writes. */
+ ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_D, start, size, ESP32P4_CACHE_WRITEBACK, 2);
+ if (ret)
+ return ret;
+
+ return esp32p4_cache_sync(ESP32P4_CACHE_L2, start, size, ESP32P4_CACHE_WRITEBACK, 2);
+}
+
+static int esp32p4_sync_text(void *addr, size_t len, bool fatal)
+{
+ unsigned long start = (unsigned long)addr;
+ unsigned long first, end, flags;
+
+ if (start < ESP32P4_PSRAM_START || start >= ESP32P4_PSRAM_END ||
+ !len || len > ESP32P4_PSRAM_END - start)
+ return 0;
+
+ first = ALIGN_DOWN(start, ESP32P4_CACHE_MAX_LINE);
+ end = ALIGN(start + len, ESP32P4_CACHE_MAX_LINE);
+
+ /* Allow interrupts between chunks if they were enabled on entry. */
+ while (first < end) {
+ u32 size = min_t(u32, end - first, SZ_32K);
+ int ret;
+
+ local_irq_save(flags);
+ ret = esp32p4_cache_writeback(first, size);
+ if (!ret)
+ ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_I0, first, size,
+ ESP32P4_CACHE_INVALIDATE, 1);
+ if (ret) {
+ if (fatal)
+ panic("ESP32-P4 text cache sync failed: %d\n", ret);
+ local_irq_restore(flags);
+ return ret;
+ }
+
+ local_flush_icache_all();
+ local_irq_restore(flags);
+ first += size;
+ }
+
+ return 0;
+}
+
+int esp32p4_sync_user_text(unsigned long start, unsigned long end)
+{
+ int ret;
+
+ if (end > start) {
+ ret = esp32p4_sync_text((void *)start, end - start, false);
+ if (ret)
+ return ret;
+ }
+
+ flush_icache_range(start, end);
+ return 0;
+}
+#endif
+
static int __patch_insn_write(void *addr, const void *insn, size_t len)
{
- return copy_to_kernel_nofault(addr, insn, len);
+ int ret = copy_to_kernel_nofault(addr, insn, len);
+
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ if (!ret)
+ esp32p4_sync_text(addr, len, true);
+#endif
+ return ret;
}
NOKPROBE_SYMBOL(__patch_insn_write);
#endif /* CONFIG_MMU */
diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c
index 59784dc117e4..05266d7c1466 100644
--- a/arch/riscv/kernel/signal.c
+++ b/arch/riscv/kernel/signal.c
@@ -7,6 +7,7 @@
*/
#include <linux/compat.h>
+#include <linux/cacheflush.h>
#include <linux/signal.h>
#include <linux/uaccess.h>
#include <linux/syscalls.h>
@@ -21,7 +22,6 @@
#include <asm/switch_to.h>
#include <asm/vector.h>
#include <asm/csr.h>
-#include <asm/cacheflush.h>
#include <asm/usercfi.h>
unsigned long signal_minsigstksz __ro_after_init;
@@ -453,7 +453,9 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
addr = (unsigned long)&frame->sigreturn_code;
/* Make sure the two instructions are pushed to icache. */
- flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
+ err = flush_icache_user_range_checked(addr, addr + sizeof(frame->sigreturn_code));
+ if (err)
+ return err;
regs->ra = addr;
#endif /* CONFIG_MMU */
diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index b5b5ca1a44f7..890a81d805dc 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -32,6 +32,7 @@
#include <linux/user.h>
#include <linux/slab.h>
#include <linux/binfmts.h>
+#include <linux/cacheflush.h>
#include <linux/personality.h>
#include <linux/init.h>
#include <linux/flat.h>
@@ -40,7 +41,6 @@
#include <asm/byteorder.h>
#include <linux/unaligned.h>
-#include <asm/cacheflush.h>
#include <asm/page.h>
#include <asm/flat.h>
@@ -830,7 +830,9 @@ static int load_flat_file(struct linux_binprm *bprm,
#endif /* CONFIG_BINFMT_FLAT_OLD */
}
- flush_icache_user_range(start_code, end_code);
+ ret = flush_icache_user_range_checked(start_code, end_code);
+ if (ret)
+ return ret;
/* zero the BSS, BRK and stack areas */
if (clear_user((void __user *)(datapos + data_len), bss_len +
diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455..9066d2bebd1a 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -23,6 +23,7 @@
* formats.
*/
+#include <linux/cacheflush.h>
#include <linux/kernel_read_file.h>
#include <linux/slab.h>
#include <linux/file.h>
@@ -827,8 +828,13 @@ EXPORT_SYMBOL(open_exec);
ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
{
ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
- if (res > 0)
- flush_icache_user_range(addr, addr + len);
+
+ if (res > 0) {
+ int ret = flush_icache_user_range_checked(addr, addr + len);
+
+ if (ret)
+ return ret;
+ }
return res;
}
EXPORT_SYMBOL(read_code);
diff --git a/include/linux/cacheflush.h b/include/linux/cacheflush.h
index 55f297b2c23f..6a7d6f425737 100644
--- a/include/linux/cacheflush.h
+++ b/include/linux/cacheflush.h
@@ -26,4 +26,11 @@ static inline void flush_icache_pages(struct vm_area_struct *vma,
#define flush_icache_page(vma, page) flush_icache_pages(vma, page, 1)
+#ifndef flush_icache_user_range_checked
+#define flush_icache_user_range_checked(start, end) ({ \
+ flush_icache_user_range((start), (end)); \
+ 0; \
+})
+#endif
+
#endif /* _LINUX_CACHEFLUSH_H */
--
2.56.1
next prev parent reply other threads:[~2026-11-25 23:31 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 01/19] riscv: Prepare ESP32-P4 early CLIC trap vectors Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 02/19] riscv: Handle ESP32-P4 CLIC trap context Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 03/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 04/19] irqchip: Add Espressif ESP32-P4 CLIC support Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 05/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt matrix Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 06/19] irqchip: Add Espressif ESP32-P4 interrupt matrix support Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 07/19] dt-bindings: timer: Add Espressif ESP32-P4 CLINT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 08/19] clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 09/19] dt-bindings: serial: Add Espressif ESP32-P4 UART Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 13/19] serial: esp32: Add early console support for ESP32-P4 Vítor Hugo Silva
2026-11-25 23:30 ` Vítor Hugo Silva [this message]
2026-11-25 23:30 ` [PATCH RFC 15/19] riscv: Align init data to the ESP32-P4 PMP granularity Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 16/19] dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 17/19] dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 18/19] riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 19/19] MAINTAINERS: Add the Espressif ESP32-P4 platform Vítor Hugo Silva
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=20261125233020.1380594-15-vitorhg20080@gmail.com \
--to=vitorhg20080@gmail.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=brauner@kernel.org \
--cc=conor+dt@kernel.org \
--cc=david@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=robh@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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