Skip to contentSkip to conversation

function_graph: Enable funcgraph-args and funcgraph-retaddr to work simultaneously

PATCH Superseded Donglin Peng 2 participants 5 messages Nov 11, 2025 – Nov 12, 2025

Subsystems (2)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 function_graph: Enable funcgraph-args and funcgraph-retaddr to work simultaneously

From: Donglin Peng <[email protected]>

Currently, the funcgraph-args and funcgraph-retaddr features are mutually exclusive. This patch resolves this limitation by modifying funcgraph-retaddr to adopt the same implementation approach as funcgraph-args, specifically by storing the return address in the first entry of the args array.

As a result, both features now coexist seamlessly and function as intended.

To verify the change, use perf to trace vfs_write with both options enabled:

Before:

 # perf_6_17 ftrace -G vfs_write --graph-opts args,retaddr
 ......
 0)               |  down_read() { /* <-n_tty_write+0xa3/0x540 */
 0)   0.075 us    |    __cond_resched(); /* <-down_read+0x12/0x160 */
 0)   0.079 us    |    preempt_count_add(); /* <-down_read+0x3b/0x160 */
 0)   0.077 us    |    preempt_count_sub(); /* <-down_read+0x8b/0x160 */
 0)   0.754 us    |  }

After:

 # perf_6_17 ftrace -G vfs_write --graph-opts args,retaddr
 ......
 0)               |  down_read(sem=0xffff8880100bea78) { /* <-n_tty_write+0xa3/0x540 */
 0)   0.075 us    |    __cond_resched(); /* <-down_read+0x12/0x160 */
 0)   0.079 us    |    preempt_count_add(val=1); /* <-down_read+0x3b/0x160 */
 0)   0.077 us    |    preempt_count_sub(val=1); /* <-down_read+0x8b/0x160 */
 0)   0.754 us    |  }
Cc Steven Rostedt (Google) <[email protected]>
Cc Sven Schnelle <[email protected]>
Cc Masami Hiramatsu <[email protected]>

---
v2:
- do not remove retaddr event, suggested by Steven
- store the retaddr in args[0], suggested by Steven

---
 include/linux/ftrace.h               |  11 --
 kernel/trace/trace.h                 |   4 -
 kernel/trace/trace_entries.h         |   6 +-
 kernel/trace/trace_functions_graph.c | 148 ++++++++++++---------------
 4 files changed, 71 insertions(+), 98 deletions(-)
include/linux/ftrace.h +0 -11
index 7ded7df6e9b5..88cb54d73bdb 100644
@@ -1129,17 +1129,6 @@ struct ftrace_graph_ent {
 	int depth;
 } __packed;
 
