mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nickolai Zeldovich <nickolai@csail.mit.edu>
To: Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexandre Ghiti <alex@ghiti.fr>,
	Andrew Waterman <andrew@sifive.com>,
	Jisheng Zhang <jszhang@kernel.org>,
	Karl Mehltretter <kmehltretter@gmail.com>,
	linux-riscv@lists.infradead.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	Nickolai Zeldovich <nickolai@csail.mit.edu>
Subject: [PATCH v2] riscv: mm: Skip the icache flush for special PTEs
Date: Sun, 15 Nov 2026 21:24:33 -0500	[thread overview]
Message-ID: <20261116022433.49952-1-nickolai@csail.mit.edu> (raw)

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


                 reply	other threads:[~2026-11-16  2:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261116022433.49952-1-nickolai@csail.mit.edu \
    --to=nickolai@csail.mit.edu \
    --cc=alex@ghiti.fr \
    --cc=andrew@sifive.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=jszhang@kernel.org \
    --cc=kmehltretter@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    /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

all inboxes | Powered by JetHome®