[PATCH v4 07/17] perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso()

From: Ian Rogers

Date: Fri Oct 16 2026 - 18:20:21 EST


__machine__addnew_vdso() called __dsos__add(&machine->dsos, dso) without
holding machine->dsos.lock. Acquire machine->dsos.lock in write mode,
double-check __dsos__find() to avoid inserting duplicate VDSO entries
when racing with another thread, and set the long name before calling
__dsos__add() so it does not attempt to re-acquire machine->dsos.lock.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/dsos.c | 7 ++++++-
tools/perf/util/dsos.h | 1 +
tools/perf/util/vdso.c | 17 +++++++++++++++--
3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c
index a2cd37bd1660..40267d3a725d 100644
--- a/tools/perf/util/dsos.c
+++ b/tools/perf/util/dsos.c
@@ -284,12 +284,17 @@ static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const st
return res;
}

+struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked)
+{
+ return __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, write_locked);
+}
+
struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short)
{
struct dso *res;

down_read(&dsos->lock);
- res = __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, /*write_locked=*/false);
+ res = __dsos__find(dsos, name, cmp_short, /*write_locked=*/false);
up_read(&dsos->lock);
return res;
}
diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h
index a26774950866..857a79fd8805 100644
--- a/tools/perf/util/dsos.h
+++ b/tools/perf/util/dsos.h
@@ -30,6 +30,7 @@ void dsos__exit(struct dsos *dsos);

int __dsos__add(struct dsos *dsos, struct dso *dso);
int dsos__add(struct dsos *dsos, struct dso *dso);
+struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short, bool write_locked);
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);
diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c
index c12f5d8c4bf6..dca4f683be9b 100644
--- a/tools/perf/util/vdso.c
+++ b/tools/perf/util/vdso.c
@@ -127,13 +127,26 @@ void machine__exit_vdso(struct machine *machine)
static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
const char *long_name)
{
- struct dso *dso;
+ struct dso *dso, *existing;
+
+ down_write(&machine->dsos.lock);
+ existing = __dsos__find(&machine->dsos, short_name, true, /*write_locked=*/true);
+ if (existing) {
+ up_write(&machine->dsos.lock);
+ return existing;
+ }

dso = dso__new(short_name);
if (dso != NULL) {
- __dsos__add(&machine->dsos, dso);
+ /*
+ * Set the long name before adding to machine->dsos so that
+ * __dsos__add() inserts it in sorted order without needing to
+ * re-take machine->dsos.lock.
+ */
dso__set_long_name(dso, long_name, false);
+ __dsos__add(&machine->dsos, dso);
}
+ up_write(&machine->dsos.lock);

return dso;
}
--
2.56.0.212.g129ede20d9-goog