Skip to contentSkip to conversation

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

PATCH Superseded Donglin Peng 2 participants 3 messages Nov 13, 2025

Subsystems (2)

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 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]>

---
v5:
- Avoid unnecessary branch overhead by first verifying CONFIG_FUNCTION_GRAPH_RETADDR is enabled prior to testing the TRACE_GRAPH_PRINT_RETADDR flag

v4:
- Remove redundant TRACE_GRAPH_ARGS flag check
- Eliminate unnecessary retaddr initialization
- Resend to add missing add Acked-by tags

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

v2:
- Preserve retaddr event functionality (suggested by Steven)
- Store the retaddr in args[0] (suggested by Steven)
- Refactor implementation logic and commit message clarity

---
 include/linux/ftrace.h               |  11 ---
 kernel/trace/trace.h                 |   4 -
 kernel/trace/trace_entries.h         |   6 +-
 kernel/trace/trace_functions_graph.c | 142 ++++++++++++---------------
 4 files changed, 66 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 +63 -79

272 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Donglin,

kernel test robot noticed the following build warnings:

[auto build test WARNING on v6.18-rc5]
[also build test WARNING on linus/master]
[cannot apply to trace/for-next next-20251113]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Donglin-Peng/function_graph-Enable-funcgraph-args-and-funcgraph-retaddr-to-work-simultaneously/20251113-153332
base:   v6.18-rc5
patch link:    https://lore.kernel.org/r/20251113072938.333657-1-dolinux.peng%40gmail.com
patch subject: [PATCH v5] function_graph: Enable funcgraph-args and funcgraph-retaddr to work simultaneously
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20251114/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251114/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/trace/trace_functions_graph.c:169:20: warning: variable 'retaddr' is uninitialized when used here [-Wuninitialized]
     169 |                 entry->args[0] = retaddr;
         |                                  ^~~~~~~
   kernel/trace/trace_functions_graph.c:143:23: note: initialize the variable 'retaddr' to silence this warning
     143 |         unsigned long retaddr;
         |                              ^
         |                               = 0

1 warning generated.

vim +/retaddr +169 kernel/trace/trace_functions_graph.c

132
133 static void
134 print_graph_duration(struct trace_array *tr, unsigned long long duration,
135 struct trace_seq *s, u32 flags);
136
137 static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace,
138 unsigned int trace_ctx, struct ftrace_regs *fregs)
139 {
140 struct ring_buffer_event *event;
141 struct trace_buffer *buffer = tr->array_buffer.buffer;
142 struct ftrace_graph_ent_entry *entry;
143 unsigned long retaddr;
144 int size = sizeof(*entry);
145 int type = TRACE_GRAPH_ENT;
146 int nr_args = 0, args_offs = 0;
147
148 if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) &&
149 tracer_flags_is_set(TRACE_GRAPH_PRINT_RETADDR)) {
150 retaddr = ftrace_graph_top_ret_addr(current);
151 type = TRACE_GRAPH_RETADDR_ENT;
152 nr_args += 1;
153 }
154
155 /* If fregs is defined, add FTRACE_REGS_MAX_ARGS long size words */
156 if (!!fregs)
157 nr_args += FTRACE_REGS_MAX_ARGS;
158
159 size += nr_args * sizeof(long);
160 event = trace_buffer_lock_reserve(buffer, type, size, trace_ctx);
161 if (!event)
162 return 0;
163
164 entry = ring_buffer_event_data(event);
165 entry->graph_ent = *trace;
166
167 /* Store the retaddr in args[0] */
168 if (type == TRACE_GRAPH_RETADDR_ENT) {

 > 169			entry->args[0] = retaddr;

170 args_offs += 1;
171 }
172

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Donglin Peng
kernel test robot noticed the following build warnings:

Hi Donglin,

kernel test robot noticed the following build warnings:

[auto build test WARNING on v6.18-rc5]
[also build test WARNING on linus/master]
[cannot apply to trace/for-next next-20251113]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Donglin-Peng/function_graph-Enable-funcgraph-args-and-funcgraph-retaddr-to-work-simultaneously/20251113-153332
base:   v6.18-rc5
patch link:    https://lore.kernel.org/r/20251113072938.333657-1-dolinux.peng%40gmail.com
patch subject: [PATCH v5] function_graph: Enable funcgraph-args and funcgraph-retaddr to work simultaneously
config: sparc64-randconfig-001-20251113 (https://download.01.org/0day-ci/archive/20251114/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251114/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/trace/trace_functions_graph.c:146:19: warning: variable 'args_offs' set but not used [-Wunused-but-set-variable]
     146 |         int nr_args = 0, args_offs = 0;
         |                          ^
   kernel/trace/trace_functions_graph.c:169:20: warning: variable 'retaddr' is uninitialized when used here [-Wuninitialized]
     169 |                 entry->args[0] = retaddr;
         |                                  ^~~~~~~
   kernel/trace/trace_functions_graph.c:143:23: note: initialize the variable 'retaddr' to silence this warning
     143 |         unsigned long retaddr;
         |                              ^
         |                               = 0

2 warnings generated.

vim +/args_offs +146 kernel/trace/trace_functions_graph.c

132
133 static void
134 print_graph_duration(struct trace_array *tr, unsigned long long duration,
135 struct trace_seq *s, u32 flags);
136
137 static int __graph_entry(struct trace_array *tr, struct ftrace_graph_ent *trace,
138 unsigned int trace_ctx, struct ftrace_regs *fregs)
139 {
140 struct ring_buffer_event *event;
141 struct trace_buffer *buffer = tr->array_buffer.buffer;
142 struct ftrace_graph_ent_entry *entry;
143 unsigned long retaddr;
144 int size = sizeof(*entry);
145 int type = TRACE_GRAPH_ENT;

 > 146		int nr_args = 0, args_offs = 0;

147
148 if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) &&
149 tracer_flags_is_set(TRACE_GRAPH_PRINT_RETADDR)) {
150 retaddr = ftrace_graph_top_ret_addr(current);
151 type = TRACE_GRAPH_RETADDR_ENT;
152 nr_args += 1;
153 }
154
155 /* If fregs is defined, add FTRACE_REGS_MAX_ARGS long size words */
156 if (!!fregs)
157 nr_args += FTRACE_REGS_MAX_ARGS;
158
159 size += nr_args * sizeof(long);
160 event = trace_buffer_lock_reserve(buffer, type, size, trace_ctx);
161 if (!event)
162 return 0;
163
164 entry = ring_buffer_event_data(event);
165 entry->graph_ent = *trace;
166
167 /* Store the retaddr in args[0] */
168 if (type == TRACE_GRAPH_RETADDR_ENT) {
169 entry->args[0] = retaddr;
170 args_offs += 1;
171 }
172

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki