Skip to content

Commit 95bb5b6

Browse files
lienzechenhuacai
authored andcommitted
LoongArch: Get partial stack information when providing regs parameter
Currently, arch_stack_walk() can only get the full stack information including NMI. This is because the implementation of arch_stack_walk() is forced to ignore the information passed by the regs parameter and use the current stack information instead. For some detection systems like KFENCE, only partial stack information is needed. In particular, the stack frame where the interrupt occurred. To support KFENCE, this patch modifies the implementation of the arch_stack_walk() function so that if this function is called with the regs argument passed, it retains all the stack information in regs and uses it to provide accurate information. Before this patch: [ 1.531195 ] ================================================================== [ 1.531442 ] BUG: KFENCE: out-of-bounds read in stack_trace_save_regs+0x48/0x6c [ 1.531442 ] [ 1.531900 ] Out-of-bounds read at 0xffff800012267fff (1B left of kfence-#12): [ 1.532046 ] stack_trace_save_regs+0x48/0x6c [ 1.532169 ] kfence_report_error+0xa4/0x528 [ 1.532276 ] kfence_handle_page_fault+0x124/0x270 [ 1.532388 ] no_context+0x50/0x94 [ 1.532453 ] do_page_fault+0x1a8/0x36c [ 1.532524 ] tlb_do_page_fault_0+0x118/0x1b4 [ 1.532623 ] test_out_of_bounds_read+0xa0/0x1d8 [ 1.532745 ] kunit_generic_run_threadfn_adapter+0x1c/0x28 [ 1.532854 ] kthread+0x124/0x130 [ 1.532922 ] ret_from_kernel_thread+0xc/0xa4 <snip> After this patch: [ 1.320220 ] ================================================================== [ 1.320401 ] BUG: KFENCE: out-of-bounds read in test_out_of_bounds_read+0xa8/0x1d8 [ 1.320401 ] [ 1.320898 ] Out-of-bounds read at 0xffff800012257fff (1B left of kfence-#10): [ 1.321134 ] test_out_of_bounds_read+0xa8/0x1d8 [ 1.321264 ] kunit_generic_run_threadfn_adapter+0x1c/0x28 [ 1.321392 ] kthread+0x124/0x130 [ 1.321459 ] ret_from_kernel_thread+0xc/0xa4 <snip> Suggested-by: Jinyang He <[email protected]> Signed-off-by: Enze Li <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 8b5cb1c commit 95bb5b6

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

arch/loongarch/kernel/stacktrace.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
1818
struct pt_regs dummyregs;
1919
struct unwind_state state;
2020

21-
regs = &dummyregs;
21+
if (!regs) {
22+
regs = &dummyregs;
2223

23-
if (task == current) {
24-
regs->regs[3] = (unsigned long)__builtin_frame_address(0);
25-
regs->csr_era = (unsigned long)__builtin_return_address(0);
26-
} else {
27-
regs->regs[3] = thread_saved_fp(task);
28-
regs->csr_era = thread_saved_ra(task);
24+
if (task == current) {
25+
regs->regs[3] = (unsigned long)__builtin_frame_address(0);
26+
regs->csr_era = (unsigned long)__builtin_return_address(0);
27+
} else {
28+
regs->regs[3] = thread_saved_fp(task);
29+
regs->csr_era = thread_saved_ra(task);
30+
}
31+
regs->regs[1] = 0;
2932
}
3033

31-
regs->regs[1] = 0;
3234
for (unwind_start(&state, task, regs);
3335
!unwind_done(&state) && !unwind_error(&state); unwind_next_frame(&state)) {
3436
addr = unwind_get_return_address(&state);

0 commit comments

Comments
 (0)