Skip to contentSkip to conversation

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

PATCH Superseded Donglin Peng 2 participants 6 messages Nov 14, 2025 – Nov 25, 2025

Subsystems (2)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 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 ftrace -G vfs_write --graph-opts args,retaddr
   ......
   down_read() { /* <-n_tty_write+0xa3/0x540 */
     __cond_resched(); /* <-down_read+0x12/0x160 */
     preempt_count_add(); /* <-down_read+0x3b/0x160 */
     preempt_count_sub(); /* <-down_read+0x8b/0x160 */
   }

After:

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

---
v7:
- Remove nr_args to eliminate a multiplication instruction
- Link to v6: https://lore.kernel.org/all/[email protected]/

v6:
- Initialize retaddr to NULL to silence uninitialized variable warning.
- Use ARGS_OFFS(size) macro instead of args_offs variable to eliminate
"set but not used" warning
- Link to v5: https://lore.kernel.org/all/[email protected]/

v5:
- Avoid unnecessary branch overhead by first verifying CONFIG_FUNCTION_GRAPH_RETADDR is enabled prior to testing
the TRACE_GRAPH_PRINT_RETADDR flag
- Link to v4: https://lore.kernel.org/all/[email protected]/

v4:
- Remove redundant TRACE_GRAPH_ARGS flag check
- Eliminate unnecessary retaddr initialization
- Resend to add missing add Acked-by tags
- Link to v3: https://lore.kernel.org/all/[email protected]/

v3:
- Replace min() with min_t() to improve type safety and maintainability
- Keep only one Signed-off-by for cleaner attribution
- Code refactoring for improved readability
- Link to v2: https://lore.kernel.org/all/[email protected]/

v2:
- Preserve retaddr event functionality (suggested by Steven)
- Store the retaddr in args[0] (suggested by Steven)
- Refactor implementation logic and commit message clarity
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 include/linux/ftrace.h               |  11 ---
 kernel/trace/trace.h                 |   4 -
 kernel/trace/trace_entries.h         |   6 +-
 kernel/trace/trace_functions_graph.c | 140 ++++++++++++---------------
 4 files changed, 64 insertions(+), 97 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 +61 -79

270 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 101

Quoted 11 lines · On Fri, 14 Nov 2025 10:55:22 +0800 Donglin Peng <[email protected]> wrote:
kernel/trace/trace_entries.h +1 -2
@@ -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		)

You can't delete the retaddr without breaking user space.

Please keep that here.

Thanks,

-- Steve

Quoted 8 lines
kernel/trace/trace_entries.h +2 -1
+		__dynamic_array(unsigned long,	args				)
 	),
 
 	F_printk("--> %ps (%u) <- %ps", (void *)__entry->func, __entry->depth,
-		(void *)__entry->retaddr)
+		(void *)__entry->args[0])
 );
 
maintainer
#2 lore raw
Quoted 17 lines · On Mon, 24 Nov 2025 16:47:01 -0500 Steven Rostedt <[email protected]> wrote:

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 101

Quoted 11 lines · On Fri, 14 Nov 2025 10:55:22 +0800 Donglin Peng <[email protected]> wrote:
kernel/trace/trace_entries.h +1 -2
@@ -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		)  

You can't delete the retaddr without breaking user space.

Quoted 2 lines

Please keep that here.

Also, please rebase on top of:

git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git trace/for-next

Thanks!

-- Steve

maintainer
#3 lore raw
In reply to Steven Rostedt
Quoted 14 lines · On Mon, 24 Nov 2025 16:47:01 -0500 Steven Rostedt <[email protected]> wrote:

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 101

Quoted 11 lines
kernel/trace/trace_entries.h +1 -2
@@ -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		)  

You can't delete the retaddr without breaking user space.

Quoted 1 line

Please keep that here.

I added this, and user space works again:

kernel/trace/trace_entries.h +1 -0
index 593a74663c65..4666b6179056 100644
@@ -98,6 +98,7 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry,
 		__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				)
 	),
 

-- Steve

maintainer
#4 lore raw
Quoted 26 lines · On Mon, 24 Nov 2025 16:54:40 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 14 lines · On Mon, 24 Nov 2025 16:47:01 -0500 Steven Rostedt <[email protected]> wrote:

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 101

Quoted 11 lines
kernel/trace/trace_entries.h +1 -2
@@ -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		)    

You can't delete the retaddr without breaking user space.

Quoted 1 line

Please keep that here.

I added this, and user space works again:

kernel/trace/trace_entries.h +0 -0
index 593a74663c65..4666b6179056 100644

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 104

Quoted 7 lines
kernel/trace/trace_entries.h +1 -0
@@ -98,6 +98,7 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry,
 		__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				)
 	),
 

Nope, this wasn't enough. I got garbage from this. The following patch appears to make it all work though (this is against your patch that was forward ported to my for-next branch):

include/linux/ftrace.h +1 -3
index 88cb54d73bdb..ba180d19423e 100644
@@ -1121,12 +1121,10 @@ static inline void ftrace_init(void) { }
 
 /*
  * Structure that defines an entry function trace.
- * It's already packed but the attribute "packed" is needed
- * to remove extra padding at the end.
  */
 struct ftrace_graph_ent {
 	unsigned long func; /* Current function */
-	int depth;
+	unsigned long depth;
 } __packed;
 
 /*
kernel/trace/trace.h +7 -0
index 05fb6c9279e3..d509a5041d38 100644
@@ -60,6 +60,13 @@ enum trace_type {
 	__TRACE_LAST_TYPE,
 };
 
+/*
+ * Structure that defines an entry function trace with retaddr.
+ */
+struct fgraph_retaddr_ent {
+	struct ftrace_graph_ent		ent;
+	unsigned long retaddr;  /* Return address */
+} __packed;
 
 #undef __field
 #define __field(type, item)		type	item;
