From: sashiko-bot@kernel.org
To: "Vítor Hugo Silva" <vitorhg20080@gmail.com>
Cc: linux-mm@kvack.org, pjw@kernel.org, palmer@dabbelt.com,
aou@eecs.berkeley.edu, alex@ghiti.fr, viro@zeniv.linux.org.uk,
brauner@kernel.org, jack@suse.cz, kees@kernel.org,
david@kernel.org
Subject: Re: [PATCH RFC v2] riscv: Synchronize instructions written to ESP32-P4 PSRAM
Date: Sat, 28 Nov 2026 22:16:31 +0000 [thread overview]
Message-ID: <sashiko-outbox-239081@kernel.org> (raw)
In-Reply-To: <20261127221346.197911-1-vitorhg20080@gmail.com>
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
next prev parent reply other threads:[~2026-11-28 22:16 UTC|newest]
Thread overview: 72+ 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 ` 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 ` 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 ` 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 ` 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 ` 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 ` 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 ` 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 ` 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 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 16:18 ` [PATCH RFC v2] " Vítor Hugo Silva
2026-11-27 16:18 ` 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 ` 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 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 16:55 ` [PATCH RFC 1/4] serial: esp32: Validate the baud rate before programming the UART Vítor Hugo Silva
2026-11-27 16:55 ` Vítor Hugo Silva
2026-11-27 16:55 ` [PATCH RFC 2/4] serial: esp32: Fix ESP32-S3 baud rate prescaler calculation Vítor Hugo Silva
2026-11-27 16:55 ` Vítor Hugo Silva
2026-11-27 16:55 ` [PATCH RFC 3/4] serial: esp32: Deliver and mask receive break interrupts Vítor Hugo Silva
2026-11-27 16:55 ` Vítor Hugo Silva
2026-11-27 16:55 ` [PATCH RFC 4/4] serial: esp32: Enable the UART clock during port registration Vítor Hugo Silva
2026-11-27 16:55 ` Vítor Hugo Silva
2026-11-27 19:25 ` [PATCH RFC 1/2] serial: esp32: Enable hardware RTS/CTS flow control Vítor Hugo Silva
2026-11-27 19:25 ` Vítor Hugo Silva
2026-11-27 19:25 ` [PATCH RFC 2/2] serial: esp32: Report active-low CTS correctly on ESP32-S3 and ESP32-P4 Vítor Hugo Silva
2026-11-27 19:25 ` 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 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 19:39 ` [PATCH RFC v2] " Vítor Hugo Silva
2026-11-27 19:39 ` 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 ` Vítor Hugo Silva
2026-11-26 23:32 ` sashiko-bot
2026-11-27 15:40 ` Vítor Hugo Silva
2026-11-27 15:56 ` [PATCH RFC v2] " Vítor Hugo Silva
2026-11-27 15:56 ` 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
2026-11-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM 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
2026-11-27 22:13 ` Vítor Hugo Silva
2026-11-28 22:16 ` sashiko-bot [this message]
2026-11-30 10:31 ` Vítor Hugo Silva
2026-11-30 12:23 ` [PATCH RFC v3] " Vítor Hugo Silva
2026-11-30 12:23 ` Vítor Hugo Silva
2026-12-01 12:24 ` sashiko-bot
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 ` 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 ` 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 ` 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 ` Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 19/19] MAINTAINERS: Add the Espressif ESP32-P4 platform Vítor Hugo Silva
2026-11-25 23:30 ` Vítor Hugo Silva
2026-11-26 23:32 ` [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 sashiko-bot
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=sashiko-outbox-239081@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=brauner@kernel.org \
--cc=david@kernel.org \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=linux-mm@kvack.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=viro@zeniv.linux.org.uk \
--cc=vitorhg20080@gmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.