diff mbox series

[RESEND,2/2] perf/core: Allow reading package events from perf_event_read_local

Message ID 20230912124432.3616761-3-tero.kristo@linux.intel.com (mailing list archive)
State Not Applicable
Headers show
Series perf/x86: Package residency counter improvements | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Tero Kristo Sept. 12, 2023, 12:44 p.m. UTC
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@linux.intel.com>
---
 kernel/events/core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Peter Zijlstra Sept. 12, 2023, 2:04 p.m. UTC | #1
On Tue, Sep 12, 2023 at 03:44:32PM +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@linux.intel.com>
> ---
>  kernel/events/core.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 4c72a41f11af..780dde646e8a 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -4528,6 +4528,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>  {
>  	unsigned long flags;
>  	int ret = 0;
> +	int event_cpu;
>  
>  	/*
>  	 * Disabling interrupts avoids all counter scheduling (context
> @@ -4551,15 +4552,18 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>  		goto out;
>  	}
>  
> +	event_cpu = READ_ONCE(event->oncpu);
> +	event_cpu = __perf_event_read_cpu(event, event_cpu);

What happens with __perf_event_read_cpu() when event_cpu == -1 ?

> +
>  	/* If this is a per-CPU event, it must be for this CPU */
>  	if (!(event->attach_state & PERF_ATTACH_TASK) &&
> -	    event->cpu != smp_processor_id()) {
> +	    event_cpu != smp_processor_id()) {
>  		ret = -EINVAL;
>  		goto out;
>  	}
>  
>  	/* 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_cpu != smp_processor_id()) {
>  		ret = -EBUSY;
>  		goto out;
>  	}
> @@ -4569,7 +4573,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>  	 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
>  	 * oncpu == -1).
>  	 */
> -	if (event->oncpu == smp_processor_id())
> +	if (event_cpu == smp_processor_id())
>  		event->pmu->read(event);
>  
>  	*value = local64_read(&event->count);
> -- 
> 2.40.1
>
Tero Kristo Sept. 12, 2023, 2:54 p.m. UTC | #2
On 12/09/2023 17:04, Peter Zijlstra wrote:
> On Tue, Sep 12, 2023 at 03:44:32PM +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@linux.intel.com>
>> ---
>>   kernel/events/core.c | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/events/core.c b/kernel/events/core.c
>> index 4c72a41f11af..780dde646e8a 100644
>> --- a/kernel/events/core.c
>> +++ b/kernel/events/core.c
>> @@ -4528,6 +4528,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>>   {
>>   	unsigned long flags;
>>   	int ret = 0;
>> +	int event_cpu;
>>   
>>   	/*
>>   	 * Disabling interrupts avoids all counter scheduling (context
>> @@ -4551,15 +4552,18 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>>   		goto out;
>>   	}
>>   
>> +	event_cpu = READ_ONCE(event->oncpu);
>> +	event_cpu = __perf_event_read_cpu(event, event_cpu);
> What happens with __perf_event_read_cpu() when event_cpu == -1 ?

Good question. It looks like I need to add a check against that. Will 
update and send v2 out.

-Tero


>
>> +
>>   	/* If this is a per-CPU event, it must be for this CPU */
>>   	if (!(event->attach_state & PERF_ATTACH_TASK) &&
>> -	    event->cpu != smp_processor_id()) {
>> +	    event_cpu != smp_processor_id()) {
>>   		ret = -EINVAL;
>>   		goto out;
>>   	}
>>   
>>   	/* 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_cpu != smp_processor_id()) {
>>   		ret = -EBUSY;
>>   		goto out;
>>   	}
>> @@ -4569,7 +4573,7 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>>   	 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
>>   	 * oncpu == -1).
>>   	 */
>> -	if (event->oncpu == smp_processor_id())
>> +	if (event_cpu == smp_processor_id())
>>   		event->pmu->read(event);
>>   
>>   	*value = local64_read(&event->count);
>> -- 
>> 2.40.1
>>
diff mbox series

Patch

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4c72a41f11af..780dde646e8a 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4528,6 +4528,7 @@  int perf_event_read_local(struct perf_event *event, u64 *value,
 {
 	unsigned long flags;
 	int ret = 0;
+	int event_cpu;
 
 	/*
 	 * Disabling interrupts avoids all counter scheduling (context
@@ -4551,15 +4552,18 @@  int perf_event_read_local(struct perf_event *event, u64 *value,
 		goto out;
 	}
 
+	event_cpu = READ_ONCE(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 (!(event->attach_state & PERF_ATTACH_TASK) &&
-	    event->cpu != smp_processor_id()) {
+	    event_cpu != smp_processor_id()) {
 		ret = -EINVAL;
 		goto out;
 	}
 
 	/* 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_cpu != smp_processor_id()) {
 		ret = -EBUSY;
 		goto out;
 	}
@@ -4569,7 +4573,7 @@  int perf_event_read_local(struct perf_event *event, u64 *value,
 	 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
 	 * oncpu == -1).
 	 */
-	if (event->oncpu == smp_processor_id())
+	if (event_cpu == smp_processor_id())
 		event->pmu->read(event);
 
 	*value = local64_read(&event->count);