Skip to contentSkip to conversation

riscv: stacktrace: fix backtracing through exceptions

PATCH Applied Clément Léger 3 participants 3 messages Dec 9, 2024 – Jan 9, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: stacktrace: fix backtracing through exceptions

Prior to commit 5d5fc33ce58e ("riscv: Improve exception and system call latency"), backtrace through exception worked since ra was filled with ret_from_exception symbol address and the stacktrace code checked 'pc' to be equal to that symbol. Now that handle_exception uses regular 'call' instructions, this isn't working anymore and backtrace stops at handle_exception(). Since there are multiple call site to C code in the exception handling path, rather than checking multiple potential return addresses, add a new symbol at the end of exception handling and check pc to be in that range.

Fixes 5d5fc33ce58e ("riscv: Improve exception and system call latency")
---
 arch/riscv/kernel/entry.S      | 1 +
 arch/riscv/kernel/stacktrace.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
arch/riscv/kernel/entry.S +1 -0
index 216581835eb0..33a5a9f2a0d4 100644
@@ -278,6 +278,7 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
 #else
 	sret
 #endif
+SYM_INNER_LABEL(ret_from_exception_end, SYM_L_GLOBAL)
 SYM_CODE_END(ret_from_exception)
 ASM_NOKPROBE(ret_from_exception)
 
arch/riscv/kernel/stacktrace.c +3 -1
index 153a2db4c5fa..d4355c770c36 100644
@@ -17,6 +17,7 @@
 #ifdef CONFIG_FRAME_POINTER
 
 extern asmlinkage void handle_exception(void);
+extern unsigned long ret_from_exception_end;
 
 static inline int fp_is_valid(unsigned long fp, unsigned long sp)
 {
@@ -71,7 +72,8 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
 			fp = frame->fp;
 			pc = ftrace_graph_ret_addr(current, &graph_idx, frame->ra,
 						   &frame->ra);
-			if (pc == (unsigned long)handle_exception) {
+			if (pc >= (unsigned long)handle_exception &&
+			    pc < (unsigned long)&ret_from_exception_end) {
 				if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
 					break;
 
Signature · 1 line
2.45.2
reviewer Tested-by Reviewed-by on v1 1/1
#1 lore raw

Hi Clément,

Quoted 43 lines · Clément Léger

Prior to commit 5d5fc33ce58e ("riscv: Improve exception and system call latency"), backtrace through exception worked since ra was filled with ret_from_exception symbol address and the stacktrace code checked 'pc' to be equal to that symbol. Now that handle_exception uses regular 'call' instructions, this isn't working anymore and backtrace stops at handle_exception(). Since there are multiple call site to C code in the exception handling path, rather than checking multiple potential return addresses, add a new symbol at the end of exception handling and check pc to be in that range.

Fixes 5d5fc33ce58e ("riscv: Improve exception and system call latency")
---
  arch/riscv/kernel/entry.S      | 1 +
  arch/riscv/kernel/stacktrace.c | 4 +++-
  2 files changed, 4 insertions(+), 1 deletion(-)
arch/riscv/kernel/entry.S +1 -0
index 216581835eb0..33a5a9f2a0d4 100644
@@ -278,6 +278,7 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
  #else
  	sret
  #endif
+SYM_INNER_LABEL(ret_from_exception_end, SYM_L_GLOBAL)
  SYM_CODE_END(ret_from_exception)
  ASM_NOKPROBE(ret_from_exception)
 
arch/riscv/kernel/stacktrace.c +1 -0
index 153a2db4c5fa..d4355c770c36 100644
@@ -17,6 +17,7 @@
  #ifdef CONFIG_FRAME_POINTER
 
  extern asmlinkage void handle_exception(void);
+extern unsigned long ret_from_exception_end;
 
  static inline int fp_is_valid(unsigned long fp, unsigned long sp)
  {

arch/riscv/kernel/stacktrace.c void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, · line 79

Quoted 9 lines
arch/riscv/kernel/stacktrace.c +2 -1
@@ -71,7 +72,8 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
  			fp = frame->fp;
  			pc = ftrace_graph_ret_addr(current, &graph_idx, frame->ra,
  						   &frame->ra);
-			if (pc == (unsigned long)handle_exception) {
+			if (pc >= (unsigned long)handle_exception &&
+			    pc < (unsigned long)&ret_from_exception_end) {
  				if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc)))
  					break;
 

You can add:

Thanks,

Alex

#2 lore raw
In reply to Clément Léger
This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

Quoted 11 lines · On Mon, 9 Dec 2024 16:57:12 +0100 you wrote:

Prior to commit 5d5fc33ce58e ("riscv: Improve exception and system call latency"), backtrace through exception worked since ra was filled with ret_from_exception symbol address and the stacktrace code checked 'pc' to be equal to that symbol. Now that handle_exception uses regular 'call' instructions, this isn't working anymore and backtrace stops at handle_exception(). Since there are multiple call site to C code in the exception handling path, rather than checking multiple potential return addresses, add a new symbol at the end of exception handling and check pc to be in that range.

[...]

Here is the summary with links:
- riscv: stacktrace: fix backtracing through exceptions

    https://git.kernel.org/riscv/c/51356ce60e59

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html