* [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match
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
2026-11-18 8:03 ` 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
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Ian Rogers @ 2026-11-18 7:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark,
linux-perf-users, linux-kernel
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match
2026-11-18 7:55 ` [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match Ian Rogers
@ 2026-11-18 8:03 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-18 8:03 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users
> 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>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118075516.1278903-1-irogers@google.com?part=1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v1 2/4] perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs
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 ` [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match Ian Rogers
@ 2026-11-18 7:55 ` 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:55 ` [PATCH v1 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias() Ian Rogers
3 siblings, 1 reply; 9+ messages in thread
From: Ian Rogers @ 2026-11-18 7:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark,
linux-perf-users, linux-kernel
In process_counter_values(), when check_per_pkg() sets skip = true for a
secondary CPU in an already-counted package/die, count was pointed at a
local 'static struct perf_counts_values zero' before calling
evsel__compute_deltas().
evsel__compute_deltas() saves *count into evsel->prev_raw_counts and
subtracts the previous interval's raw counts from *count in place. If a
CPU had non-zero counts in an earlier interval and is later skipped,
subtracting tmp from zero underflows the static zero struct in place
(corrupting all subsequent skipped CPUs for the remainder of the perf
stat run) and overwrites prev_raw_counts for that CPU with zeros.
Make zero a zero-initialized stack variable, compute interval deltas on
the real CPU counter values first so prev_raw_counts stays accurate, and
only substitute &zero afterwards when skipping aggregation.
Fixes: 779d0b997e07 ("perf stat: Add support for per-pkg counters")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/stat.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
index 6da3dbb..267e280 100644
--- a/tools/perf/util/stat.c
+++ b/tools/perf/util/stat.c
@@ -398,7 +398,7 @@ process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
struct perf_counts_values *count)
{
struct perf_stat_evsel *ps = evsel->stats;
- static struct perf_counts_values zero;
+ struct perf_counts_values zero = {};
bool skip = false;
if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) {
@@ -406,12 +406,12 @@ process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
return -1;
}
- if (skip)
- count = &zero;
-
if (!evsel->snapshot)
evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
- perf_counts_values__scale(count, config->scale, NULL);
+ if (skip)
+ count = &zero;
+ else
+ perf_counts_values__scale(count, config->scale, NULL);
if (config->aggr_mode == AGGR_THREAD) {
struct perf_counts_values *aggr_counts = &ps->aggr[thread].counts;
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v1 2/4] perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-18 8:22 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf stat: zero count on skipped CPUs in
process_counter_values() fails aggr
--
commit 90a7348e9c069c01e179e728774f2e1d53d53fb3
Author: Ian Rogers <irogers@google.com>
perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs
This commit replaces the static zero perf_counts_values structure in
process_counter_values() with a stack variable and computes interval deltas
before substituting zero for skipped per-package CPUs.
> diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
> index 6da3dbbde0e2a..267e280c5aa23 100644
> --- a/tools/perf/util/stat.c
> +++ b/tools/perf/util/stat.c
> @@ -398,7 +398,7 @@ process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
> struct perf_counts_values *count)
> {
> struct perf_stat_evsel *ps = evsel->stats;
> - static struct perf_counts_values zero;
> + struct perf_counts_values zero = {};
> bool skip = false;
>
> if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) {
> @@ -406,12 +406,12 @@ process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
> return -1;
> }
>
> - if (skip)
> - count = &zero;
> -
> if (!evsel->snapshot)
> evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
> - perf_counts_values__scale(count, config->scale, NULL);
> + if (skip)
> + count = &zero;
> + else
> + perf_counts_values__scale(count, config->scale, NULL);
[Severity: Medium]
Should process_counter_values() return 0 when skip is true instead of
falling through with count = &zero?
In non-global aggregation modes such as AGGR_SOCKET (--per-socket) or
AGGR_DIE (--per-die), a skipped secondary CPU maps to the same ps_aggr entry
as the first CPU in that package or die:
tools/perf/util/stat.c:process_counter_values() {
...
ps_aggr = &ps->aggr[i];
ps_aggr->nr++;
...
if (evsel__count_has_error(evsel, count, config) && !ps_aggr->failed) {
ps_aggr->counts.val = 0;
ps_aggr->counts.ena = 0;
ps_aggr->counts.run = 0;
ps_aggr->failed = true;
}
...
}
Because zero has ena == 0 and run == 0, evsel__count_has_error() returns
true for non-cgroup events, wiping out the package count accumulated from
the primary CPU and marking the bucket failed (<not counted>) while also
inflating ps_aggr->nr. In interval mode, where underflow previously gave
the static zero structure non-zero ena and run after the first interval,
this regression causes every subsequent interval to fail too.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118075516.1278903-1-irogers@google.com?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v1 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric()
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 ` [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match Ian Rogers
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 7:55 ` 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
3 siblings, 1 reply; 9+ messages in thread
From: Ian Rogers @ 2026-11-18 7:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark,
linux-perf-users, linux-kernel
In resolve_metric(), growing the pending array assigns the result of
realloc() directly back to pending; if realloc() fails after earlier
iterations succeeded, the existing pending allocation is overwritten
with NULL and leaked.
Similarly, in __add_metric(), assigning realloc() directly to
root_metric->metric_refs overwrites the existing pointer with NULL on
failure, preventing metric__free(root_metric) in the root caller from
freeing the prior metric_refs array.
Use temporary pointers for both realloc() calls, freeing pending before
returning -ENOMEM in resolve_metric() and preserving
root_metric->metric_refs for metric__free() in __add_metric().
Fixes: 80be6434c36f ("perf metric: Modify resolution and recursion check")
Fixes: 46bdc0bf8d21 ("perf metric: Simplify metric_refs calculation")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/metricgroup.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 779fe56..489f13b 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -771,10 +771,15 @@ static int resolve_metric(struct list_head *metric_list,
if (pmu_metrics_table__find_metric(table, pmu, cur->pkey,
metricgroup__find_metric_callback,
&pm) != PMU_METRICS__NOT_FOUND) {
- pending = realloc(pending,
- (pending_cnt + 1) * sizeof(struct to_resolve));
- if (!pending)
+ struct to_resolve *new_pending;
+
+ new_pending = realloc(pending,
+ (pending_cnt + 1) * sizeof(struct to_resolve));
+ if (!new_pending) {
+ free(pending);
return -ENOMEM;
+ }
+ pending = new_pending;
memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm));
pending[pending_cnt].key = cur->pkey;
@@ -863,6 +868,7 @@ static int __add_metric(struct list_head *metric_list,
return -ENOMEM;
} else {
+ struct metric_ref *new_refs;
int cnt = 0;
/*
@@ -879,10 +885,11 @@ static int __add_metric(struct list_head *metric_list,
}
/* Create reference. Need space for the entry and the terminator. */
- root_metric->metric_refs = realloc(root_metric->metric_refs,
- (cnt + 2) * sizeof(struct metric_ref));
- if (!root_metric->metric_refs)
+ new_refs = realloc(root_metric->metric_refs,
+ (cnt + 2) * sizeof(struct metric_ref));
+ if (!new_refs)
return -ENOMEM;
+ root_metric->metric_refs = new_refs;
/*
* Intentionally passing just const char pointers,
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v1 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric()
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-18 7:58 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users
> In resolve_metric(), growing the pending array assigns the result of
> realloc() directly back to pending; if realloc() fails after earlier
> iterations succeeded, the existing pending allocation is overwritten
> with NULL and leaked.
>
> Similarly, in __add_metric(), assigning realloc() directly to
> root_metric->metric_refs overwrites the existing pointer with NULL on
> failure, preventing metric__free(root_metric) in the root caller from
> freeing the prior metric_refs array.
>
> Use temporary pointers for both realloc() calls, freeing pending before
> returning -ENOMEM in resolve_metric() and preserving
> root_metric->metric_refs for metric__free() in __add_metric().
>
> Fixes: 80be6434c36f ("perf metric: Modify resolution and recursion check")
> Fixes: 46bdc0bf8d21 ("perf metric: Simplify metric_refs calculation")
> Signed-off-by: Ian Rogers <irogers@google.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118075516.1278903-1-irogers@google.com?part=3
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v1 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias()
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
` (2 preceding siblings ...)
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:55 ` Ian Rogers
2026-11-18 7:59 ` sashiko-bot
3 siblings, 1 reply; 9+ messages in thread
From: Ian Rogers @ 2026-11-18 7:55 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark,
linux-perf-users, linux-kernel
In perf_pmu__new_alias(), alias is allocated with zalloc(), but if
parse_double() fails on any of the retirement latency fields or
io__getline() fails when reading sysfs alias terms, the function returns
ret directly without freeing alias.
Call perf_pmu_free_alias(alias) on both error paths, matching the
existing perf_pmu__convert_scale() failure path.
Fixes: 84bae3af20d0 ("perf pmu: Don't eagerly parse event terms")
Fixes: fc807b6bde6a ("perf pmu-events: Add retirement latency to JSON events inside of perf")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/pmu.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 1ae8bcf..adf85d0 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -607,8 +607,10 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
ret = parse_double(pe->retirement_latency_max, NULL,
&alias->retirement_latency_max);
}
- if (ret)
+ if (ret) {
+ perf_pmu_free_alias(alias);
return ret;
+ }
if (val_fd < 0) {
alias->terms = strdup(val);
@@ -621,6 +623,7 @@ static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name,
ret = io__getline(&io, &alias->terms, &line_len) < 0 ? -errno : 0;
if (ret) {
pr_err("Failed to read alias %s\n", name);
+ perf_pmu_free_alias(alias);
return ret;
}
if (line_len >= 1 && alias->terms[line_len - 1] == '\n')
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply related [flat|nested] 9+ messages in thread