[PATCH v9 11/11] perf test symbols: Add session allocation wrapper and lock contention tests

From: Ian Rogers

Date: Mon Nov 23 2026 - 22:30:23 EST


Add test_session_alloc_shrink() to tools/perf/tests/symbols.c verifying:
- machine__session() and dso__session() back-pointer resolution.
- perf_session__malloc(), zalloc(), calloc(), realloc(), and strdup()
allocation wrappers and perf_session__shrink() eviction.
- Multithreaded contention between dso__find_binary_type()
(dso__data_open_lock -> dso__lock(dso)) and perf_session__shrink() under
dso__lock(dso), plus perf_session__e_machine() shard iteration.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/tests/symbols.c | 162 +++++++++++++++++++++++++++++++++++++
1 file changed, 162 insertions(+)

diff --git a/tools/perf/tests/symbols.c b/tools/perf/tests/symbols.c
index 1fd6bfdc4147..c6375bd7e3d9 100644
--- a/tools/perf/tests/symbols.c
+++ b/tools/perf/tests/symbols.c
@@ -8,6 +8,8 @@
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
+#include <pthread.h>
+#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@@ -19,6 +21,7 @@
#include "dso.h"
#include "env.h"
#include "machine.h"
+#include "session.h"
#include "srcline.h"
#include "thread.h"
#include "symbol.h"
@@ -320,6 +323,161 @@ static int test_dsos_shrink(void)
return ret;
}

+struct shrink_lock_race_args {
+ struct dso *dso;
+ atomic_bool stop;
+};
+
+static void *shrink_lock_race_worker(void *arg)
+{
+ struct shrink_lock_race_args *args = arg;
+
+ while (!atomic_load_explicit(&args->stop, memory_order_relaxed))
+ dso__find_binary_type(args->dso);
+
+ return NULL;
+}
+
+static int test_session_alloc_shrink(void)
+{
+ struct perf_session *session = zalloc(sizeof(*session));
+ struct dso *dso;
+ struct thread *th;
+ pid_t pid = getpid();
+ char *p1 = NULL, *p2 = NULL, *p3 = NULL, *s = NULL, *tmp;
+ int ret = TEST_FAIL;
+
+ if (!session)
+ return TEST_FAIL;
+
+ perf_env__init(&session->header.env);
+ if (machines__init(&session->machines, session))
+ goto out;
+ session->machines.host.env = &session->header.env;
+ if (machine__session(&session->machines.host) != session) {
+ pr_debug("machine__session did not return owning session!\n");
+ goto out;
+ }
+
+ dso = machine__findnew_dso(&session->machines.host, "/tmp/perf_test_session_dso");
+ if (!dso)
+ goto out;
+ if (dso__session(dso) != session) {
+ pr_debug("dso__session did not return owning session!\n");
+ dso__put(dso);
+ goto out;
+ }
+ dso__put(dso);
+
+ p1 = perf_session__malloc(session, 64);
+ p2 = perf_session__zalloc(session, 64);
+ p3 = perf_session__calloc(session, 4, 16);
+ s = perf_session__strdup(session, "perf_session_alloc_test");
+ if (!p1 || !p2 || !p3 || !s || strcmp(s, "perf_session_alloc_test") != 0) {
+ pr_debug("perf_session allocation wrappers failed!\n");
+ goto out;
+ }
+
+ tmp = perf_session__realloc(session, p1, 128);
+ if (!tmp) {
+ pr_debug("perf_session__realloc failed!\n");
+ goto out;
+ }
+ p1 = tmp;
+
+ if (perf_session__shrink(session) == 0) {
+ pr_debug("perf_session__shrink failed to evict unreferenced DSO!\n");
+ goto out;
+ }
+ down_read(&session->machines.host.dsos.lock);
+ if (session->machines.host.dsos.cnt != 0) {
+ up_read(&session->machines.host.dsos.lock);
+ pr_debug("perf_session__shrink failed to evict unreferenced DSO!\n");
+ goto out;
+ }
+ up_read(&session->machines.host.dsos.lock);
+
+ /*
+ * Regression test for AB-BA lock inversion between dso__lock(dso_A)
+ * (held by dso__load() when calling perf_session__shrink() to evict
+ * unreferenced dso_B) and dso__data_open_lock (acquired before
+ * dso__lock(dso_A) by dso__find_binary_type()).
+ */
+ dso = machine__findnew_dso(&session->machines.host, "/tmp/perf_test_contended_dso");
+ if (!dso)
+ goto out;
+ {
+ bool was_singlethreaded = perf_singlethreaded;
+ struct shrink_lock_race_args race_args = {
+ .dso = dso,
+ .stop = false,
+ };
+ pthread_t worker;
+
+ perf_set_multithreaded();
+ if (pthread_create(&worker, NULL, shrink_lock_race_worker, &race_args) != 0) {
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ dso__put(dso);
+ goto out;
+ }
+ for (int i = 0; i < 200; i++) {
+ struct dso *evict = machine__findnew_dso(&session->machines.host,
+ "/tmp/perf_test_evict_dso");
+
+ dso__put(evict);
+ mutex_lock(dso__lock(dso));
+ RC_CHK_ACCESS(dso)->bintype_searched = 0;
+ perf_session__shrink(session);
+ mutex_unlock(dso__lock(dso));
+ }
+ atomic_store_explicit(&race_args.stop, true, memory_order_relaxed);
+ pthread_join(worker, NULL);
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ }
+ dso__put(dso);
+ perf_session__shrink(session);
+
+ /*
+ * Verify perf_session__e_machine() -> threads__for_each_thread() does
+ * not self-deadlock on table->lock when a non-leader thread looks up or
+ * creates its leader thread in the same shard.
+ */
+ {
+ bool was_singlethreaded = perf_singlethreaded;
+ uint16_t e_machine;
+
+ perf_set_multithreaded();
+ th = machine__findnew_thread(&session->machines.host, pid,
+ pid + THREADS__TABLE_SIZE);
+ if (!th) {
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ goto out;
+ }
+ thread__put(th);
+ e_machine = perf_session__e_machine(session, NULL);
+ if (was_singlethreaded)
+ perf_set_singlethreaded();
+ if (e_machine == EM_NONE) {
+ pr_debug("perf_session__e_machine failed!\n");
+ goto out;
+ }
+ }
+
+ ret = TEST_OK;
+out:
+ free(p1);
+ free(p2);
+ free(p3);
+ free(s);
+ machines__exit(&session->machines);
+ perf_env__exit(&session->header.env);
+ free(session);
+ return ret;
+}
+
struct shrink_reload_find_args {
struct symbol *kept;
u64 first_start;
@@ -822,6 +980,10 @@ static int test__symbols(struct test_suite *test __maybe_unused, int subtest __m
if (ret != TEST_OK)
return ret;

+ ret = test_session_alloc_shrink();
+ if (ret != TEST_OK)
+ return ret;
+
ret = init_test_info(&ti);
if (ret != TEST_OK)
return ret;
--
2.56.1.809.ga0b61035b5-goog