From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3 05/11] perf config: Add pmu-events.dir configuration option
Date: Fri, 04 Dec 2026 01:27:04 +0000 [thread overview]
Message-ID: <sashiko-outbox-248894@kernel.org> (raw)
In-Reply-To: <20261204011029.1946921-6-irogers@google.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] perf pmu-layer: unexpanded tilde path in
pmu_layers_add_user_dirs()
- [Medium] perf pmu-layer: per-lookup openat and getdents64 storms in
user_dir_layer_ops
- [Medium] perf pmu-layer: unchecked cap doubling and file type in
dir_read_fd()
- [Low] perf pmu-layer: tilde overflow aborts init in
pmu_layers_add_user_dirs()
--
commit ef894413c604fbf05187e1f7bae3997e7d585869
Author: Ian Rogers <irogers@google.com>
perf config: Add pmu-events.dir configuration option
This commit adds the pmu-events.dir configuration option to register
colon-separated PMU overlay directories with leading ~/ expansion in the
PMU layer stack. It activates user_dir_layer_ops, resets the layer stack
in perf_config__exit() and perf_pmus__destroy(), and documents the option
in perf-config.txt.
> diff --git a/tools/perf/util/pmu-layer.c b/tools/perf/util/pmu-layer.c
> index d4fb8c64f617a..7ab72c8adf9cc 100644
> --- a/tools/perf/util/pmu-layer.c
> +++ b/tools/perf/util/pmu-layer.c
[ ... ]
> @@ -2079,7 +2080,7 @@ static int user_dir_load_metricgroup(const struct pmu_layer *layer,
> return ret < 0 ? ret : 0;
> }
>
> -static const struct pmu_layer_ops __maybe_unused user_dir_layer_ops = {
> +static const struct pmu_layer_ops user_dir_layer_ops = {
[Severity: Medium]
Now that user_dir_layer_ops is active, can non-regular or oversized files
in a pmu-events.dir overlay hang perf or corrupt the heap in
dir_read_fd()?
dir_read_file_at_mode() calls openat() with O_RDONLY and no O_NONBLOCK or
fstat() S_ISREG check, and dir_read_fd() reads in an unbounded loop with
an unchecked cap *= 2:
tools/perf/util/pmu-layer.c:dir_read_fd() {
...
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);
...
}
A symlink to /dev/zero in an overlay directory reads until OOM on 64-bit
systems, and on 32-bit builds cap *= 2 wraps size_t: either passing 0 to
realloc() (double free on glibc or out-of-bounds write on musl) or
shrinking buf below len and overflowing the heap buffer on the next
read(). A FIFO in the directory also blocks openat() indefinitely.
Opening with O_NONBLOCK, rejecting non-regular files via fstat(), and
enforcing a size limit in dir_read_fd() would prevent both failures.
[ ... ]
> @@ -2318,13 +2319,65 @@ void pmu_layers__exit(void)
> pmu_layers_initialized = false;
> }
>
> +static int pmu_layers_add_user_dirs(const char *cfg)
> +{
> + const char *p = cfg;
> +
> + if (!cfg || !cfg[0])
> + return 0;
> +
> + while (*p) {
> + const char *colon = strchr(p, ':');
> + size_t seg_len = colon ? (size_t)(colon - p) : strlen(p);
> +
> + if (seg_len > 0 && seg_len < PATH_MAX) {
> + char raw[PATH_MAX];
> + char expanded[PATH_MAX];
> + const char *use_path = raw;
> + int ret;
> +
> + memcpy(raw, p, seg_len);
> + raw[seg_len] = '\0';
> +
> + if (raw[0] == '~' && (raw[1] == '\0' || raw[1] == '/')) {
> + const char *home = getenv("HOME");
> +
> + if (home && home[0]) {
> + int n = snprintf(expanded, sizeof(expanded),
> + "%s%s", home, raw + 1);
> +
> + if (n < 0 || (size_t)n >= sizeof(expanded))
> + return -ENAMETOOLONG;
[Severity: Low]
Should an overlong tilde expansion skip this entry instead of returning
-ENAMETOOLONG?
pmu_layers_ensure_init() treats any error from pmu_layers_add_user_dirs()
as fatal and tears down the stack before appending the built-in and sysfs
layers, so every later event or metric lookup silently returns NULL. An
unexpanded segment of length >= PATH_MAX is already skipped above; warning
and skipping here would keep the lower layers working.
> + use_path = expanded;
> + }
> + }
[Severity: Medium]
If HOME is unset or empty, does this register the unexpanded tilde path
as a working-directory-relative overlay?
When perf runs under cron or systemd with a system perfconfig entry such
as ~/.perf-events/local:/opt/pmu-events, use_path stays as the literal
"~/.perf-events/local" string. dir_for_each_scope() then passes it
straight to open(), which resolves ./~/.perf-events/local against the
current working directory and lets a ./~ tree in a shared directory like
/tmp override system overlays, built-in tables, and sysfs.
Skipping the segment when HOME is unset or empty would match how
home_perfconfig() handles missing HOME.
> +
> + ret = pmu_layers_append(PMU_LAYER_USER_DIR,
> + &user_dir_layer_ops, use_path);
[Severity: Medium]
Can registering uncached user_dir_layer_ops entries here cause severe
openat() and getdents64() syscall amplification on routine perf
invocations?
pmu_layers_add_user_dirs() appends every configured path without checking
if it exists or caching an open root fd. On each lookup,
dir_for_each_scope() re-opens root_path and its cpus/ and sys/ scopes,
and for every uncore PMU dir_for_each_pmu_subdir() runs dup() and a full
io_dir__readdir() scan of each matched scope. Because bare event parsing
in parse_events_multi_pmu_add() and metric lookup scan across all PMUs,
systems with many uncore PMUs incur repeated directory scans per event.
On sparse overlays, an absent event also triggers up to 45 openat()
sidecar probes per matched PMU subdir per scope in
user_dir_find_event_pmu_cb(), and pmu_layer__find_event() and
pmu_layer__find_metric() do not cache negative results so repeated probes
replay the full sequence. Skipping nonexistent roots and caching
discovered scope/PMU directories (or negative lookups) would avoid this
overhead.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204011029.1946921-1-irogers@google.com?part=5
next prev parent reply other threads:[~2026-12-04 1:27 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 [this message]
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-248894@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