Linux Trace Kernel
 help / color / mirror / Atom feed
From: Zhenwen Liang <zhenwenliang@google.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	 linux-trace-kernel@vger.kernel.org
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Tom Zanussi <zanussi@kernel.org>,
	 linux-kernel@vger.kernel.org
Subject: [PATCH] tracing: Order var_set against the variable value in tracing_map
Date: Sat,  5 Dec 2026 13:24:41 +0000	[thread overview]
Message-ID: <20261205132441.3471472-1-zhenwenliang@google.com> (raw)

tracing_map_set_var() stores a variable's value and then sets its
var_set flag; resolve_var_refs() tests the flag with
tracing_map_var_set() and then reads the value. Nothing orders these
accesses, so a histogram trigger on another CPU can see the flag set
but read the old value (0 on a fresh element) and record a wrong
user-visible value, such as a wrong latency. This needs two events
with the same key on different CPUs that are not otherwise ordered,
so it should be rare.

Set the flag with smp_store_release(), test it with smp_load_acquire(),
and use WRITE_ONCE() for the clear in tracing_map_read_var_once().

The problem was found, and this patch prepared, with the help of a
formal analysis tool we are developing that uses AI assistance; I have
reviewed the analysis and the patch.

Fixes: 2734b629525a ("tracing: Add per-element variable support to tracing_map")
Signed-off-by: Zhenwen Liang <zhenwenliang@google.com>
---
Compile-tested only (arm64, W=1); the flag store and load are emitted as
stlrb and ldarb.

 kernel/trace/tracing_map.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/kernel/trace/tracing_map.c b/kernel/trace/tracing_map.c
index d7922f4..035527f 100644
--- a/kernel/trace/tracing_map.c
+++ b/kernel/trace/tracing_map.c
@@ -71,7 +71,8 @@ u64 tracing_map_read_sum(struct tracing_map_elt *elt, unsigned int i)
 void tracing_map_set_var(struct tracing_map_elt *elt, unsigned int i, u64 n)
 {
 	atomic64_set(&elt->vars[i], n);
-	elt->var_set[i] = true;
+	/* Pairs with smp_load_acquire() in tracing_map_var_set() */
+	smp_store_release(&elt->var_set[i], true);
 }
 
 /**
@@ -85,7 +86,8 @@ void tracing_map_set_var(struct tracing_map_elt *elt, unsigned int i, u64 n)
  */
 bool tracing_map_var_set(struct tracing_map_elt *elt, unsigned int i)
 {
-	return elt->var_set[i];
+	/* Pairs with smp_store_release() in tracing_map_set_var() */
+	return smp_load_acquire(&elt->var_set[i]);
 }
 
 /**
@@ -121,7 +123,7 @@ u64 tracing_map_read_var(struct tracing_map_elt *elt, unsigned int i)
  */
 u64 tracing_map_read_var_once(struct tracing_map_elt *elt, unsigned int i)
 {
-	elt->var_set[i] = false;
+	WRITE_ONCE(elt->var_set[i], false);
 	return (u64)atomic64_read(&elt->vars[i]);
 }
 

                 reply	other threads:[~2026-12-05 13:24 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=20261205132441.3471472-1-zhenwenliang@google.com \
    --to=zhenwenliang@google.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 \
    --cc=zanussi@kernel.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