Skip to content

Commit 5bb8d34

Browse files
MQ-mengqingchenhuacai
authored andcommitted
LoongArch: Use correct sp value to get graph addr in stack unwinders
The stack frame when function_graph enable like follows, --------- <- function sp_on_entry | | | FAKE_RA <- sp_on_entry - sizeof(pt_regs) + PT_R1 | --------- <- sp_on_entry - sizeof(pt_regs) So if we want to get the &FAKE_RA we should get sp_on_entry first. In the unwinder_prologue case, we can get the sp_on_entry as state->sp, because we try to calculate each CFA and the ra saved address. But in the unwinder_guess case, we cannot get it because we do not try to calculate the CFA. Although LoongArch have not fixed frame, the $ra is saved at CFA - 8 in most cases, we can try guess, too. As we store the pc in state, we not need to dereference state->sp, too. Signed-off-by: Jinyang He <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 429a967 commit 5bb8d34

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

arch/loongarch/include/asm/ftrace.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#define FTRACE_REGS_PLT_IDX 1
1111
#define NR_FTRACE_PLTS 2
1212

13-
#define GRAPH_FAKE_OFFSET (sizeof(struct pt_regs) - offsetof(struct pt_regs, regs[1]))
14-
1513
#ifdef CONFIG_FUNCTION_TRACER
1614

1715
#define MCOUNT_INSN_SIZE 4 /* sizeof mcount call */

arch/loongarch/include/asm/unwind.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#define _ASM_UNWIND_H
99

1010
#include <linux/sched.h>
11+
#include <linux/ftrace.h>
1112

13+
#include <asm/ptrace.h>
1214
#include <asm/stacktrace.h>
1315

1416
enum unwinder_type {
@@ -40,4 +42,12 @@ static inline bool unwind_error(struct unwind_state *state)
4042
return state->error;
4143
}
4244

45+
#define GRAPH_FAKE_OFFSET (sizeof(struct pt_regs) - offsetof(struct pt_regs, regs[1]))
46+
47+
static inline unsigned long unwind_graph_addr(struct unwind_state *state,
48+
unsigned long pc, unsigned long cfa)
49+
{
50+
return ftrace_graph_ret_addr(state->task, &state->graph_idx,
51+
pc, (unsigned long *)(cfa - GRAPH_FAKE_OFFSET));
52+
}
4353
#endif /* _ASM_UNWIND_H */

arch/loongarch/kernel/unwind_guess.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ unsigned long unwind_get_return_address(struct unwind_state *state)
1111
{
1212
if (unwind_done(state))
1313
return 0;
14-
else if (state->first)
15-
return state->pc;
1614

17-
return *(unsigned long *)(state->sp);
15+
return state->pc;
1816
}
1917
EXPORT_SYMBOL_GPL(unwind_get_return_address);
2018

@@ -36,7 +34,7 @@ void unwind_start(struct unwind_state *state, struct task_struct *task,
3634

3735
state->task = task;
3836
state->first = true;
39-
37+
state->pc = unwind_graph_addr(state, state->pc, state->sp);
4038
get_stack_info(state->sp, state->task, &state->stack_info);
4139

4240
if (!unwind_done(state) && !__kernel_text_address(state->pc))
@@ -60,9 +58,8 @@ bool unwind_next_frame(struct unwind_state *state)
6058
state->sp < info->end;
6159
state->sp += sizeof(unsigned long)) {
6260
addr = *(unsigned long *)(state->sp);
63-
state->pc = ftrace_graph_ret_addr(state->task, &state->graph_idx,
64-
addr, (unsigned long *)(state->sp - GRAPH_FAKE_OFFSET));
65-
if (__kernel_text_address(addr))
61+
state->pc = unwind_graph_addr(state, addr, state->sp + 8);
62+
if (__kernel_text_address(state->pc))
6663
return true;
6764
}
6865

arch/loongarch/kernel/unwind_prologue.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@ static inline void unwind_state_fixup(struct unwind_state *state)
2121

2222
unsigned long unwind_get_return_address(struct unwind_state *state)
2323
{
24-
2524
if (unwind_done(state))
2625
return 0;
27-
else if (state->type)
28-
return state->pc;
29-
else if (state->first)
30-
return state->pc;
31-
32-
return *(unsigned long *)(state->sp);
3326

27+
return state->pc;
3428
}
3529
EXPORT_SYMBOL_GPL(unwind_get_return_address);
3630

@@ -43,9 +37,8 @@ static bool unwind_by_guess(struct unwind_state *state)
4337
state->sp < info->end;
4438
state->sp += sizeof(unsigned long)) {
4539
addr = *(unsigned long *)(state->sp);
46-
state->pc = ftrace_graph_ret_addr(state->task, &state->graph_idx,
47-
addr, (unsigned long *)(state->sp - GRAPH_FAKE_OFFSET));
48-
if (__kernel_text_address(addr))
40+
state->pc = unwind_graph_addr(state, addr, state->sp + 8);
41+
if (__kernel_text_address(state->pc))
4942
return true;
5043
}
5144

@@ -166,7 +159,7 @@ void unwind_start(struct unwind_state *state, struct task_struct *task,
166159

167160
state->task = task;
168161
state->first = true;
169-
162+
state->pc = unwind_graph_addr(state, state->pc, state->sp);
170163
get_stack_info(state->sp, state->task, &state->stack_info);
171164

172165
if (!unwind_done(state) && !__kernel_text_address(state->pc))
@@ -193,8 +186,7 @@ bool unwind_next_frame(struct unwind_state *state)
193186

194187
case UNWINDER_PROLOGUE:
195188
if (unwind_by_prologue(state)) {
196-
state->pc = ftrace_graph_ret_addr(state->task, &state->graph_idx,
197-
state->pc, (unsigned long *)(state->sp - GRAPH_FAKE_OFFSET));
189+
state->pc = unwind_graph_addr(state, state->pc, state->sp);
198190
return true;
199191
}
200192

@@ -209,8 +201,7 @@ bool unwind_next_frame(struct unwind_state *state)
209201
state->first = true;
210202
state->ra = regs->regs[1];
211203
state->sp = regs->regs[3];
212-
state->pc = ftrace_graph_ret_addr(state->task, &state->graph_idx,
213-
pc, (unsigned long *)(state->sp - GRAPH_FAKE_OFFSET));
204+
state->pc = pc;
214205
get_stack_info(state->sp, state->task, info);
215206

216207
return true;

0 commit comments

Comments
 (0)