Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3 01/11] perf pmu: Generalize pmu_alias_info_file() and lazy single-event sysfs loading
Date: Fri, 04 Dec 2026 01:32:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-248896@kernel.org> (raw)
In-Reply-To: <20261204011029.1946921-2-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] perf: perf_pmu__find_alias() drops fncache negative caching
  on sysfs misses
- [Low] perf pmu: missing limits.h include and unused stdint.h in pmu.c
- [Low] perf pmu: perf_pmu__find_alias() rejects NAME_MAX-byte filenames
--

commit c18e0db8cd70b14b1f0446e07236f035cf280476
Author: Ian Rogers <irogers@google.com>

perf pmu: Generalize pmu_alias_info_file() and lazy single-event sysfs loading

This commit adds extended event sidecar classification to
pmu_alias_info_file(), switches sysfs sidecar readers to openat() relative
to an open events/ directory fd, and changes perf_pmu__find_alias() to
lazily open only the requested event and its sidecars.

> diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
> index 1ae8bcf71c68f..c5ab6c6881990 100644
> --- a/tools/perf/util/pmu.c
> +++ b/tools/perf/util/pmu.c
> @@ -10,9 +10,11 @@
>  #include <unistd.h>
>  #include <errno.h>
>  #include <stdbool.h>
> +#include <stdint.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> +#include <strings.h>
>  #include <dirent.h>
>  #include <api/fs/fs.h>
>  #include <api/io.h>

[Severity: Low]
Is <stdint.h> needed here?  No stdint.h types or macros are used in
tools/perf/util/pmu.c, while this commit adds new uses of NAME_MAX and
PATH_MAX without explicitly including <limits.h>, which can break builds
on libc implementations such as musl if transitive includes change.

[ ... ]

>  static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
>  						   const char *name,
>  						   bool load)
>  {
> -	struct perf_pmu_alias *alias;
> -	bool has_sysfs_event;
> -	char event_file_name[NAME_MAX + 8];
> +	struct perf_pmu_alias *alias = NULL;
> +	char path[PATH_MAX];
> +	char lower_name[NAME_MAX];
> +	char upper_name[NAME_MAX];
> +	const char *candidates[3];
> +	size_t nr_candidates = 0, name_len, path_len;
> +	int events_dir_fd;
>  
>  	if (hashmap__find(pmu->aliases, name, &alias))
>  		return alias;
> @@ -452,29 +569,61 @@ static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu,
>  	if (!load || pmu->sysfs_aliases_loaded)
>  		return NULL;
>  
> -	/*
> -	 * Test if alias/event 'name' exists in the PMU's sysfs/events
> -	 * directory. If not skip parsing the sysfs aliases. Sysfs event
> -	 * name must be all lower or all upper case.
> -	 */
> -	scnprintf(event_file_name, sizeof(event_file_name), "events/%s", name);
> -	for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
> -		event_file_name[i] = tolower(event_file_name[i]);
> +	name_len = strlen(name);
> +	if (name_len == 0 || name_len >= NAME_MAX || name[0] == '.' ||
> +	    strchr(name, '/') || pmu_alias_info_file(name))
> +		return NULL;

[Severity: Low]
Does perf_pmu__find_alias() silently reject valid NAME_MAX-byte event
filenames?

NAME_MAX (255) excludes the null terminator, whereas __pmu_aliases_parse()
enumerates up to NAME_MAX bytes via io_dirent64's d_name[NAME_MAX + 1].
Sizing lower_name and upper_name to NAME_MAX + 1 and checking
name_len > NAME_MAX would keep lazy lookup consistent with full scans.

Later in this series, commit "perf pmu: Switch alias resolution and
enumeration to layered PMU stack" carries the same NAME_MAX sizing and
len >= NAME_MAX check into sysfs_layer_find_event() and
user_dir_find_event_pmu_cb().

> -	has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
> -	if (!has_sysfs_event) {
> -		for (size_t i = 7, n = 7 + strlen(name); i < n; i++)
> -			event_file_name[i] = toupper(event_file_name[i]);
> +	path_len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
> +	if (!path_len)
> +		return NULL;
> +	scnprintf(path + path_len, sizeof(path) - path_len, "%s/events", pmu->name);
>  
> -		has_sysfs_event = perf_pmu__file_exists(pmu, event_file_name);
> +	events_dir_fd = open(path, O_DIRECTORY | O_RDONLY);
> +	if (events_dir_fd == -1) {
> +		if (errno == ENOENT)
> +			pmu->sysfs_aliases_loaded = true;
> +		return NULL;
>  	}
> -	if (has_sysfs_event) {
> -		pmu_aliases_parse(pmu);
> -		if (hashmap__find(pmu->aliases, name, &alias))
> -			return alias;
> +
> +	candidates[nr_candidates++] = name;
> +	for (size_t i = 0; i < name_len; i++) {
> +		lower_name[i] = tolower((unsigned char)name[i]);
> +		upper_name[i] = toupper((unsigned char)name[i]);
>  	}
> +	lower_name[name_len] = '\0';
> +	upper_name[name_len] = '\0';
>  
> -	return NULL;
> +	if (strcmp(lower_name, name) != 0)
> +		candidates[nr_candidates++] = lower_name;
> +	if (strcmp(upper_name, name) != 0 && strcmp(upper_name, lower_name) != 0)
> +		candidates[nr_candidates++] = upper_name;
> +
> +	/*
> +	 * Lazily open only the requested event and its sidecars via openat()
> +	 * rather than scanning every file in sysfs/events/.
> +	 */
> +	for (size_t i = 0; i < nr_candidates; i++) {
> +		struct stat st;
> +		int fd = openat(events_dir_fd, candidates[i], O_RDONLY);

[Severity: Medium]
Does bypassing perf_pmu__file_exists() here reintroduce redundant sysfs
open syscalls on repeated negative lookups?

Previously, perf_pmu__file_exists() used fncache via file_available()
(d96645821e94 ("perf pmu: Use file system cache to optimize sysfs
access")), avoiding syscalls on repeated misses, and the first sysfs hit
set sysfs_aliases_loaded.  Now every miss opens events/ and calls openat().

When parsing multiple metrics, parse_events_multi_pmu_add() scans every
sysfs PMU via perf_pmu__have_event() for each bare metric event, repeating
these uncached syscalls across all non-matching PMUs with an events/
directory.

Later in the series, commit "perf pmu: Switch alias resolution and
enumeration to layered PMU stack" removes the per-miss directory
open/close, but sysfs_layer_find_event() and pmu_layer__find_event() still
do not cache negative lookups.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204011029.1946921-1-irogers@google.com?part=1

  reply	other threads:[~2026-12-04  1:32 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 [this message]
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
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-248896@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