[PATCH v2] riscv: traps: handle uprobe event in software-check exception

Zong Li zong.li at sifive.com
Wed Jun 4 02:34:03 PDT 2025


Handle the uprobe event first before handling the CFI violation in
software-check exception handler. Because when the landing pad is
activated, if the uprobe point is set at the lpad instruction at
the beginning of a function, the system triggers a software-check
exception instead of an ebreak exception due to the exception
priority, then uprobe can't work successfully.

Co-developed-by: Deepak Gupta <debug at rivosinc.com>
Signed-off-by: Deepak Gupta <debug at rivosinc.com>
Signed-off-by: Zong Li <zong.li at sifive.com>
---

Changed in v2:
- Handle uprobe events only in the forward CFI case
- Move uprobe handling from do_trap_software_check to handle_user_cfi_violation

 arch/riscv/kernel/traps.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index d7b5fd576011..3aa5ecda29a8 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -369,17 +369,25 @@ void do_trap_ecall_u(struct pt_regs *regs)
 /* handle cfi violations */
 bool handle_user_cfi_violation(struct pt_regs *regs)
 {
-	bool ret = false;
 	unsigned long tval = csr_read(CSR_TVAL);
-
-	if ((tval == CFI_TVAL_FCFI_CODE && cpu_supports_indirect_br_lp_instr()) ||
-	    (tval == CFI_TVAL_BCFI_CODE && cpu_supports_shadow_stack())) {
+	bool is_fcfi = (tval == CFI_TVAL_FCFI_CODE && cpu_supports_indirect_br_lp_instr());
+	bool is_bcfi = (tval == CFI_TVAL_BCFI_CODE && cpu_supports_shadow_stack());
+
+	/*
+	 * Handle uprobe event frist. The probe point can be a valid target
+	 * of indirect jumps or calls, in this case, forward cfi violation
+	 * will be triggered instead of breakpoint exception.
+	 */
+	if (is_fcfi && probe_breakpoint_handler(regs))
+		return true;
+
+	if (is_fcfi || is_bcfi) {
 		do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc,
 			      "Oops - control flow violation");
-		ret = true;
+		return true;
 	}
 
-	return ret;
+	return false;
 }
 
 /*
-- 
2.17.1




More information about the linux-riscv mailing list