From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
James Clark <james.clark@linaro.org>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Clark Williams <williams@redhat.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH v11 4/9] perf mutex: Add DO_ONCE() for one-time initialization
Date: Sun, 4 Oct 2026 19:29:55 +0200 [thread overview]
Message-ID: <20261004173000.2863935-5-acme@kernel.org> (raw)
In-Reply-To: <20261004173000.2863935-1-acme@kernel.org>
From: Arnaldo Carvalho de Melo <acme@redhat.com>
For lazy init that more than one thread can race into, without
pthread_once() so that it stays within perf's locking primitives and
clang's -Wthread-safety keeps an eye on the mutexes involved.
Mirrors the kernel's include/linux/once.h DO_ONCE(), dropping its
static key fast path, unnecessary at these cold init paths: a per call
site static bool plus a statically initialized mutex, double checked.
As there, code reachable from more than one call site must go through
a common helper.
The lockless fast path reads ___done with an acquire, paired with a
release store after fn(), so that fn()'s side effects are visible to
threads taking the fast path, which the dropped static key used to
guarantee.
The __atomic builtins are used rather than smp_load_acquire()/
smp_store_release() because ThreadSanitizer understands them,
avoiding reports on ___done and on what fn() initialized.
Assisted-by: LLM
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/mutex.h | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h
index fe04db21d19c5b0b..5554937675886ef3 100644
--- a/tools/perf/util/mutex.h
+++ b/tools/perf/util/mutex.h
@@ -104,6 +104,30 @@ struct LOCKABLE mutex {
#endif
#define DEFINE_MUTEX(name) struct mutex name = { .lock = __PERF_MUTEX_INITIALIZER }
+/*
+ * Runs fn() exactly once however many threads race to it, the rest wait
+ * for it to finish; per call site state, so multiple sites wanting the
+ * same once use a common helper, as in the kernel's DO_ONCE().
+ */
+#define DO_ONCE(fn, ...) \
+ ({ \
+ static bool ___done; \
+ static DEFINE_MUTEX(___once_lock); \
+ bool ___ret = false; \
+ \
+ if (!__atomic_load_n(&___done, __ATOMIC_ACQUIRE)) { \
+ mutex_lock(&___once_lock); \
+ if (!___done) { \
+ fn(__VA_ARGS__); \
+ __atomic_store_n(&___done, true, \
+ __ATOMIC_RELEASE); \
+ ___ret = true; \
+ } \
+ mutex_unlock(&___once_lock); \
+ } \
+ ___ret; \
+ })
+
/* A wrapper around the condition variable implementation. */
struct cond {
pthread_cond_t cond;
--
2.55.0
next prev parent reply other threads:[~2026-10-04 17:30 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-04 17:29 [PATCH v11 0/9] perf tools: Add progress diagnostics and a false-sharing workload Arnaldo Carvalho de Melo
2026-10-04 17:29 ` [PATCH v11 1/9] perf config: Move perf_config__set_variable() to util/config.c Arnaldo Carvalho de Melo
2026-10-04 17:44 ` sashiko-bot
2026-10-04 17:29 ` [PATCH v11 2/9] perf config: Make perf_etc_perfconfig() never return NULL Arnaldo Carvalho de Melo
2026-10-04 17:37 ` sashiko-bot
2026-10-04 17:29 ` [PATCH v11 3/9] perf mutex: Add DEFINE_MUTEX() static initializer Arnaldo Carvalho de Melo
2026-10-04 17:35 ` sashiko-bot
2026-10-04 17:29 ` Arnaldo Carvalho de Melo [this message]
2026-10-04 17:36 ` [PATCH v11 4/9] perf mutex: Add DO_ONCE() for one-time initialization sashiko-bot
2026-10-04 17:29 ` [PATCH v11 5/9] perf config: Serialize config file access with a mutex Arnaldo Carvalho de Melo
2026-10-04 17:40 ` sashiko-bot
2026-10-04 17:29 ` [PATCH v11 6/9] perf report: Add --progress option Arnaldo Carvalho de Melo
2026-10-04 17:37 ` sashiko-bot
2026-10-04 17:29 ` [PATCH v11 7/9] perf report: Add --no-progress option Arnaldo Carvalho de Melo
2026-10-04 17:37 ` sashiko-bot
2026-10-04 17:29 ` [PATCH v11 8/9] perf scripts: Add perf-stuck, to tell where a running perf is stuck Arnaldo Carvalho de Melo
2026-10-04 17:38 ` sashiko-bot
2026-10-04 17:30 ` [PATCH v11 9/9] perf test: Add false_sharing workload exhibiting cross-CPU false sharing Arnaldo Carvalho de Melo
2026-10-04 17:36 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261004173000.2863935-5-acme@kernel.org \
--to=acme@kernel.org \
--cc=acme@redhat.com \
--cc=adrian.hunter@intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
--cc=tglx@linutronix.de \
--cc=williams@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.