[PATCH v2 15/18] rtla: Make stop_tracing variable volatile

Wander Lairson Costa posted 18 patches 1 month ago
[PATCH v2 15/18] rtla: Make stop_tracing variable volatile
Posted by Wander Lairson Costa 1 month ago
The stop_tracing global variable is accessed from both the signal
handler context and the main program flow without synchronization.
This creates a potential race condition where compiler optimizations
could cache the variable value in registers, preventing the signal
handler's updates from being visible to other parts of the program.

Add the volatile qualifier to stop_tracing in both common.c and
common.h to ensure all accesses to this variable bypass compiler
optimizations and read directly from memory. This guarantees that
when the signal handler sets stop_tracing, the change is immediately
visible to the main program loop, preventing potential hangs or
delayed shutdown when termination signals are received.

Signed-off-by: Wander Lairson Costa <wander@redhat.com>
---
 tools/tracing/rtla/src/common.c | 2 +-
 tools/tracing/rtla/src/common.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/tracing/rtla/src/common.c b/tools/tracing/rtla/src/common.c
index d608ffe12e7b0..1e6542a1e9630 100644
--- a/tools/tracing/rtla/src/common.c
+++ b/tools/tracing/rtla/src/common.c
@@ -8,7 +8,7 @@
 #include "common.h"
 
 struct trace_instance *trace_inst;
-int stop_tracing;
+volatile int stop_tracing;
 
 static void stop_trace(int sig)
 {
diff --git a/tools/tracing/rtla/src/common.h b/tools/tracing/rtla/src/common.h
index f2c9e21c03651..283641f3e7c9b 100644
--- a/tools/tracing/rtla/src/common.h
+++ b/tools/tracing/rtla/src/common.h
@@ -54,7 +54,7 @@ struct osnoise_context {
 };
 
 extern struct trace_instance *trace_inst;
-extern int stop_tracing;
+extern volatile int stop_tracing;
 
 struct hist_params {
 	char			no_irq;
-- 
2.52.0
Re: [PATCH v2 15/18] rtla: Make stop_tracing variable volatile
Posted by Steven Rostedt 1 month ago
On Tue,  6 Jan 2026 08:49:51 -0300
Wander Lairson Costa <wander@redhat.com> wrote:

> Add the volatile qualifier to stop_tracing in both common.c and
> common.h to ensure all accesses to this variable bypass compiler
> optimizations and read directly from memory. This guarantees that
> when the signal handler sets stop_tracing, the change is immediately
> visible to the main program loop, preventing potential hangs or
> delayed shutdown when termination signals are received.

In the kernel, this is handled via the READ_ONCE() macro. Perhaps rtla
should implement that too.

-- Steve
Re: [PATCH v2 15/18] rtla: Make stop_tracing variable volatile
Posted by Wander Lairson Costa 1 month ago
On Tue, Jan 6, 2026 at 1:05 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Tue,  6 Jan 2026 08:49:51 -0300
> Wander Lairson Costa <wander@redhat.com> wrote:
>
> > Add the volatile qualifier to stop_tracing in both common.c and
> > common.h to ensure all accesses to this variable bypass compiler
> > optimizations and read directly from memory. This guarantees that
> > when the signal handler sets stop_tracing, the change is immediately
> > visible to the main program loop, preventing potential hangs or
> > delayed shutdown when termination signals are received.
>
> In the kernel, this is handled via the READ_ONCE() macro. Perhaps rtla
> should implement that too.
>

I considered that, but, in this use case, I saw no point because it
didn't bring any advantage and volatile was simpler.
Furthermore, as Crystal pointed out, using volatile for variables
shared with signals is a pretty standard practice.

> -- Steve
>
Re: [PATCH v2 15/18] rtla: Make stop_tracing variable volatile
Posted by Steven Rostedt 1 month ago
On Wed, 7 Jan 2026 10:24:43 -0300
Wander Lairson Costa <wander@redhat.com> wrote:

> > In the kernel, this is handled via the READ_ONCE() macro. Perhaps rtla
> > should implement that too.
> >  
> 
> I considered that, but, in this use case, I saw no point because it
> didn't bring any advantage and volatile was simpler.
> Furthermore, as Crystal pointed out, using volatile for variables
> shared with signals is a pretty standard practice.

OK, I've just been broken in by Linus yelling at anyone defining any
variable as volatile ;-)  I now avoid it at all costs, and only have the
locations that need to be volatile defined that way.

-- Steve
Re: [PATCH v2 15/18] rtla: Make stop_tracing variable volatile
Posted by Crystal Wood 1 month ago
On Tue, 2026-01-06 at 11:05 -0500, Steven Rostedt wrote:
> On Tue,  6 Jan 2026 08:49:51 -0300
> Wander Lairson Costa <wander@redhat.com> wrote:
> 
> > Add the volatile qualifier to stop_tracing in both common.c and
> > common.h to ensure all accesses to this variable bypass compiler
> > optimizations and read directly from memory. This guarantees that
> > when the signal handler sets stop_tracing, the change is immediately
> > visible to the main program loop, preventing potential hangs or
> > delayed shutdown when termination signals are received.
> 
> In the kernel, this is handled via the READ_ONCE() macro. Perhaps rtla
> should implement that too.

Or just get it from tools/include/linux/compiler.h.  No need to reinvent
the wheel (even though several other tools do).

That said, signal safety is a pretty routine use for volatile.

-Crystal