Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report
Date: Mon, 23 Nov 2026 07:35:18 +0000	[thread overview]
Message-ID: <sashiko-outbox-228821@kernel.org> (raw)
In-Reply-To: <20261123071913.653315-6-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf test: false-positive grep match on error output in
  stat.sh
- [Low] perf stat: NULL counter->counts dereference in
  perf_event__process_stat_event()
--

commit d4e30cf2ee96dddd1f10329db7c3af1b94429be0
Author: Ian Rogers <irogers@google.com>

perf stat: Preserve per-evsel CPU maps across stat record and report

This commit serializes and restores per-evsel CPU maps in
PERF_EVENT_UPDATE__CPUS events so that per-evsel CPU restrictions are
preserved across perf stat record and report, with a fallback to the "any
CPU" map for older perf.data files.

> diff --git a/tools/perf/tests/shell/stat.sh b/tools/perf/tests/shell/stat.sh
> index ac9e031831dac..43a158ee36098 100755
> --- a/tools/perf/tests/shell/stat.sh
> +++ b/tools/perf/tests/shell/stat.sh
> @@ -77,6 +77,14 @@ test_stat_record_report() {
>      err=1
>      return
>    fi
> +  cpu=$(sed 's/[-,].*//' /sys/devices/system/cpu/online)
> +  if ! perf stat record -e "task-clock/cpu=${cpu}/,task-clock" -o - true | \
> +    perf stat report -i - 2>&1 | grep -E -q "task-clock/cpu=${cpu}/"
> +  then
> +    echo "stat record and report test [Failed per-evsel CPU]"
> +    err=1
> +    return
> +  fi
>    echo "stat record and report test [Success]"
>  }
>  
> @@ -89,6 +97,14 @@ test_stat_record_script() {
>      err=1
>      return
>    fi
> +  cpu=$(sed 's/[-,].*//' /sys/devices/system/cpu/online)
> +  if ! perf stat record -e "task-clock/cpu=${cpu}/,task-clock" -o - true | \
> +    perf script -i - 2>&1 | grep -E -q "task-clock/cpu=${cpu}/"
> +  then
> +    echo "stat record and script test [Failed per-evsel CPU]"
> +    err=1
> +    return
> +  fi
>    echo "stat record and script test [Success]"
>  }

[Severity: Medium]
Do these two pipelines pass even when perf stat report or perf script fails
with the bug this commit fixes?

When perf_event__process_stat_event() rejects the CPU, it prints "Invalid
CPU 0 for event task-clock/cpu=0/." to stderr.  Because stat.sh does not
enable pipefail and 2>&1 merges stderr into the pipe, grep matches
"task-clock/cpu=${cpu}/" inside the error message itself and returns 0.

> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 9eec6deee190f..e9af99b8c0f8d 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -5962,6 +5962,11 @@ int perf_event__process_event_update(const struct perf_tool *tool __maybe_unused
>  		if (map) {
>  			perf_cpu_map__put(evsel->core.pmu_cpus);
>  			evsel->core.pmu_cpus = map;
> +			if (!evsel->counts) {
> +				perf_cpu_map__put(evsel->core.cpus);
> +				evsel->core.cpus = perf_cpu_map__get(map);
> +				evsel->core.requires_cpu = !perf_cpu_map__has_any_cpu(map);
> +			}
>  		} else
>  			pr_err("failed to get event_update cpus\n");
>  		break;
> diff --git a/tools/perf/util/stat.c b/tools/perf/util/stat.c
> index 25f31a1743682..0b139b93c5565 100644
> --- a/tools/perf/util/stat.c
> +++ b/tools/perf/util/stat.c
[ ... ]
> @@ -670,6 +671,23 @@ int perf_event__process_stat_event(const struct perf_tool *tool __maybe_unused,
>  		return -EINVAL;
>  	}
>  	cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu});
> +	if (cpu_map_idx == -1 && (s32)st->cpu == -1 && counter->counts &&
> +	    evsel__nr_cpus(counter) > 0) {
> +		/*
> +		 * Older perf tools wrote pmu_cpus instead of cpus in
> +		 * PERF_EVENT_UPDATE__CPUS, leaving per-task counters with a
> +		 * CPU list instead of the "any CPU" (-1) map used by their
> +		 * PERF_RECORD_STAT events.
> +		 */
> +		struct perf_cpu_map *any_cpu = perf_cpu_map__new_any_cpu();
> +
> +		if (any_cpu) {
> +			perf_cpu_map__put(counter->core.cpus);
> +			counter->core.cpus = any_cpu;
> +			counter->core.requires_cpu = false;
> +			cpu_map_idx = 0;
> +		}
> +	}
>  	if (cpu_map_idx == -1) {
>  		pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter));
>  		return -EINVAL;

[Severity: Low]
Can perf_event__process_stat_event() dereference a NULL counter->counts
pointer in perf_counts()?

Previously, evsel->core.cpus remained NULL until both the thread map and
CPU map events were processed and set_maps() allocated counter->counts, so
perf_cpu_map__idx() returned -1 whenever counter->counts was NULL.  Now
that perf_event__process_event_update() populates evsel->core.cpus before
counter->counts is allocated, a PERF_RECORD_STAT event arriving before
set_maps() runs can match a valid cpu_map_idx and reach
perf_counts(counter->counts, cpu_map_idx, st->thread) while counter->counts
is still NULL.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123071913.653315-1-irogers@google.com?part=5

  reply	other threads:[~2026-11-23  7:35 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  7:19 [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes Ian Rogers
2026-11-23  7:19 ` [PATCH v1 01/13] perf off-cpu: Bound callchain length in off_cpu_write() Ian Rogers
2026-11-23  7:19 ` [PATCH v1 02/13] perf off-cpu: Fix swapped PID and TID in direct off_cpu_dump() samples Ian Rogers
2026-11-23  7:19 ` [PATCH v1 03/13] libperf evlist: Recompute CPU map when an evsel goes system wide Ian Rogers
2026-11-23  7:19 ` [PATCH v1 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record Ian Rogers
2026-11-23  7:37   ` sashiko-bot
2026-11-23  7:19 ` [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report Ian Rogers
2026-11-23  7:35   ` sashiko-bot [this message]
2026-11-23  7:19 ` [PATCH v1 06/13] perf pmu: Don't treat tool PMU as a kernel software PMU for event grouping Ian Rogers
2026-11-23  7:19 ` [PATCH v1 07/13] perf evlist: Enable tool PMU events when starting a workload Ian Rogers
2026-11-23  7:19 ` [PATCH v1 08/13] perf stat: Respect the enabled time of exited user_time/system_time events Ian Rogers
2026-11-23  7:19 ` [PATCH v1 09/13] perf stat: Fix --bpf-counters with non-bperf, hybrid, and mixed events Ian Rogers
2026-11-23  7:19 ` [PATCH v1 10/13] perf bpf-filter: Fix bpf_link leak by using PERF_EVENT_IOC_SET_BPF Ian Rogers
2026-11-23  7:19 ` [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close() Ian Rogers
2026-11-23  7:36   ` sashiko-bot
2026-11-23  7:19 ` [PATCH v1 12/13] perf bpf-filter: Support BPF sample filters on tracepoint events Ian Rogers
2026-11-23  7:19 ` [PATCH v1 13/13] perf parse-events: Route non-field tracepoint filter terms to BPF filter Ian Rogers
2026-11-23  7:44 ` [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes sashiko-bot

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=sashiko-outbox-228821@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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