Skip to contentSkip to conversation

tracing/wakeup: Balance prolog on fgraph failure

PATCH In review Sasha Levin 2 participants 4 messages Oct 6, 2025 – Oct 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tracing/wakeup: Balance prolog on fgraph failure

Commit a485ea9e3ef3 ("tracing: Fix irqsoff and wakeup latency tracers when using function graph") added fgraph_reserve_data()/fgraph_retrieve_data() to the wakeup tracer's function-graph hooks.

When func_prolog_preempt_disable() succeeded but those calls failed, the code returned without undoing the prolog (no local_dec(&data->disabled), no preempt_enable_notrace()). That leaked a preempt disable into later code and could trigger "BUG: scheduling while atomic".

Balance the prolog on these failure paths in both entry and return hooks.

Fixes a485ea9e3ef3 ("tracing: Fix irqsoff and wakeup latency tracers when using function graph")
Assisted-by gpt-5-codex
---
 kernel/trace/trace_sched_wakeup.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/trace/trace_sched_wakeup.c +8 -2
index bf1cb80742aed..fa48bbdf0851c 100644
@@ -138,8 +138,11 @@ static int wakeup_graph_entry(struct ftrace_graph_ent *trace,
 		return 0;
 
 	calltime = fgraph_reserve_data(gops->idx, sizeof(*calltime));
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
+		preempt_enable_notrace();
 		return 0;
+	}
 
 	*calltime = trace_clock_local();
 
@@ -169,8 +172,11 @@ static void wakeup_graph_return(struct ftrace_graph_ret *trace,
 	rettime = trace_clock_local();
 
 	calltime = fgraph_retrieve_data(gops->idx, &size);
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
+		preempt_enable_notrace();
 		return;
+	}
 
 	__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
 	local_dec(&data->disabled);
Signature · 1 line
2.51.0
#1 lore raw

PATCH 2/2 tracing/irqsoff: Balance prolog on fgraph failure

Commit a485ea9e3ef3 ("tracing: Fix irqsoff and wakeup latency tracers when using function graph") added fgraph_reserve_data()/fgraph_retrieve_data() to the irqsoff tracer's function-graph hooks.

When func_prolog_dec() succeeded but those calls failed, the code returned without undoing the prolog (no local_dec(&data->disabled)). While func_prolog_dec() does not disable preemption, this left the tracer recursion guard (data->disabled) incremented on this CPU, suppressing subsequent tracing and leaving the tracer state inconsistent.

Balance the prolog on these failure paths in both entry and return hooks.

Fixes a485ea9e3ef3 ("tracing: Fix irqsoff and wakeup latency tracers when using function graph")
Assisted-by gpt-5-codex
---
 kernel/trace/trace_irqsoff.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/trace/trace_irqsoff.c +6 -2
index 5496758b6c760..39152ef3d2432 100644
@@ -202,8 +202,10 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent *trace,
 		return 0;
 
 	calltime = fgraph_reserve_data(gops->idx, sizeof(*calltime));
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
 		return 0;
+	}
 
 	*calltime = trace_clock_local();
 
@@ -233,8 +235,10 @@ static void irqsoff_graph_return(struct ftrace_graph_ret *trace,
 
 	rettime = trace_clock_local();
 	calltime = fgraph_retrieve_data(gops->idx, &size);
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
 		return;
+	}
 
 	trace_ctx = tracing_gen_ctx_flags(flags);
 	__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
Signature · 1 line
2.51.0
maintainer
#2 lore raw

Re: [PATCH 2/2] tracing/irqsoff: Balance prolog on fgraph failure

Quoted 19 lines · On Mon, 6 Oct 2025 13:58:48 -0400 Sasha Levin <[email protected]> wrote:
kernel/trace/trace_irqsoff.c +3 -1
index 5496758b6c760..39152ef3d2432 100644
@@ -202,8 +202,10 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent *trace,
 		return 0;
 
 	calltime = fgraph_reserve_data(gops->idx, sizeof(*calltime));
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
 		return 0;
+	}
 
 	*calltime = trace_clock_local();
 
@@ -233,8 +235,10 @@ static void irqsoff_graph_return(struct ftrace_graph_ret *trace,
 

kernel/trace/trace_irqsoff.c static void irqsoff_graph_return(struct ftrace_graph_ret *trace, · line 244

Quoted 10 lines
kernel/trace/trace_irqsoff.c +3 -1
 	rettime = trace_clock_local();
 	calltime = fgraph_retrieve_data(gops->idx, &size);
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
 		return;
+	}
 
 	trace_ctx = tracing_gen_ctx_flags(flags);
 	__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);

