From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3 07/11] perf metricgroup: Route metric and metricgroup lookups through struct perf_pmu
Date: Fri, 04 Dec 2026 01:40:38 +0000 [thread overview]
Message-ID: <sashiko-outbox-248907@kernel.org> (raw)
In-Reply-To: <20261204011029.1946921-8-irogers@google.com>
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [Medium] perf metricgroup: metric__same_definition() omits metric
metadata fields
- [Medium] perf pmus: pmus_metric_iter_adapter_cb() omits metric_group
in dedup key
- [Medium] perf metricgroup: user threshold file triggers assert() in
__add_metric()
- [Medium] perf: O(N) uncore PMU fan-out in
perf_pmus__scan_for_metrics()
- [Medium] perf: O(G * N_pmu) metricgroup lookup amplification across
all PMUs
- [Medium] perf: other_pmus re-sorted in place during
perf_pmus__scan_for_metrics()
- [Low] perf metricgroup: repeated failed perf_pmus__find() lookups for
uncore PMUs
- [Low] perf pmu: perf_pmu__is_core_at() probes host sysfs via
is_pmu_core()
- [Low] perf: perf_pmus__describe_metricgroup() drops builtin_desc
without core PMU
--
commit 8ba4237c22785cc46bdf1cdc1a19fe92c9141e4f
Author: Ian Rogers <irogers@google.com>
perf metricgroup: Route metric and metricgroup lookups through struct perf_pmu
This commit routes live metric and metricgroup lookups through struct
perf_pmu and perf_pmus helpers backed by the layered PMU stack instead of
querying compiled-in tables directly. It updates callers across
metricgroup.c, print-events.c, builtin-list.c, builtin-script.c, and
util/python.c while keeping explicit table overrides for unit tests.
> diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
> index 88aaf9cfd132a..384f3abb1eb4d 100644
> --- a/tools/perf/builtin-script.c
> +++ b/tools/perf/builtin-script.c
> @@ -2546,7 +2546,7 @@ static void perf_sample__fprint_metric(struct thread *thread,
> /*needs_sort=*/false);
> }
>
> - metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_metrics, &args);
> + metricgroup__for_each_metric(/*table=*/NULL, script_find_metrics, &args);
> init_metrics = true;
> }
[Severity: Medium]
Can this callback re-sort other_pmus in place while
perf_pmus__scan_for_metrics() is iterating it?
perf_pmus__scan_for_metrics() leaves hwmon and drm PMUs unread and walks
other_pmus with list_for_each_entry_continue(). Inside the callback,
script_find_metrics() calls metricgroup__parse_groups(), which triggers
hwmon or drm PMU discovery when an event name matches a hwmon prefix such
as cpu_cycles:
perf_pmus__for_each_pmu_metric()
perf_pmus__scan_for_metrics() /* cursor on other_pmus */
script_find_metrics()
metricgroup__parse_groups()
...
pmu_read_sysfs(hwmon/drm)
list_sort(NULL, &other_pmus, pmus_cmp) <-- reorders cursor
Because pmu_name_cmp() returns 0 whenever one base PMU name is a prefix of
another (such as uncore_imc_1 and uncore_imc_free_running_0), it is not a
strict weak ordering and re-sorting can move pre-existing PMUs across the
active cursor. That silently skips or revisits PMU metrics during perf
script, and inconsistently visits newly appended hwmon or drm PMUs.
> diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
> index 779fe56fe8d0c..ebe9873d273f4 100644
> --- a/tools/perf/util/metricgroup.c
> +++ b/tools/perf/util/metricgroup.c
[ ... ]
> @@ -281,6 +285,16 @@ static bool contains_metric_id(struct evsel **metric_events, int num_events,
> return false;
> }
>
> +static bool is_core_pmu_name(const char *pmu)
> +{
> + struct perf_pmu *perf_pmu;
> +
> + if (is_pmu_core(pmu))
> + return true;
> + perf_pmu = perf_pmus__find(pmu);
> + return perf_pmu && perf_pmu->is_core;
> +}
[Severity: Low]
Does is_core_pmu_name() trigger repeated failed sysfs lookups for uncore
metrics with suffixless PMU prefixes such as uncore_imc or arm_cmn?
Because perf_pmus__scan_for_metrics() leaves hwmon and drm masks unset in
read_pmu_types, perf_pmus__find() does not hit the all-types fast path on
a miss. Each call re-opens sysfs, allocates a temporary perf_pmu with
three hashmaps, fails to open the type file, and tears it all down again.
metric__needs_per_pmu_evlist(), parse_groups(), and setup_metric_events()
call is_core_pmu_name() multiple times per metric even when m->source_pmu
is already known, and perf_pmus__find_metric_pmu() hits the same failed
lookup before falling back to wildcard matching. Checking
m->source_pmu->is_core when available and scanning core_pmus directly
avoids these failed lookups.
[ ... ]
> +static bool metric__same_definition(const struct metric *a, const struct metric *b)
> +{
> + size_t na, nb;
> +
> + if (strcmp(a->metric_name ?: "", b->metric_name ?: "") ||
> + strcmp(a->modifier ?: "", b->modifier ?: "") ||
> + strcmp(a->metric_expr ?: "", b->metric_expr ?: "") ||
> + strcmp(a->metric_threshold ?: "", b->metric_threshold ?: "") ||
> + a->pctx->sctx.runtime != b->pctx->sctx.runtime)
> + return false;
[Severity: Medium]
Should metric__same_definition() also compare metric_unit, group_events,
default_show_events, and default_metricgroup_name?
On a heterogeneous system or with per-CPUID overlays, two core PMUs can
expose a default_core metric with the same formula and threshold but a
different scale unit, grouping constraint, or default display flag.
Omitting those fields causes metricgroup__dedup_and_pin_metrics() to treat
both entries as uniform and free the second PMU's definition instead of
pinning each one to source_pmu->name.
[ ... ]
> @@ -1589,14 +1840,12 @@ int metricgroup__parse_groups(struct evlist *perf_evlist,
> bool system_wide,
> bool hardware_aware_grouping)
> {
> - const struct pmu_metrics_table *table = pmu_metrics_table__find();
> -
> if (hardware_aware_grouping)
> pr_debug("Use hardware aware grouping instead of traditional metric grouping method\n");
>
> return parse_groups(perf_evlist, pmu, cputype_filter, str, metric_no_group, metric_no_merge,
> metric_no_threshold, user_requested_cpu_list, system_wide,
> - /*fake_pmu=*/false, table);
> + /*fake_pmu=*/false, /*table=*/NULL);
> }
[Severity: Medium]
Can a user-supplied .threshold overlay file now trigger an assertion abort
in __add_metric()?
Routing live metric parsing through the layered PMU stack exposes
__add_metric() to user pmu-events.dir files, where dir_apply_metric_file()
stores .threshold contents without checking that metric_name appears in
the expression:
tools/perf/util/metricgroup.c:__add_metric() {
...
assert(strstr(pm->metric_threshold, pm->metric_name));
expr = metric_no_threshold ? pm->metric_name : pm->metric_threshold;
...
}
A malformed or empty .threshold file aborts debug builds, and in non-debug
builds causes __add_metric() to parse only pm->metric_threshold and drop
pm->metric_expr. Validating the threshold expression or returning an
error instead of asserting would handle invalid user files safely.
> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 3c1c24f1804b9..1b8fcd84d61b4 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
[ ... ]
> @@ -864,6 +911,22 @@ static __u32 wellknown_pmu_type(const char *pmu_name)
> return PERF_TYPE_MAX;
> }
>
> +bool perf_pmu__is_core_at(int dirfd, const char *name)
> +{
> + int fd;
> +
> + if (is_pmu_core(name))
> + return true;
> + if (dirfd < 0)
> + return false;
> +
> + fd = perf_pmu__pathname_fd(dirfd, name, "cpus", O_RDONLY);
> + if (fd < 0)
> + return false;
> + close(fd);
> + return true;
> +}
[Severity: Low]
Should perf_pmu__is_core_at() avoid calling is_pmu_core(name) before
checking dirfd?
is_pmu_core() calls is_sysfs_pmu_core(), which unconditionally probes host
sysfs for <name>/cpus. That bypasses the dirfd < 0 guard and causes
perf_pmus__add_test_pmu() to misclassify a synthetic non-core test PMU as
a core PMU whenever the host has a core PMU with the same name.
It also makes perf_pmu__lookup() probe <name>/cpus twice (access() then
openat()) for every non-core PMU during sysfs discovery. Checking only
the static core names (cpu, cpum_cf, default_core) before probing dirfd
fixes both issues.
> diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c
> index 5c21b28e420dd..d56057df3826e 100644
> --- a/tools/perf/util/pmus.c
> +++ b/tools/perf/util/pmus.c
[ ... ]
> @@ -514,6 +520,31 @@ static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
> return NULL;
> }
>
> +struct perf_pmu *perf_pmus__scan_for_metrics(struct perf_pmu *pmu)
> +{
> + bool use_core_pmus = !pmu || pmu->is_core;
> +
> + if (!pmu) {
> + const unsigned int to_read_pmus =
> + PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
> + PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
> + PERF_TOOL_PMU_TYPE_TOOL_MASK;
> +
> + pmu_read_sysfs(to_read_pmus);
> + pmu = list_prepare_entry(pmu, &core_pmus, list);
> + }
> + if (use_core_pmus) {
> + list_for_each_entry_continue(pmu, &core_pmus, list)
> + return pmu;
> +
> + pmu = NULL;
> + pmu = list_prepare_entry(pmu, &other_pmus, list);
> + }
> + list_for_each_entry_continue(pmu, &other_pmus, list)
> + return pmu;
> + return NULL;
> +}
[Severity: Medium]
Does iterating every uncollapsed uncore PMU instance here cause O(N)
startup latency and memory amplification on multi-socket servers?
Because perf_pmus__for_each_pmu_metric() calls pmu_layer__load_metrics()
on every PMU returned by perf_pmus__scan_for_metrics(), systems with
hundreds of identical uncore instances (such as uncore_cha_0..119)
heap-clone the same metrics into each instance's hashmap and re-run
pmu_for_each_sys_metric() regex matching on every instance with pmu->id.
perf_pmus__for_each_metric() then allocates deduplication keys only to
discard those duplicates.
This also affects metricgroups__topdown_max_level(), which previously
scanned only the compiled-in CPU table in memory and now forces full
uncore sysfs discovery and per-PMU metric loading.
[ ... ]
> +static int pmus_metric_iter_adapter_cb(struct perf_pmu *pmu __maybe_unused,
> + const struct pmu_metric *pm,
> + const struct pmu_metrics_table *table,
> + void *vdata)
> +{
> + struct pmus_metric_iter_adapter_ctx *ctx = vdata;
> + char *key = NULL;
> + int ret;
> +
> + if (asprintf(&key, "%s\x1f%s\x1f%s\x1f%s",
> + (pm->pmu && pm->pmu[0]) ? pm->pmu : "default_core",
> + pm->metric_name ?: "",
> + pm->metric_expr ?: "",
> + pm->metric_threshold ?: "") < 0)
> + return -ENOMEM;
[Severity: Medium]
Can this deduplication key drop distinct metrics that differ in
metric_group or other metadata?
The key only includes pmu, metric_name, metric_expr, and
metric_threshold, omitting metric_group, unit, desc, long_desc, compat,
aggr_mode, event_grouping, and default_show_events. When two PMUs expose
a metric with the same formula and threshold under default_core but
different metric_group or unit metadata, perf_pmus__for_each_metric()
suppresses the second entry, causing perf list and
metricgroups__topdown_max_level() to miss it.
[ ... ]
> +const char *perf_pmus__describe_metricgroup(const char *group)
> +{
> + struct perf_pmu *pmu = NULL;
> + const char *builtin_desc = NULL;
> + bool saw_core_pmu = false;
> +
> + if (!group || !group[0])
> + return NULL;
> +
> + while ((pmu = perf_pmus__scan_for_metrics(pmu)) != NULL) {
> + const char *desc = perf_pmu__describe_metricgroup(pmu, group);
> + struct perf_pmu_metricgroup *mg = NULL;
> +
> + if (pmu->is_core)
> + saw_core_pmu = true;
> + if (!desc)
> + continue;
> + if (pmu->metricgroups)
> + hashmap__find(pmu->metricgroups, group, &mg);
> + if (mg && !mg->from_builtin)
> + return desc;
> + if (!builtin_desc)
> + builtin_desc = desc;
> + }
[Severity: Medium]
Does this loop turn perf list metricgroup description lookups into an
O(G * N_pmu) scan across all uncollapsed PMUs?
When no user overlay layer is configured, mg->from_builtin is always true,
so the loop never stops early on builtin_desc and probes every PMU for
every printed metricgroup. For each (group, pmu) pair,
pmu_layer__describe_metricgroup() allocates and caches a
perf_pmu_metricgroup entry, runs a redundant second hashmap__find() here,
and scans system metric tables with regex matching when pmu->id is set.
Stopping early when no user overlay layer is active would avoid scanning
every uncore PMU for every metricgroup.
> + if (saw_core_pmu)
> + return builtin_desc;
> + return describe_metricgroup(group);
> +}
[Severity: Low]
Does this discard builtin_desc when no core PMU was scanned?
If an uncore or system PMU already populated builtin_desc while
saw_core_pmu is false, falling back to describe_metricgroup(group) drops
that description because describe_metricgroup() does not search system
PMU metric tables. Returning builtin_desc whenever it is non-NULL
preserves the resolved description.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204011029.1946921-1-irogers@google.com?part=7
next prev parent reply other threads:[~2026-12-04 1:40 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
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 [this message]
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-248907@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