[PATCH stalld 42/52] stalld: fix data race on config_buffer_size

Wander Lairson Costa <[email protected]> Mon, 8 Jun 2026 15:31:52 -0300
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
In aggressive mode multiple cpu_main threads execute sched_debug_get()
concurrently. Each thread may double config_buffer_size when the
sched_debug output outgrows the current buffer, and
resize_buffer_if_needed() reads the variable from every thread without
synchronization. These concurrent accesses to a plain size_t
constitute a data race under the C11 memory model.

Declare config_buffer_size as _Atomic size_t so that stores and loads
are indivisible and visible across threads.

Signed-off-by: Wander Lairson Costa <[email protected]>
---
 src/stalld.c | 2 +-
 src/stalld.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/stalld.c b/src/stalld.c
index 0edb30c..feb0099 100644
--- a/src/stalld.c
+++ b/src/stalld.c
@@ -94,7 +94,7 @@ long page_size;
  * in detect_task_format. May change over time as the
  * system gets loaded
  */
-size_t config_buffer_size;
+_Atomic size_t config_buffer_size;
 
 /*
  * Boolean for if running under systemd.
diff --git a/src/stalld.h b/src/stalld.h
index 81ce9f7..03a4ca1 100644
--- a/src/stalld.h
+++ b/src/stalld.h
@@ -243,7 +243,7 @@ extern regex_t *compiled_regex_thread;
 extern regex_t *compiled_regex_process;
 extern char *config_sched_debug_path;
 extern int config_reservation;
-extern size_t config_buffer_size;
+extern _Atomic size_t config_buffer_size;
 extern long page_size;
 extern struct stalld_backend *backend;
 extern char *config_affinity_cpus;
-- 
2.54.0