[PATCH v2] riscv: mm: Skip the icache flush for special PTEs

From: Nickolai Zeldovich

Date: Sun Nov 15 2026 - 21:24:49 EST


An executable mmap() of memory outside RAM through remap_pfn_range() or
vmf_insert_pfn(), such as /dev/mem or a PCI BAR's sysfs resource file,
oopses in flush_icache_pte() with the page table lock held, and the task
never exits. With FLATMEM the kernel instead sets a bit in whatever
memory lies past mem_map.

__set_pte_at() calls flush_icache_pte() for every present executable
PTE, and flush_icache_pte() records the flush in the folio of
pte_page(). A special PTE may have no struct page behind it.

Skip the flush for special PTEs, as arm64 does since
commit 71fdb6bf61bf ("arm64: Do not synchronise I and D caches for
special ptes").

Reproduced on QEMU virt with the riscv64 defconfig by mapping the PCIe
ECAM window from /dev/mem with PROT_EXEC; the patched kernel maps it.

Fixes: 08f051eda33b ("RISC-V: Flush I$ when making a dirty page executable")
Tested-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
Reviewed-by: Jisheng Zhang <jszhang@xxxxxxxxxx>
Signed-off-by: Nickolai Zeldovich <nickolai@xxxxxxxxxxxxx>
---

Notes:
Changes in v2:
- Reworded the source comment as suggested by Jisheng Zhang.
- Shortened the commit message.
- Rebased on v7.4-rc3.

v1: https://lore.kernel.org/all/20261017013634.792220-1-nickolai@xxxxxxxxxxxxx/

This patch was prepared with the help of an AI coding assistant.

Reproducer (QEMU virt, riscv64 defconfig, run as /init with /dev/mem
c 1 1 in the initramfs; 0x30000000 is the PCIe ECAM window, any non-RAM
range does):

riscv64-linux-gnu-gcc -O2 -static -o test exec_mmio_mmap.c
qemu-system-riscv64 -M virt -cpu max -smp 2 -m 1G -nographic \
-kernel Image -initrd initramfs.cpio -append "console=ttyS0 rdinit=/init"

unpatched: Unable to handle kernel paging request at virtual address
ff1c000000c00008, epc : flush_icache_pte+0x5c/0xa2,
ra : do_remap_pfn_range+0x284/0x3e6; the task never exits
patched: mapped at 0x7fff8418b000, first word 0x00081b36

#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

int main(int argc, char **argv)
{
unsigned long phys = argc > 1 ? strtoul(argv[1], NULL, 0) : 0x30000000UL;
volatile uint32_t *p;
int fd;

fd = open("/dev/mem", O_RDONLY | O_SYNC);
if (fd < 0) {
perror("open /dev/mem");
return 2;
}
printf("mmap(/dev/mem, phys 0x%lx, PROT_READ|PROT_EXEC, MAP_SHARED)\n", phys);
fflush(stdout);
p = mmap(NULL, 4096, PROT_READ | PROT_EXEC, MAP_SHARED, fd, phys);
if (p == MAP_FAILED) {
perror("mmap");
return 2;
}
printf("mapped at %p, first word 0x%08x\n", p, p[0]);
return 0;
}

arch/riscv/include/asm/pgtable.h | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h
index 2ebd5fa5adc5..48eeeac1019b 100644
--- a/arch/riscv/include/asm/pgtable.h
+++ b/arch/riscv/include/asm/pgtable.h
@@ -624,7 +624,11 @@ void flush_icache_pte(struct mm_struct *mm, pte_t pte);

static inline void __set_pte_at(struct mm_struct *mm, pte_t *ptep, pte_t pteval)
{
- if (pte_present(pteval) && pte_exec(pteval))
+ /*
+ * Special PTE mappings are not intended to be executable and may not
+ * even have an associated struct page.
+ */
+ if (pte_present(pteval) && pte_exec(pteval) && !pte_special(pteval))
flush_icache_pte(mm, pteval);

set_pte(ptep, pteval);

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.53.0