linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fgraph: Keep track of when fgraph_ops are registered or not
@ 2025-06-18 11:31 Steven Rostedt
  2025-06-19  3:45 ` Masami Hiramatsu
  0 siblings, 1 reply; 3+ messages in thread
From: Steven Rostedt @ 2025-06-18 11:31 UTC (permalink / raw)
  To: LKML, Linux Trace Kernel
  Cc: Masami Hiramatsu, Mathieu Desnoyers, Mark Rutland

From: Steven Rostedt <rostedt@goodmis.org>

Add a warning if unregister_ftrace_graph() is called without ever
registering it, or if register_ftrace_graph() is called twice. This can
detect errors when they happen and not later when there's a side effect:

Link: https://lore.kernel.org/all/20250617120830.24fbdd62@gandalf.local.home/

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/fgraph.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c
index c5b207992fb4..c6bb6c68764f 100644
--- a/kernel/trace/fgraph.c
+++ b/kernel/trace/fgraph.c
@@ -1325,6 +1325,10 @@ int register_ftrace_graph(struct fgraph_ops *gops)
 	int ret = 0;
 	int i = -1;
 
+	if (WARN_ONCE(gops->ops.flags & FTRACE_OPS_FL_GRAPH,
+		      "function graph ops registered again"))
+		return -EBUSY;
+
 	guard(mutex)(&ftrace_lock);
 
 	if (!fgraph_stack_cachep) {
@@ -1401,17 +1405,21 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 {
 	int command = 0;
 
+	if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH),
+		      "function graph ops unregistered without registering"))
+		return;
+
 	guard(mutex)(&ftrace_lock);
 
 	if (unlikely(!ftrace_graph_active))
-		return;
+		goto out;
 
 	if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE ||
 		     fgraph_array[gops->idx] != gops))
-		return;
+		goto out;
 
 	if (fgraph_lru_release_index(gops->idx) < 0)
-		return;
+		goto out;
 
 	fgraph_array[gops->idx] = &fgraph_stub;
 
@@ -1434,4 +1442,6 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
 		unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
 	}
 	gops->saved_func = NULL;
+ out:
+	gops->ops.flags = 0;
 }
-- 
2.47.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] fgraph: Keep track of when fgraph_ops are registered or not
  2025-06-18 11:31 [PATCH] fgraph: Keep track of when fgraph_ops are registered or not Steven Rostedt
@ 2025-06-19  3:45 ` Masami Hiramatsu
  2025-07-01 22:28   ` Steven Rostedt
  0 siblings, 1 reply; 3+ messages in thread
From: Masami Hiramatsu @ 2025-06-19  3:45 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: LKML, Linux Trace Kernel, Masami Hiramatsu, Mathieu Desnoyers,
	Mark Rutland

On Wed, 18 Jun 2025 07:31:17 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> From: Steven Rostedt <rostedt@goodmis.org>
> 
> Add a warning if unregister_ftrace_graph() is called without ever
> registering it, or if register_ftrace_graph() is called twice. This can
> detect errors when they happen and not later when there's a side effect:
> 
> Link: https://lore.kernel.org/all/20250617120830.24fbdd62@gandalf.local.home/
> 
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
>  kernel/trace/fgraph.c | 16 +++++++++++++---
>  1 file changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c
> index c5b207992fb4..c6bb6c68764f 100644
> --- a/kernel/trace/fgraph.c
> +++ b/kernel/trace/fgraph.c
> @@ -1325,6 +1325,10 @@ int register_ftrace_graph(struct fgraph_ops *gops)
>  	int ret = 0;
>  	int i = -1;
>  
> +	if (WARN_ONCE(gops->ops.flags & FTRACE_OPS_FL_GRAPH,
> +		      "function graph ops registered again"))
> +		return -EBUSY;
> +
>  	guard(mutex)(&ftrace_lock);
>  
>  	if (!fgraph_stack_cachep) {
> @@ -1401,17 +1405,21 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
>  {
>  	int command = 0;
>  
> +	if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH),
> +		      "function graph ops unregistered without registering"))
> +		return;
> +
>  	guard(mutex)(&ftrace_lock);
>  
>  	if (unlikely(!ftrace_graph_active))
> -		return;
> +		goto out;
>  
>  	if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE ||
>  		     fgraph_array[gops->idx] != gops))
> -		return;
> +		goto out;
>  
>  	if (fgraph_lru_release_index(gops->idx) < 0)
> -		return;
> +		goto out;
>  
>  	fgraph_array[gops->idx] = &fgraph_stub;
>  
> @@ -1434,4 +1442,6 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)
>  		unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
>  	}
>  	gops->saved_func = NULL;
> + out:
> +	gops->ops.flags = 0;

Can we make it cleaning only FTRACE_OPS_FL_GRAPH flag here?
Or, ops.flags should be set again before registering it?

Thank you,

>  }
> -- 
> 2.47.2
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] fgraph: Keep track of when fgraph_ops are registered or not
  2025-06-19  3:45 ` Masami Hiramatsu
@ 2025-07-01 22:28   ` Steven Rostedt
  0 siblings, 0 replies; 3+ messages in thread
From: Steven Rostedt @ 2025-07-01 22:28 UTC (permalink / raw)
  To: Masami Hiramatsu (Google)
  Cc: LKML, Linux Trace Kernel, Mathieu Desnoyers, Mark Rutland

On Thu, 19 Jun 2025 12:45:17 +0900
Masami Hiramatsu (Google) <mhiramat@kernel.org> wrote:

> > + out:
> > +	gops->ops.flags = 0;  
> 
> Can we make it cleaning only FTRACE_OPS_FL_GRAPH flag here?
> Or, ops.flags should be set again before registering it?

At first I thought it shouldn't matter, as there shouldn't be any flags
set, but just in case, I'll just clear this one flag.

Thanks,

-- Steve

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-07-01 22:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-18 11:31 [PATCH] fgraph: Keep track of when fgraph_ops are registered or not Steven Rostedt
2025-06-19  3:45 ` Masami Hiramatsu
2025-07-01 22:28   ` Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).