From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match
Date: Tue, 17 Nov 2026 23:55:12 -0800 [thread overview]
Message-ID: <20261118075516.1278903-2-irogers@google.com> (raw)
In-Reply-To: <20261118075516.1278903-1-irogers@google.com>
threads__find() looks up the thread under down_read(&table->lock), drops
the read lock, and then acquires down_write(&table->lock) inside
threads_table_entry__set_last_match() to update table->last_match.
If threads__remove() (or threads__remove_all_threads()) runs in the
window between up_read(&table->lock) and down_write(&table->lock), it
clears table->last_match and deletes the thread from table->shard, only
for threads__find() to overwrite table->last_match with the removed
thread. Subsequent lookups for that tid then return the removed thread
from table->last_match, causing stale event attribution and keeping a
cached reference until another lookup evicts it. In addition,
threads__find() unnecessarily took down_write(&table->lock) to re-set
table->last_match even when last_match already hit.
Only update table->last_match on a cache miss, and recheck under
down_write(&table->lock) that tid still maps to the same struct thread
in table->shard before updating table->last_match.
Fixes: d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/threads.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c
index 5db81b9..736058e 100644
--- a/tools/perf/util/threads.c
+++ b/tools/perf/util/threads.c
@@ -85,10 +85,13 @@ static void __threads_table_entry__set_last_match(struct threads_table_entry *ta
}
static void threads_table_entry__set_last_match(struct threads_table_entry *table,
- struct thread *th)
+ pid_t tid, struct thread *th)
{
+ struct thread *cur;
+
down_write(&table->lock);
- __threads_table_entry__set_last_match(table, th);
+ if (hashmap__find(&table->shard, tid, &cur) && RC_CHK_EQUAL(cur, th))
+ __threads_table_entry__set_last_match(table, th);
up_write(&table->lock);
}
@@ -96,16 +99,19 @@ struct thread *threads__find(struct threads *threads, pid_t tid)
{
struct threads_table_entry *table = threads__table(threads, tid);
struct thread *res;
+ bool update_last_match = false;
down_read(&table->lock);
res = __threads_table_entry__get_last_match(table, tid);
if (!res) {
- if (hashmap__find(&table->shard, tid, &res))
+ if (hashmap__find(&table->shard, tid, &res)) {
res = thread__get(res);
+ update_last_match = true;
+ }
}
up_read(&table->lock);
- if (res)
- threads_table_entry__set_last_match(table, res);
+ if (update_last_match)
+ threads_table_entry__set_last_match(table, tid, res);
return res;
}
--
2.56.1.703.gf848ed807c-goog
next prev parent reply other threads:[~2026-11-18 7:55 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 7:55 [PATCH v1 0/4] perf tools: Fix thread cache race, stat per-pkg delta bug, and memory leaks Ian Rogers
2026-11-18 7:55 ` Ian Rogers [this message]
2026-11-18 8:03 ` [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match sashiko-bot
2026-11-18 7:55 ` [PATCH v1 2/4] perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs Ian Rogers
2026-11-18 8:22 ` sashiko-bot
2026-11-18 7:55 ` [PATCH v1 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric() Ian Rogers
2026-11-18 7:58 ` sashiko-bot
2026-11-18 7:55 ` [PATCH v1 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias() Ian Rogers
2026-11-18 7:59 ` 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=20261118075516.1278903-2-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.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 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.