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 v3 03/11] perf pmu-layer: Introduce stateless struct pmu_layer abstraction and builtin adapter
Date: Fri, 04 Dec 2026 01:35:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-248897@kernel.org> (raw)
In-Reply-To: <20261204011029.1946921-4-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] perf pmu-layer: NULL hashmap dereference in public
  pmu_layer__*() APIs
- [Medium] perf pmu-layer: pmu_layer__find_event() returns unresolved
  aliases
- [Medium] perf pmu-layer: pmu_layer__find_metric() returns partial
  metrics on OOM
- [Medium] perf pmu-layer: unfiltered sys table decompression on lookup
  misses
- [Medium] perf pmu-layer: redundant sys scan in
  builtin_cpu_load_metricgroup()
--

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

perf pmu-layer: Introduce stateless struct pmu_layer abstraction and builtin adapter

This commit introduces a stateless pmu_layer abstraction in
tools/perf/util/pmu-layer.[ch] for resolving and enumerating PMU events,
metrics, and metricgroup descriptions into struct perf_pmu, along with
builtin adapters for compiled-in CPU and common event and metric tables.

> diff --git a/tools/perf/util/pmu-layer.c b/tools/perf/util/pmu-layer.c
> new file mode 100644
> index 0000000000000..8552291f899a8
> --- /dev/null
> +++ b/tools/perf/util/pmu-layer.c
> @@ -0,0 +1,1176 @@

[ ... ]

> +static int builtin_cpu_find_event(const struct pmu_layer *layer __maybe_unused,
> +				  struct perf_pmu *pmu, const char *name,
> +				  struct perf_pmu_alias *alias)
> +{

[ ... ]

> +	if (!found && pmu->id) {
> +		ret = pmu_for_each_sys_event(builtin_find_sys_event_cb, &ctx);
> +		if (ret < 0)
> +			return ret;
> +	}
> +	return 0;
> +}

[Severity: Medium]
Does falling back to pmu_for_each_sys_event() on a lookup miss decompress
all cross-architecture sys event tables on every probe?

pmu_for_each_sys_event() passes a NULL pmu to
pmu_events_table__for_each_event(), scanning and decompressing every PMU
bucket across all sys tables instead of filtering by PMU and binary
searching.  Because pmu_layer__find_event() does not negatively cache
misses, repeated probes on uncore PMUs with pmu->id set (such as during
parse_events_multi_pmu_add()) repeat this full scan for every unknown or
other-PMU event name.

The same unfiltered scan occurs in builtin_cpu_find_metric() and
builtin_cpu_for_each_metric() via pmu_for_each_sys_metric(), which also
bypasses the PMU-bucket pre-filtering added in
pmu_metrics_table__for_each_pmu_metric().

[ ... ]

> +static int builtin_sys_metricgroup_cb(const struct pmu_metric *pm,
> +				      const struct pmu_metrics_table *table,
> +				      void *vdata)
> +{
> +	struct builtin_sys_mg_ctx *ctx = vdata;
> +	const char *desc;
> +
> +	if (table == ctx->last_table)
> +		return 0;
> +	desc = pmu_metrics_table__describe_metricgroup(table, ctx->group);
> +	if (!desc) {
> +		ctx->last_table = table;
> +		return 0;
> +	}
> +	if (!pm->compat || !pm->pmu)
> +		return 0;
> +	if (!perf_pmu__name_wildcard_match(ctx->pmu, pm->pmu))
> +		return 0;
> +	if (!pmu_uncore_identifier_match(pm->compat, ctx->pmu->id))
> +		return 0;
> +
> +	ctx->desc = desc;
> +	return 1;
> +}

[Severity: Medium]
Can builtin_cpu_load_metricgroup() avoid decompressing all sys metrics and
repeating metricgroup and regex lookups for every entry?

Setting ctx->last_table when pmu_metrics_table__describe_metricgroup()
returns NULL does not stop pmu_for_each_sys_metric() from decompressing
every remaining metric in that table just to invoke the callback.  When a
table does describe ctx->group, ctx->last_table is not updated, so every
non-matching metric in that table repeats the binary search in
pmu_metrics_table__describe_metricgroup() and invokes regcomp()/regexec()
in pmu_uncore_identifier_match().

During perf list metricgroups on systems with uncore PMUs that expose a
sysfs identifier, checking pmu_metrics_table__describe_metricgroup() once
per sys table before iterating PMU-filtered metrics would avoid this
decompression and regex overhead.

[ ... ]

> +struct perf_pmu_alias *pmu_layer__find_event(struct perf_pmu *pmu,
> +					     const char *name)
> +{
> +	struct perf_pmu_alias *alias = NULL, *old_alias = NULL;
> +
> +	if (!pmu || !name || !name[0] || name[0] == '.' ||
> +	    pmu_event_file_kind(name, NULL) != PMU_EVENT_FILE_BASE)
> +		return NULL;
> +
> +	if (hashmap__find(pmu->aliases, name, &alias))
> +		return alias->terms ? alias : NULL;

[Severity: Medium]
Can this dereference a NULL pmu->aliases hashmap?

hashmap__find() unconditionally dereferences map->hash_fn, and
perf_pmus__fake_pmu() returns a static struct perf_pmu whose aliases,
metrics, and metricgroups pointers are all NULL.  In
tools/perf/util/parse-events.c, parse_events_multi_pmu_add_or_add_pmu()
passes perf_pmus__fake_pmu() into parse_events_add_pmu(), which reaches
fix_raw() -> perf_pmu__have_event() before perf_pmu__check_alias() checks
perf_pmu__is_fake(), causing a NULL pointer dereference once alias lookups
go through pmu_layer__find_event().

The same missing NULL check on pmu->aliases, pmu->metrics, or
pmu->metricgroups occurs in pmu_layer__load_events(),
pmu_layer__merge_event_table(), pmu_layer__find_metric(),
pmu_layer__load_metrics(), and pmu_layer__describe_metricgroup().

[ ... ]

> +	for (size_t i = 0; i < nr_pmu_layers; i++) {
> +		const struct pmu_layer *layer = &pmu_layers[i];
> +
> +		if (!layer->ops->for_each_event)
> +			continue;
> +		ret = layer->ops->for_each_event(layer, pmu);
> +		if (ret) {
> +			pmu_layer_finalize_aliases(pmu, /*promote_legacy=*/false);
> +			return ret;
> +		}
> +	}

[ ... ]

> +int pmu_layer__merge_event_table(struct perf_pmu *pmu,
> +				 const struct pmu_events_table *table)
> +{
> +	struct builtin_iter_event_ctx ctx = {
> +		.pmu = pmu,
> +		.is_common = false,
> +		.override_existing = true,
> +	};
> +	int ret;
> +
> +	if (!pmu || !table)
> +		return 0;
> +
> +	ret = pmu_events_table__for_each_event(table, pmu,
> +					       builtin_for_each_event_cb, &ctx);
> +	pmu_layer_finalize_aliases(pmu, /*promote_legacy=*/false);
> +	return ret;
> +}

[Severity: Medium]
Can pmu_layer__find_event() return partially populated or unresolved
aliases left behind by these two paths?

In pmu_layer_merge_pe(), alias->terms is duplicated before desc, unit,
scale, and retirement latency fields.  If a later allocation or lower layer
fails during pmu_layer__load_events(), pmu_layer_finalize_aliases(pmu,
false) only removes entries with a NULL alias->terms, leaving truncated or
unresolved aliases cached in pmu->aliases with alias->resolved still false.
pmu_layer__merge_event_table() similarly leaves merged entries unresolved
without lower-layer fallback fields.

Because pmu_layer__find_event() checks only alias->terms and ignores
alias->resolved, subsequent lookups return these incomplete entries as
valid, yielding wrong counter scaling, units, or event metadata.

[ ... ]

> +	if (hashmap__find(pmu->metrics, name, &metric))
> +		return metric->metric_expr ? metric : NULL;

[ ... ]

> +	for (size_t i = 0; i < nr_pmu_layers; i++) {
> +		const struct pmu_layer *layer = &pmu_layers[i];
> +
> +		if (!layer->ops->for_each_metric)
> +			continue;
> +		ret = layer->ops->for_each_metric(layer, pmu);
> +		if (ret) {
> +			pmu_layer_prune_incomplete_metrics(pmu);
> +			return ret;
> +		}
> +	}

[Severity: Medium]
Can a mid-enumeration allocation failure in pmu_layer__load_metrics() leave
truncated metrics cached for pmu_layer__find_metric()?

pmu_layer_merge_pm() duplicates metric->metric_expr before allocating
metric_threshold, unit, and other fields or setting aggr_mode and
event_grouping.  If a later strdup() or lower layer fails with -ENOMEM,
pmu_layer_prune_incomplete_metrics() only deletes entries with a NULL
metric_expr, so partially populated metrics remain in pmu->metrics and
pmu_layer__find_metric() returns them with missing scaling, thresholds, or
grouping constraints.

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

  reply	other threads:[~2026-12-04  1:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  1:10 [PATCH v3 00/11] perf pmu: Layered sysfs-style PMU event and metric directories Ian Rogers
2026-12-04  1:10 ` [PATCH v3 01/11] perf pmu: Generalize pmu_alias_info_file() and lazy single-event sysfs loading Ian Rogers
2026-12-04  1:32   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 02/11] perf pmu: Add metric and metricgroup storage on struct perf_pmu Ian Rogers
2026-12-04  1:26   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 03/11] perf pmu-layer: Introduce stateless struct pmu_layer abstraction and builtin adapter Ian Rogers
2026-12-04  1:35   ` sashiko-bot [this message]
2026-12-04  1:10 ` [PATCH v3 04/11] perf pmu-layer: Add openat-based directory layer with literal CPUID/symlink lookup Ian Rogers
2026-12-04  1:35   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 05/11] perf config: Add pmu-events.dir configuration option Ian Rogers
2026-12-04  1:27   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 06/11] perf pmu: Switch alias resolution and enumeration to layered PMU stack Ian Rogers
2026-12-04  1:38   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 07/11] perf metricgroup: Route metric and metricgroup lookups through struct perf_pmu Ian Rogers
2026-12-04  1:40   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 08/11] perf list, jevents: Add --export-dir and --output-dir sysfs-style exporters Ian Rogers
2026-12-04  1:36   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 09/11] perf jevents: Add compiled-in model and sys table iterators Ian Rogers
2026-12-04  1:10 ` [PATCH v3 10/11] perf list: Add --export-cpuid to export compiled-in CPU models Ian Rogers
2026-12-04  1:44   ` sashiko-bot
2026-12-04  1:10 ` [PATCH v3 11/11] perf tests: Add round-trip export, CPUID symlink, and pmu-events.dir overlay tests Ian Rogers
2026-12-04  1:49   ` sashiko-bot
2026-12-04  1:49 ` [PATCH v3 00/11] perf pmu: Layered sysfs-style PMU event and metric directories 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-248897@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