[PATCH v2] perf evlist: Don't restrict uncore events to PMU CPUs

Nia Su posted 1 patch 2 weeks, 1 day ago
tools/lib/perf/evlist.c                 | 5 +++--
tools/lib/perf/include/internal/evsel.h | 2 ++
tools/perf/util/evsel.c                 | 1 +
tools/perf/util/parse-events.c          | 1 +
4 files changed, 7 insertions(+), 2 deletions(-)
[PATCH v2] perf evlist: Don't restrict uncore events to PMU CPUs
Posted by Nia Su 2 weeks, 1 day ago
The PMU CPU map has different semantics for core and uncore PMUs.

For core PMUs, pmu_cpus contains the CPUs on which the event can be
opened. Intersect the user-requested CPU map with the PMU CPU map to
reject CPUs unsupported by the PMU.

For uncore PMUs, pmu_cpus is read from the PMU's cpumask sysfs attribute,
which lists the default CPU to open the event on per socket/die, not the
set of CPUs it can only be opened on. The user-requested CPU may override
these defaults, and the driver can redirect the event to the appropriate
control CPU.

Currently, __perf_evlist__propagate_maps() intersects the user-requested
CPU map with pmu_cpus for all PMUs. If the user-requested CPU is not in
pmu_cpus, the intersection becomes empty and the evsel is removed before
perf_event_open() is called.

Add evsel->is_pmu_uncore, set once from pmu->is_uncore at parse time,
to skip the intersection for uncore PMUs. Core PMUs, and other
non-core PMUs such as software events, breakpoints, or tracepoints,
continue to be restricted to their PMU CPU map.

Fixes: 811082e4b668 ("perf parse-events: Support user CPUs mixed with threads/processes")

Assisted-by: Claude Sonnet 5
Signed-off-by: Nia Su <nia.su@sifive.com>
---
Changes in v2:
- Add a dedicated is_pmu_uncore flag instead of reusing requires_cpu,
  since it more precisely represents whether the PMU is uncore.
- Link to v1: https://lore.kernel.org/r/20260904-perf-evlist-uncore-cpu-b4-v1-1-604b55c025ee@sifive.com
---
 tools/lib/perf/evlist.c                 | 5 +++--
 tools/lib/perf/include/internal/evsel.h | 2 ++
 tools/perf/util/evsel.c                 | 1 +
 tools/perf/util/parse-events.c          | 1 +
 4 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c
index 1f210dadd666ff818a23712ab95acaa659ccf8dc..c1c5b0684eedb7a6ab8f3df85f326fbd5ef809a2 100644
--- a/tools/lib/perf/evlist.c
+++ b/tools/lib/perf/evlist.c
@@ -79,8 +79,9 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
 		}
 	}
 