kernel/trace/trace_entries.h +7 -6
index 593a74663c65..87f4228374cd 100644
@@ -80,11 +80,11 @@ FTRACE_ENTRY(funcgraph_entry, ftrace_graph_ent_entry,
 	F_STRUCT(
 		__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,	depth		)
 		__dynamic_array(unsigned long,	args				)
 	),
 
-	F_printk("--> %ps (%u)", (void *)__entry->func, __entry->depth)
+	F_printk("--> %ps (%lu)", (void *)__entry->func, __entry->depth)
 );
 
 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR
@@ -95,13 +95,14 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry,
 	TRACE_GRAPH_RETADDR_ENT,
 
 	F_STRUCT(
-		__field_struct(	struct ftrace_graph_ent,	graph_ent	)
-		__field_packed(	unsigned long,	graph_ent,	func		)
-		__field_packed(	unsigned int,	graph_ent,	depth		)
+		__field_struct(	struct fgraph_retaddr_ent,	graph_ent	)
+		__field_packed(	unsigned long,	graph_ent.ent,	func		)
+		__field_packed(	unsigned long,	graph_ent.ent,	depth		)
+		__field_packed(	unsigned long,	graph_ent,	retaddr		)
 		__dynamic_array(unsigned long,	args				)
 	),
 
-	F_printk("--> %ps (%u) <- %ps", (void *)__entry->func, __entry->depth,
+	F_printk("--> %ps (%lu) <- %ps", (void *)__entry->func, __entry->depth,
 		(void *)__entry->args[0])
 );
 

-- Steve

#5 lore raw
Quoted 103 lines · Steven Rostedt
Quoted 26 lines · On Mon, 24 Nov 2025 16:54:40 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 14 lines · On Mon, 24 Nov 2025 16:47:01 -0500 Steven Rostedt <[email protected]> wrote:

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 101

Quoted 11 lines
kernel/trace/trace_entries.h +1 -2
@@ -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         )

You can't delete the retaddr without breaking user space.

Quoted 1 line

Please keep that here.

I added this, and user space works again:

kernel/trace/trace_entries.h +0 -0
index 593a74663c65..4666b6179056 100644

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 104

Quoted 7 lines
kernel/trace/trace_entries.h +1 -0
@@ -98,6 +98,7 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry,
              __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                            )
      ),
 

Nope, this wasn't enough. I got garbage from this. The following patch appears to make it all work though (this is against your patch that was forward ported to my for-next branch):

include/linux/ftrace.h +1 -3
index 88cb54d73bdb..ba180d19423e 100644
@@ -1121,12 +1121,10 @@ static inline void ftrace_init(void) { }
 
 /*
  * Structure that defines an entry function trace.
- * It's already packed but the attribute "packed" is needed
- * to remove extra padding at the end.
  */
 struct ftrace_graph_ent {
        unsigned long func; /* Current function */
-       int depth;
+       unsigned long depth;
 } __packed;
 
 /*
kernel/trace/trace.h +7 -0
index 05fb6c9279e3..d509a5041d38 100644
@@ -60,6 +60,13 @@ enum trace_type {
        __TRACE_LAST_TYPE,
 };
 
+/*
+ * Structure that defines an entry function trace with retaddr.
+ */
+struct fgraph_retaddr_ent {
+       struct ftrace_graph_ent         ent;
+       unsigned long retaddr;  /* Return address */
+} __packed;
 
 #undef __field
 #define __field(type, item)            type    item;
kernel/trace/trace_entries.h +2 -5
index 593a74663c65..87f4228374cd 100644
@@ -80,11 +80,11 @@ FTRACE_ENTRY(funcgraph_entry, ftrace_graph_ent_entry,
        F_STRUCT(
                __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,      depth           )
                __dynamic_array(unsigned long,  args                            )
        ),
 
-       F_printk("--> %ps (%u)", (void *)__entry->func, __entry->depth)
+       F_printk("--> %ps (%lu)", (void *)__entry->func, __entry->depth)
 );
 
 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR
@@ -95,13 +95,14 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry,
        TRACE_GRAPH_RETADDR_ENT,
 
        F_STRUCT(
-               __field_struct( struct ftrace_graph_ent,        graph_ent       )
-               __field_packed( unsigned long,  graph_ent,      func            )
-               __field_packed( unsigned int,   graph_ent,      depth           )

kernel/trace/trace_entries.h FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retaddr_ent_entry, · line 106

Quoted 10 lines
kernel/trace/trace_entries.h +5 -1
+               __field_struct( struct fgraph_retaddr_ent,      graph_ent       )
+               __field_packed( unsigned long,  graph_ent.ent,  func            )
+               __field_packed( unsigned long,  graph_ent.ent,  depth           )
+               __field_packed( unsigned long,  graph_ent,      retaddr         )
                __dynamic_array(unsigned long,  args                            )
        ),
 
-       F_printk("--> %ps (%u) <- %ps", (void *)__entry->func, __entry->depth,
+       F_printk("--> %ps (%lu) <- %ps", (void *)__entry->func, __entry->depth,
                (void *)__entry->args[0])

Thanks. The args[0] holds the return address in this patch, so if the retaddr member is added back, we may need to reconstruct this patch to store the return address in the retaddr member, with the args array storing only function parameters. I will fix it in the next version.

Thanks
Donglin

Quoted 3 lines
kernel/trace/trace_entries.h +0 -1
 );
 
-- Steve