Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: <wang.yaxin@zte.com.cn>
Cc: <mhiramat@kernel.org>, <mark.rutland@arm.com>,
	<mathieu.desnoyers@efficios.com>, <linux-kernel@vger.kernel.org>,
	<linux-trace-kernel@vger.kernel.org>, <hu.shengming@zte.com.cn>,
	<hang.run@zte.com.cn>, <yang.yang29@zte.com.cn>
Subject: Re: [PATCH linux-next] fgraph: Fix and tighten PID filtering support
Date: Tue, 25 Nov 2025 13:05:04 -0500	[thread overview]
Message-ID: <20251125130504.6f1b442f@gandalf.local.home> (raw)
In-Reply-To: <20251113135627439rGwzvG7JzdmnN8VMHAfMs@zte.com.cn>

On Thu, 13 Nov 2025 13:56:27 +0800 (CST)
<wang.yaxin@zte.com.cn> wrote:

> From: Shengming Hu <hu.shengming@zte.com.cn>
> 
> Function graph tracing did not honor set_ftrace_pid() rules properly.
> 
> The root cause is that for fgraph_ops, the underlying ftrace_ops->private
> was left uninitialized. As a result, ftrace_pids_enabled(op) always
> returned false, effectively disabling PID filtering in the function graph
> tracer.
> 
> PID filtering seemed to "work" only because graph_entry() performed an
> extra coarse-grained check via ftrace_trace_task(). Specifically,
> ftrace_ignore_pid is updated by ftrace_filter_pid_sched_switch_probe
> during sched_switch events. Under the original logic, when the intent
> is to trace only PID A, a context switch from task B to A sets 
> ftrace_ignore_pid to A’s PID. However, there remains a window
> where B’s functions are still captured by the function-graph tracer. 
> The following trace demonstrates this leakage
> (B = haveged-213, A = test.sh-385):

Thanks for the patch.


> Fix this by:

The below should really be three different patches.

>   1. Properly initializing gops->ops->private so that
>      ftrace_pids_enabled() works as expected.
>   2. Removing the imprecise fallback check in graph_entry().
>   3. Updating register_ftrace_graph() to set gops->entryfunc =
>      fgraph_pid_func whenever PID filtering is active, so the correct
>      per-task filtering is enforced at entry time.
> 
> With this change, function graph tracing will respect the configured
> PID filter list consistently, and the redundant coarse check is no
> longer needed.
> 
> Signed-off-by: Shengming Hu <hu.shengming@zte.com.cn>
> ---
>  kernel/trace/fgraph.c                | 9 +++++++--
>  kernel/trace/trace.h                 | 9 ---------
>  kernel/trace/trace_functions_graph.c | 3 ---
>  3 files changed, 7 insertions(+), 14 deletions(-)
> 
> diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c
> index 484ad7a18..00df3d4ac 100644
> --- a/kernel/trace/fgraph.c
> +++ b/kernel/trace/fgraph.c
> @@ -1019,6 +1019,7 @@ void fgraph_init_ops(struct ftrace_ops *dst_ops,
>  		mutex_init(&dst_ops->local_hash.regex_lock);
>  		INIT_LIST_HEAD(&dst_ops->subop_list);
>  		dst_ops->flags |= FTRACE_OPS_FL_INITIALIZED;
> +		dst_ops->private = src_ops->private;
>  	}
>  #endif
>  }
> @@ -1375,6 +1376,12 @@ int register_ftrace_graph(struct fgraph_ops *gops)
>  	gops->idx = i;
> 
>  	ftrace_graph_active++;

Please keep a space here.

> +	/* Always save the function, and reset at unregistering */
> +	gops->saved_func = gops->entryfunc;
> +#ifdef CONFIG_DYNAMIC_FTRACE
> +	if (ftrace_pids_enabled(&gops->ops))
> +		gops->entryfunc = fgraph_pid_func;
> +#endif

Thanks,

-- Steve

> 
>  	if (ftrace_graph_active == 2)
>  		ftrace_graph_disable_direct(true);
> @@ -1395,8 +1402,6 @@ int register_ftrace_graph(struct fgraph_ops *gops)
>  	} else {
>  		init_task_vars(gops->idx);
>  	}
> -	/* Always save the function, and reset at unregistering */
> -	gops->saved_func = gops->entryfunc;
> 
>  	gops->ops.flags |= FTRACE_OPS_FL_GRAPH;
> 
> diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
> index a3a15cfab..048a53282 100644
> --- a/kernel/trace/trace.h
> +++ b/kernel/trace/trace.h
> @@ -1162,11 +1162,6 @@ struct ftrace_func_command {
>  					char *params, int enable);
>  };
>  extern bool ftrace_filter_param __initdata;
> -static inline int ftrace_trace_task(struct trace_array *tr)
> -{
> -	return this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid) !=
> -		FTRACE_PID_IGNORE;
> -}
>  extern int ftrace_is_dead(void);
>  int ftrace_create_function_files(struct trace_array *tr,
>  				 struct dentry *parent);
> @@ -1184,10 +1179,6 @@ void ftrace_clear_pids(struct trace_array *tr);
>  int init_function_trace(void);
>  void ftrace_pid_follow_fork(struct trace_array *tr, bool enable);
>  #else
> -static inline int ftrace_trace_task(struct trace_array *tr)
> -{
> -	return 1;
> -}
>  static inline int ftrace_is_dead(void) { return 0; }
>  static inline int
>  ftrace_create_function_files(struct trace_array *tr,
> diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c
> index fe9607edc..0efe831e4 100644
> --- a/kernel/trace/trace_functions_graph.c
> +++ b/kernel/trace/trace_functions_graph.c
> @@ -232,9 +232,6 @@ static int graph_entry(struct ftrace_graph_ent *trace,
>  		return 1;
>  	}
> 
> -	if (!ftrace_trace_task(tr))
> -		return 0;
> -
>  	if (ftrace_graph_ignore_func(gops, trace))
>  		return 0;
> 


  reply	other threads:[~2025-11-25 18:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-13  5:56 [PATCH linux-next] fgraph: Fix and tighten PID filtering support wang.yaxin
2025-11-25 18:05 ` Steven Rostedt [this message]
2025-11-26  9:50   ` wang.yaxin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251125130504.6f1b442f@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=hang.run@zte.com.cn \
    --cc=hu.shengming@zte.com.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=wang.yaxin@zte.com.cn \
    --cc=yang.yang29@zte.com.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox