* [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4
@ 2026-11-25 23:30 Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
0 siblings, 1 reply; 5+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Paul Walmsley, Palmer Dabbelt,
Albert Ou, Alexandre Ghiti, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Thomas Gleixner, Daniel Lezcano, Greg Kroah-Hartman,
Jiri Slaby, linux-serial, Christian Brauner, Kees Cook,
David Hildenbrand, linux-fsdevel, linux-mm
Hello,
This series of patches adds the initial support for the Espressif's
ESP32-P4 on the Waveshare ESP32-P4-Module-DEV-KIT. It boots in M-mode on
a RV32 NOMMU kernel with one HP hart.
The series adds CLIC trap handling, drivers for clic and for interrupt
matrix, and extends the CLINT timer driver. It restores a previously
removed ESP32 UART driver with ESP32-P4 UART support and early console
support. It also synchronizes the instructions written to the PSRAM,
aligns the init data to the ESP32-P4 PMP granularity, and it adds DT
bindings and the board devicetree.
The boot firmware initializes PSRAM, loads the kernel, DTB and the
initramfs from the microSD, and it supplies the timebase-frequency
before jumping into the kernel.
The bootloader source and microSD boot instructions are available at:
https://github.com/pushdword/waveshare-esp32-p4-bootloader/blob/390880e881208ab16aa16eb520b3e5ea0c63c0a2/README.md
I tested this series on the hardware, the Waveshare
ESP32-P4-Module-DEV-KIT with the ESP32-P4 revision v3.2 with 32MB of
PSRAM. The kernel booted to the UART shell from the initramfs. The
/proc/interrupts showed the CLINT IRQ's increased, UART0 interrupts were
routed through the interrupts matrix.
It would be nice to you all send feedback on restoring the interrupted
CLIC level before irq_exit_rcu() and the interface for propagating
instruction-cache sync failures to NOMMU executable loading, and signal
setup.
AI disclaimer: I have used 6.2-Sol to assist development and to assist
debug/troubleshoot some ongoing issues. I have used Codex's 6-Sol
with Daybreak Blue enabled to generate peer-review-like report to
pinpoint potential security issues regarding the patches. I manually
reviewed and validated each change, including the AI-assisted changes.
I also manually tested each change on the hardware as described above.
Vítor Hugo Silva (19):
riscv: Prepare ESP32-P4 early CLIC trap vectors
riscv: Handle ESP32-P4 CLIC trap context
dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC
irqchip: Add Espressif ESP32-P4 CLIC support
dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt
matrix
irqchip: Add Espressif ESP32-P4 interrupt matrix support
dt-bindings: timer: Add Espressif ESP32-P4 CLINT
clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support
dt-bindings: serial: Add Espressif ESP32-P4 UART
serial: esp32: Restore the ESP32 UART driver
serial: esp32: Add support for the ESP32-P4 UART
serial: esp32: Rearm TX interrupts when the FIFO is full
serial: esp32: Add early console support for ESP32-P4
riscv: Synchronize instructions written to ESP32-P4 PSRAM
riscv: Align init data to the ESP32-P4 PMP granularity
dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT
dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts
riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT
MAINTAINERS: Add the Espressif ESP32-P4 platform
.../esp,esp32p4-clic.yaml | 53 +
.../esp,esp32p4-intmtx.yaml | 67 ++
.../devicetree/bindings/riscv/cpus.yaml | 17 +-
.../devicetree/bindings/riscv/espressif.yaml | 22 +
.../bindings/serial/esp,esp32-uart.yaml | 4 +-
.../bindings/timer/sifive,clint.yaml | 28 +
MAINTAINERS | 14 +
arch/riscv/Kconfig.socs | 9 +
arch/riscv/boot/dts/Makefile | 1 +
arch/riscv/boot/dts/espressif/Makefile | 2 +
.../esp32p4-waveshare-module-dev-kit.dts | 46 +
arch/riscv/boot/dts/espressif/esp32p4.dtsi | 64 ++
arch/riscv/include/asm/asm-prototypes.h | 4 +
arch/riscv/include/asm/cacheflush.h | 6 +
arch/riscv/include/asm/ptrace.h | 4 +
arch/riscv/kernel/asm-offsets.c | 3 +
arch/riscv/kernel/entry.S | 67 ++
arch/riscv/kernel/head.S | 13 +
arch/riscv/kernel/patch.c | 132 ++-
arch/riscv/kernel/signal.c | 6 +-
arch/riscv/kernel/traps.c | 4 +
arch/riscv/kernel/vmlinux.lds.S | 4 +
drivers/clocksource/timer-clint.c | 57 +-
drivers/irqchip/Makefile | 2 +
drivers/irqchip/irq-esp32p4-clic.c | 178 ++++
drivers/irqchip/irq-esp32p4-intmtx.c | 139 +++
drivers/tty/serial/Kconfig | 15 +
drivers/tty/serial/Makefile | 1 +
drivers/tty/serial/esp32_uart.c | 940 ++++++++++++++++++
fs/binfmt_flat.c | 6 +-
fs/exec.c | 10 +-
include/linux/cacheflush.h | 7 +
32 files changed, 1910 insertions(+), 15 deletions(-)
create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
create mode 100644 Documentation/devicetree/bindings/riscv/espressif.yaml
create mode 100644 arch/riscv/boot/dts/espressif/Makefile
create mode 100644 arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts
create mode 100644 arch/riscv/boot/dts/espressif/esp32p4.dtsi
create mode 100644 drivers/irqchip/irq-esp32p4-clic.c
create mode 100644 drivers/irqchip/irq-esp32p4-intmtx.c
create mode 100644 drivers/tty/serial/esp32_uart.c
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM
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 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 22:13 ` [PATCH RFC v2] " Vítor Hugo Silva
0 siblings, 2 replies; 5+ messages in thread
From: Vítor Hugo Silva @ 2026-11-25 23:30 UTC (permalink / raw)
To: linux-riscv
Cc: devicetree, linux-kernel, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Paul Walmsley, Palmer Dabbelt, Albert Ou,
Alexandre Ghiti, Alexander Viro, Christian Brauner, Jan Kara,
Kees Cook, David Hildenbrand, linux-fsdevel, linux-mm
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM
2026-11-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
@ 2026-11-26 23:32 ` sashiko-bot
2026-11-27 22:13 ` [PATCH RFC v2] " Vítor Hugo Silva
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-26 23:32 UTC (permalink / raw)
To: Vítor Hugo Silva
Cc: devicetree, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
Alexander Viro, Christian Brauner, Jan Kara, Kees Cook,
David Hildenbrand, linux-mm
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] riscv: missing ESP32-P4 icache sync in NOMMU mmap/brk and
flush syscall
- [Medium] riscv: missing esp32p4_sync_text() in NOMMU
__patch_insn_set()
- [Medium] riscv: missing NOKPROBE_SYMBOL() on ESP32-P4 text sync
helpers
--
commit 7842ba5a926e273d15f8232bcb20b17ab7d9d227
Author: Vítor Hugo Silva <vitorhg20080@gmail.com>
riscv: Synchronize instructions written to ESP32-P4 PSRAM
This commit adds ESP32-P4 PSRAM L1D/L2 writeback and L1I invalidation
helpers and wires them into NOMMU kernel instruction patching, executable
loading, and signal frame setup.
Link: https://docs.espressif.com/projects/esp-idf/en/stable/esp32p4/api-reference/system/mm_sync.html
> diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h
> index c2b0a2928f067..a5c20d7d1c9d8 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;
[Severity: Medium]
Does leaving flush_icache_user_range() as a plain fence.i miss executable
NOMMU mappings created via do_mmap()?
In mm/nommu.c, do_mmap_private() populates private mappings in PSRAM with
kernel_read() and do_mmap() then flushes them via
flush_icache_user_range():
mm/nommu.c:do_mmap() {
...
if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
flush_icache_user_range(region->vm_start, region->vm_end);
region->vm_icache_flushed = true;
}
...
}
When an ELF FDPIC binary is loaded via elf_fdpic_map_file_by_direct_mmap()
or userspace creates a PROT_EXEC mapping, instruction fetch can still
observe stale PSRAM instructions because L1D/L2 are not written back and
L1_I0 is not invalidated. The NOMMU brk() path in mm/nommu.c and
sys_riscv_flush_icache() also still only execute fence.i without
synchronizing the ESP32-P4 caches.
> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
> index 2b5adf01c5502..e0687c943358e 100644
> --- a/arch/riscv/kernel/patch.c
> +++ b/arch/riscv/kernel/patch.c
[ ... ]
> @@ -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
[ ... ]
> +static int esp32p4_cache_sync(u32 map, u32 start, u32 size, u32 op,
> + unsigned int count)
> +{
[ ... ]
> +}
> +
> +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)
> +{
[ ... ]
> +}
[Severity: Medium]
Should esp32p4_sync_text(), esp32p4_cache_writeback(), and
esp32p4_cache_sync() be marked with NOKPROBE_SYMBOL()?
__patch_insn_write() is marked NOKPROBE_SYMBOL() and is called when arming
or disarming kprobes and preparing single-step slots. If a kprobe is
placed on any of these three helpers, patching kernel text for kprobes
will recursively trap into the kprobe handler while reprogramming the
cache controller.
[ ... ]
> +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 */
[Severity: Medium]
Should the !CONFIG_MMU implementation of __patch_insn_set() also call
esp32p4_sync_text(addr, len, true)?
__patch_insn_set() modifies kernel text with memset(addr, c, len), and its
caller patch_text_set_nosync() only calls flush_icache_range(), which
issues fence.i without writing back L1D/L2 or invalidating L1_I0 on
ESP32-P4.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125233020.1380594-1-vitorhg20080@gmail.com?part=14
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH RFC v2] riscv: Synchronize instructions written to ESP32-P4 PSRAM
2026-11-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
@ 2026-11-27 22:13 ` Vítor Hugo Silva
2026-11-28 22:16 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: Vítor Hugo Silva @ 2026-11-27 22:13 UTC (permalink / raw)
To: linux-riscv
Cc: linux-mm, linux-fsdevel, linux-kernel, pjw, palmer, aou, alex,
viro, brauner, jack, kees, david, sashiko-reviews,
Vítor Hugo Silva
On ESP32-P4, loads can observe updated instruction bytes in the data
cache while instruction fetch still sees stale bytes. During boot,
runtime constant patching can then use an invalid address.
Write back L1D and L2 for the affected PSRAM range, invalidate L1I
and issue fence.i. Use 32 KiB chunks so interrupts can run between
chunks. Bound polling per command and stop issuing commands after a
timeout. Treat failure to synchronize patched kernel text as fatal
and exclude the synchronization helpers from kprobes.
Use checked synchronization for executable loading, signal return
trampolines, the first executable NOMMU mapping of a region and
growth of the NOMMU program break. A failed mmap synchronization
discards the new mapping. A failed brk synchronization returns the
current break.
On ESP32-P4, honor nonempty intervals in riscv_flush_icache(). Check
that the requested interval is covered by the calling process's
mappings under mmap_lock. Return -EINVAL for reversed intervals and
-EFAULT for gaps. Propagate cache synchronization errors. Keep the
existing whole-cache flush for empty intervals.
An earlier board test reproduced stale instructions after fence.i
alone. On the Waveshare ESP32-P4-Module-DEV-KIT, a userspace probe
received SIGILL at the first instruction written to an executable
mapping with the published v1 patch. With v2, the same probe executes
modified instructions after riscv_flush_icache() with flags 0 and
LOCAL. It also checks return values for unmapped, reversed and empty
ranges and invalid flags. The board boots to a shell, runs bFLT
programs and delivers SIGUSR1 to the shell.
The syscall also builds with ESP32-P4 support disabled in an RV32
NOMMU configuration.
AI assisted with the cache paths and the test preparation.
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>
---
Changes in v2:
- Synchronize after filling kernel text and exclude cache helpers from
kprobes.
- Propagate synchronization errors in NOMMU mmap and brk.
- Validate and synchronize the userspace range in riscv_flush_icache
on ESP32-P4.
This replaces patch 14/19 and applies after patch 13/19 of the RFC.
v1: https://lore.kernel.org/all/20261125233020.1380594-15-vitorhg20080@gmail.com/
arch/riscv/include/asm/cacheflush.h | 6 ++
arch/riscv/kernel/patch.c | 146 +++++++++++++++++++++++++++-
arch/riscv/kernel/signal.c | 6 +-
arch/riscv/kernel/sys_riscv.c | 41 +++++++-
fs/binfmt_flat.c | 6 +-
fs/exec.c | 10 +-
include/linux/cacheflush.h | 7 ++
mm/nommu.c | 31 +++---
8 files changed, 228 insertions(+), 25 deletions(-)
diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h
index c2b0a29..a5c20d7 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 2b5adf0..7ef5565 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,136 @@ 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;
+}
+NOKPROBE_SYMBOL(esp32p4_cache_sync);
+
+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);
+}
+NOKPROBE_SYMBOL(esp32p4_cache_writeback);
+
+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;
+}
+NOKPROBE_SYMBOL(esp32p4_sync_text);
+
+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;
+}
+NOKPROBE_SYMBOL(esp32p4_sync_user_text);
+#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 */
@@ -208,10 +342,14 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len)
int ret;
ret = patch_insn_set(addr, c, len);
- if (!ret)
- flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
+ if (ret)
+ return ret;
- return ret;
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ esp32p4_sync_text(addr, len, true);
+#endif
+ flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
+ return 0;
}
NOKPROBE_SYMBOL(patch_text_set_nosync);
diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c
index 59784dc..05266d7 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/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c
index 22fc9b3..04c6883 100644
--- a/arch/riscv/kernel/sys_riscv.c
+++ b/arch/riscv/kernel/sys_riscv.c
@@ -5,8 +5,9 @@
* Copyright (C) 2017 SiFive
*/
+#include <linux/cacheflush.h>
+#include <linux/mm.h>
#include <linux/syscalls.h>
-#include <asm/cacheflush.h>
#include <asm-generic/mman-common.h>
static long riscv_sys_mmap(unsigned long addr, unsigned long len,
@@ -52,6 +53,33 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
}
#endif
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+static int esp32p4_flush_icache_mapped_range(unsigned long start,
+ unsigned long end)
+{
+ struct mm_struct *mm = current->mm;
+ struct vm_area_struct *vma;
+ unsigned long covered = start;
+ int ret = -EFAULT;
+
+ VMA_ITERATOR(vmi, mm, start);
+
+ mmap_read_lock(mm);
+ for_each_vma_range(vmi, vma, end) {
+ if (vma->vm_start > covered)
+ break;
+ covered = vma->vm_end;
+ if (covered >= end) {
+ ret = flush_icache_user_range_checked(start, end);
+ break;
+ }
+ }
+ mmap_read_unlock(mm);
+
+ return ret;
+}
+#endif
+
/*
* Allows the instruction cache to be flushed from userspace. Despite RISC-V
* having a direct 'fence.i' instruction available to userspace (which we
@@ -63,8 +91,8 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
*
* sys_riscv_flush_icache() is defined to flush the instruction cache over an
* address range, with the flush applying to either all threads or just the
- * caller. We don't currently do anything with the address range, that's just
- * in there for forwards compatibility.
+ * caller. The generic implementation uses a whole-cache flush. ESP32-P4
+ * also synchronizes mapped PSRAM in the requested range.
*/
SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
uintptr_t, flags)
@@ -73,6 +101,13 @@ SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
return -EINVAL;
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ if (end < start)
+ return -EINVAL;
+ if (end > start)
+ return esp32p4_flush_icache_mapped_range(start, end);
+#endif
+
flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
return 0;
diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index b5b5ca1..890a81d 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 8d2b14e..9066d2b 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 55f297b..6a7d6f4 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 */
diff --git a/mm/nommu.c b/mm/nommu.c
index 6c6f953..f310d1e 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -16,6 +16,7 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <linux/cacheflush.h>
#include <linux/export.h>
#include <linux/mm.h>
#include <linux/sched/mm.h>
@@ -395,10 +396,9 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)
return brk;
}
- /*
- * Ok, looks good - let it rip.
- */
- flush_icache_user_range(mm->brk, brk);
+ if (flush_icache_user_range_checked(mm->brk, brk))
+ return mm->brk;
+
return mm->brk = brk;
}
@@ -1193,7 +1193,7 @@ unsigned long do_mmap(struct file *file,
/* okay... we have a mapping; now we have to register it */
result = vma->vm_start;
- current->mm->total_vm += len >> PAGE_SHIFT;
+
share:
BUG_ON(!vma->vm_region);
@@ -1201,6 +1201,19 @@ share:
if (vma_iter_prealloc(&vmi, vma))
goto error_vma_iter_prealloc;
+ /* Flush the region before publishing its first executable mapping. */
+ if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
+ ret = flush_icache_user_range_checked(region->vm_start,
+ region->vm_end);
+ if (ret)
+ goto error_free_mapped;
+
+ region->vm_icache_flushed = true;
+ }
+
+ if (region->vm_usage == 1)
+ current->mm->total_vm += len >> PAGE_SHIFT;
+
add_nommu_region(region);
setup_vma_to_mm(vma, current->mm);
@@ -1208,13 +1221,6 @@ share:
/* add the VMA to the tree */
vma_iter_store_new(&vmi, vma);
- /* we flush the region from the icache only when the first executable
- * mapping of it is made */
- if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
- flush_icache_user_range(region->vm_start, region->vm_end);
- region->vm_icache_flushed = true;
- }
-
up_write(&nommu_region_sem);
return result;
@@ -1250,6 +1256,7 @@ error_vma_iter_prealloc:
show_mem();
ret = -ENOMEM;
+error_free_mapped:
/* in case that the region is allocated via do_mmap_private() */
if ((region->vm_usage == 1) && (region->vm_flags & VM_MAPPED_COPY))
free_page_series(region->vm_start, region->vm_top);
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH RFC v2] riscv: Synchronize instructions written to ESP32-P4 PSRAM
2026-11-27 22:13 ` [PATCH RFC v2] " Vítor Hugo Silva
@ 2026-11-28 22:16 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-28 22:16 UTC (permalink / raw)
To: Vítor Hugo Silva
Cc: linux-mm, pjw, palmer, aou, alex, viro, brauner, jack, kees,
david
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] riscv: unconditional compile-time ESP32-P4 MMIO access in
esp32p4_sync_text()
- [High] riscv: missing ESP32-P4 PSRAM cache sync in
flush_icache_range()
- [Medium] riscv: CONFIG_MMU guard mismatch for esp32p4_sync_text() in
patch.c
--
commit e405e543af82d7fb854ccf2765af90d8953d2261
Author: Vítor Hugo Silva <vitorhg20080@gmail.com>
riscv: Synchronize instructions written to ESP32-P4 PSRAM
This commit adds ESP32-P4 PSRAM L1D/L2 writeback and L1I invalidation
helpers for patched kernel text, executable loading, signal trampolines,
NOMMU mappings, and the riscv_flush_icache() syscall.
Link: https://docs.espressif.com/projects/esp-idf/en/stable/esp32p4/api-reference/system/mm_sync.html
> diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h
> index c2b0a2928f067..a5c20d7d1c9d8 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
> +
[Severity: High]
Does leaving flush_icache_range() as a fence.i-only operation miss kernel
paths that write executable text to PSRAM without going through
__patch_insn_write() or flush_icache_user_range_checked()?
For example, loadable kernel modules copy and relocate text directly in
memory and rely on flush_module_icache() -> flush_icache_range(), and kgdb
single-step breakpoints write opcodes via copy_to_kernel_nofault() followed
by flush_icache_range(). On ESP32-P4, those paths will not write back
L1D/L2 or invalidate L1I, leaving stale instructions in the instruction
cache. Conversely, __runtime_fixup_32() calls patch_insn_write() twice on
adjacent 4-byte instructions before calling flush_icache_range(), so
hooking __patch_insn_write() runs the five-command MMIO cache sync sequence
twice on the same 128-byte cache line for every 32-bit runtime constant.
> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
> index 2b5adf01c5502..7ef5565765e63 100644
> --- a/arch/riscv/kernel/patch.c
> +++ b/arch/riscv/kernel/patch.c
[ ... ]
> @@ -173,9 +180,136 @@ 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_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;
> + }
[Severity: High]
Will a kernel built with CONFIG_RISCV_ESP32P4_CLIC=y poke hardcoded MMIO
address 0x3ff10000 when booted on any other RISC-V machine whose memory
overlaps 0x48000000..0x4c000000?
esp32p4_sync_text() only checks whether the target address falls within
ESP32P4_PSRAM_START..ESP32P4_PSRAM_END before accessing
ESP32P4_CACHE_BASE, without verifying at runtime that the running platform
is an ESP32-P4. On any other platform or QEMU machine with RAM in that
range, boot-time text patching will fault on 0x3ff10000 or time out and
panic(). SYSCALL_DEFINE3(riscv_flush_icache) in
arch/riscv/kernel/sys_riscv.c also changes its behavior at compile time
under CONFIG_RISCV_ESP32P4_CLIC rather than gating on the running platform,
and flush_icache_user_range_checked() adds SoC-specific hooks across
generic fs/ and mm/ code instead of using standard architectural cacheflush
callbacks.
[ ... ]
> @@ -208,10 +342,14 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len)
> int ret;
>
> ret = patch_insn_set(addr, c, len);
> - if (!ret)
> - flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
> + if (ret)
> + return ret;
>
> - return ret;
> +#ifdef CONFIG_RISCV_ESP32P4_CLIC
> + esp32p4_sync_text(addr, len, true);
> +#endif
> + flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len);
> + return 0;
> }
> NOKPROBE_SYMBOL(patch_text_set_nosync);
[Severity: Medium]
Does this fail to build if CONFIG_RISCV_ESP32P4_CLIC is enabled alongside
CONFIG_MMU=y?
esp32p4_sync_text() and esp32p4_sync_user_text() are defined inside the
#else (!CONFIG_MMU) branch of #ifdef CONFIG_MMU in
arch/riscv/kernel/patch.c, whereas patch_text_set_nosync(),
arch/riscv/include/asm/cacheflush.h, and arch/riscv/kernel/sys_riscv.c
reference them outside #ifndef CONFIG_MMU. In addition,
CONFIG_RISCV_ESP32P4_CLIC is not defined in any Kconfig file in the tree.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127221346.197911-1-vitorhg20080@gmail.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-28 22:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 22:13 ` [PATCH RFC v2] " Vítor Hugo Silva
2026-11-28 22:16 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox