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 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record
Date: Mon, 23 Nov 2026 07:37:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-228825@kernel.org> (raw)
In-Reply-To: <20261123071913.653315-5-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf evsel: evlist__next_synthetic_id() assigns -1ULL
  sample.id sentinel
--

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

perf stat: Give non-perf_event_open events unique IDs for stat record

This commit assigns unique synthetic IDs counting down from ~0ULL to events
that are not opened via perf_event_open(), so that perf stat record and perf
stat report can distinguish them.

> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index d060fca8090f6..0ed598a0ed151 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -4724,15 +4724,51 @@ struct perf_env *evsel__env(struct evsel *evsel)
>  	return session ? perf_session__env(session) : NULL;
>  }
>  
> +/*
> + * Get an ID for an event not opened with perf_event_open, such as a tool PMU
> + * event. Kernel assigned IDs count up from 1, so count down from the largest
> + * ID to keep as far away from them as possible. Reaching the kernel's IDs
> + * would need ~2^63 perf events to have been created, but also skip any ID
> + * already in use by the evlist.
> + */
> +static u64 evlist__next_synthetic_id(struct evlist *evlist)
> +{
> +	static u64 next_synthetic_id = ~0ULL;
> +	u64 id;
> +
> +	do {
> +		id = next_synthetic_id--;
> +	} while (evlist__id2sid(evlist, id));
> +
> +	return id;
> +}

[Severity: Medium]
Does post-decrementing next_synthetic_id from ~0ULL cause the first synthetic
ID to collide with the -1ULL sentinel used for unset sample.id values?

__evsel__parse_sample() initializes data->id to -1ULL (~0ULL) and leaves it
at -1ULL for non-sample events when sample_id_all is not set, such as the
PERF_RECORD_MMAP event synthesized during perf stat record:

tools/perf/util/evsel.c:__evsel__parse_sample() {
	perf_sample__init(data, /*all=*/true);
	data->evsel = evsel__get(evsel);
	data->cpu = data->pid = data->tid = -1;
	data->stream_id = data->id = data->time = -1ULL;
    ...
}

When the first synthetic ID (~0ULL) is assigned to an evsel that is not
first in the evlist (for example, perf stat record -e
task-clock,duration_time true), evlist__id2evsel(evlist, -1ULL) resolves to
that synthetic-ID evsel while sample->evsel on the PERF_RECORD_MMAP event is
evlist__first(evlist).  Running perf stat report then trips the assertion in
machines__deliver_event():

tools/perf/util/session.c:machines__deliver_event() {
    ...
	if (!sample->evsel) {
		sample->evsel = evlist__id2evsel(evlist, sample->id);
		if (sample->evsel)
			sample->evsel = evsel__get(sample->evsel);
	}
	else
		assert(sample->evsel == evlist__id2evsel(evlist, sample->id));
    ...
}

Pre-decrementing next_synthetic_id (or initializing it to ~0ULL - 1) would
avoid assigning ~0ULL as a valid event ID.

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

  reply	other threads:[~2026-11-23  7:37 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 [this message]
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
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-228825@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