Skip to content

Commit 60b1f57

Browse files
Jeff Xierostedt
authored andcommitted
ftrace: Get the true parent ip for function tracer
When using both function tracer and function graph simultaneously, it is found that function tracer sometimes captures a fake parent ip (return_to_handler) instead of the true parent ip. This issue is easy to reproduce. Below are my reproduction steps: jeff-labs:~/bin # ./trace-net.sh jeff-labs:~/bin # cat /sys/kernel/debug/tracing/instances/foo/trace | grep return_to_handler trace-net.sh-405 [001] ...2. 31.859501: avc_has_perm+0x4/0x190 <-return_to_handler+0x0/0x40 trace-net.sh-405 [001] ...2. 31.859503: simple_setattr+0x4/0x70 <-return_to_handler+0x0/0x40 trace-net.sh-405 [001] ...2. 31.859503: truncate_pagecache+0x4/0x60 <-return_to_handler+0x0/0x40 trace-net.sh-405 [001] ...2. 31.859505: unmap_mapping_range+0x4/0x140 <-return_to_handler+0x0/0x40 trace-net.sh-405 [001] ...3. 31.859508: _raw_spin_unlock+0x4/0x30 <-return_to_handler+0x0/0x40 [...] The following is my simple trace script: <snip> jeff-labs:~/bin # cat ./trace-net.sh TRACE_PATH="/sys/kernel/tracing" set_events() { echo 1 > $1/events/net/enable echo 1 > $1/events/tcp/enable echo 1 > $1/events/sock/enable echo 1 > $1/events/napi/enable echo 1 > $1/events/fib/enable echo 1 > $1/events/neigh/enable } set_events ${TRACE_PATH} echo 1 > ${TRACE_PATH}/options/sym-offset echo 1 > ${TRACE_PATH}/options/funcgraph-tail echo 1 > ${TRACE_PATH}/options/funcgraph-proc echo 1 > ${TRACE_PATH}/options/funcgraph-abstime echo 'tcp_orphan*' > ${TRACE_PATH}/set_ftrace_notrace echo function_graph > ${TRACE_PATH}/current_tracer INSTANCE_FOO=${TRACE_PATH}/instances/foo if [ ! -e $INSTANCE_FOO ]; then mkdir ${INSTANCE_FOO} fi set_events ${INSTANCE_FOO} echo 1 > ${INSTANCE_FOO}/options/sym-offset echo 'tcp_orphan*' > ${INSTANCE_FOO}/set_ftrace_notrace echo function > ${INSTANCE_FOO}/current_tracer echo 1 > ${TRACE_PATH}/tracing_on echo 1 > ${INSTANCE_FOO}/tracing_on echo > ${TRACE_PATH}/trace echo > ${INSTANCE_FOO}/trace </snip> Link: https://lore.kernel.org/20241008033159.22459-1-jeff.xie@linux.dev Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Signed-off-by: Jeff Xie <jeff.xie@linux.dev> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 6371b4b commit 60b1f57

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

kernel/trace/trace_functions.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ static void function_trace_start(struct trace_array *tr)
176176
tracing_reset_online_cpus(&tr->array_buffer);
177177
}
178178

179+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
180+
static __always_inline unsigned long
181+
function_get_true_parent_ip(unsigned long parent_ip, struct ftrace_regs *fregs)
182+
{
183+
unsigned long true_parent_ip;
184+
int idx = 0;
185+
186+
true_parent_ip = parent_ip;
187+
if (unlikely(parent_ip == (unsigned long)&return_to_handler) && fregs)
188+
true_parent_ip = ftrace_graph_ret_addr(current, &idx, parent_ip,
189+
(unsigned long *)ftrace_regs_get_stack_pointer(fregs));
190+
return true_parent_ip;
191+
}
192+
#else
193+
static __always_inline unsigned long
194+
function_get_true_parent_ip(unsigned long parent_ip, struct ftrace_regs *fregs)
195+
{
196+
return parent_ip;
197+
}
198+
#endif
199+
179200
static void
180201
function_trace_call(unsigned long ip, unsigned long parent_ip,
181202
struct ftrace_ops *op, struct ftrace_regs *fregs)
@@ -192,6 +213,8 @@ function_trace_call(unsigned long ip, unsigned long parent_ip,
192213
if (bit < 0)
193214
return;
194215

216+
parent_ip = function_get_true_parent_ip(parent_ip, fregs);
217+
195218
trace_ctx = tracing_gen_ctx();
196219

197220
data = this_cpu_ptr(tr->array_buffer.data);
@@ -239,6 +262,7 @@ function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
239262
* recursive protection is performed.
240263
*/
241264
local_irq_save(flags);
265+
parent_ip = function_get_true_parent_ip(parent_ip, fregs);
242266
cpu = raw_smp_processor_id();
243267
data = per_cpu_ptr(tr->array_buffer.data, cpu);
244268
disabled = atomic_inc_return(&data->disabled);
@@ -306,6 +330,7 @@ function_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip,
306330
if (bit < 0)
307331
return;
308332

333+
parent_ip = function_get_true_parent_ip(parent_ip, fregs);
309334
data = this_cpu_ptr(tr->array_buffer.data);
310335
if (atomic_read(&data->disabled))
311336
goto out;
@@ -352,6 +377,7 @@ function_stack_no_repeats_trace_call(unsigned long ip, unsigned long parent_ip,
352377
* recursive protection is performed.
353378
*/
354379
local_irq_save(flags);
380+
parent_ip = function_get_true_parent_ip(parent_ip, fregs);
355381
cpu = raw_smp_processor_id();
356382
data = per_cpu_ptr(tr->array_buffer.data, cpu);
357383
disabled = atomic_inc_return(&data->disabled);

0 commit comments

Comments
 (0)