[PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match
From: Ian Rogers
Date: Wed Nov 18 2026 - 02:55:35 EST
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@xxxxxxxxxx>
---
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