[PATCH v2] tracing: Keep preemption disabled until the recursion bit is cleared
From: Karl Mehltretter
Date: Wed Dec 02 2026 - 20:38:44 EST
Function tracing can falsely reject callbacks for interrupt entry and
exit functions, such as irq_enter_rcu(), as recursive.
trace_test_and_set_recursion() sets the recursion bit before disabling
preemption, and trace_clear_recursion() enables preemption before
clearing the bit. If the task is preempted in either window, a traced
scheduler function sees the bit set and uses the transition bit.
Tracing in an interrupt arriving during that callback then finds both
bits set and rejects the callback.
Disable preemption right before setting the bit and re-enable it after
clearing the bit. Leaving the RCU and recursion checks before
preempt_disable_notrace() avoids touching the preempt count on rejected
calls.
The preempt count helpers then run outside the recursion guard. They
must remain untraced, as preempt_schedule_notrace() already requires.
Suggested-by: Steven Rostedt <rostedt@xxxxxxxxxxx>
Signed-off-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
---
Assisted by an LLM.
Changes since v1 [1]:
- Move preempt_disable_notrace() after the RCU and recursion checks,
following Steven's suggestion [2], so rejected calls do not update
the preempt count.
- Rebase from v7.4-rc2 to v7.4-rc5.
No Fixes: tag. Most callers had the same ordering before ce5e48036c9e
("ftrace: disable preemption when recursion locked") moved the preempt
calls into these helpers.
Tested on v7.4-rc5 in QEMU x86_64 TCG with PREEMPT, DEBUG_PREEMPT,
PROVE_LOCKING, FTRACE_RECORD_RECURSION, FTRACE_VALIDATE_RCU_IS_WATCHING
and FTRACE_STARTUP_TEST. Each boot ran fork bursts and the callback/IPI
overlap test module under the function and function_graph tracers.
Distinct recursion sites recorded, two boots each
(function / function_graph):
v7.4-rc5 + this patch
x86_64 0 / 13-14 0 / 0
All 14 ftrace startup self-tests passed on every boot with no warnings.
Earlier v7.4-rc2 tests of this diff with the transition bit removed for
testing, distinct recursion sites recorded (function / function_graph):
v7.4-rc2 + this patch
x86_64 103-105 / 124-128 12-13 / 13-14
arm64 36 / 62 4 / 6
x86_64 ran under KVM with and without VMAP_STACK (matching v1), and
arm64 ran under TCG. Forcing a traced call right after
preempt_disable_notrace() on v7.4-rc2 was rejected by mainline's
recursion guard. With this patch it hung with VMAP_STACK and
triple-faulted without it. Replacing preempt_disable_notrace() with
traced preempt_count_add() also hung mainline.
[1] https://lore.kernel.org/all/20261113011333.20813-1-kmehltretter@xxxxxxxxx/
[2] https://lore.kernel.org/all/20261130113108.293e4d1d@xxxxxxxxxxxxxxxxxx/
include/linux/trace_recursion.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/include/linux/trace_recursion.h b/include/linux/trace_recursion.h
index e6ca052b2a8..95c3a066dab 100644
--- a/include/linux/trace_recursion.h
+++ b/include/linux/trace_recursion.h
@@ -149,12 +149,12 @@ static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsign
}
}
+ preempt_disable_notrace();
+
val |= 1 << bit;
current->trace_recursion = val;
barrier();
- preempt_disable_notrace();
-
return bit;
}
@@ -163,9 +163,9 @@ static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsign
*/
static __always_inline void trace_clear_recursion(int bit)
{
- preempt_enable_notrace();
barrier();
trace_recursion_clear(bit);
+ preempt_enable_notrace();
}
/**
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.39.5 (Apple Git-154)