[PATCH stalld 34/52] stalld: fix data race on thread_running

Wander Lairson Costa <[email protected]> Mon, 8 Jun 2026 15:31:44 -0300
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
The thread_running field in struct cpu_info is accessed concurrently by
the main monitoring thread and per-CPU worker threads without any
synchronization. The main thread reads thread_running in
conservative_main() to decide whether to spawn a new worker, while
worker threads clear it in cpu_main() upon exit. This constitutes a
data race under the C11 memory model.

Declare thread_running as _Atomic int to ensure proper visibility and
ordering of cross-thread accesses.

Signed-off-by: Wander Lairson Costa <[email protected]>
---
 src/stalld.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/stalld.h b/src/stalld.h
index 73e20df..69bc9e7 100644
--- a/src/stalld.h
+++ b/src/stalld.h
@@ -11,6 +11,7 @@
 
 #include <regex.h>
 #include <sched.h>
+#include <stdatomic.h>
 
 #define BUFFER_PAGES		10
 #define MAX_WAITING_PIDS	30
@@ -68,7 +69,7 @@ struct cpu_info {
        int nr_rt_running;
        int ctxsw;
        int nr_waiting_tasks;
-       int thread_running;
+       _Atomic int thread_running;
        long idle_time;
        struct task_info *starving;
        pthread_t thread;
-- 
2.54.0