Re: [PATCHv2 2/2] perf/core: Allow reading package events from perf_event_read_local

From: Peter Zijlstra
Date: Tue Oct 03 2023 - 07:00:41 EST


On Wed, Sep 13, 2023 at 03:59:56PM +0300, Tero Kristo wrote:
> 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: Tero Kristo <tero.kristo@xxxxxxxxxxxxxxx>
> ---
> v2:
> * prevent illegal array access in case event->oncpu == -1
> * split the event->cpu / event->oncpu handling to their own variables
>
> kernel/events/core.c | 18 +++++++++++++++---
> 1 file changed, 15 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 4c72a41f11af..6b343bac0a71 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -4425,6 +4425,9 @@ static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
> {
> u16 local_pkg, event_pkg;
>
> + if (event_cpu < 0 || event_cpu >= nr_cpu_ids)
> + return event_cpu;

if ((unsigned)event_cpu >= nr_cpu_ids)
return event_cpu;

As you could also find at the current __perf_event_read_cpu() callsite.

> +
> if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
> int local_cpu = smp_processor_id();
>
> @@ -4528,6 +4531,8 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
> {
> unsigned long flags;
> int ret = 0;
> + int event_cpu;
> + int event_oncpu;

You wrecked the x-mas tree :-)

I'll fix both up.

Thanks!