Linux Trace Kernel
 help / color / mirror / Atom feed
From: Karl Mehltretter <kmehltretter@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] tracing: Don't use TRACE_BRANCH_BIT as a mask
Date: Sun, 29 Nov 2026 01:06:09 +0100	[thread overview]
Message-ID: <20261129000609.45955-1-kmehltretter@gmail.com> (raw)

TRACE_BRANCH_BIT is a bit number, but the branch tracer uses it as a
mask on current->trace_recursion. Instead of its own bit, the branch
tracer tests, sets and clears bits that function tracing uses for its
own recursion protection.

While a function tracing callback holds one of these bits, the current
task's branch events are lost. This includes events in an interrupt that
arrives during the callback.

Use the trace_recursion_*() helpers, which take a bit number.

Fixes: 6224beb12e19 ("tracing: Have branch tracer use recursive field of task struct")
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
Assisted by an LLM.

In mainline, the mask only covered unused bits until v5.7, when commit
5412e0b763e0 ("tracing: Remove unused TRACE_BUFFER bits") moved the
function tracing bits to 0-3. TRACE_BRANCH_BIT is now 15 (bits 0-3) in
mainline and 6.1.y to 7.3.y, and 10 (bits 1 and 3) in 5.10.y and 5.15.y.

Tested on v7.4-rc2 with PREEMPT and PROVE_LOCKING in an arm64 KVM guest
on a Raspberry Pi 500+. I ran function and function_graph with the
branch option set while forking processes. Test-only counters recorded
skips that were not due to recursion.

Share of non-recursive branch events skipped, three boots each:

                      v7.4-rc2                + this patch
             function     function_graph     (both tracers)
  hardirq    7.5-18.8%    10.6-11.0%         0
  softirq    4.0-5.4%     6.3-7.3%           0
  task       0.1%         0.4%               0

The branch tracer alone shows 0 on both kernels. Recursion is still
caught with the patch. These are debug kernels with test counters, so
other setups will show different rates. NMIs and other architectures are
untested.

 kernel/trace/trace_branch.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/trace_branch.c b/kernel/trace/trace_branch.c
index d8e97ad798f0..ed19e69f9294 100644
--- a/kernel/trace/trace_branch.c
+++ b/kernel/trace/trace_branch.c
@@ -38,7 +38,7 @@ probe_likely_condition(struct ftrace_likely_data *f, int val, int expect)
 	unsigned int trace_ctx;
 	const char *p;
 
-	if (current->trace_recursion & TRACE_BRANCH_BIT)
+	if (trace_recursion_test(TRACE_BRANCH_BIT))
 		return;
 
 	/*
@@ -52,7 +52,7 @@ probe_likely_condition(struct ftrace_likely_data *f, int val, int expect)
 		return;
 
 	raw_local_irq_save(flags);
-	current->trace_recursion |= TRACE_BRANCH_BIT;
+	trace_recursion_set(TRACE_BRANCH_BIT);
 	if (!tracer_tracing_is_on_cpu(tr, raw_smp_processor_id()))
 		goto out;
 
@@ -80,7 +80,7 @@ probe_likely_condition(struct ftrace_likely_data *f, int val, int expect)
 	trace_buffer_unlock_commit_nostack(buffer, event);
 
  out:
-	current->trace_recursion &= ~TRACE_BRANCH_BIT;
+	trace_recursion_clear(TRACE_BRANCH_BIT);
 	raw_local_irq_restore(flags);
 }
 

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.39.5


                 reply	other threads:[~2026-11-29  0:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261129000609.45955-1-kmehltretter@gmail.com \
    --to=kmehltretter@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    /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