Re: [PATCH] tracing: Keep preemption disabled until the recursion bit is cleared
From: Karl Mehltretter
Date: Sun Nov 29 2026 - 17:35:07 EST
On Tue, Nov 24, 2026 at 11:14:25AM +0100, Steven Rostedt wrote:
> > + preempt_disable_notrace();
> > +
> > bit = trace_get_context_bit() + start;
> > if (unlikely(val & (1 << bit))) {
> > /*
> > @@ -145,6 +147,7 @@ static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsign
> > bit = TRACE_CTX_TRANSITION + start;
> > if (val & (1 << bit)) {
> > do_ftrace_record_recursion(ip, pip);
> > + preempt_enable_notrace();
> > return -1;
>
> Honestly, the reason I did it this way was because if for some reason
> something in preempt_disable_notrace() was traced, this would catch it.
>
> I guess we could change it to do it this way, but if something were to be
> traced in preempt_disable_notrace() it would triple fault and reboot the
> machine with no warning. :-/
>
Good point, better keep this check.
Making the acquire preempt-count update call traced preempt_count_add()
confirms it: the current order catches it on x86 and arm64 (recorded in
recursed_functions), while my patch locks up both with no output and
triple-faults x86 without VMAP_STACK (under TCG, and in 1 of 12 KVM
runs, with gdb attached).
Over 99% of scheduler runs with the bit held on x86 come from
preempt_enable_notrace() in trace_clear_recursion() rescheduling before
trace_recursion_clear(). Moving only the reschedule keeps both count
updates under the bit:
static __always_inline void trace_clear_recursion(int bit)
{
barrier();
__preempt_count_dec();
barrier();
trace_recursion_clear(bit);
if (unlikely(should_resched(0)))
__preempt_schedule_notrace();
}
With traced count updates, this still catches the recursion on x86 and
arm64. Without them, rejected callbacks per boot on x86:
current this change my patch
TCG, function 6-58 0 0
TCG, function_graph 755-1463 0 0
KVM + KASAN, function 160-291 0-12 0
KVM + KASAN, function_graph 104-362 0-14 0
With KASAN, this change hit rejects in 1/10 boots per tracer. On arm64
KVM, all variants had 0 rejects in 23 boots each.
Two short windows remain where an interrupt can preempt with the bit
held (between setting the bit and preempt_disable_notrace(), and between
the decrement and clearing the bit). In the KASAN runs, those were the
only scheduler entries with the bit held.
Would you rather drop the patch, or go with one of these?
1. Send the partial fix above as v2 (keeps your check, fixes >99%,
leaves the two short interrupt windows).
2. Reorder as in my patch using small noinstr helpers for the count
updates (closes both windows, objtool checks noinstr on x86, loses
the runtime check, adds a helper call).
Thanks,
Karl