From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3 04/11] perf pmu-layer: Add openat-based directory layer with literal CPUID/symlink lookup
Date: Fri, 04 Dec 2026 01:35:44 +0000 [thread overview]
Message-ID: <sashiko-outbox-248898@kernel.org> (raw)
In-Reply-To: <20261204011029.1946921-5-irogers@google.com>
Thank you for your contribution! Sashiko AI review found 14 potential
issues to consider:
- [Medium] perf: dir_apply_event_file() misparses scale and scale-unit
sidecars
- [Medium] perf: dir_read_fd() size_t overflow on realloc and unbounded
overlay reads
- [Medium] perf: uncached scope opens and uncore readdir() scans on
every lazy lookup
- [Low] perf: dir_apply_event_file() skips sysfs flags and pmu_name if
terms set
- [Low] perf: dir_probe_metricgroups_fd() swallows -ENOMEM from
dir_read_file_at()
- [Low] perf: pmu-layer.c sizes filename buffers to NAME_MAX instead of
NAME_MAX + 1
- [Low] perf: commit message misstates CPUID helper and x86 hybrid CPUID
lookup
- [Low] perf: pmu_layer_get_cpuid() calls get_cpuid_str(-1) twice when
cpu is -1
- [Low] perf: dir_apply_{event,metric}_file() swallow errors and accept
bad sidecars
- [Low] perf: dir_scan_events_fd() applies scale/unit sidecars in
readdir order
- [Low] perf: dir_for_each_scope() allows path traversal via PERF_CPUID
and pmu->id
- [Low] perf: dir_read_fd() permanently over-allocates attribute strings
by 256 bytes
- [Low] perf pmu-layer: unreachable override fd branch in
sysfs_layer_find_event()
- [Low] perf: uncached misses in pmu_layer__find_event() and dead
faccessat()
--
commit 1f8706655a3faa41b11489a03ea0b23c2b9b4114
Author: Ian Rogers <irogers@google.com>
perf pmu-layer: Add openat-based directory layer with literal CPUID/symlink lookup
This commit adds the filesystem-backed user_dir_layer_ops and
sysfs_layer_ops backends in tools/perf/util/pmu-layer.c using openat() and
io_dir, supporting CPUID, system identifier, and host-direct overlay scopes
along with event, metric, and metricgroup sidecar files.
> 1. CPUID scope (<root>/cpus/<cpuid>/):
> Queries the PMU's CPUID via get_cpuid_allow_env_override() (using
> perf_cpu_map__min(pmu->cpus) so heterogeneous core PMUs such as
> cpu_core and cpu_atom resolve against their own CPU's CPUID, and
> honoring PERF_CPUID).
[Severity: Low]
Is this description accurate? pmu_layer_get_cpuid() checks
getenv("PERF_CPUID") and calls get_cpuid_str() directly rather than
get_cpuid_allow_env_override(). In addition, x86 get_cpuid_str() ignores
its cpu argument, so cpu_core and cpu_atom share the same CPUID and are
distinguished by PMU subdirectory name rather than per-CPU CPUID.
> diff --git a/tools/perf/util/pmu-layer.c b/tools/perf/util/pmu-layer.c
> index 8552291f899a8..d4fb8c64f617a 100644
> --- a/tools/perf/util/pmu-layer.c
> +++ b/tools/perf/util/pmu-layer.c
[ ... ]
> + cap = len < sizeof(stack_buf) ? len + 256 : sizeof(stack_buf) * 2;
> + buf = malloc(cap);
> + if (!buf) {
> + close(fd);
> + return -ENOMEM;
> + }
> + memcpy(buf, stack_buf, len);
> +
> + for (;;) {
> + if (len + 1 >= cap) {
> + char *next;
> +
> + cap *= 2;
> + next = realloc(buf, cap);
> + if (!next) {
> + free(buf);
> + close(fd);
> + return -ENOMEM;
> + }
> + buf = next;
> + }
> + n = read(fd, buf + len, cap - len - 1);
[ ... ]
> +static int dir_read_file_at_mode(int dirfd, const char *filename,
> + bool single_read, char **out_str)
> +{
> + int fd = openat(dirfd, filename, O_RDONLY);
> +
> + if (fd < 0)
> + return -errno;
> + return dir_read_fd(fd, single_read, out_str);
> +}
[Severity: Medium]
Can doubling cap in dir_read_fd() overflow size_t or allow unbounded reads?
On 32-bit builds, cap *= 2 has no overflow check or upper limit. When a
power-of-two cap wraps to 0, glibc realloc(buf, 0) frees buf and returns
NULL, causing a double free on the error path; when a non-power-of-two cap
(from len + 256) wraps below len, realloc() shrinks buf and the next read()
writes past the allocation with an underflowed size.
Separately, dir_read_file_at_mode() opens overlay files without O_NONBLOCK
or an fstat() S_ISREG() check, so a FIFO or a symlink to /dev/zero in an
overlay directory can hang perf or exhaust memory.
> +static int dir_read_file_at(int dirfd, const char *filename, char **out_str)
> +{
> + return dir_read_file_at_mode(dirfd, filename, /*single_read=*/false, out_str);
> +}
[Severity: Low]
Does passing single_read = false here cause every small user-directory
attribute string to permanently waste 256 bytes?
In dir_read_fd(), the exact-sized malloc(len + 1) fast path is skipped when
single_read is false, so short files allocate cap = len + 256 bytes and
return the unshrunk buffer to live in struct perf_pmu_alias or struct
perf_pmu_metric. Looping read() into stack_buf until EOF or full before
allocating len + 1 bytes would avoid the per-string bloat.
[ ... ]
> + switch (kind) {
> + case PMU_EVENT_FILE_BASE:
> + if (!alias->terms) {
> + r = dir_read_file_at_mode(events_fd, filename, is_sysfs, &val);
> + if (r == 0) {
> + alias->terms = val;
> + if (is_sysfs) {
> + alias->from_sysfs = true;
> + alias->info_loaded = true;
> + }
> + }
> + }
> + break;
[Severity: Low]
Should this still check that the base file exists when alias->terms is
already populated by a higher-priority layer?
Skipping the entire case leaves r as -ENOENT, so full sysfs enumeration
never sets alias->from_sysfs or alias->info_loaded (unlike
sysfs_apply_found_event()), and user overlays skip updating *matched and
alias->pmu_name when pmu_name was still NULL after default_core/. Using
fstatat() to verify the base file exists and is not a directory when
alias->terms is set would keep both paths consistent.
[ ... ]
> + case PMU_EVENT_FILE_SCALE:
> + if (!alias->has_scale) {
> + r = dir_read_file_at_mode(events_fd, filename, is_sysfs, &val);
> + if (r == 0) {
> + if (perf_pmu__convert_scale(val, NULL, &alias->scale) == 0)
> + alias->has_scale = true;
> + free(val);
> + }
> + }
> + break;
> + case PMU_EVENT_FILE_SCALE_UNIT:
> + if (!alias->has_scale || !alias->has_unit) {
> + r = dir_read_file_at(events_fd, filename, &val);
> + if (r == 0) {
> + char *unit_str = NULL;
> + double sval = 1.0;
> +
> + if (perf_pmu__convert_scale(val, &unit_str, &sval) == 0) {
> + if (!alias->has_scale) {
> + alias->scale = sval;
> + alias->has_scale = true;
> + }
> + if (!alias->has_unit && unit_str) {
> + snprintf(alias->unit, sizeof(alias->unit),
> + "%s", unit_str);
> + alias->has_unit = true;
> + }
> + }
> + free(val);
> + }
> + }
> + break;
[Severity: Medium]
Can .scale-unit and .scale sidecars corrupt event scaling or block
lower-layer units here?
perf_pmu__convert_scale() calls strtod() in parse_double() and returns 0
even when no digits are parsed or when *end is NUL. A scale-only
.scale-unit file like "1e-6" leaves unit_str non-NULL pointing at '\0',
which sets alias->unit to "" with has_unit = true and blocks lower layers
from providing a unit.
Conversely, a unit-only .scale-unit file like "Joules" parses zero
characters and sets alias->scale = 0.0 with has_scale = true, zeroing
counter values. Passing end = NULL for PMU_EVENT_FILE_SCALE and
PMU_EVENT_FILE_RETIRE_LAT_{MEAN,MIN,MAX} similarly accepts empty or
non-numeric files as 0.0. Checking end != val for numeric values and
unit_str && unit_str[0] for the unit would prevent both.
[ ... ]
> + if (r == 0) {
> + if (matched)
> + *matched = true;
> + if (!is_sysfs && !alias->pmu_name && pmu_dir_name &&
> + strcmp(pmu_dir_name, "default_core") != 0) {
> + alias->pmu_name = strdup(pmu_dir_name);
> + if (!alias->pmu_name)
> + return -ENOMEM;
> + }
> + }
> + return r == -ENOMEM ? -ENOMEM : 0;
[Severity: Low]
Are parse errors and -ENOMEM from perf_pmu__convert_scale() swallowed before
reaching this check?
When perf_pmu__convert_scale() returns -ENOMEM (from strdup(lc) in
parse_double()) or parse_bool_str() returns -EINVAL, r stays 0 from
dir_read_file_at*(). As a result, -ENOMEM is dropped and malformed
sidecars still set *matched = true and alias->pmu_name without applying any
attribute. dir_apply_metric_file() has the same behavior for invalid
.aggr-mode, .constraint, and .show-events values.
[ ... ]
> + if (!pmu->cpuid_loaded) {
> + struct perf_cpu cpu = perf_cpu_map__min(pmu->cpus);
> +
> + pmu->cpuid = get_cpuid_str(cpu);
> + if (!pmu->cpuid)
> + pmu->cpuid = get_cpuid_str((struct perf_cpu){ -1 });
> + pmu->cpuid_loaded = true;
> + }
[Severity: Low]
Can this invoke get_cpuid_str({ -1 }) twice when pmu->cpus is NULL or empty?
perf_cpu_map__min(pmu->cpus) already returns { -1 } in that case, so a
failed lookup repeats the same call (rescanning online CPUs and printing a
duplicate pr_err on arm64). Checking cpu.cpu != -1 before the fallback
avoids the redundant call.
[ ... ]
> + for (size_t i = 0; i < nr_cands; i++) {
> + int scope_fd = openat(cpus_fd, cands[i],
> + O_RDONLY | O_DIRECTORY);
[ ... ]
> + /* 2. Uncore/system PMU identifier scope under <root>/sys/<pmu->id>/. */
> + if (pmu && pmu->id && pmu->id[0]) {
> + int sys_fd = openat(root_fd, "sys", O_RDONLY | O_DIRECTORY);
> +
> + if (sys_fd >= 0) {
> + int scope_fd = openat(sys_fd, pmu->id, O_RDONLY | O_DIRECTORY);
[Severity: Low]
Can PERF_CPUID or pmu->id escape <root>/cpus/ or <root>/sys/ via path
traversal?
build_cpuid_candidates() copies getenv("PERF_CPUID") verbatim without
rejecting '/', '.', or '..', so an absolute path ignores cpus_fd and a
relative ".." path escapes the overlay root. Rejecting '/' and leading '.'
in both cands[i] and pmu->id would match the checks already used for event
and metric filenames.
[ ... ]
> + if (pmu->is_uncore) {
> + int dup_fd = dup(scope_fd);
> +
> + if (dup_fd >= 0) {
> + struct io_dir dir;
> + struct io_dirent64 *dent;
> +
> + io_dir__init(&dir, dup_fd);
> + while ((dent = io_dir__readdir(&dir)) != NULL) {
[Severity: Medium]
Does every lazy lookup on an uncore PMU re-open all overlay scopes and run a
full readdir() over every scope directory?
dir_for_each_scope() re-opens root_path, cpus/, up to six CPUID candidates,
and sys/<id> from scratch on every call without caching. Because
user_dir_find_event_pmu_cb() and user_dir_find_metric_pmu_cb() return 0
even after matching a candidate directory, dir_for_each_pmu_subdir() still
runs dup() and io_dir__readdir() across every scope.
On multi-socket systems with hundreds of uncore PMUs, unqualified lookups
through parse_events_multi_pmu_add() multiply these openat() and
getdents64() scans across every event, PMU, and overlay layer.
[ ... ]
> + io_dir__init(&dir, dup_fd);
> + while ((dent = io_dir__readdir(&dir)) != NULL) {
> + size_t base_len = 0;
> + enum pmu_event_file_kind kind;
> + char decoded[NAME_MAX];
> + struct perf_pmu_alias *alias;
[ ... ]
> + ret = dir_apply_event_file(events_fd, dent->d_name, kind,
> + pmu_dir_name, is_sysfs, alias, NULL);
> + if (ret)
> + break;
> + }
[Severity: Low]
Can .scale-unit shadow .scale or .unit depending on getdents64() order
during full enumeration?
dir_probe_user_event_base() always probes .unit and .scale before
.scale-unit, giving them precedence on single-event lookups. Here, if
io_dir__readdir() yields <event>.scale-unit first, dir_apply_event_file()
sets has_scale and has_unit and silently ignores the explicit .scale and
.unit files in the same directory.
[ ... ]
> +static int user_dir_find_event_pmu_cb(int pmu_fd, const char *pmu_dir_name,
> + struct perf_pmu *pmu __maybe_unused,
> + void *vctx)
> +{
> + struct dir_find_event_ctx *ctx = vctx;
> + char lower[NAME_MAX], upper[NAME_MAX];
> + char enc_exact[NAME_MAX], enc_lower[NAME_MAX], enc_upper[NAME_MAX];
> + const char *enc_cands[3], *raw_cands[3];
> + size_t nr_cands = 0, len = strlen(ctx->name);
> + const char *pmu_label;
> + int events_fd;
> +
> + if (len == 0 || len >= NAME_MAX || ctx->name[0] == '.' ||
> + pmu_event_file_kind(ctx->name, NULL) != PMU_EVENT_FILE_BASE)
> + return 0;
[Severity: Low]
Should these filename buffers be sized to NAME_MAX + 1 instead of NAME_MAX?
NAME_MAX (255) excludes the terminating NUL, so valid 255-byte event,
metric, or metricgroup filenames are rejected with -ENAMETOOLONG or by the
len >= NAME_MAX check and silently skipped here and in dir_scan_events_fd(),
dir_scan_metrics_fd(), user_dir_find_metric_pmu_cb(),
user_dir_metricgroup_{pmu,scope}_cb(), and sysfs_layer_find_event().
[ ... ]
> + for (size_t i = 0; i < nr_cands; i++) {
> + bool matched = false;
> + int ret = dir_probe_user_event_base(events_fd, enc_cands[i], raw_cands[i],
> + pmu_label, ctx->alias, &matched);
> + if (ret) {
> + close(events_fd);
> + return ret;
> + }
> + if (matched)
> + break;
> + }
[Severity: Low]
Does repeating this 15-suffix probe across up to three case variants on
every miss become expensive without negative caching in
pmu_layer__find_event()?
When an event is absent on a PMU, pmu_layer__find_event() frees alias and
returns NULL without recording the miss in pmu->aliases, so repeated
unqualified lookups via parse_events_multi_pmu_add() (for example
inst_retired.any:u,inst_retired.any:k) re-run 30 to 45 failed openat()
calls per matched events/ directory on every non-matching PMU.
Separately, the faccessat() in dir_apply_metric_file() when
metric->metric_expr is already set appears to be a dead syscall, since both
callers pass matched = NULL and any earlier user-directory layer that set
metric_expr already populated metric->pmu.
[ ... ]
> +static int dir_probe_metricgroups_fd(int mg_fd, const char *enc_group,
> + struct perf_pmu_metricgroup *mg)
> +{
> + char filename[NAME_MAX + 16];
> + char *val = NULL;
> +
> + snprintf(filename, sizeof(filename), "%s.desc", enc_group);
> + if (dir_read_file_at(mg_fd, filename, &val) == 0) {
> + mg->desc = val;
> + return 1;
> + }
> + return 0;
> +}
[Severity: Low]
Does this swallow -ENOMEM from dir_read_file_at()?
Returning 0 instead of propagating -ENOMEM (as dir_apply_event_file() and
dir_apply_metric_file() do) causes pmu_layer__describe_metricgroup() to
fall through to lower layers or cache a permanent negative entry with
mg->desc == NULL on a transient allocation failure.
[ ... ]
> + if (active_sysfs_override_fd < 0 &&
> + (pmu->sysfs_aliases_loaded || pmu->eager_sysfs_loaded))
> + return 0;
[ ... ]
> + if (active_sysfs_override_fd >= 0) {
> + int events_fd = sysfs_open_events_dir(pmu);
> + int ret = 0;
> +
> + if (events_fd < 0)
> + return 0;
> +
> + for (size_t i = 0; i < nr_cands; i++) {
> + int fd = openat(events_fd, cands[i], O_RDONLY);
> +
> + if (fd < 0)
> + continue;
> + ret = sysfs_apply_found_event(fd, events_fd, cands[i],
> + cands[i], alias);
> + if (ret == -EISDIR) {
> + ret = 0;
> + continue;
> + }
> + break;
> + }
> +
> + close(events_fd);
> + return ret;
> + }
[Severity: Low]
Is this active_sysfs_override_fd >= 0 branch in sysfs_layer_find_event()
unreachable?
active_sysfs_override_fd is only set non-negative inside
pmu_layer__load_events_at(), which only invokes for_each_event() callbacks
and resets it to -1 before returning. Passing sysfs_fd explicitly rather
than through a file-scope global would also keep struct pmu_layer_ops
stateless as documented in pmu-layer.h.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204011029.1946921-1-irogers@google.com?part=4
next prev parent 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
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 [this message]
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-248898@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