mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: linux-perf-users@vger.kernel.org, acme@kernel.org, namhyung@kernel.org
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
	mingo@redhat.com,  jolsa@kernel.org, adrian.hunter@intel.com,
	james.clark@linaro.org,  dsahern@gmail.com, andrii@kernel.org,
	Ian Rogers <irogers@google.com>
Subject: [PATCH 2/6] perf synthetic-events: Fix race on DSO build IDs when synthesizing
Date: Thu,  3 Dec 2026 17:04:47 -0800	[thread overview]
Message-ID: <20261204010452.1937224-3-irogers@google.com> (raw)
In-Reply-To: <20261204010452.1937224-1-irogers@google.com>

perf_event__synthesize_threads() may use several threads that share the
machine's dsos. perf_record_mmap2__read_build_id() reads a DSO's build
ID, to avoid reading the file again, and sets it after reading the
file, without any locking. Threads synthesizing processes that map the
same file, such as libc, can race reading and writing the same build
ID, and the build ID is also read by dsos__findnew_id() when comparing
DSO ids and checking if an id needs improving.

Those lookups hold the dsos lock, and DSO ids are already updated by
__dso__improve_id() with the dsos write lock held. Add
dsos__findnew_id_build_id(), which also copies the DSO's build ID
while the lookup holds the dsos lock, and dsos__set_dso_build_id() to
set the build ID, if unset, holding the dsos write lock, and use them
when synthesizing. The writer then excludes both the synthesis readers
and the lookups, and reading the build ID needs no extra locking. As
before, setting the build ID doesn't re-sort dsos; DSOs created when
synthesizing are keyed by device and inode, which are compared before
the build ID.

ThreadSanitizer reported 8 data races on the build ID, between
dso__set_build_id() and dso__has_build_id()/dso__needs_improve_id(), in
'perf bench internals synthesize -t -m 8 -M 8' (with the benchmark
fixed to initialize libelf so build IDs are read) and none with this
change.

Fixes: fd643afc8f60 ("perf record: Save DSO build-ID for synthesizing")
Signed-off-by: Ian Rogers <irogers@google.com>
---

Notes:
    The race was found by the Sashiko AI review tool when reviewing an
    earlier version of this series.
    
    This change was developed with the help of an AI coding assistant
    (Gemini in Jetski), which wrote the initial code, tests and
    changelog; it was reviewed, built and tested by the author.

 tools/perf/util/dsos.c             | 38 +++++++++++++++++++++++++++++-
 tools/perf/util/dsos.h             |  4 ++++
 tools/perf/util/synthetic-events.c | 12 ++++++----
 3 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index 9ae27ee93413..e095eb59ae7b 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -340,13 +340,34 @@ static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, const
 	return dso ? dso : __dsos__addnew_id(dsos, name, id);
 }
 
-struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id)
+/* Copy the build ID of dso, size 0 if it has none, with dsos->lock held. */
+static void dso__copy_build_id(const struct dso *dso, struct build_id *bid)
+{
+	if (!bid)
+		return;
+
+	if (dso && dso__has_build_id(dso))
+		*bid = *dso__bid(dso);
+	else
+		bid->size = 0;
+}
+
+/*
+ * Like dsos__findnew_id() but, if bid isn't NULL, also copies the DSO's build
+ * ID to bid, with size 0 if there is none, while holding the dsos lock. A
+ * DSO's build ID is part of the id compared by lookups holding the lock, and
+ * it may be set by threads synthesizing events in parallel, see
+ * dsos__set_dso_build_id(), so it must not be read without the lock.
+ */
+struct dso *dsos__findnew_id_build_id(struct dsos *dsos, const char *name,
+				      const struct dso_id *id, struct build_id *bid)
 {
 	struct dso *dso;
 
 	down_read(&dsos->lock);
 	dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/false);
 	if (dso && !dso__needs_improve_id(dso, id)) {
+		dso__copy_build_id(dso, bid);
 		up_read(&dsos->lock);
 		return dso;
 	}
@@ -355,10 +376,25 @@ struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct d
 
 	down_write(&dsos->lock);
 	dso = __dsos__findnew_id(dsos, name, id);
+	dso__copy_build_id(dso, bid);
 	up_write(&dsos->lock);
 	return dso;
 }
 
+struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id)
+{
+	return dsos__findnew_id_build_id(dsos, name, id, /*bid=*/NULL);
+}
+
+/* Set the build ID of a DSO in dsos, if it has none, holding the dsos lock. */
+void dsos__set_dso_build_id(struct dsos *dsos, struct dso *dso, const struct build_id *bid)
+{
+	down_write(&dsos->lock);
+	if (!dso__has_build_id(dso))
+		dso__set_build_id(dso, bid);
+	up_write(&dsos->lock);
+}
+
 struct dsos__fprintf_buildid_cb_args {
 	FILE *fp;
 	bool (*skip)(struct dso *dso, int parm);
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index cc7e3a42f9cb..b3c6a6e9ced5 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -8,6 +8,7 @@
 #include <linux/rbtree.h>
 #include "rwsem.h"
 
+struct build_id;
 struct dso;
 struct dso_id;
 struct kmod_path;
@@ -35,6 +36,9 @@ struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bo
 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short);
 
 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id);
+struct dso *dsos__findnew_id_build_id(struct dsos *dsos, const char *name,
+				      const struct dso_id *id, struct build_id *bid);
+void dsos__set_dso_build_id(struct dsos *dsos, struct dso *dso, const struct build_id *bid);
  
 bool dsos__read_build_ids(struct dsos *dsos, bool with_hits);
 
diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 3c8d29f27fd9..445199d7bbd4 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -4,6 +4,7 @@
 #include "util/data.h"
 #include "util/debug.h"
 #include "util/dso.h"
+#include "util/dsos.h"
 #include "util/event.h"
 #include "util/evlist.h"
 #include "util/machine.h"
@@ -462,9 +463,10 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
 		dso_id.mmap2_ino_generation_valid = true;
 	}
 
-	dso = dsos__findnew_id(&machine->dsos, event->filename, &dso_id);
-	if (dso && dso__has_build_id(dso)) {
-		bid = *dso__bid(dso);
+	dso = dsos__findnew_id_build_id(&machine->dsos, event->filename, &dso_id, &bid);
+	if (dso && bid.size) {
+		/* The DSO already has the build ID, don't set it again. */
+		dso__zput(dso);
 		rc = 0;
 		goto out;
 	}
@@ -485,8 +487,8 @@ static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
 		event->__reserved_1 = 0;
 		event->__reserved_2 = 0;
 
-		if (dso && !dso__has_build_id(dso))
-			dso__set_build_id(dso, &bid);
+		if (dso)
+			dsos__set_dso_build_id(&machine->dsos, dso, &bid);
 	} else {
 		if (event->filename[0] == '/') {
 			pr_debug2("Failed to read build ID for %s\n",
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-04  1:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  1:04 [PATCH 0/6] perf synthetic-events: Use PROCMAP_QUERY for mmap synthesis Ian Rogers
2026-12-04  1:04 ` [PATCH 1/6] perf machine: Fix leak when creating host kernel maps fails Ian Rogers
2026-12-04  1:04 ` Ian Rogers [this message]
2026-12-04  1:04 ` [PATCH 3/6] perf bench synthesize: Initialize symbols so build IDs can be read Ian Rogers
2026-12-04  1:04 ` [PATCH 4/6] perf synthetic-events: Use PROCMAP_QUERY to synthesize mmap events Ian Rogers
2026-12-04  1:04 ` [PATCH 5/6] perf bench synthesize: Add --procmap-query option Ian Rogers
2026-12-04  1:04 ` [PATCH 6/6] perf test: Add test comparing PROCMAP_QUERY and /proc/pid/maps synthesis Ian Rogers

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=20261204010452.1937224-3-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=andrii@kernel.org \
    --cc=dsahern@gmail.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@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®