[PATCH v12 05/11] riscv: mm: avoid enabling interrupts for nofault page faults

From: Zhanpeng Zhang

Date: Thu Oct 29 2026 - 03:53:59 EST


RISC-V enables interrupts in handle_page_fault() before checking whether
the fault occurred with fault handling disabled. A nofault access from an
atomic context can therefore run tracepoints and open an interrupt window
before reaching the exception-table fixup.

Handle an exception-table entry before entering the generic fault path when
fault handling is disabled. Also keep interrupts disabled until such a
fault has been resolved.

On 32-bit RISC-V the vmalloc mapping must still be synchronised into the
current page table before that fixup runs: otherwise a nofault access to a
not-yet-synced vmalloc address would take the exception-table fixup and
fail spuriously instead of being resolved. Order the vmalloc
synchronisation ahead of the nofault fixup. That synchronisation only
copies an already-established kernel mapping, takes no locks and waits for
no I/O, so it is safe in the nofault path; if the mapping does not exist it
falls through to the existing no_context() error handling. This early
return skips the page-fault tracepoints, as before.

This makes RISC-V consistent with the expectation that an in-atomic
nofault access does not enter the normal fault-handling path. It also
removes one source of re-entry when perf sampling is delivered through an
SBI Supervisor Software Event (SSE).

Signed-off-by: Zhanpeng Zhang <zhangzhanpeng.jasper@xxxxxxxxxxxxx>
---
arch/riscv/mm/fault.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/mm/fault.c b/arch/riscv/mm/fault.c
index 04ed6f8acae..3934248f7a2 100644
--- a/arch/riscv/mm/fault.c
+++ b/arch/riscv/mm/fault.c
@@ -294,19 +294,23 @@ void handle_page_fault(struct pt_regs *regs)
if (kprobe_page_fault(regs, cause))
return;

- if (user_mode(regs))
- trace_page_fault_user(addr, regs, cause);
- else
- trace_page_fault_kernel(addr, regs, cause);
-
/*
* Fault-in kernel-space virtual memory on-demand.
* The 'reference' page table is init_mm.pgd.
*
+ * This only synchronises an already-established kernel vmalloc mapping
+ * into the current page table; it neither allocates memory nor waits for
+ * I/O, so it is safe in the nofault path. It must run before the
+ * exception-table fixup below: otherwise a nofault access to a
+ * not-yet-synced vmalloc address would take the fixup and fail
+ * spuriously instead of being resolved. If the mapping does not exist,
+ * vmalloc_fault() falls through to no_context() error handling.
+ *
* NOTE! We MUST NOT take any locks for this case. We may
* be in an interrupt or a critical region, and should
* only copy the information from the master page table,
- * nothing more.
+ * nothing more. This path returns early and therefore skips the
+ * trace_page_fault_*() tracepoints below.
*/
if ((!IS_ENABLED(CONFIG_MMU) || !IS_ENABLED(CONFIG_64BIT)) &&
unlikely(addr >= VMALLOC_START && addr < VMALLOC_END)) {
@@ -314,8 +318,20 @@ void handle_page_fault(struct pt_regs *regs)
return;
}

- /* Enable interrupts if they were enabled in the parent context. */
- if (!regs_irqs_disabled(regs))
+ /*
+ * Nofault accesses must be resolved through the exception table before
+ * entering the generic fault path or enabling interrupts.
+ */
+ if (unlikely(faulthandler_disabled()) && fixup_exception(regs))
+ return;
+
+ if (user_mode(regs))
+ trace_page_fault_user(addr, regs, cause);
+ else
+ trace_page_fault_kernel(addr, regs, cause);
+
+ /* Do not open an interrupt window before a nofault fixup completes. */
+ if (!regs_irqs_disabled(regs) && !faulthandler_disabled())
local_irq_enable();

/*
--
2.50.1 (Apple Git-155)