From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796078622; b=YWIOH6/1dfSCDTCtxzSA4MjfPV8sn2rIGSy41ZTlnASx00R5uhcBHkk+PDPk7GethOBK jL43arJb8yZA1JPWCVmkwmA9/dJvBdV3tJoaOfxGz7yGjYHErK0PWSj3rnx5SVmRmf16r zkMLW/IrM0WdFSff3o7ynn8W72lI23/o9+fRb15w+Y9mmw2aB3uNC5YVHF2N+wrzwmrsE K51lPuEqtAhOFM043eYBhlig5OZCFNtL+8ykeiGnpBXbYmrOImxDv4kKbzHQv8dC8rzgx BMLrdmKTdOdLd3q1ZiRr1otmV5uXa5LIP8VuPVs1T1WtVbqdTWigCneMmjcHNGihglg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796078622; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=i/C/AHoe0DnO4qrXm/P/OYE39CRJwLMccuwLr8KjW4s=; b=bHUiyXJ3ziQyirVg4BBQHvoX8H/989qCYpoTjEqx4ECFGTNkzPzO3bYbxUwgEhLPg+B7 6VTmKMi9cIBqaT4YWBOElZljKW6f29AU5KwQqwgpYub5j+aPA6szPoOVYAUBhhYATS6tC fyII0Hu4cZkGtWc7zca6i2HhjMYU7u/liivtGyYB6KAh06pM0YQxCb6Ej91evXrscGkiD zDIk2oGs+Y0imY5h2VE0MvrvaxhPv+VJ9hhCSBneCOsaHokzUjJU+CHyWNaHZzgvVImIh vThRQ0QTAX6+3oaJK5R7c0g+Ap5WVhZsU+pJKn8PQIsdXcwGUb69mrYQXY2LDBbvLXA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pQZl951o; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7914A606A335 for ; Mon, 30 Nov 2026 22:43:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7914A606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pQZl951o Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-16bc6f3fdb1so7710949c88.0 for ; Mon, 30 Nov 2026 14:43:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796078621; x=1796683421; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=i/C/AHoe0DnO4qrXm/P/OYE39CRJwLMccuwLr8KjW4s=; b=pQZl951o86zFw+/TCubh+YzrdRFe4qJq4FgtIiov9NWdnb7HrlS7xzsoUz4Mm9+veR B0FSvbWIUEJfAMGJdoKjsM4MFgLjCO59UM0Z7ExWXNRsV34LCmMRF/VAuz7fWq5x1NbH Kb6D9KLU2+gAt7iFbHHUCUze15Guhd5mJsCb9S7wi8lRTmZY02rTPtAyEcLsnPg9vZa/ /NZ9euC8FUtqBD9U9/qPp/Hd1t/CKQrExRa976gX501Cj3HUwKI+oFw7IokxSctkhTRH Um7to+S1i7ELREDRtM6OMEMcX1yuuyt6AdEChnW5U7NgDLLDXdF0p4egQ/u046eNPW8B h1KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796078621; x=1796683421; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=i/C/AHoe0DnO4qrXm/P/OYE39CRJwLMccuwLr8KjW4s=; b=QrLZ16b90nriwQzBDcwQAtDmoaE9RK/5Ma6hs2TfNIUVywpDayKiECiWnvFHA+QluZ 9XY19t/cuvHEurNVqmkflMg25faqBV+375v6F9oT1NwKJVEz20KasWaP8YDKfG5+mh06 pC6AbF3cOs3dPoBWcm0+wS3K3498yImBOlgdKJpRnyUAgMgUdki9Y0Kh57GkFrRyyoBr WpWHj3TJilvKNyVzdaXFtrG2owaJoS9edgwDXZI4VmuZQ1tsKrFC3YxB3cv+pxjJF6Q2 dAWinkz2un/Fdj+63yNaXQLIvaDBYmi8E3P7kig3Wsw6Xv2ayl0QOXfVDPfaPU51O1Vw NPNg== X-Forwarded-Encrypted: i=1; AL+hErHd1y8wUq8iMjYq7eTGQy7G46g22MwCQUnaV/m0Xf3nGmfrRsBsjN9tetThJVFzuXELUErzh2I+ywzTlys=@vger.kernel.org X-Gm-Message-State: APuU4Z9mLv5ilLpMOeSXod16sGEoRmFM84/ZmI+RFBKe5hySFXtCxADF d0y4gaj2mgTxY6C8rtxDJdFIF8fByEnH+gSGJlq0z7UQb/5x0O6Ji3I+KTe2hvMketqWBE8rG6l a4RBs5j14BQ== X-Received: from dleb3-n2.prod.google.com ([2002:a05:701b:4243:20b0:189:931c:18ef]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:1587:b0:1b5:a243:198a with SMTP id a92af1059eb24-1b5a24319bamr11963132c88.18.1796078621195; Mon, 30 Nov 2026 14:43:41 -0800 (PST) Date: Mon, 30 Nov 2026 14:43:30 -0800 In-Reply-To: <20261130224332.79575-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261130224332.79575-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130224332.79575-3-irogers@google.com> Subject: [PATCH v2 2/4] perf stat: Skip per-pkg secondary CPUs after computing deltas From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Jiri Olsa , Adrian Hunter , James Clark , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 (), 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 --- 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