Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] riscv: mm: Skip the icache flush for special PTEs
@ 2026-11-16  2:24 Nickolai Zeldovich
  2026-11-17 17:59 ` Ze Huang
  0 siblings, 1 reply; 2+ messages in thread
From: Nickolai Zeldovich @ 2026-11-16  2:24 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Alexandre Ghiti, Andrew Waterman, Jisheng Zhang, Karl Mehltretter,
	linux-riscv, linux-mm, linux-kernel, Nickolai Zeldovich

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@gmail.com>
Reviewed-by: Jisheng Zhang <jszhang@kernel.org>
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
---

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@csail.mit.edu/
    
    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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] riscv: mm: Skip the icache flush for special PTEs
  2026-11-16  2:24 [PATCH v2] riscv: mm: Skip the icache flush for special PTEs Nickolai Zeldovich
@ 2026-11-17 17:59 ` Ze Huang
  0 siblings, 0 replies; 2+ messages in thread
From: Ze Huang @ 2026-11-17 17:59 UTC (permalink / raw)
  To: Nickolai Zeldovich, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Alexandre Ghiti, Andrew Waterman, Jisheng Zhang, Karl Mehltretter,
	linux-riscv, linux-mm, linux-kernel, owner-linux-mm

On Mon Nov 16, 2026 at 10:24 AM CST, Nickolai Zeldovich wrote:
> 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.

Tested on a SpacemiT K3 with v7.4-rc3.

mmap() of the pinctrl@d401e000 MMIO window from /dev/mem with
PROT_READ|PROT_EXEC oopses in flush_icache_pte() without the patch and
suceeds with it. PROT_READ-only mappings and anonymous executable pages
are unaffected.

Tested-by: Ze Huang <ze.huang@oss.qualcomm.com>

>
> Fixes: 08f051eda33b ("RISC-V: Flush I$ when making a dirty page executable")
> Tested-by: Karl Mehltretter <kmehltretter@gmail.com>
> Reviewed-by: Jisheng Zhang <jszhang@kernel.org>
> Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
> ---
>
> 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@csail.mit.edu/
>     
>     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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17 17:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  2:24 [PATCH v2] riscv: mm: Skip the icache flush for special PTEs Nickolai Zeldovich
2026-11-17 17:59 ` Ze Huang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox