From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796365417; b=4A3f1R6wMl0cDa/WbM0VuDBZM5LqnKKiCnB07dIcQ46Pm2fUvPKyGgM87oWdCZ/8EkmX SO0yDSks5wQYQ+ll/PYT3D/IL3757dZEqAe50gBfVrW8PrIJUrF0TOkyy+9yMGVayM/Hm 05aezb2Ip9fncPoJJ/CdnG27AMmkkiIRdKhQBSdLRwOYarGEn9Ko5lt7ULh4T2rSAY8Oi +2TU9qjT0I3xM18xQh5bmzJ6myLMLRy+HmLhEWU++sCgXS9U6k4sWJAFJd3KIURIDO/20 YhV4ANdKOpA+a0xsb3kx1qbLPajCYykTyRXv5USVE1+oQ0D+JjV6/JBfaB/hxRuEc0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796365417; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:In-Reply-To:Mime-Version: References:X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=cVvG+xzfGphrO2v5kmA5FAHSgkeNgorS8Bnh9skRXrA=; b=N6l8fkCOl68YonlSDy23AMQQLUgVgMeLU6PdR9AFyRCeytWxuGc6TmE2uA2Zc1BCfQ+E fObCqf77L8rIPTxFNXaRQseGXeFwhotk/1g0TNoKna4UiCEjfUmgeJoFeMS0hbmblVK6a YzknEFXm0Ik6uzqeCBgZRlup1bI8oek8LrnWvxsaDhZgYH0VAQ8c6bPi84AWn3UE5oxfF 5PTj1LTFwdxNyjAr0Uweg3zlXATvZfuJVNZNM2ybYjFo9fRa/vRu2Sj6b0rO91chH9gKz rzNlQHaXFd4Bmw2yDQ5GE1J/A6PLbH75TIZOlPWws6TNMpS69gQhGWFshWeTLwSFpYw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ZZh63o5Y; arc=none smtp.remote-ip=209.85.160.69 Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6B6D606A692 for ; Fri, 4 Dec 2026 06:23:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E6B6D606A692 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ZZh63o5Y Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-492f39f29c4so766134fac.1 for ; Thu, 03 Dec 2026 22:23:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796365416; x=1796970216; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cVvG+xzfGphrO2v5kmA5FAHSgkeNgorS8Bnh9skRXrA=; b=ZZh63o5Y15MwVreqN07w0RLLt1cGiB+9nWSAcpfnbAQ/JLBBXLe0CxEQOxqm7VUTEt ySbiV3ApMt9V0Xfqu5aGpMFAGnbQbVx/poZ3ETj1XiB6j9hQqQYvf9ExNll7PvKZEa7S eA5AGkt7nykE809B2n0BqYv6p+7G67CZqtY32LWTnCi8v2l6ODhz54Ckg1uxnHM6N4Rw iGudhcH8WQu6MdPgVBJNMoaVcvVQTBTRo3Y5OiGedNAs3GHq/OrRsS03CgSzg71UNsNi l6BPs51Pf43zNjW1FRVr4OHRIpeRy1Cz3xvTKDWuVgL3QV/iZLIZJpwY8tpmOF4bzu6N Vm6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796365416; x=1796970216; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cVvG+xzfGphrO2v5kmA5FAHSgkeNgorS8Bnh9skRXrA=; b=SrvIhEHsLmwnw3Lwxd4VKdbGXci0x2KrdrR46P2cr1jlIdK4lwGOYeQCJxpRRwgv71 Zx+C77fSz84r2/5K6umeSlfl9VFORJ4BOt8kYKoZ7kpJzJD3MzUEFGQkgN6eaZ6s/ri2 sAJUAo8KolSoT0WAbun9NGSaCGIbhkA/hGly36250Pjnd3sVbowpT0HokE35p4Qbn8Qp xJuAhcxL3aoeOeqX+QB6S8Z9w+3s1OcQ2pel6WifBKNhB6WKgQQfRDtdoN7vRre8vo7J /muNrt81W4iufbibwhbJncOFLiB8JTIQ18TG33+V1OPb/HOqsWMRqgR9TK50Q1JSIhGv d1iw== X-Gm-Message-State: APuU4Z/1vGxoPP3ljaEOglxDxglGPC7CKv52vKQWGvpeGZPgcHSNf1iT fKfd79/JxEaj4fRtPD0SN0PNEa/RFBk7vABZH0u/FU2psqDkP1HDF6kqGn5e5JtCY4LtDG61MCC 3g96ftF/v/vuWIrLD/AgYg1kfi87/41VIYKABV0hbtvCpDWBIIlo9N97iLkjcYUg8ZaiZCSuXh5 Xg1VzhI+GJQqykIXewzpEvPl7WkFWDn9n+nG16CyepNTEJqRpc+MHFtCM= X-Received: from oabya4.prod.google.com ([2002:a05:6871:4cc4:b0:4e2:43b3:bba1]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:b0d6:b0:48f:e157:eb25 with SMTP id 586e51a60fabf-4e3d74dbc5dmr839432fac.44.1796365415642; Thu, 03 Dec 2026 22:23:35 -0800 (PST) Date: Thu, 3 Dec 2026 22:23:23 -0800 In-Reply-To: <20261204062328.2508164-1-irogers@google.com> X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261204062328.2508164-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261204062328.2508164-3-irogers@google.com> Subject: [PATCH v3 2/7] perf synthetic-events: Fix race on DSO build IDs when synthesizing From: Ian Rogers To: linux-perf-users@vger.kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, jolsa@kernel.org, adrian.hunter@intel.com, james.clark@linaro.org, dsahern@gmail.com, andrii@kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" 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 --- 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 #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