mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>,
	Ian Rogers <irogers@google.com>,
	 linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/4] perf stat: Skip per-pkg secondary CPUs after computing deltas
Date: Mon, 30 Nov 2026 14:43:30 -0800	[thread overview]
Message-ID: <20261130224332.79575-3-irogers@google.com> (raw)
In-Reply-To: <20261130224332.79575-1-irogers@google.com>

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() and falling through into ps->aggr aggregation.

This caused multiple bugs:
1. 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.
2. In non-global aggregation modes (such as --per-socket or --per-die),
   falling through with zero (ena == 0 and run == 0) into ps->aggr makes
   evsel__count_has_error() return true for non-cgroup events, wiping out
   the package count accumulated from the first CPU, marking ps_aggr as
   failed (<not counted>), and inflating ps_aggr->nr with skipped CPUs.
3. Calling check_per_pkg() before evsel__compute_deltas() checked
   cumulative rather than per-interval ena/run, so in interval mode a CPU
   that ran in an earlier interval could mark the package used even when
   idle during the current interval.

Remove the static zero struct, compute interval deltas on the real CPU
counter values before calling check_per_pkg(), and return 0 immediately
when skip is true.

Fixes: 779d0b997e07 ("perf stat: Add support for per-pkg counters")
Fixes: f976bc6b6bfc ("perf stat: Aggregate events using evsel->stats->aggr")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/stat.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
index 6da3dbbde0e2..2f3dce698d1f 100644
--- a/tools/perf/util/stat.c
+++ b/tools/perf/util/stat.c
@@ -398,19 +398,18 @@ 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;
 	bool skip = false;
 
+	if (!evsel->snapshot)
+		evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
+
 	if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) {
 		pr_err("failed to read per-pkg counter\n");
 		return -1;
 	}
-
 	if (skip)
-		count = &zero;
+		return 0;
 
-	if (!evsel->snapshot)
-		evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
 	perf_counts_values__scale(count, config->scale, NULL);
 
 	if (config->aggr_mode == AGGR_THREAD) {
-- 
2.56.1.839.g22a8290db9-goog


  parent reply	other threads:[~2026-11-30 22:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:43 [PATCH v2 0/4] perf tools: Fix thread cache race, stat per-pkg delta bug, and memory leaks Ian Rogers
2026-11-30 22:43 ` [PATCH v2 1/4] perf threads: Don't resurrect removed threads in last_match Ian Rogers
2026-11-30 22:43 ` Ian Rogers [this message]
2026-11-30 22:43 ` [PATCH v2 3/4] perf metricgroup: Fix realloc() leaks in resolve_metric() and __add_metric() Ian Rogers
2026-11-30 22:43 ` [PATCH v2 4/4] perf pmu: Free alias on error paths in perf_pmu__new_alias() Ian Rogers

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=20261130224332.79575-3-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 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®