From nobody Tue Dec 2 00:02:16 2025 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27B6E2E6CBF for ; Tue, 25 Nov 2025 09:27:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764062840; cv=none; b=gCVeC02sLyzI8whboTinXjPdhWgV+sTMJRyAAPEN4reU/YMqWmmEDQSnEkNpmN52sZmZrVTfL0tL54paWk51dbFPqRZHNGrXT+AJEOWB5fR9csrhMUqtSES9pZ02gn/crVg+hH/4uvk3YPv5kwHvb3k9UxObC5DId1kphPTFHPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764062840; c=relaxed/simple; bh=KzayFlwK7ulaSY5h4Nfq1PA3v2PPRbmVn9FA7wpUjjE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=hnXA3UWxUYx7nK21uUIxOBQn0jNpGkBDU0u1F8gxZPg1tuiX8a0IKuhbteUA84b+eK1h01iBpef9Z8MIJ5YX1xF330TbmEUoWbPDpGLhzStUD58VzLm99MUP0MHZKvEC4FvkMbRD10UBQ33EgHXTA6LKl/sgEXy6E08v8u8NV9o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S+deZVnW; arc=none smtp.client-ip=209.85.210.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S+deZVnW" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7bc0cd6a13aso3255220b3a.0 for ; Tue, 25 Nov 2025 01:27:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764062837; x=1764667637; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2juxySKyn1fmrQg9Vi4fMT6rQH7rPO6NwRqpHFTUY6Q=; b=S+deZVnWpioJCZnmBys/HrGo8XlUy284s09MaX0UMIJcxGUa5qqIiS7k3ZhfgaV9ls i+4cjl6bB2hH1xhtVawCvNrQX9ZCtu7hpu5U1PSks61d0rGQ51WMpYw7JTLwGO2doDT2 yvXYG+lhsZaqNNgmIM1UT1N10OKmGU4bOUv6af8Qx9EWelbN2pn1WFKKJ3Lrm6M34kQr sdMp6c/0BxTn8fDJfHHHxljNeOaKzLVjT9c5ObyYfcBaNc58f3NfSk5V7NmS+0QwgYfE JodzWn78ElED7mpIZNZD9n9KGK2COmkXoA5F40iQLDQMCSba4JYs1t6803tgyYqjCa7D k5/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764062837; x=1764667637; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=2juxySKyn1fmrQg9Vi4fMT6rQH7rPO6NwRqpHFTUY6Q=; b=LnfCxwbpM0R5jbXP/sb52vFznMATGKrMZsGXjbRTzc9RvmhkLnUlVV7I9VWLm73K+T cRNCAhrsRCjNqAzeOMcsnXX/5kxuHVC++aT99mtjSTxWhJ8dmYDMQkmGIlq0UG/QIi2J Qn8xMgBkZu0X4zwq9LHHSa4LRmkp2l1XuetmRhUT9d2G/wXvYDe70YyrGcjLk1PKOj7w VcmlnlNnpuCbFSlpV81MGx0t5v05C0Gk/w0WWln/QVIwgNVHepq16DWpqnIkE1UpjjD9 9EGyDlwfAeFiFJ083QFsk72SCVPEcJ8I7urHwDpB76Nkh4SKUVTLgNKy/xKdrK9M30YJ 8uYQ== X-Forwarded-Encrypted: i=1; AJvYcCV2kgRLmYu+YBkAJsqNwzAwATeivAMotBgSyL9O0xw3wnnHNY/I0+h3G2spVle5mgnavaJ7EktgHeJ6rv0=@vger.kernel.org X-Gm-Message-State: AOJu0YyzAScaz8kgas3N5Qsi4+g2N+l54KZ5cdhFSOI3BQP66e4wiQyv 3hvb8V17UuFxziKV/tlEVW7OcswQNcFY3Bu8x6eSkxjsWvq87Xo3CzZt X-Gm-Gg: ASbGnctZ3Pok2WoDbiuLkC3ipDZnixiQWZrGOt9ZM5DSFDRpJA7LUOcoQ0ZM8INUfsd K6l0kol9kF582637IXknWAr4MLmBu7VCYIg6cR4b0J4EPrawY7qjj+8O5Y2sjaNIP/0VMTTWQM8 1SUAIQDhK4L/zV86UdL/k5k18Frzu6WrrqLjKQGzOAk/tvMzD2+K7x+D22hPPdLHKP6VdJUhcVl /T93SIaeDU4s7R6h7MULTP9IYqVh4+gXU6O/FUdtjpN3+C71jmUYpaVYbGEG+ki9aTgChy/cA0z CPbE5NGhJgXgCqkaiJWpUxxMtSB3zRpMnfl2m1YPTnvsSYVopGwgN4oSZj2K4Mm7W9vgCrzq8KY ps4S+/Rlbx//aqIa/TrZvEUnhZmqL/KZKKogtm6wllkekn+yQrjJuGT+lvAbFJgH0IOpjS5jAMJ 8c2LR99Boba0++QBnxB6/mN5pQAtnAPUSAkx9r2g== X-Google-Smtp-Source: AGHT+IGq7FWTiSRpMjplb+Jhvuq6nmL8VaYuYWNGf3oFTepm+y1j90mkrBdwEDgYK/4Wu9JRK8jQ+g== X-Received: by 2002:a05:6a20:939f:b0:2cf:afc1:cc3c with SMTP id adf61e73a8af0-3614f4affeamr16210301637.16.1764062837202; Tue, 25 Nov 2025 01:27:17 -0800 (PST) Received: from localhost.localdomain ([43.224.245.179]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7c3ecf80f4csm17296252b3a.8.2025.11.25.01.27.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Nov 2025 01:27:16 -0800 (PST) From: Donglin Peng To: rostedt@goodmis.org Cc: linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, pengdonglin , Sven Schnelle , Masami Hiramatsu , Xiaoqin Zhang Subject: [PATCH for-next v8] function_graph: Enable funcgraph-args and funcgraph-retaddr to work simultaneously Date: Tue, 25 Nov 2025 17:27:11 +0800 Message-Id: <20251125092711.2558787-1-dolinux.peng@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: pengdonglin Currently, the funcgraph-args and funcgraph-retaddr features are mutually exclusive. This patch resolves this limitation by allowing funcgraph-retaddr to have an args array. 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=3D0xffff8880100bea78) { /* <-n_tty_write+0xa3/0x540 */ __cond_resched(); /* <-down_read+0x12/0x160 */ preempt_count_add(val=3D1); /* <-down_read+0x3b/0x160 */ preempt_count_sub(val=3D1); /* <-down_read+0x8b/0x160 */ } Cc: Steven Rostedt (Google) Cc: Sven Schnelle Cc: Masami Hiramatsu Cc: Xiaoqin Zhang Signed-off-by: pengdonglin --- v8: - Add the retaddr member back and reconstruct the patch - Rebase on the trace/for-next branch - Link to v7: https://lore.kernel.org/linux-trace-kernel/20251114025522.211= 5359-1-dolinux.peng@gmail.com/ v7: - Remove nr_args to eliminate a multiplication instruction - Link to v6: https://lore.kernel.org/all/20251114023453.2061590-1-dolinux.= peng@gmail.com/ 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/20251113072938.333657-1-dolinux.p= eng@gmail.com/ 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/20251112093650.3345108-1-dolinux.= peng@gmail.com/ 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/20251112034333.2901601-1-dolinux.= peng@gmail.com/ 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/20251111135432.2143993-1-dolinux.= peng@gmail.com/ 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/20251011164156.3678012-1-dolinux.= peng@gmail.com/ --- include/linux/ftrace.h | 7 +-- kernel/trace/trace.h | 24 +++++++++- kernel/trace/trace_entries.h | 15 +++--- kernel/trace/trace_functions_graph.c | 71 ++++++++++++++++++---------- 4 files changed, 80 insertions(+), 37 deletions(-) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 7ded7df6e9b5..6ca9c6229d93 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -1126,17 +1126,14 @@ static inline void ftrace_init(void) { } */ struct ftrace_graph_ent { unsigned long func; /* Current function */ - int depth; + unsigned long depth; } __packed; =20 /* * 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; + struct ftrace_graph_ent ent; unsigned long retaddr; /* Return address */ } __packed; =20 diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 58be6d741d72..a06d836b7d92 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -962,7 +962,8 @@ extern int __trace_graph_entry(struct trace_array *tr, extern int __trace_graph_retaddr_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, unsigned int trace_ctx, - unsigned long retaddr); + unsigned long retaddr, + struct ftrace_regs *fregs); extern void __trace_graph_return(struct trace_array *tr, struct ftrace_graph_ret *trace, unsigned int trace_ctx, @@ -2287,4 +2288,25 @@ static inline int rv_init_interface(void) */ #define FTRACE_TRAMPOLINE_MARKER ((unsigned long) INT_MAX) =20 +/* + * This is used to get the address of the args array based on + * the type of the entry. + */ +#define FGRAPH_ENTRY_ARGS(e) \ + ({ \ + unsigned long *_args; \ + struct ftrace_graph_ent_entry *_e =3D e; \ + \ + if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) && \ + e->ent.type =3D=3D TRACE_GRAPH_RETADDR_ENT) { \ + struct fgraph_retaddr_ent_entry *_re; \ + \ + _re =3D (typeof(_re))_e; \ + _args =3D _re->args; \ + } else { \ + _args =3D _e->args; \ + } \ + _args; \ + }) + #endif /* _LINUX_KERNEL_TRACE_H */ diff --git a/kernel/trace/trace_entries.h b/kernel/trace/trace_entries.h index de294ae2c5c5..f6a8d29c0d76 100644 --- a/kernel/trace/trace_entries.h +++ b/kernel/trace/trace_entries.h @@ -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 ) ), =20 - F_printk("--> %ps (%u)", (void *)__entry->func, __entry->depth) + F_printk("--> %ps (%lu)", (void *)__entry->func, __entry->depth) ); =20 #ifdef CONFIG_FUNCTION_GRAPH_RETADDR @@ -95,13 +95,14 @@ FTRACE_ENTRY_PACKED(fgraph_retaddr_entry, fgraph_retadd= r_ent_entry, TRACE_GRAPH_RETADDR_ENT, =20 F_STRUCT( - __field_struct( struct fgraph_retaddr_ent, graph_ent ) - __field_packed( unsigned long, graph_ent, func ) - __field_packed( unsigned int, graph_ent, depth ) - __field_packed( unsigned long, graph_ent, retaddr ) + __field_struct( struct fgraph_retaddr_ent, graph_rent ) + __field_packed( unsigned long, graph_rent.ent, func ) + __field_packed( unsigned long, graph_rent.ent, depth ) + __field_packed( unsigned long, graph_rent, retaddr ) + __dynamic_array(unsigned long, args ) ), =20 - F_printk("--> %ps (%u) <- %ps", (void *)__entry->func, __entry->depth, + F_printk("--> %ps (%lu) <- %ps", (void *)__entry->func, __entry->depth, (void *)__entry->retaddr) ); =20 diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_func= tions_graph.c index 44d5dc5031e2..b04dcbdb236f 100644 --- a/kernel/trace/trace_functions_graph.c +++ b/kernel/trace/trace_functions_graph.c @@ -36,14 +36,19 @@ struct fgraph_ent_args { unsigned long args[FTRACE_REGS_MAX_ARGS]; }; =20 +struct fgraph_retaddr_ent_args { + struct fgraph_retaddr_ent_entry ent; + /* Force the sizeof of args[] to have FTRACE_REGS_MAX_ARGS entries */ + unsigned long args[FTRACE_REGS_MAX_ARGS]; +}; + struct fgraph_data { struct fgraph_cpu_data __percpu *cpu_data; =20 /* Place to preserve last processed entry. */ union { struct fgraph_ent_args ent; - /* TODO allow retaddr to have args */ - struct fgraph_retaddr_ent_entry rent; + struct fgraph_retaddr_ent_args rent; }; struct ftrace_graph_ret_entry ret; int failed; @@ -160,20 +165,32 @@ int __trace_graph_entry(struct trace_array *tr, int __trace_graph_retaddr_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, unsigned int trace_ctx, - unsigned long retaddr) + unsigned long retaddr, + struct ftrace_regs *fregs) { struct ring_buffer_event *event; struct trace_buffer *buffer =3D tr->array_buffer.buffer; struct fgraph_retaddr_ent_entry *entry; + int size; + + /* If fregs is defined, add FTRACE_REGS_MAX_ARGS long size words */ + size =3D sizeof(*entry) + (FTRACE_REGS_MAX_ARGS * !!fregs * sizeof(long)); =20 event =3D trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RETADDR_ENT, - sizeof(*entry), trace_ctx); + size, trace_ctx); if (!event) return 0; entry =3D ring_buffer_event_data(event); - entry->graph_ent.func =3D trace->func; - entry->graph_ent.depth =3D trace->depth; - entry->graph_ent.retaddr =3D retaddr; + entry->graph_rent.ent =3D *trace; + entry->graph_rent.retaddr =3D retaddr; + +#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API + if (fregs) { + for (int i =3D 0; i < FTRACE_REGS_MAX_ARGS; i++) + entry->args[i] =3D ftrace_regs_get_argument(fregs, i); + } +#endif + trace_buffer_unlock_commit_nostack(buffer, event); =20 return 1; @@ -182,7 +199,8 @@ int __trace_graph_retaddr_entry(struct trace_array *tr, int __trace_graph_retaddr_entry(struct trace_array *tr, struct ftrace_graph_ent *trace, unsigned int trace_ctx, - unsigned long retaddr) + unsigned long retaddr, + struct ftrace_regs *fregs) { return 1; } @@ -267,7 +285,8 @@ static int graph_entry(struct ftrace_graph_ent *trace, if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) && tracer_flags_is_set(tr, TRACE_GRAPH_PRINT_RETADDR)) { unsigned long retaddr =3D ftrace_graph_top_ret_addr(current); - ret =3D __trace_graph_retaddr_entry(tr, trace, trace_ctx, retaddr); + ret =3D __trace_graph_retaddr_entry(tr, trace, trace_ctx, + retaddr, fregs); } else { ret =3D __graph_entry(tr, trace, trace_ctx, fregs); } @@ -654,13 +673,9 @@ get_return_for_leaf(struct trace_iterator *iter, * Save current and next entries for later reference * if the output fails. */ - if (unlikely(curr->ent.type =3D=3D TRACE_GRAPH_RETADDR_ENT)) { - data->rent =3D *(struct fgraph_retaddr_ent_entry *)curr; - } else { - int size =3D min((int)sizeof(data->ent), (int)iter->ent_size); + int size =3D min_t(int, sizeof(data->rent), iter->ent_size); =20 - memcpy(&data->ent, curr, size); - } + memcpy(&data->rent, curr, size); /* * If the next event is not a return type, then * we only care about what type it is. Otherwise we can @@ -838,7 +853,7 @@ static void print_graph_retaddr(struct trace_seq *s, st= ruct fgraph_retaddr_ent_e trace_seq_puts(s, " /*"); =20 trace_seq_puts(s, " <-"); - seq_print_ip_sym_offset(s, entry->graph_ent.retaddr, trace_flags); + seq_print_ip_sym_offset(s, entry->graph_rent.retaddr, trace_flags); =20 if (comment) trace_seq_puts(s, " */"); @@ -984,7 +999,7 @@ print_graph_entry_leaf(struct trace_iterator *iter, trace_seq_printf(s, "%ps", (void *)ret_func); =20 if (args_size >=3D FTRACE_REGS_MAX_ARGS * sizeof(long)) { - print_function_args(s, entry->args, ret_func); + print_function_args(s, FGRAPH_ENTRY_ARGS(entry), ret_func); trace_seq_putc(s, ';'); } else trace_seq_puts(s, "();"); @@ -1036,7 +1051,7 @@ print_graph_entry_nested(struct trace_iterator *iter, args_size =3D iter->ent_size - offsetof(struct ftrace_graph_ent_entry, ar= gs); =20 if (args_size >=3D FTRACE_REGS_MAX_ARGS * sizeof(long)) - print_function_args(s, entry->args, func); + print_function_args(s, FGRAPH_ENTRY_ARGS(entry), func); else trace_seq_puts(s, "()"); =20 @@ -1218,11 +1233,14 @@ print_graph_entry(struct ftrace_graph_ent_entry *fi= eld, struct trace_seq *s, /* * print_graph_entry() may consume the current event, * thus @field may become invalid, so we need to save it. - * sizeof(struct ftrace_graph_ent_entry) is very small, - * it can be safely saved at the stack. + * This function is shared by ftrace_graph_ent_entry and + * fgraph_retaddr_ent_entry, the size of the latter one + * is larger, but it is very small and can be safely saved + * at the stack. */ struct ftrace_graph_ent_entry *entry; - u8 save_buf[sizeof(*entry) + FTRACE_REGS_MAX_ARGS * sizeof(long)]; + struct fgraph_retaddr_ent_entry *rentry; + u8 save_buf[sizeof(*rentry) + FTRACE_REGS_MAX_ARGS * sizeof(long)]; =20 /* The ent_size is expected to be as big as the entry */ if (iter->ent_size > sizeof(save_buf)) @@ -1451,12 +1469,17 @@ print_graph_function_flags(struct trace_iterator *i= ter, u32 flags) } #ifdef CONFIG_FUNCTION_GRAPH_RETADDR case TRACE_GRAPH_RETADDR_ENT: { - struct fgraph_retaddr_ent_entry saved; + /* + * ftrace_graph_ent_entry and fgraph_retaddr_ent_entry have + * similar functions and memory layouts. The only difference + * is that the latter once has an extra retaddr member, so + * they can share most of the logic. + */ struct fgraph_retaddr_ent_entry *rfield; =20 trace_assign_type(rfield, entry); - saved =3D *rfield; - return print_graph_entry((struct ftrace_graph_ent_entry *)&saved, s, ite= r, flags); + return print_graph_entry((struct ftrace_graph_ent_entry *)rfield, + s, iter, flags); } #endif case TRACE_GRAPH_RET: { --=20 2.34.1