Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vítor Hugo Silva" <vitorhg20080@gmail.com>
Cc: devicetree@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-mm@kvack.org
Subject: Re: [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM
Date: Thu, 26 Nov 2026 23:32:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-236336@kernel.org> (raw)
In-Reply-To: <20261125233020.1380594-15-vitorhg20080@gmail.com>

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


      reply	other threads:[~2026-11-26 23:32 UTC|newest]

Thread overview: 3+ 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 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
2026-11-26 23:32   ` sashiko-bot [this message]

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-236336@kernel.org \
    --to=sashiko-bot@kernel.org \
    --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-mm@kvack.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=robh@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox