From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796348439; b=ebD7BC+4HMwwgZ1ho1/CU/zsblWjBHcRTCH0lPsNIGQaODAqEn0khdZK27Kv1p8hw0f/ 5rHXkPp5BGghlznZasIstkenrQQO+soRbkhJP4DuF7dXuje/cXFkeKJ7Io/R6duFvBlhf t5XDPZQl4ldsIUv88IS/P15Cf7dLzGOYtqFtdTos3dqpknVbYq8oNm17Vdm7Y1VP7HzYG uegB4PLqgIquytrxFNLneeF0OdPo3smoUCSWI0GwCIewUcYPUtI+gBUybPEM64uVDK9K4 ADafIDQDh9aGLGg8LKXwL2qIWYoTbqTNplP8QPTN16HyPgnmiKWzdQH1mUl2mIlLFKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796348439; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=7IvJ57165tw3sFanmmT+/GgtXrIYIrWinwSX++5p3Dw=; b=BQwCOOOfIa7UD8TgNww+KbZRi+J3B1/s1m9Ram0rge+dB/SKbJ2xjgdNfkFgm+uriQto Nq+Zvhg/r9GgxaOVmT07qnoa7GwVt8YhSkUEx9o5+Jmn0Esu+2MxqMDmk3jdgkrxjpRHS u5vjyKtmoECPmu2hjyPs+9NAtuLKOtx8BbisRF1FFNK4oVIiOgpAPpeklgcI0631vf0NV jl2icudy6++MnZ5BmOEs41efaXTXOcTtk37+fwiRUvTCpG5JJ0KNakxSDFC8DBzBAQi6v sb4LoYyPjBNyzo7vR33rKfmumHUHJx6oMq88xzSpFpay+8jhEUau1Ybl40k6xLQEJZw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=P9ktUj91; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B65DE606A322 for ; Fri, 4 Dec 2026 01:40:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=P9ktUj91 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E7511F000CE; Fri, 4 Dec 2026 01:40:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796348439; bh=7IvJ57165tw3sFanmmT+/GgtXrIYIrWinwSX++5p3Dw=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=P9ktUj91/Gt8tJ4OpG9mrHnYUOpYWD+3AuDOKP1RxgbKfmbnBcSF0S525OFP1Cm6b lzQDVhmC/qnYN4zwhGl9Ye3BQOZV9aIOtjL3QNt3Axz4HzAHHINawzH/6j4il0DEz7 3bSDeNQX0B3LFBE1/L5VAPa9v3YgGPmVh4jbOdP1RBxIwQNsmUs1/nJyYKSikh8s+n PJwPFgqb6HZZDimxnAjJ5zD/zwoQS4ggsqC7j1jkDD8UQYd7vHKFU8ddpxhv3X0lT/ KGddtCFcWr+ZBLccUJn2ArmkC9jf9GtKGH9Tkv79CQUV9JuRaenyNQTFfr89rPrejc waIZk2JWtThsQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 07/11] perf metricgroup: Route metric and metricgroup lookups through struct perf_pmu Reply-To: sashiko-reviews@lists.linux.dev To: "Ian Rogers" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261204011029.1946921-8-irogers@google.com> References: <20261204011029.1946921-1-irogers@google.com> <20261204011029.1946921-8-irogers@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 01:40:38 +0000 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 threa= d *thread, > /*needs_sort=3D*/false); > } > =20 > - metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_me= trics, &args); > + metricgroup__for_each_metric(/*table=3D*/NULL, script_find_metrics, &a= rgs); > init_metrics =3D 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; > } > =20 > +static bool is_core_pmu_name(const char *pmu) > +{ > + struct perf_pmu *perf_pmu; > + > + if (is_pmu_core(pmu)) > + return true; > + perf_pmu =3D 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 !=3D 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 =3D pmu_metrics_table__find(); > - > if (hardware_aware_grouping) > pr_debug("Use hardware aware grouping instead of traditional metric gr= ouping method\n"); > =20 > return parse_groups(perf_evlist, pmu, cputype_filter, str, metric_no_gr= oup, metric_no_merge, > metric_no_threshold, user_requested_cpu_list, system_wide, > - /*fake_pmu=3D*/false, table); > + /*fake_pmu=3D*/false, /*table=3D*/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 =3D 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; > } > =20 > +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 =3D 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 /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 /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_duplica= tes(struct perf_pmu *pmu) > return NULL; > } > =20 > +struct perf_pmu *perf_pmus__scan_for_metrics(struct perf_pmu *pmu) > +{ > + bool use_core_pmus =3D !pmu || pmu->is_core; > + > + if (!pmu) { > + const unsigned int to_read_pmus =3D > + 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 =3D list_prepare_entry(pmu, &core_pmus, list); > + } > + if (use_core_pmus) { > + list_for_each_entry_continue(pmu, &core_pmus, list) > + return pmu; > + > + pmu =3D NULL; > + pmu =3D 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_unus= ed, > + const struct pmu_metric *pm, > + const struct pmu_metrics_table *table, > + void *vdata) > +{ > + struct pmus_metric_iter_adapter_ctx *ctx =3D vdata; > + char *key =3D 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 =3D NULL; > + const char *builtin_desc =3D NULL; > + bool saw_core_pmu =3D false; > + > + if (!group || !group[0]) > + return NULL; > + > + while ((pmu =3D perf_pmus__scan_for_metrics(pmu)) !=3D NULL) { > + const char *desc =3D perf_pmu__describe_metricgroup(pmu, group); > + struct perf_pmu_metricgroup *mg =3D NULL; > + > + if (pmu->is_core) > + saw_core_pmu =3D 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 =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204011029.1946= 921-1-irogers@google.com?part=3D7