perf tools: Fix thread cache race, stat per-pkg delta bug, and memory leaks
Ian Rogers
reviewercover perf tools: Fix thread cache race, stat per-pkg delta bug, and memory leaks
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
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Ian Rogers
reviewerPATCH 1/4 perf threads: Don't resurrect removed threads in last_match
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.
--- tools/perf/util/threads.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
tools/perf/util/threads.c +11 -5
@@ -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; }
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewRe: [PATCH v1 1/4] perf threads: Don't resurrect removed threads in last_match
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
threads__find()looks up the thread underdown_read(&table->lock), drops the read lock, and then acquiresdown_write(&table->lock)insidethreads_table_entry__set_last_match()to updatetable->last_match.If
threads__remove()(orthreads__remove_all_threads()) runs in the window betweenup_read(&table->lock)anddown_write(&table->lock), it clearstable->last_matchand deletes the thread fromtable->shard, only forthreads__find()to overwritetable->last_matchwith the removed thread. Subsequent lookups for that tid then return the removed thread fromtable->last_match, causing stale event attribution and keeping a cached reference until another lookup evicts it. In addition,threads__find()unnecessarily tookdown_write(&table->lock)to re-settable->last_matcheven whenlast_matchalready hit.
Quoted 6 lines
Only update
table->last_matchon a cache miss, and recheck underdown_write(&table->lock)that tid still maps to the same struct thread intable->shardbefore updatingtable->last_match.Fixes d436f90a64f3 ("perf machine: Move machine's threads into its own abstraction")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Ian Rogers
reviewerPATCH 2/4 perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs
In reply to Ian RogersIn 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.
--- tools/perf/util/stat.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
tools/perf/util/stat.c +5 -5
@@ -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;
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewRe: [PATCH v1 2/4] perf stat: Don't corrupt zero or prev_raw_counts on skipped per-pkg CPUs
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 --
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 <[email protected]>
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.
Quoted 17 lines
tools/perf/util/stat.c +1
-1@@ -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 ; }
tools/perf/util/stat.c process_counter_values(struct perf_stat_config *config, struct evsel *evsel, · line 414
Quoted 10 lines
tools/perf/util/stat.c +4
-4-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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Ian Rogers
reviewerPATCH 3/4 perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric()
In reply to Ian RogersIn 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().
--- tools/perf/util/metricgroup.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
tools/perf/util/metricgroup.c +13 -6
@@ -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,
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewRe: [PATCH v1 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric()
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
In
resolve_metric(), growing the pending array assigns the result ofrealloc()directly back to pending; ifrealloc()fails after earlier iterations succeeded, the existing pending allocation is overwritten with NULL and leaked.Similarly, in
__add_metric(), assigningrealloc()directly toroot_metric->metric_refsoverwrites the existing pointer with NULL on failure, preventingmetric__free(root_metric)in the root caller from freeing the priormetric_refsarray.
Quoted 7 lines
Use temporary pointers for both
realloc()calls, freeing pending before returning-ENOMEMinresolve_metric()and preservingroot_metric->metric_refsformetric__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 <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Ian Rogers
reviewerPATCH 4/4 perf pmu: Free alias on error paths in perf_pmu__new_alias()
In reply to Ian RogersIn 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.
--- tools/perf/util/pmu.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/pmu.c +4 -1
@@ -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' )
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewRe: [PATCH v1 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias()
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
In
perf_pmu__new_alias(), alias is allocated withzalloc(), but ifparse_double()fails on any of the retirement latency fields orio__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 existingperf_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 <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4