[PATCH v11 16/19] perf env: Add mutex to protect lazy environment initialization
From: Ian Rogers
Date: Tue Jun 02 2026 - 02:32:23 EST
Introduce a mutex to 'struct perf_env' to safely protect lazy
metadata setup, such as os_release or e_machine resolution,
preventing concurrent initialization data races and memory leaks
during multi-threaded profiling or symbol loading.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/env.c | 3 +++
tools/perf/util/env.h | 3 +++
2 files changed, 6 insertions(+)
diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
index 28c54c58193e..4de07cc7ef5d 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -250,6 +250,8 @@ void perf_env__exit(struct perf_env *env)
{
int i, j;
+ mutex_destroy(&env->lock);
+
perf_env__purge_bpf(env);
perf_env__purge_cgroups(env);
zfree(&env->hostname);
@@ -307,6 +309,7 @@ void perf_env__init(struct perf_env *env)
init_rwsem(&env->bpf_progs.lock);
#endif
env->kernel_is_64_bit = -1;
+ mutex_init(&env->lock);
}
static void perf_env__init_kernel_mode(struct perf_env *env)
diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h
index 83e74328798f..6aaf80c640bd 100644
--- a/tools/perf/util/env.h
+++ b/tools/perf/util/env.h
@@ -6,6 +6,7 @@
#include <linux/rbtree.h>
#include "cpumap.h"
#include "rwsem.h"
+#include "mutex.h"
struct perf_cpu_map;
@@ -156,6 +157,8 @@ struct perf_env {
*/
bool enabled;
} clock;
+ /* Protects lazy environment initialization (e.g. os_release, e_machine). */
+ struct mutex lock;
};
enum perf_compress_type {
--
2.54.0.929.g9b7fa37559-goog