Technically correct, but this would look better:

kernel/trace/trace_irqsoff.c +10 -13
index 5496758b6c76..4c45c49b06c8 100644
@@ -184,7 +184,7 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent *trace,
 	unsigned long flags;
 	unsigned int trace_ctx;
 	u64 *calltime;
-	int ret;
+	int ret = 0;
 
 	if (ftrace_graph_ignore_func(gops, trace))
 		return 0;
@@ -202,13 +202,11 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent *trace,
 		return 0;
 
 	calltime = fgraph_reserve_data(gops->idx, sizeof(*calltime));
-	if (!calltime)
-		return 0;
-
-	*calltime = trace_clock_local();
-
-	trace_ctx = tracing_gen_ctx_flags(flags);
-	ret = __trace_graph_entry(tr, trace, trace_ctx);
+	if (calltime) {
+		*calltime = trace_clock_local();
+		trace_ctx = tracing_gen_ctx_flags(flags);
+		ret = __trace_graph_entry(tr, trace, trace_ctx);
+	}
 	local_dec(&data->disabled);
 
 	return ret;
@@ -233,11 +231,10 @@ static void irqsoff_graph_return(struct ftrace_graph_ret *trace,
 
 	rettime = trace_clock_local();
 	calltime = fgraph_retrieve_data(gops->idx, &size);
-	if (!calltime)
-		return;
-
-	trace_ctx = tracing_gen_ctx_flags(flags);
-	__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
+	if (calltime) {
+		trace_ctx = tracing_gen_ctx_flags(flags);
+		__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
+	}
 	local_dec(&data->disabled);
 }
 

-- Steve

maintainer
#3 lore raw
In reply to Sasha Levin
Quoted 20 lines · On Mon, 6 Oct 2025 13:58:47 -0400 Sasha Levin <[email protected]> wrote:
kernel/trace/trace_sched_wakeup.c +4 -1
index bf1cb80742aed..fa48bbdf0851c 100644
@@ -138,8 +138,11 @@ static int wakeup_graph_entry(struct ftrace_graph_ent *trace,
 		return 0;
 
 	calltime = fgraph_reserve_data(gops->idx, sizeof(*calltime));
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
+		preempt_enable_notrace();
 		return 0;
+	}
 
 	*calltime = trace_clock_local();
 
@@ -169,8 +172,11 @@ static void wakeup_graph_return(struct ftrace_graph_ret *trace,
 	rettime = trace_clock_local();
 

kernel/trace/trace_sched_wakeup.c static void wakeup_graph_return(struct ftrace_graph_ret *trace, · line 182

Quoted 10 lines
kernel/trace/trace_sched_wakeup.c +4 -1
 	calltime = fgraph_retrieve_data(gops->idx, &size);
-	if (!calltime)
+	if (!calltime) {
+		local_dec(&data->disabled);
+		preempt_enable_notrace();
 		return;
+	}
 
 	__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
 	local_dec(&data->disabled);

Technically correct, but this would look better:

kernel/trace/trace_sched_wakeup.c +6 -10
index bf1cb80742ae..e3f2e4f56faa 100644
@@ -138,12 +138,10 @@ static int wakeup_graph_entry(struct ftrace_graph_ent *trace,
 		return 0;
 
 	calltime = fgraph_reserve_data(gops->idx, sizeof(*calltime));
-	if (!calltime)
-		return 0;
-
-	*calltime = trace_clock_local();
-
-	ret = __trace_graph_entry(tr, trace, trace_ctx);
+	if (calltime) {
+		*calltime = trace_clock_local();
+		ret = __trace_graph_entry(tr, trace, trace_ctx);
+	}
 	local_dec(&data->disabled);
 	preempt_enable_notrace();
 
@@ -169,12 +167,10 @@ static void wakeup_graph_return(struct ftrace_graph_ret *trace,
 	rettime = trace_clock_local();
 
 	calltime = fgraph_retrieve_data(gops->idx, &size);
-	if (!calltime)
-		return;
+	if (calltime)
+		__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
 
-	__trace_graph_return(tr, trace, trace_ctx, *calltime, rettime);
 	local_dec(&data->disabled);
-
 	preempt_enable_notrace();
 	return;
 }

-- Steve