Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] tracing: Order var_set against the variable value in tracing_map
@ 2026-12-05 13:24 Zhenwen Liang
  0 siblings, 0 replies; only message in thread
From: Zhenwen Liang @ 2026-12-05 13:24 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, linux-trace-kernel
  Cc: Mathieu Desnoyers, Tom Zanussi, linux-kernel

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]);
 }
 

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-05 13:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 13:24 [PATCH] tracing: Order var_set against the variable value in tracing_map Zhenwen Liang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox