Skip to contentSkip to conversation

riscv: kprobes/ftrace: Add recursion protection to the ftrace callback

PATCH In review Jisheng Zhang 3 participants 3 messages Mar 29, 2021 – Apr 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: kprobes/ftrace: Add recursion protection to the ftrace callback

From: Jisheng Zhang <[email protected]>

Currently, the riscv's kprobes(powerred by ftrace) handler is preemptible. Futher check indicates we miss something similar as the commit c536aa1c5b17 ("kprobes/ftrace: Add recursion protection to the ftrace callback"), so do similar modifications as the commit does.

Fixes 829adda597fe ("riscv: Add KPROBES_ON_FTRACE supported")
---
 arch/riscv/kernel/probes/ftrace.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/riscv/kernel/probes/ftrace.c +10 -1
index 17ca5e923bb0..aab85a82f419 100644
@@ -9,10 +9,16 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 	struct kprobe *p;
 	struct pt_regs *regs;
 	struct kprobe_ctlblk *kcb;
+	int bit;
 
+	bit = ftrace_test_recursion_trylock(ip, parent_ip);
+	if (bit < 0)
+		return;
+
+	preempt_disable_notrace();
 	p = get_kprobe((kprobe_opcode_t *)ip);
 	if (unlikely(!p) || kprobe_disabled(p))
-		return;
+		goto out;
 
 	regs = ftrace_get_regs(fregs);
 	kcb = get_kprobe_ctlblk();
@@ -45,6 +51,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 		 */
 		__this_cpu_write(current_kprobe, NULL);
 	}
+out:
+	preempt_enable_notrace();
+	ftrace_test_recursion_unlock(bit);
 }
 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
 
Signature · 1 line
2.31.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Tue, 30 Mar 2021 02:14:40 +0800 Jisheng Zhang <[email protected]>…

From: Jisheng Zhang <[email protected]>

Currently, the riscv's kprobes(powerred by ftrace) handler is preemptible. Futher check indicates we miss something similar as the commit c536aa1c5b17 ("kprobes/ftrace: Add recursion protection to the ftrace callback"), so do similar modifications as the commit does.

Fixes 829adda597fe ("riscv: Add KPROBES_ON_FTRACE supported")

Looks fine to me.

-- Steve

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#2 lore raw
In reply to Jisheng Zhang
Quoted 36 lines · On Mon, 29 Mar 2021 11:14:40 PDT (-0700), [email protected] wrote:

From: Jisheng Zhang <[email protected]>

Currently, the riscv's kprobes(powerred by ftrace) handler is preemptible. Futher check indicates we miss something similar as the commit c536aa1c5b17 ("kprobes/ftrace: Add recursion protection to the ftrace callback"), so do similar modifications as the commit does.

Fixes 829adda597fe ("riscv: Add KPROBES_ON_FTRACE supported")
---
 arch/riscv/kernel/probes/ftrace.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/riscv/kernel/probes/ftrace.c +7 -1
index 17ca5e923bb0..aab85a82f419 100644
@@ -9,10 +9,16 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 	struct kprobe *p;
 	struct pt_regs *regs;
 	struct kprobe_ctlblk *kcb;
+	int bit;
 
+	bit = ftrace_test_recursion_trylock(ip, parent_ip);
+	if (bit < 0)
+		return;
+
+	preempt_disable_notrace();
 	p = get_kprobe((kprobe_opcode_t *)ip);
 	if (unlikely(!p) || kprobe_disabled(p))
-		return;
+		goto out;
 
 	regs = ftrace_get_regs(fregs);
 	kcb = get_kprobe_ctlblk();

arch/riscv/kernel/probes/ftrace.c void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, · line 58

Quoted 8 lines
arch/riscv/kernel/probes/ftrace.c +3 -0
@@ -45,6 +51,9 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 		 */
 		__this_cpu_write(current_kprobe, NULL);
 	}
+out:
+	preempt_enable_notrace();
+	ftrace_test_recursion_unlock(bit);
 }
 NOKPROBE_SYMBOL(kprobe_ftrace_handler);

Thanks, this is on fixes.