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=1796348139; b=V1cpojNYb7fqeZTnskseNrPux+qOybtsq1b6nQOWKMh9ACFQg1oQv8BOnUWxBqQbTA7/ MEPE633P11jwqwu3+uI157nJ+SnRYyvfJdAnFoMhRmU52KCtjfUAeAPQpSW6LhM5XktsS kPvWIFjxZCTlsqtQcjU7aCJ+lL++F1G47Y5AiXEEkzvmvwrD1EMgdIR5pqCx+QxJI5iJe lfb5Epe1rEDi8a6QdU4OM/3l/Few4MeHn+4sGG46UreqMFVNj7OswjtOiNnPsYC4Xq9Hq wGfToxyi4fYn0Ach3kn0KVgXbOBQ4jequ8henHzOEr4p97ZJT/3DgYxY8pj1//BSaBA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796348139; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=IfEwfVAWeuB0Syx0Y5D2LK1xdOcAPqEzUEnyP0Y3Bxo=; b=cOHPsgUeAq6pX13zp967OxdNZnuhyHUf7MHN3CQL6MQ0lQsa4Zn7eNBP+PltmXza3Vxv 0CsDK4z1ix0pVdEU6pbORmauC18IiornzJI9gJXqvJfB1XvUWIZQuzlLVYJtWC2JYSMz8 FXlBlFffBJIr119tCD2oPuIqJuzTFgYa1pvyiv3Qt/7Txo4JIqftuvDIB2LPMXTSERLXw t5yq7HWfBOYER3YwIYnjZ8QXNMgzOXeW6qGaMz/HekkeoEAh1LY6fNQcnjt9OAxuDJxQk R1EC6GS+4ShukQZowtCE9nPVE3OJp75+VuqFPEXrgefQBXbxTgkFU2x67+t4daMS+Kg== 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=K4zFzHro; 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 3A2DA606A322 for ; Fri, 4 Dec 2026 01:35: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=K4zFzHro Received: by smtp.kernel.org (Postfix) with ESMTPSA id 073331F000C2; Fri, 4 Dec 2026 01:35:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796348139; bh=IfEwfVAWeuB0Syx0Y5D2LK1xdOcAPqEzUEnyP0Y3Bxo=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=K4zFzHrofGBcoTBjLtYNqKS+cyJH+GhKXIZCMPXAJUUp7UGFRcUG/zQL7d42ODrpp rP4rxGAn+7n+xEKIVvjEfBWtOKIWTRB0YOMY9w2nraDWIyxX28ndlVl9z+WuLGLYI0 oVjk1jsLvy5XS3jb/hOY/1ttl0PE1REGc0YlZsq56MeOT0v7kkQ2TqpgAz1/N2MnT0 dGoyuCkyufhwInufZhPcWEiaXthUvuysNMOLU35EeNK1Ky9+lYtXIBQ1C77N8YTzhL uRR9+SUZG9PF+Z56dNu7SOWQEotKtlq1pP18o855J5zdPXQ0sVxv9VzGJejT4MQstN X+ftdEnlXZn1A== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 03/11] perf pmu-layer: Introduce stateless struct pmu_layer abstraction and builtin adapter Reply-To: sashiko-reviews@lists.linux.dev To: "Ian Rogers" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261204011029.1946921-4-irogers@google.com> References: <20261204011029.1946921-1-irogers@google.com> <20261204011029.1946921-4-irogers@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 01:35: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 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 perf pmu-layer: Introduce stateless struct pmu_layer abstraction and builti= n 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 =3D 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 =3D vdata; > + const char *desc; > + > + if (table =3D=3D ctx->last_table) > + return 0; > + desc =3D pmu_metrics_table__describe_metricgroup(table, ctx->group); > + if (!desc) { > + ctx->last_table =3D 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 =3D 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 =3D NULL, *old_alias =3D NULL; > + > + if (!pmu || !name || !name[0] || name[0] =3D=3D '.' || > + pmu_event_file_kind(name, NULL) !=3D 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 =3D 0; i < nr_pmu_layers; i++) { > + const struct pmu_layer *layer =3D &pmu_layers[i]; > + > + if (!layer->ops->for_each_event) > + continue; > + ret =3D layer->ops->for_each_event(layer, pmu); > + if (ret) { > + pmu_layer_finalize_aliases(pmu, /*promote_legacy=3D*/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 =3D { > + .pmu =3D pmu, > + .is_common =3D false, > + .override_existing =3D true, > + }; > + int ret; > + > + if (!pmu || !table) > + return 0; > + > + ret =3D pmu_events_table__for_each_event(table, pmu, > + builtin_for_each_event_cb, &ctx); > + pmu_layer_finalize_aliases(pmu, /*promote_legacy=3D*/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 =3D 0; i < nr_pmu_layers; i++) { > + const struct pmu_layer *layer =3D &pmu_layers[i]; > + > + if (!layer->ops->for_each_metric) > + continue; > + ret =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204011029.1946= 921-1-irogers@google.com?part=3D3