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=1796348145; b=U2YaQxWJM1vKelXfTFtHNebZoZYVZK1ul5bPNT+xCOadypHOOesKiaFt0ABPBjKfKkGs x6WjkQQpQL8jJdRHuIfksi8uN+iVtAY2Ggk2xcTxzz2AUcBv+UxbDI5Nkn0x40+vXRxDi 9H941SbPzzxn5C6gCGFma2wXuB6FeSWrqbPbvatjrUhOznxpMTrYlBZLFF5BA+XSz0eJw w9U4BOrwh8qMLjyIYhh+6AS06cZ7DkNM51HrEpZMpeK+DBKqgpg5avh7m7ABq7n/Dd9Tw Tac/zxmbf/cqfw8OzhYZNK0NFXg3P9J7OHvfqEfoMMzy50hA98/aF2m29lNHJCjO3Pg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796348145; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=dYmF+ZrGM24lhjJOBJl75X91hR0en0WOKB8G0raD4GE=; b=hHsp6HwCaScTiiGENOwkNqLf1jH+cD4s8fVcMbTIodYVDEbHJt1x1J3jU9PUm8X56yZ+ feZvuwGYNDzDGAxH7cGyERbrF73jkqvms6u6JccHwNyj9/nnh4AUTyH2n6UcREkns5VUH Bfu4Y/3KmMm8RrCPJHTvCwFLd6caidNaef/Wz/mApr3k4FJ4t7S6zD3sk+LA7Y57uG3HZ xjaEHFi6inoegv3G/uMVHB8lRM/ihNGUOXGyBRs9DSA9kDjTrFSr8UTCzwJ/FI5G9wq/W yViu6haY36HJYVNLnNeFyQmM+8dsM2G+hEh+moV4s+N9YHbZHAi4D0qwbTO042btw6g== 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=BDK7RVRg; 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 113CA606A322 for ; Fri, 4 Dec 2026 01:35:44 +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=BDK7RVRg Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7D3AF1F00897; Fri, 4 Dec 2026 01:35:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796348144; bh=dYmF+ZrGM24lhjJOBJl75X91hR0en0WOKB8G0raD4GE=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=BDK7RVRgJfNvVDhfKjeB+UdAvepNtW6uEkGJovcmH/aHx63651AU3121a3sanUqFJ cL8BRbp1l7MAjrZnStt3cCv8QI/RutFykLHAQSS/PCd1HW0FoAgJ+9VyaNVp7zbGC3 vbgawSQiKvMf5WMIyvLsHTzQxopF5QvCh/08wKZDUG74SMsDJufKm5pG+ckylDHYll PNGxNeAJitIJOmLpLbcTEeWvW64EbdKSrGlhvsbAhYilkm/7ufqHfgXVISZko28Lla XMz0OdDavfCex2yfpjghxij5uUVzfylx+pQXnPQgrxdNZsW5UhmLbulrMBzIBArg/9 oYN8/da80LHGQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 04/11] perf pmu-layer: Add openat-based directory layer with literal CPUID/symlink lookup Reply-To: sashiko-reviews@lists.linux.dev To: "Ian Rogers" Cc: linux-perf-users@vger.kernel.org In-Reply-To: <20261204011029.1946921-5-irogers@google.com> References: <20261204011029.1946921-1-irogers@google.com> <20261204011029.1946921-5-irogers@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 01:35:44 +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 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 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 (/cpus//): > 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 =3D len < sizeof(stack_buf) ? len + 256 : sizeof(stack_buf) * 2; > + buf =3D malloc(cap); > + if (!buf) { > + close(fd); > + return -ENOMEM; > + } > + memcpy(buf, stack_buf, len); > + > + for (;;) { > + if (len + 1 >=3D cap) { > + char *next; > + > + cap *=3D 2; > + next =3D realloc(buf, cap); > + if (!next) { > + free(buf); > + close(fd); > + return -ENOMEM; > + } > + buf =3D next; > + } > + n =3D 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 =3D 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 *=3D 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=3D*/false, = out_str); > +} [Severity: Low] Does passing single_read =3D 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 =3D 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 =3D dir_read_file_at_mode(events_fd, filename, is_sysfs, &val); > + if (r =3D=3D 0) { > + alias->terms =3D val; > + if (is_sysfs) { > + alias->from_sysfs =3D true; > + alias->info_loaded =3D 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 =3D dir_read_file_at_mode(events_fd, filename, is_sysfs, &val); > + if (r =3D=3D 0) { > + if (perf_pmu__convert_scale(val, NULL, &alias->scale) =3D=3D 0) > + alias->has_scale =3D true; > + free(val); > + } > + } > + break; > + case PMU_EVENT_FILE_SCALE_UNIT: > + if (!alias->has_scale || !alias->has_unit) { > + r =3D dir_read_file_at(events_fd, filename, &val); > + if (r =3D=3D 0) { > + char *unit_str =3D NULL; > + double sval =3D 1.0; > + > + if (perf_pmu__convert_scale(val, &unit_str, &sval) =3D=3D 0) { > + if (!alias->has_scale) { > + alias->scale =3D sval; > + alias->has_scale =3D true; > + } > + if (!alias->has_unit && unit_str) { > + snprintf(alias->unit, sizeof(alias->unit), > + "%s", unit_str); > + alias->has_unit =3D 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 =3D 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 =3D 0.0 with has_scale =3D true, zeroing counter values. Passing end =3D 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 !=3D val for numeric values and unit_str && unit_str[0] for the unit would prevent both. [ ... ] > + if (r =3D=3D 0) { > + if (matched) > + *matched =3D true; > + if (!is_sysfs && !alias->pmu_name && pmu_dir_name && > + strcmp(pmu_dir_name, "default_core") !=3D 0) { > + alias->pmu_name =3D strdup(pmu_dir_name); > + if (!alias->pmu_name) > + return -ENOMEM; > + } > + } > + return r =3D=3D -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 =3D true and alias->pmu_name without applying a= ny 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 =3D perf_cpu_map__min(pmu->cpus); > + > + pmu->cpuid =3D get_cpuid_str(cpu); > + if (!pmu->cpuid) > + pmu->cpuid =3D get_cpuid_str((struct perf_cpu){ -1 }); > + pmu->cpuid_loaded =3D 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 !=3D -1 before the fallback avoids the redundant call. [ ... ] > + for (size_t i =3D 0; i < nr_cands; i++) { > + int scope_fd =3D openat(cpus_fd, cands[i], > + O_RDONLY | O_DIRECTORY); [ ... ] > + /* 2. Uncore/system PMU identifier scope under /sys/id>/. */ > + if (pmu && pmu->id && pmu->id[0]) { > + int sys_fd =3D openat(root_fd, "sys", O_RDONLY | O_DIRECTORY); > + > + if (sys_fd >=3D 0) { > + int scope_fd =3D openat(sys_fd, pmu->id, O_RDONLY | O_DIRECTORY); [Severity: Low] Can PERF_CPUID or pmu->id escape /cpus/ or /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 =3D dup(scope_fd); > + > + if (dup_fd >=3D 0) { > + struct io_dir dir; > + struct io_dirent64 *dent; > + > + io_dir__init(&dir, dup_fd); > + while ((dent =3D io_dir__readdir(&dir)) !=3D 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/ 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 =3D io_dir__readdir(&dir)) !=3D NULL) { > + size_t base_len =3D 0; > + enum pmu_event_file_kind kind; > + char decoded[NAME_MAX]; > + struct perf_pmu_alias *alias; [ ... ] > + ret =3D 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 .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_na= me, > + struct perf_pmu *pmu __maybe_unused, > + void *vctx) > +{ > + struct dir_find_event_ctx *ctx =3D 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 =3D 0, len =3D strlen(ctx->name); > + const char *pmu_label; > + int events_fd; > + > + if (len =3D=3D 0 || len >=3D NAME_MAX || ctx->name[0] =3D=3D '.' || > + pmu_event_file_kind(ctx->name, NULL) !=3D 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 >=3D 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 =3D 0; i < nr_cands; i++) { > + bool matched =3D false; > + int ret =3D dir_probe_user_event_base(events_fd, enc_cands[i], raw_can= ds[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 =3D 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 =3D NULL; > + > + snprintf(filename, sizeof(filename), "%s.desc", enc_group); > + if (dir_read_file_at(mg_fd, filename, &val) =3D=3D 0) { > + mg->desc =3D 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 =3D=3D 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 >=3D 0) { > + int events_fd =3D sysfs_open_events_dir(pmu); > + int ret =3D 0; > + > + if (events_fd < 0) > + return 0; > + > + for (size_t i =3D 0; i < nr_cands; i++) { > + int fd =3D openat(events_fd, cands[i], O_RDONLY); > + > + if (fd < 0) > + continue; > + ret =3D sysfs_apply_found_event(fd, events_fd, cands[i], > + cands[i], alias); > + if (ret =3D=3D -EISDIR) { > + ret =3D 0; > + continue; > + } > + break; > + } > + > + close(events_fd); > + return ret; > + } [Severity: Low] Is this active_sysfs_override_fd >=3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204011029.1946= 921-1-irogers@google.com?part=3D4