-	/* Ensure cpus only references valid PMU CPUs. */
-	if (!perf_cpu_map__has_any_cpu(evsel->cpus) &&
+	/* Ensure cpus only references valid PMU CPUs, except for uncore PMUs. */
+	if (!evsel->is_pmu_uncore &&
+	    !perf_cpu_map__has_any_cpu(evsel->cpus) &&
 	    !perf_cpu_map__is_subset(evsel->pmu_cpus, evsel->cpus)) {
 		struct perf_cpu_map *tmp = perf_cpu_map__intersect(evsel->pmu_cpus, evsel->cpus);
 
diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/include/internal/evsel.h
index b988034f13710ec4d49d942d486c4b9afe21a1f0..35447bed0f9586b538dad40c223e70d50486c5f6 100644
--- a/tools/lib/perf/include/internal/evsel.h
+++ b/tools/lib/perf/include/internal/evsel.h
@@ -128,6 +128,8 @@ struct perf_evsel {
 	bool			 requires_cpu;
 	/** Is the PMU for the event a core one? Effects the handling of own_cpus. */
 	bool			 is_pmu_core;
+	/** Is the PMU for the event an uncore one? */
+	bool			 is_pmu_uncore;
 	/** Does the evsel on read on the first CPU index such as tool time events? */
 	bool			 reads_only_on_cpu_idx0;
 	int			 idx;
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index d4cb455f4a7d45ae2f9133709ff3248b54163ce0..5acec3f6345099ef9e46e81614b9df2595925b1c 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -551,6 +551,7 @@ struct evsel *evsel__clone(struct evsel *orig)
 	evsel->core.system_wide = orig->core.system_wide;
 	evsel->core.requires_cpu = orig->core.requires_cpu;
 	evsel->core.is_pmu_core = orig->core.is_pmu_core;
+	evsel->core.is_pmu_uncore = orig->core.is_pmu_uncore;
 
 	if (orig->name) {
 		evsel->name = strdup(orig->name);
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index cc7ad331a49f34536f66724370b8bb94ab65bacd..8302be207c1d6ecf8ae6fd67f54d66d08d50e01c 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -269,6 +269,7 @@ __add_event(struct list_head *list, int *idx,
 	evsel->core.pmu_cpus = pmu_cpus;
 	evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
 	evsel->core.is_pmu_core = is_pmu_core;
+	evsel->core.is_pmu_uncore = pmu ? pmu->is_uncore : false;
 	evsel->core.reads_only_on_cpu_idx0 = perf_pmu__reads_only_on_cpu_idx0(attr);
 	evsel->pmu = pmu;
 	evsel->alternate_hw_config = alternate_hw_config;

---
base-commit: 73e3f0710014fe6d4ed98cfc02292f6121db7558
change-id: 20260904-perf-evlist-uncore-cpu-b4-418af0f17c80

--
Re: [PATCH v2] perf evlist: Don't restrict uncore events to PMU CPUs
Posted by Ian Rogers 1 week, 2 days ago
On Wed, Sep 9, 2026 at 7:31 PM Nia Su <nia.su@sifive.com> wrote:
>
> The PMU CPU map has different semantics for core and uncore PMUs.
>
> For core PMUs, pmu_cpus contains the CPUs on which the event can be
> opened. Intersect the user-requested CPU map with the PMU CPU map to
> reject CPUs unsupported by the PMU.
>
> For uncore PMUs, pmu_cpus is read from the PMU's cpumask sysfs attribute,
> which lists the default CPU to open the event on per socket/die, not the
> set of CPUs it can only be opened on. The user-requested CPU may override
> these defaults, and the driver can redirect the event to the appropriate
> control CPU.
>
> Currently, __perf_evlist__propagate_maps() intersects the user-requested
> CPU map with pmu_cpus for all PMUs. If the user-requested CPU is not in
> pmu_cpus, the intersection becomes empty and the evsel is removed before
> perf_event_open() is called.
>
> Add evsel->is_pmu_uncore, set once from pmu->is_uncore at parse time,
> to skip the intersection for uncore PMUs. Core PMUs, and other
> non-core PMUs such as software events, breakpoints, or tracepoints,
> continue to be restricted to their PMU CPU map.
>
> Fixes: 811082e4b668 ("perf parse-events: Support user CPUs mixed with threads/processes")
>
> Assisted-by: Claude Sonnet 5
> Signed-off-by: Nia Su <nia.su@sifive.com>

Reviewed-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
> Changes in v2:
> - Add a dedicated is_pmu_uncore flag instead of reusing requires_cpu,
>   since it more precisely represents whether the PMU is uncore.
> - Link to v1: https://lore.kernel.org/r/20260904-perf-evlist-uncore-cpu-b4-v1-1-604b55c025ee@sifive.com
> ---
>  tools/lib/perf/evlist.c                 | 5 +++--
>  tools/lib/perf/include/internal/evsel.h | 2 ++
>  tools/perf/util/evsel.c                 | 1 +
>  tools/perf/util/parse-events.c          | 1 +
>  4 files changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c
> index 1f210dadd666ff818a23712ab95acaa659ccf8dc..c1c5b0684eedb7a6ab8f3df85f326fbd5ef809a2 100644
> --- a/tools/lib/perf/evlist.c
> +++ b/tools/lib/perf/evlist.c
> @@ -79,8 +79,9 @@ static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
>                 }
>         }
>
> -       /* Ensure cpus only references valid PMU CPUs. */
> -       if (!perf_cpu_map__has_any_cpu(evsel->cpus) &&
> +       /* Ensure cpus only references valid PMU CPUs, except for uncore PMUs. */
> +       if (!evsel->is_pmu_uncore &&
> +           !perf_cpu_map__has_any_cpu(evsel->cpus) &&
>             !perf_cpu_map__is_subset(evsel->pmu_cpus, evsel->cpus)) {
>                 struct perf_cpu_map *tmp = perf_cpu_map__intersect(evsel->pmu_cpus, evsel->cpus);
>
> diff --git a/tools/lib/perf/include/internal/evsel.h b/tools/lib/perf/include/internal/evsel.h
> index b988034f13710ec4d49d942d486c4b9afe21a1f0..35447bed0f9586b538dad40c223e70d50486c5f6 100644
> --- a/tools/lib/perf/include/internal/evsel.h
> +++ b/tools/lib/perf/include/internal/evsel.h
> @@ -128,6 +128,8 @@ struct perf_evsel {
>         bool                     requires_cpu;
>         /** Is the PMU for the event a core one? Effects the handling of own_cpus. */
>         bool                     is_pmu_core;
> +       /** Is the PMU for the event an uncore one? */
> +       bool                     is_pmu_uncore;
>         /** Does the evsel on read on the first CPU index such as tool time events? */
>         bool                     reads_only_on_cpu_idx0;
>         int                      idx;
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index d4cb455f4a7d45ae2f9133709ff3248b54163ce0..5acec3f6345099ef9e46e81614b9df2595925b1c 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -551,6 +551,7 @@ struct evsel *evsel__clone(struct evsel *orig)
>         evsel->core.system_wide = orig->core.system_wide;
>         evsel->core.requires_cpu = orig->core.requires_cpu;
>         evsel->core.is_pmu_core = orig->core.is_pmu_core;
> +       evsel->core.is_pmu_uncore = orig->core.is_pmu_uncore;
>
>         if (orig->name) {
>                 evsel->name = strdup(orig->name);
> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> index cc7ad331a49f34536f66724370b8bb94ab65bacd..8302be207c1d6ecf8ae6fd67f54d66d08d50e01c 100644
> --- a/tools/perf/util/parse-events.c
> +++ b/tools/perf/util/parse-events.c
> @@ -269,6 +269,7 @@ __add_event(struct list_head *list, int *idx,
>         evsel->core.pmu_cpus = pmu_cpus;
>         evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
>         evsel->core.is_pmu_core = is_pmu_core;
> +       evsel->core.is_pmu_uncore = pmu ? pmu->is_uncore : false;
>         evsel->core.reads_only_on_cpu_idx0 = perf_pmu__reads_only_on_cpu_idx0(attr);
>         evsel->pmu = pmu;
>         evsel->alternate_hw_config = alternate_hw_config;
>
> ---
> base-commit: 73e3f0710014fe6d4ed98cfc02292f6121db7558
> change-id: 20260904-perf-evlist-uncore-cpu-b4-418af0f17c80
>
> --
>
Re: [PATCH v2] perf evlist: Don't restrict uncore events to PMU CPUs
Posted by Arnaldo Carvalho de Melo 3 hours ago
On Tue, Sep 15, 2026 at 12:42:43PM -0700, Ian Rogers wrote:
> On Wed, Sep 9, 2026 at 7:31 PM Nia Su <nia.su@sifive.com> wrote:
> > The PMU CPU map has different semantics for core and uncore PMUs.
<SNIP>
> > Add evsel->is_pmu_uncore, set once from pmu->is_uncore at parse time,
> > to skip the intersection for uncore PMUs. Core PMUs, and other
> > non-core PMUs such as software events, breakpoints, or tracepoints,
> > continue to be restricted to their PMU CPU map.

> > Fixes: 811082e4b668 ("perf parse-events: Support user CPUs mixed with threads/processes")

> > Assisted-by: Claude Sonnet 5
> > Signed-off-by: Nia Su <nia.su@sifive.com>
 
> Reviewed-by: Ian Rogers <irogers@google.com>

Thanks, applied to perf-tools-next, for v7.4.

- Arnaldo