* [PATCH v1 0/4] perf tools: Fix thread cache race, stat per-pkg delta bug, and memory leaks
@ 2026-11-18 7:55 Ian Rogers
2026-11-18 7:55 ` [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match Ian Rogers
` (3 more replies)
0 siblings, 4 replies; 5+ 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
Four standalone bug fixes for pre-existing issues in perf tools uncovered
during Sashiko review:
1. util/threads.c: Fix a race between threads__find() and
threads__remove() where dropping table->lock between lookup and
updating table->last_match could resurrect a removed thread in
last_match (and avoid taking down_write(&table->lock) on last_match
cache hits).
2. util/stat.c: Fix process_counter_values() passing a static zero
struct into evsel__compute_deltas() when skipping per-pkg CPUs, which
underflowed and corrupted the static struct in place and clobbered
prev_raw_counts when a previously active CPU was later skipped.
3. util/metricgroup.c: Fix realloc() failure memory leaks of pending in
resolve_metric() and root_metric->metric_refs in __add_metric().
4. util/pmu.c: Free alias on parse_double() and io__getline() error
returns in perf_pmu__new_alias().
AI Assistance Disclosure (per Documentation/process/coding-assistants.rst
and Documentation/process/generated-content.rst):
- Tools used: Sashiko automated patch review (sashiko.dev) for bug
detection, and Google Antigravity / Jetski coding assistant for root
cause verification and patch drafting.
- Summary of prompts and assistance: Verified four pre-existing issues
flagged by Sashiko during review of the perf generic hashmap series and
prepared standalone fixes with Fixes tags against perf-tools-next.
- Testing performed: Built on perf-tools-next (b7ff10b9ddec) with GCC,
Clang (BUILD_NONDISTRO=1), and DEBUG=1 (REFCNT_CHECKING=1); ran perf
test (Sysfs PMU, PMU JSON event, Symbols, and perf stat suites) and
scripts/checkpatch.pl.
Ian Rogers (4):
perf threads: Don't resurrect removed threads in last_match
perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg
CPUs
perf metricgroup: Fix realloc() leaks in resolve_metric() and
__add_metric()
perf pmu: Free alias on error paths in perf_pmu__new_alias()
tools/perf/util/metricgroup.c | 19 +++++++++++++------
tools/perf/util/pmu.c | 5 ++++-
tools/perf/util/stat.c | 10 +++++-----
tools/perf/util/threads.c | 16 +++++++++++-----
4 files changed, 33 insertions(+), 17 deletions(-)
base-commit: b7ff10b9ddec8da75304c40fe4abf1ee4f9f0a43
--
2.56.1.703.gf848ed807c-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* [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 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, 0 replies; 5+ 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 [flat|nested] 5+ 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 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, 0 replies; 5+ 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 [flat|nested] 5+ 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:55 ` [PATCH v1 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias() Ian Rogers
3 siblings, 0 replies; 5+ 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 [flat|nested] 5+ 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
3 siblings, 0 replies; 5+ 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 [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-18 7:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®