-/*
- * Structure that defines an entry function trace with retaddr.
- * It's already packed but the attribute "packed" is needed
- * to remove extra padding at the end.
- */
-struct fgraph_retaddr_ent {
-	unsigned long func; /* Current function */
-	int depth;
-	unsigned long retaddr;  /* Return address */
-} __packed;
-
 /*
  * Structure that defines a return function trace.
  * It's already packed but the attribute "packed" is needed
kernel/trace/trace.h +0 -4
index 85eabb454bee..9fac291b913a 100644
@@ -955,10 +955,6 @@ extern void graph_trace_close(struct trace_iterator *iter);
 extern int __trace_graph_entry(struct trace_array *tr,
 			       struct ftrace_graph_ent *trace,
 			       unsigned int trace_ctx);
-extern int __trace_graph_retaddr_entry(struct trace_array *tr,
-				struct ftrace_graph_ent *trace,
-				unsigned int trace_ctx,
-				unsigned long retaddr);
 extern void __trace_graph_return(struct trace_array *tr,
 				 struct ftrace_graph_ret *trace,
 				 unsigned int trace_ctx,
kernel/trace/trace_entries.h +3 -3
index de294ae2c5c5..593a74663c65 100644
@@ -95,14 +95,14 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry,
 	TRACE_GRAPH_RETADDR_ENT,
 
 	F_STRUCT(
-		__field_struct(	struct fgraph_retaddr_ent,	graph_ent	)
+		__field_struct(	struct ftrace_graph_ent,	graph_ent	)
 		__field_packed(	unsigned long,	graph_ent,	func		)
 		__field_packed(	unsigned int,	graph_ent,	depth		)
-		__field_packed(	unsigned long,	graph_ent,	retaddr		)
+		__dynamic_array(unsigned long,	args				)
 	),
 
 	F_printk("--> %ps (%u) <- %ps", (void *)__entry->func, __entry->depth,
-		(void *)__entry->retaddr)
+		(void *)__entry->args[0])
 );
 
 #else
kernel/trace/trace_functions_graph.c +68 -80

277 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw
Quoted 10 lines · Steven Rostedt
Quoted 2 lines · On Tue, 11 Nov 2025 21:54:32 +0800 Donglin Peng <[email protected]> wrote:

BTW, are you two people?

You only need one signed-off-by. Which one do you want to keep?

Thanks for catching this. I'll fix it in v3 by keeping only one Signed-off-by - I'll use my Xiaomi email since this is work-related.

Apologies for the oversight.

Quoted 2 lines

-- Steve

maintainer
#3 lore raw
In reply to Donglin Peng

kernel/trace/trace_functions_graph.c static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, · line 176

Quoted 10 lines · On Tue, 11 Nov 2025 21:54:32 +0800 Donglin Peng <[email protected]> wrote:
kernel/trace/trace_functions_graph.c +6 -3
+	/* Store the retaddr in args[0] */
+	if (type == TRACE_GRAPH_RETADDR_ENT)
+		entry->args[i++] = retaddr;
+
 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
-	if (fregs) {
-		for (int i = 0; i < FTRACE_REGS_MAX_ARGS; i++)
-			entry->args[i] = ftrace_regs_get_argument(fregs, i);
+	if (store_args) {
+		while (i < nr_args) {

Nit, this could stay as a for loop. You just don't initialize the 'i':

		for (; i < nr_args; i++) {

kernel/trace/trace_functions_graph.c static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, · line 179

Quoted 3 lines
kernel/trace/trace_functions_graph.c +3 -0
+			entry->args[i] = ftrace_regs_get_argument(fregs,
+						i - (type == TRACE_GRAPH_RETADDR_ENT ? 1 : 0));
+			i++;

Then you don't need the "i++" here.

-- Steve

Quoted 2 lines
(quoted diff) +1 -0
+		}
 	}
#4 lore raw
Quoted 15 lines · Steven Rostedt

kernel/trace/trace_functions_graph.c static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, · line 176

Quoted 10 lines · On Tue, 11 Nov 2025 21:54:32 +0800 Donglin Peng <[email protected]> wrote:
kernel/trace/trace_functions_graph.c +6 -3
+     /* Store the retaddr in args[0] */
+     if (type == TRACE_GRAPH_RETADDR_ENT)
+             entry->args[i++] = retaddr;
+
 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
-     if (fregs) {
-             for (int i = 0; i < FTRACE_REGS_MAX_ARGS; i++)
-                     entry->args[i] = ftrace_regs_get_argument(fregs, i);
+     if (store_args) {
+             while (i < nr_args) {
Quoted 1 line

Nit, this could stay as a for loop. You just don't initialize the 'i':

Thanks, I will fix it in v3.

Quoted 13 lines
                for (; i < nr_args; i++) {

kernel/trace/trace_functions_graph.c static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, · line 179

Quoted 3 lines
kernel/trace/trace_functions_graph.c +3 -0
+                     entry->args[i] = ftrace_regs_get_argument(fregs,
+                                             i - (type == TRACE_GRAPH_RETADDR_ENT ? 1 : 0));
+                     i++;

Then you don't need the "i++" here.

-- Steve

Quoted 2 lines

+ }

      }