From: Tao Cui Enabling the function graph tracer on LoongArch breaks tracing: the recorded call tree does not match what the traced function actually calls, and set_graph_function cannot scope the trace to the requested function. With set_graph_function set to vfs_write, the tracer records a bogus vfs_write frame whose children are unrelated IRQ handlers: 0) | vfs_write() { 0) | irq_enter_rcu() { 0) ! 130.380 us | irqtime_account_irq(); 0) | handle_cpu_irq() { 0) | hrtimer_interrupt() { ... Worse, for shallow frames the tracer writes 8 bytes below the thread stack, and LoongArch has no VMAP_STACK, so neighbouring kernel allocations get corrupted. The offset of the saved return address is computed by ftrace_get_parent_ra_addr(), which scans the function prologue for the "st.d ra, sp, imm" instruction. is_ra_save_ins() only accepts a non-negative imm, so the slot is sp + imm, but the offset is evaluated as -((1 << 12) - imm), i.e. imm - 4096: the tracer reads the parent address from, and stores its return hook 4096 bytes below, the real slot. Compute the offset directly from the encoded immediate. The graph tracer then records the scoped subtree: 1) | vfs_write() { 1) | rw_verify_area() { 1) ! 125.050 us | security_file_permission(); 1) | } This path is reached only through the function graph tracer; plain function tracing passes the parent address in registers. Fixes: dbe3ba3018ec1fc5 ("LoongArch/ftrace: Add basic support") Cc: stable@vger.kernel.org Signed-off-by: Tao Cui --- arch/loongarch/kernel/ftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/loongarch/kernel/ftrace.c b/arch/loongarch/kernel/ftrace.c index 8c3ec1bc7aad..acdd5f1f8426 100644 --- a/arch/loongarch/kernel/ftrace.c +++ b/arch/loongarch/kernel/ftrace.c @@ -36,7 +36,7 @@ static int ftrace_get_parent_ra_addr(unsigned long insn_addr, int *ra_off) limit--; if (is_ra_save_ins(insn)) - *ra_off = -((1 << 12) - insn->reg2i12_format.immediate); + *ra_off = insn->reg2i12_format.immediate; } while (!is_stack_alloc_ins(insn) && limit); -- 2.43.0