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. Detect the ESP32-P4 from the root Device Tree compatible before jump label initialization. Use that result to gate access to the cache controller and ESP32-P4 range handling in riscv_flush_icache(). Perform PSRAM synchronization in flush_icache_range() so callers such as KGDB and the module loader get the required cache maintenance. Synchronize the two instructions of an RV32 runtime constant together. Keep checked error handling for userspace. 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 --- Changes in v3: - Detect the running SoC before enabling ESP32-P4 cache synchronization and syscall range handling. - Synchronize PSRAM from flush_icache_range() and batch the RV32 runtime constant instruction pair. - Boot-tested v3 on the Waveshare ESP32-P4-Module-DEV-KIT. The shell handled SIGUSR1 and the same userspace icache probe passed. v2: https://lore.kernel.org/r/20261127221346.197911-1-vitorhg20080@gmail.com arch/riscv/include/asm/cacheflush.h | 13 ++- arch/riscv/include/asm/runtime-const.h | 17 ++- arch/riscv/kernel/patch.c | 22 +++- arch/riscv/kernel/setup.c | 5 + arch/riscv/kernel/signal.c | 6 +- arch/riscv/kernel/sys_riscv.c | 43 +++++++- arch/riscv/mm/cacheflush.c | 137 +++++++++++++++++++++++++ fs/binfmt_flat.c | 6 +- fs/exec.c | 10 +- include/linux/cacheflush.h | 7 ++ mm/nommu.c | 31 +++--- 11 files changed, 265 insertions(+), 32 deletions(-) diff --git a/arch/riscv/include/asm/cacheflush.h b/arch/riscv/include/asm/cacheflush.h index c2b0a2928f06..b87b06292606 100644 --- a/arch/riscv/include/asm/cacheflush.h +++ b/arch/riscv/include/asm/cacheflush.h @@ -75,15 +75,26 @@ void flush_icache_mm(struct mm_struct *mm, bool local); #endif /* CONFIG_SMP */ +#define flush_icache_range flush_icache_range +#ifdef CONFIG_RISCV_ESP32P4_CLIC +extern bool riscv_esp32p4_cache_enabled; + +void flush_icache_range(unsigned long start, unsigned long end); +int esp32p4_sync_user_text(unsigned long start, unsigned long end); + +#define flush_icache_user_range_checked esp32p4_sync_user_text +#else +#define riscv_esp32p4_cache_enabled false + /* * RISC-V doesn't have an instruction to flush parts of the instruction cache, * so instead we just flush the whole thing. */ -#define flush_icache_range flush_icache_range static inline void flush_icache_range(unsigned long start, unsigned long end) { flush_icache_all(); } +#endif extern unsigned int riscv_cbom_block_size; extern unsigned int riscv_cboz_block_size; diff --git a/arch/riscv/include/asm/runtime-const.h b/arch/riscv/include/asm/runtime-const.h index ce2008bb5227..7000ced5d758 100644 --- a/arch/riscv/include/asm/runtime-const.h +++ b/arch/riscv/include/asm/runtime-const.h @@ -190,8 +190,7 @@ static inline void __runtime_fixup_caches(void *where, unsigned int insns) { - /* On riscv there are currently only cache-wide flushes so va is ignored. */ - __always_unused uintptr_t va = (uintptr_t)where; + uintptr_t va = (uintptr_t)where; flush_icache_range(va, va + 4 * insns); } @@ -243,8 +242,15 @@ static inline void __runtime_fixup_32(__le16 *lui_parcel, __le16 *addi_parcel, u addi_res = cpu_to_le32(addi_insn); lui_res = cpu_to_le32(lui_insn); mutex_lock(&text_mutex); - patch_insn_write(addi_parcel, &addi_res, sizeof(addi_res)); - patch_insn_write(lui_parcel, &lui_res, sizeof(lui_res)); + if (IS_ENABLED(CONFIG_32BIT) && riscv_esp32p4_cache_enabled) { + /* The RV32 pair is contiguous and can share one cache sync. */ + __le32 insns[] = { lui_res, addi_res }; + + patch_text_nosync(lui_parcel, insns, sizeof(insns)); + } else { + patch_insn_write(addi_parcel, &addi_res, sizeof(addi_res)); + patch_insn_write(lui_parcel, &lui_res, sizeof(lui_res)); + } mutex_unlock(&text_mutex); } @@ -252,7 +258,8 @@ static inline void __runtime_fixup_ptr(void *where, unsigned long val) { #ifdef CONFIG_32BIT __runtime_fixup_32(where, where + 4, val); - __runtime_fixup_caches(where, 2); + if (!riscv_esp32p4_cache_enabled) + __runtime_fixup_caches(where, 2); #else __runtime_fixup_32(where, where + 8, val); __runtime_fixup_32(where + 4, where + 12, val >> 32); diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 2b5adf01c550..a10b91a445c9 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -208,14 +208,15 @@ 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; + flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); + return 0; } NOKPROBE_SYMBOL(patch_text_set_nosync); -int patch_insn_write(void *addr, const void *insn, size_t len) +static int patch_insn_copy(void *addr, const void *insn, size_t len) { size_t size; int ret; @@ -237,13 +238,24 @@ int patch_insn_write(void *addr, const void *insn, size_t len) return 0; } +NOKPROBE_SYMBOL(patch_insn_copy); + +int patch_insn_write(void *addr, const void *insn, size_t len) +{ + int ret = patch_insn_copy(addr, insn, len); + + if (!ret && riscv_esp32p4_cache_enabled) + flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); + + return ret; +} NOKPROBE_SYMBOL(patch_insn_write); int patch_text_nosync(void *addr, const void *insns, size_t len) { int ret; - ret = patch_insn_write(addr, insns, len); + ret = patch_insn_copy(addr, insns, len); if (!ret) flush_icache_range((uintptr_t)addr, (uintptr_t)addr + len); diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index d79f99b86033..f99c8dc33601 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -258,6 +258,11 @@ static void __init parse_dtb(void) if (early_init_dt_scan(dtb_early_va, dtb_early_pa)) { const char *name = of_flat_dt_get_machine_name(); +#ifdef CONFIG_RISCV_ESP32P4_CLIC + /* Identify the cache hardware before jump label patching. */ + riscv_esp32p4_cache_enabled = + of_flat_dt_is_compatible(of_get_flat_dt_root(), "esp,esp32p4"); +#endif if (name) { pr_info("Machine model: %s\n", name); dump_stack_set_arch_desc("%s (DT)", name); 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 +#include #include #include #include @@ -21,7 +22,6 @@ #include #include #include -#include #include 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 22fc9b3268be..36bdfdedf116 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -5,8 +5,9 @@ * Copyright (C) 2017 SiFive */ +#include +#include #include -#include #include 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,15 @@ 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 (riscv_esp32p4_cache_enabled) { + 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/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c index f8ead7cb7c7d..b8cebf188210 100644 --- a/arch/riscv/mm/cacheflush.c +++ b/arch/riscv/mm/cacheflush.c @@ -4,10 +4,147 @@ */ #include +#include +#include +#include +#include #include +#include #include +#include #include #include +#include + +#ifdef CONFIG_RISCV_ESP32P4_CLIC +bool riscv_esp32p4_cache_enabled __ro_after_init; + +#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(unsigned long start, unsigned long end, bool fatal) +{ + unsigned long flags; + + if (!riscv_esp32p4_cache_enabled) + return 0; + + start = max(start, ESP32P4_PSRAM_START); + end = min(end, ESP32P4_PSRAM_END); + if (start >= end) + return 0; + + start = ALIGN_DOWN(start, ESP32P4_CACHE_MAX_LINE); + end = ALIGN(end, ESP32P4_CACHE_MAX_LINE); + + /* Allow interrupts between chunks if they were enabled on entry. */ + while (start < end) { + u32 size = min_t(u32, end - start, SZ_32K); + int ret; + + local_irq_save(flags); + ret = esp32p4_cache_writeback(start, size); + if (!ret) + ret = esp32p4_cache_sync(ESP32P4_CACHE_L1_I0, start, 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); + start += size; + } + + return 0; +} +NOKPROBE_SYMBOL(esp32p4_sync_text); + +void flush_icache_range(unsigned long start, unsigned long end) +{ + esp32p4_sync_text(start, end, true); + flush_icache_all(); +} +EXPORT_SYMBOL(flush_icache_range); +NOKPROBE_SYMBOL(flush_icache_range); + +int esp32p4_sync_user_text(unsigned long start, unsigned long end) +{ + int ret = esp32p4_sync_text(start, end, false); + + if (!ret) + flush_icache_all(); + + return ret; +} +NOKPROBE_SYMBOL(esp32p4_sync_user_text); +#endif #ifdef CONFIG_SMP 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 #include #include +#include #include #include #include @@ -40,7 +41,6 @@ #include #include -#include #include #include @@ -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 #include #include #include @@ -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 */ diff --git a/mm/nommu.c b/mm/nommu.c index 6c6f95329674..f310d1e29f09 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -16,6 +16,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include #include #include @@ -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 @@ unsigned long do_mmap(struct file *file, 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 @@ unsigned long do_mmap(struct file *file, /* 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 @@ unsigned long do_mmap(struct file *file, 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