Commit 1765bb61 authored by Tero Kristo's avatar Tero Kristo Committed by Peter Zijlstra

perf/core: Allow reading package events from perf_event_read_local

Per-package perf events are typically registered with a single CPU only,
however they can be read across all the CPUs within the package.
Currently perf_event_read maps the event CPU according to the topology
information to avoid an unnecessary SMP call, however
perf_event_read_local deals with hard values and rejects a read with a
failure if the CPU is not the one exactly registered. Allow similar
mapping within the perf_event_read_local if the perf event in question
can support this.

This allows users like BPF code to read the package perf events properly
across different CPUs within a package.
Signed-off-by: default avatarTero Kristo <tero.kristo@linux.intel.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lkml.kernel.org/r/20230913125956.3652667-1-tero.kristo@linux.intel.com
parent 05276d48
...@@ -4425,6 +4425,9 @@ static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) ...@@ -4425,6 +4425,9 @@ static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
{ {
u16 local_pkg, event_pkg; u16 local_pkg, event_pkg;
if ((unsigned)event_cpu >= nr_cpu_ids)
return event_cpu;
if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
int local_cpu = smp_processor_id(); int local_cpu = smp_processor_id();
...@@ -4527,6 +4530,8 @@ int perf_event_read_local(struct perf_event *event, u64 *value, ...@@ -4527,6 +4530,8 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
u64 *enabled, u64 *running) u64 *enabled, u64 *running)
{ {
unsigned long flags; unsigned long flags;
int event_oncpu;
int event_cpu;
int ret = 0; int ret = 0;
/* /*
...@@ -4551,15 +4556,22 @@ int perf_event_read_local(struct perf_event *event, u64 *value, ...@@ -4551,15 +4556,22 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
goto out; goto out;
} }
/*
* Get the event CPU numbers, and adjust them to local if the event is
* a per-package event that can be read locally
*/
event_oncpu = __perf_event_read_cpu(event, event->oncpu);
event_cpu = __perf_event_read_cpu(event, event->cpu);
/* If this is a per-CPU event, it must be for this CPU */ /* If this is a per-CPU event, it must be for this CPU */
if (!(event->attach_state & PERF_ATTACH_TASK) && if (!(event->attach_state & PERF_ATTACH_TASK) &&
event->cpu != smp_processor_id()) { event_cpu != smp_processor_id()) {
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
/* If this is a pinned event it must be running on this CPU */ /* If this is a pinned event it must be running on this CPU */
if (event->attr.pinned && event->oncpu != smp_processor_id()) { if (event->attr.pinned && event_oncpu != smp_processor_id()) {
ret = -EBUSY; ret = -EBUSY;
goto out; goto out;
} }
...@@ -4569,7 +4581,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value, ...@@ -4569,7 +4581,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
* or local to this CPU. Furthermore it means its ACTIVE (otherwise * or local to this CPU. Furthermore it means its ACTIVE (otherwise
* oncpu == -1). * oncpu == -1).
*/ */
if (event->oncpu == smp_processor_id()) if (event_oncpu == smp_processor_id())
event->pmu->read(event); event->pmu->read(event);
*value = local64_read(&event->count); *value = local64_read(&event->count);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment