[PATCH] riscv: stacktrace: Fix NULL task pointer in ftrace_graph_ret_addr()

zhang.anmeng at zte.com.cn zhang.anmeng at zte.com.cn
Tue Jul 7 18:50:46 PDT 2026


From: Anmeng Zhang <zhang.anmeng at zte.com.cn>

When dump_backtrace() is called with a NULL task argument, and the
call stack contains functions that are being traced,
walk_stackframe() eventually calls ftrace_graph_ret_addr() with the
NULL task pointer, which results in a NULL pointer dereference when
accessing task->curr_ret_stack.

Fix this by assigning the task pointer to the current task in
walk_stackframe() when it is NULL, keeping the behavior consistent
with other architectures.

Fixes: ecbf894165a2 ("riscv: Fix ftrace_graph_ret_addr() to use the correct task pointer")
Reviewed-by: Shengming Hu <hu.shengming at zte.com.cn>
Reviewed-by: Rui Gao <gao.rui at zte.com.cn>
Reviewed-by: Luo Haiyang <luo.haiyang at zte.com.cn>
Reviewed-by: Run Zhang <zhang.run at zte.com.cn>
Signed-off-by: Anmeng Zhang <zhang.anmeng at zte.com.cn>
---
 arch/riscv/kernel/stacktrace.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index c7555447149b..d19545efb5dd 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -52,11 +52,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
 	int graph_idx = 0;
 	int level = 0;

+	if (task == NULL)
+		task = current;
+
 	if (regs) {
 		fp = frame_pointer(regs);
 		sp = user_stack_pointer(regs);
 		pc = instruction_pointer(regs);
-	} else if (task == NULL || task == current) {
+	} else if (task == current) {
 		fp = (unsigned long)__builtin_frame_address(0);
 		sp = current_stack_pointer;
 		pc = (unsigned long)walk_stackframe;
-- 
2.43.5



More information about the linux-riscv mailing list