[PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs

Dhananjay Ugwekar posted 1 patch 1 year, 3 months ago
There is a newer version of this series
arch/x86/events/rapl.c | 33 +++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)
[PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Dhananjay Ugwekar 1 year, 3 months ago
After commit ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf"),
on AMD processors that support extended CPUID leaf 0x80000026, the
topology_die_cpumask() and topology_logical_die_id() macros, no longer
return the package cpumask and package id, instead they return the CCD
(Core Complex Die) mask and id respectively. This leads to the energy-pkg
event scope to be modified to CCD instead of package.

So, change the PMU scope for AMD and Hygon back to package.

On a 12 CCD 1 Package AMD Zen4 Genoa machine:

Before:
$ cat /sys/devices/power/cpumask
0,8,16,24,32,40,48,56,64,72,80,88.

The expected cpumask here is supposed to be just "0", as it is a package
scope event, only one CPU will be collecting the event for all the CPUs in
the package.

After:
$ cat /sys/devices/power/cpumask
0

Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
---
v2 Link: https://lore.kernel.org/all/20240730044917.4680-2-Dhananjay.Ugwekar@amd.com/

Changes from v2:
* Rebase on top of kan.liang's PMU scope patchset [1]
* Set pmu.scope variable to package for AMD/Hygon CPUs

tip/master + PMU scope patchset [1] to be taken as base for testing this patch. 

[1]: https://lore.kernel.org/all/20240802151643.1691631-1-kan.liang@linux.intel.com/
---
 arch/x86/events/rapl.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
index b70ad880c5bc..0c57dd5aa767 100644
--- a/arch/x86/events/rapl.c
+++ b/arch/x86/events/rapl.c
@@ -139,9 +139,32 @@ static unsigned int rapl_cntr_mask;
 static u64 rapl_timer_ms;
 static struct perf_msr *rapl_msrs;
 
+/*
+ * RAPL Package energy counter scope:
+ * 1. AMD/HYGON platforms have a per-PKG package energy counter
+ * 2. For Intel platforms
+ *	2.1. CLX-AP is multi-die and its RAPL MSRs are die-scope
+ *	2.2. Other Intel platforms are single die systems so the scope can be
+ *	     considered as either pkg-scope or die-scope, and we are considering
+ *	     them as die-scope.
+ */
+#define rapl_pmu_is_pkg_scope()				\
+	(boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||	\
+	 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
+
+/*
+ * Helper function to get the correct topology id according to the
+ * RAPL PMU scope.
+ */
+static inline unsigned int get_rapl_pmu_idx(int cpu)
+{
+	return rapl_pmu_is_pkg_scope() ? topology_logical_package_id(cpu) :
+					 topology_logical_die_id(cpu);
+}
+
 static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
 {
-	unsigned int rapl_pmu_idx = topology_logical_die_id(cpu);
+	unsigned int rapl_pmu_idx = get_rapl_pmu_idx(cpu);
 
 	/*
 	 * The unsigned check also catches the '-1' return value for non
@@ -617,7 +640,7 @@ static void __init init_rapl_pmu(void)
 		pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
 		rapl_hrtimer_init(pmu);
 
-		rapl_pmus->pmus[topology_logical_die_id(cpu)] = pmu;
+		rapl_pmus->pmus[get_rapl_pmu_idx(cpu)] = pmu;
 	}
 
 	cpus_read_unlock();
@@ -646,6 +669,12 @@ static int __init init_rapl_pmus(void)
 	rapl_pmus->pmu.module		= THIS_MODULE;
 	rapl_pmus->pmu.scope		= PERF_PMU_SCOPE_DIE;
 	rapl_pmus->pmu.capabilities	= PERF_PMU_CAP_NO_EXCLUDE;
+
+	if (rapl_pmu_is_pkg_scope()) {
+		rapl_pmus->nr_rapl_pmu	= topology_max_packages();
+		rapl_pmus->pmu.scope	= PERF_PMU_SCOPE_PKG;
+	}
+
 	return 0;
 }
 
-- 
2.34.1
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Dhananjay Ugwekar 1 year, 3 months ago
On 9/4/2024 3:39 PM, Dhananjay Ugwekar wrote:
> After commit ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf"),
> on AMD processors that support extended CPUID leaf 0x80000026, the
> topology_die_cpumask() and topology_logical_die_id() macros, no longer
> return the package cpumask and package id, instead they return the CCD
> (Core Complex Die) mask and id respectively. This leads to the energy-pkg
> event scope to be modified to CCD instead of package.
> 
> So, change the PMU scope for AMD and Hygon back to package.
> 
> On a 12 CCD 1 Package AMD Zen4 Genoa machine:
> 
> Before:
> $ cat /sys/devices/power/cpumask
> 0,8,16,24,32,40,48,56,64,72,80,88.
> 
> The expected cpumask here is supposed to be just "0", as it is a package
> scope event, only one CPU will be collecting the event for all the CPUs in
> the package.
> 
> After:
> $ cat /sys/devices/power/cpumask
> 0
> 
> Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
> ---
> v2 Link: https://lore.kernel.org/all/20240730044917.4680-2-Dhananjay.Ugwekar@amd.com/
> 
> Changes from v2:
> * Rebase on top of kan.liang's PMU scope patchset [1]
> * Set pmu.scope variable to package for AMD/Hygon CPUs
> 
> tip/master + PMU scope patchset [1] to be taken as base for testing this patch. 
> 
> [1]: https://lore.kernel.org/all/20240802151643.1691631-1-kan.liang@linux.intel.com/
> ---
>  arch/x86/events/rapl.c | 33 +++++++++++++++++++++++++++++++--
>  1 file changed, 31 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index b70ad880c5bc..0c57dd5aa767 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -139,9 +139,32 @@ static unsigned int rapl_cntr_mask;
>  static u64 rapl_timer_ms;
>  static struct perf_msr *rapl_msrs;
>  
> +/*
> + * RAPL Package energy counter scope:
> + * 1. AMD/HYGON platforms have a per-PKG package energy counter
> + * 2. For Intel platforms
> + *	2.1. CLX-AP is multi-die and its RAPL MSRs are die-scope
> + *	2.2. Other Intel platforms are single die systems so the scope can be
> + *	     considered as either pkg-scope or die-scope, and we are considering
> + *	     them as die-scope.
> + */
> +#define rapl_pmu_is_pkg_scope()				\
> +	(boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||	\
> +	 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
> +
> +/*
> + * Helper function to get the correct topology id according to the
> + * RAPL PMU scope.
> + */
> +static inline unsigned int get_rapl_pmu_idx(int cpu)
> +{
> +	return rapl_pmu_is_pkg_scope() ? topology_logical_package_id(cpu) :
> +					 topology_logical_die_id(cpu);
> +}
> +
>  static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
>  {
> -	unsigned int rapl_pmu_idx = topology_logical_die_id(cpu);
> +	unsigned int rapl_pmu_idx = get_rapl_pmu_idx(cpu);
>  
>  	/*
>  	 * The unsigned check also catches the '-1' return value for non
> @@ -617,7 +640,7 @@ static void __init init_rapl_pmu(void)
>  		pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
>  		rapl_hrtimer_init(pmu);
>  
> -		rapl_pmus->pmus[topology_logical_die_id(cpu)] = pmu;
> +		rapl_pmus->pmus[get_rapl_pmu_idx(cpu)] = pmu;
>  	}
>  
>  	cpus_read_unlock();
> @@ -646,6 +669,12 @@ static int __init init_rapl_pmus(void)
>  	rapl_pmus->pmu.module		= THIS_MODULE;
>  	rapl_pmus->pmu.scope		= PERF_PMU_SCOPE_DIE;
>  	rapl_pmus->pmu.capabilities	= PERF_PMU_CAP_NO_EXCLUDE;
> +
> +	if (rapl_pmu_is_pkg_scope()) {
> +		rapl_pmus->nr_rapl_pmu	= topology_max_packages();

Just noticed, better to update nr_rapl_pmu before we allocate the memory for rapl_pmus at
"rapl_pmus = kzalloc(struct_size(rapl_pmus, rapl_pmu, nr_rapl_pmu), GFP_KERNEL);", so that
we dont waste memory, will fix this and post new version.

Thanks,
Dhananjay

> +		rapl_pmus->pmu.scope	= PERF_PMU_SCOPE_PKG;
> +	}
> +
>  	return 0;
>  }
>
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Liang, Kan 1 year, 3 months ago

On 2024-09-04 6:09 a.m., Dhananjay Ugwekar wrote:
> After commit ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf"),
> on AMD processors that support extended CPUID leaf 0x80000026, the
> topology_die_cpumask() and topology_logical_die_id() macros, no longer
> return the package cpumask and package id, instead they return the CCD
> (Core Complex Die) mask and id respectively. This leads to the energy-pkg
> event scope to be modified to CCD instead of package.
> 
> So, change the PMU scope for AMD and Hygon back to package.
> 
> On a 12 CCD 1 Package AMD Zen4 Genoa machine:
> 
> Before:
> $ cat /sys/devices/power/cpumask
> 0,8,16,24,32,40,48,56,64,72,80,88.
> 
> The expected cpumask here is supposed to be just "0", as it is a package
> scope event, only one CPU will be collecting the event for all the CPUs in
> the package.
> 
> After:
> $ cat /sys/devices/power/cpumask
> 0
> 
> Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>

Reviewed-by: Kan Liang <kan.liang@linux.intel.com>

Thanks,
Kan

> ---
> v2 Link: https://lore.kernel.org/all/20240730044917.4680-2-Dhananjay.Ugwekar@amd.com/
> 
> Changes from v2:
> * Rebase on top of kan.liang's PMU scope patchset [1]
> * Set pmu.scope variable to package for AMD/Hygon CPUs
> 
> tip/master + PMU scope patchset [1] to be taken as base for testing this patch. 
> 
> [1]: https://lore.kernel.org/all/20240802151643.1691631-1-kan.liang@linux.intel.com/
> ---
>  arch/x86/events/rapl.c | 33 +++++++++++++++++++++++++++++++--
>  1 file changed, 31 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index b70ad880c5bc..0c57dd5aa767 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -139,9 +139,32 @@ static unsigned int rapl_cntr_mask;
>  static u64 rapl_timer_ms;
>  static struct perf_msr *rapl_msrs;
>  
> +/*
> + * RAPL Package energy counter scope:
> + * 1. AMD/HYGON platforms have a per-PKG package energy counter
> + * 2. For Intel platforms
> + *	2.1. CLX-AP is multi-die and its RAPL MSRs are die-scope
> + *	2.2. Other Intel platforms are single die systems so the scope can be
> + *	     considered as either pkg-scope or die-scope, and we are considering
> + *	     them as die-scope.
> + */
> +#define rapl_pmu_is_pkg_scope()				\
> +	(boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||	\
> +	 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
> +
> +/*
> + * Helper function to get the correct topology id according to the
> + * RAPL PMU scope.
> + */
> +static inline unsigned int get_rapl_pmu_idx(int cpu)
> +{
> +	return rapl_pmu_is_pkg_scope() ? topology_logical_package_id(cpu) :
> +					 topology_logical_die_id(cpu);
> +}
> +
>  static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
>  {
> -	unsigned int rapl_pmu_idx = topology_logical_die_id(cpu);
> +	unsigned int rapl_pmu_idx = get_rapl_pmu_idx(cpu);
>  
>  	/*
>  	 * The unsigned check also catches the '-1' return value for non
> @@ -617,7 +640,7 @@ static void __init init_rapl_pmu(void)
>  		pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
>  		rapl_hrtimer_init(pmu);
>  
> -		rapl_pmus->pmus[topology_logical_die_id(cpu)] = pmu;
> +		rapl_pmus->pmus[get_rapl_pmu_idx(cpu)] = pmu;
>  	}
>  
>  	cpus_read_unlock();
> @@ -646,6 +669,12 @@ static int __init init_rapl_pmus(void)
>  	rapl_pmus->pmu.module		= THIS_MODULE;
>  	rapl_pmus->pmu.scope		= PERF_PMU_SCOPE_DIE;
>  	rapl_pmus->pmu.capabilities	= PERF_PMU_CAP_NO_EXCLUDE;
> +
> +	if (rapl_pmu_is_pkg_scope()) {
> +		rapl_pmus->nr_rapl_pmu	= topology_max_packages();
> +		rapl_pmus->pmu.scope	= PERF_PMU_SCOPE_PKG;
> +	}
> +
>  	return 0;
>  }
>
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Ingo Molnar 1 year, 3 months ago
* Liang, Kan <kan.liang@linux.intel.com> wrote:

> 
> 
> On 2024-09-04 6:09 a.m., Dhananjay Ugwekar wrote:
> > After commit ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf"),
> > on AMD processors that support extended CPUID leaf 0x80000026, the
> > topology_die_cpumask() and topology_logical_die_id() macros, no longer
> > return the package cpumask and package id, instead they return the CCD
> > (Core Complex Die) mask and id respectively. This leads to the energy-pkg
> > event scope to be modified to CCD instead of package.
> > 
> > So, change the PMU scope for AMD and Hygon back to package.
> > 
> > On a 12 CCD 1 Package AMD Zen4 Genoa machine:
> > 
> > Before:
> > $ cat /sys/devices/power/cpumask
> > 0,8,16,24,32,40,48,56,64,72,80,88.
> > 
> > The expected cpumask here is supposed to be just "0", as it is a package
> > scope event, only one CPU will be collecting the event for all the CPUs in
> > the package.
> > 
> > After:
> > $ cat /sys/devices/power/cpumask
> > 0
> > 
> > Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
> 
> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>

Note that I just merged the older v2 version into perf/urgent, because it's 
a regression fix, and to not create a backporting complication. I added 
your Reviewed-by to it if that's fine with you.

Your hotplug series can essentially revert this:

  8d72eba1cf8c ("perf/x86/rapl: Fix the energy-pkg event for AMD CPUs")

and apply the v3 patch to gain the simpler version.

Thanks,

	Ingo
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Dhananjay Ugwekar 1 year, 3 months ago
Hello Ingo,

On 9/5/2024 3:37 PM, Ingo Molnar wrote:
> 
> * Liang, Kan <kan.liang@linux.intel.com> wrote:
> 
>>
>>
>> On 2024-09-04 6:09 a.m., Dhananjay Ugwekar wrote:
>>> After commit ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf"),
>>> on AMD processors that support extended CPUID leaf 0x80000026, the
>>> topology_die_cpumask() and topology_logical_die_id() macros, no longer
>>> return the package cpumask and package id, instead they return the CCD
>>> (Core Complex Die) mask and id respectively. This leads to the energy-pkg
>>> event scope to be modified to CCD instead of package.
>>>
>>> So, change the PMU scope for AMD and Hygon back to package.
>>>
>>> On a 12 CCD 1 Package AMD Zen4 Genoa machine:
>>>
>>> Before:
>>> $ cat /sys/devices/power/cpumask
>>> 0,8,16,24,32,40,48,56,64,72,80,88.
>>>
>>> The expected cpumask here is supposed to be just "0", as it is a package
>>> scope event, only one CPU will be collecting the event for all the CPUs in
>>> the package.
>>>
>>> After:
>>> $ cat /sys/devices/power/cpumask
>>> 0
>>>
>>> Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
>>
>> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
> 
> Note that I just merged the older v2 version into perf/urgent

I'm not able to find the v2 version in perf/urgent right now, tip-bot had given the below link before
https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?id=8d72eba1cf8cecd76a2b4c1dd7673c2dc775f514.
Have we changed our decision about keeping the v2 for backporting and "revert v2 + apply v4" post the PMU scope patchset?

Thanks,
Dhananjay

, because it's 
> a regression fix, and to not create a backporting complication. I added 
> your Reviewed-by to it if that's fine with you.
> 
> Your hotplug series can essentially revert this:
> 
>   8d72eba1cf8c ("perf/x86/rapl: Fix the energy-pkg event for AMD CPUs")
> 
> and apply the v3 patch to gain the simpler version.
> 
> Thanks,
> 
> 	Ingo
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Ingo Molnar 1 year, 3 months ago
* Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com> wrote:

> >>> Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
> >>
> >> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
> > 
> > Note that I just merged the older v2 version into perf/urgent
> 
> I'm not able to find the v2 version in perf/urgent right now, tip-bot had given the below link before
> https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?id=8d72eba1cf8cecd76a2b4c1dd7673c2dc775f514.
> Have we changed our decision about keeping the v2 for backporting and "revert v2 + apply v4" post the PMU scope patchset?

Yeah, so initially I thought that this comment of yours applied to -v2 as 
well:

 https://lore.kernel.org/all/b34e30bc-a003-4529-bcfe-8189ca7ddc5b@amd.com

... but in hindsight, it's -v3 specific, right?

So I've restored -v2 and it should go to Linus this week if all goes well.

Thanks,

	Ingo
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Dhananjay Ugwekar 1 year, 3 months ago
On 9/9/2024 3:02 PM, Ingo Molnar wrote:
> 
> * Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com> wrote:
> 
>>>>> Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
>>>>
>>>> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
>>>
>>> Note that I just merged the older v2 version into perf/urgent
>>
>> I'm not able to find the v2 version in perf/urgent right now, tip-bot had given the below link before
>> https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?id=8d72eba1cf8cecd76a2b4c1dd7673c2dc775f514.
>> Have we changed our decision about keeping the v2 for backporting and "revert v2 + apply v4" post the PMU scope patchset?
> 
> Yeah, so initially I thought that this comment of yours applied to -v2 as 
> well:
> 
>  https://lore.kernel.org/all/b34e30bc-a003-4529-bcfe-8189ca7ddc5b@amd.com
> 
> ... but in hindsight, it's -v3 specific, right?

Yes, it is only applicable to v3 and I had posted v4 to fix this and now posted v5 to rebase v4 on top of a recent fix 
in the PMU scope patchset(https://lore.kernel.org/all/8c09633c-5bf2-48a2-91a6-a0af9b9f2e8c@linux.intel.com/)

v5 Link: https://lore.kernel.org/linux-perf-users/20240910085504.204814-1-Dhananjay.Ugwekar@amd.com/T/#u,

Please "revert v2 + apply v5" post the kan.liang's PMU scope patchset.

> 
> So I've restored -v2 and it should go to Linus this week if all goes well.

Thanks!,

Dhananjay

> 
> Thanks,
> 
> 	Ingo
Re: [PATCH v3] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by Liang, Kan 1 year, 3 months ago

On 2024-09-05 6:07 a.m., Ingo Molnar wrote:
> 
> * Liang, Kan <kan.liang@linux.intel.com> wrote:
> 
>>
>>
>> On 2024-09-04 6:09 a.m., Dhananjay Ugwekar wrote:
>>> After commit ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf"),
>>> on AMD processors that support extended CPUID leaf 0x80000026, the
>>> topology_die_cpumask() and topology_logical_die_id() macros, no longer
>>> return the package cpumask and package id, instead they return the CCD
>>> (Core Complex Die) mask and id respectively. This leads to the energy-pkg
>>> event scope to be modified to CCD instead of package.
>>>
>>> So, change the PMU scope for AMD and Hygon back to package.
>>>
>>> On a 12 CCD 1 Package AMD Zen4 Genoa machine:
>>>
>>> Before:
>>> $ cat /sys/devices/power/cpumask
>>> 0,8,16,24,32,40,48,56,64,72,80,88.
>>>
>>> The expected cpumask here is supposed to be just "0", as it is a package
>>> scope event, only one CPU will be collecting the event for all the CPUs in
>>> the package.
>>>
>>> After:
>>> $ cat /sys/devices/power/cpumask
>>> 0
>>>
>>> Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
>>
>> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
> 
> Note that I just merged the older v2 version into perf/urgent, because it's 
> a regression fix, and to not create a backporting complication. I added 
> your Reviewed-by to it if that's fine with you.
> 
> Your hotplug series can essentially revert this:
> 
>   8d72eba1cf8c ("perf/x86/rapl: Fix the energy-pkg event for AMD CPUs")
> 
> and apply the v3 patch to gain the simpler version.
> 

Sure, sounds good to me.

Thanks,
Kan
[tip: perf/urgent] perf/x86/rapl: Fix the energy-pkg event for AMD CPUs
Posted by tip-bot2 for Dhananjay Ugwekar 1 year, 3 months ago
The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     8d72eba1cf8cecd76a2b4c1dd7673c2dc775f514
Gitweb:        https://git.kernel.org/tip/8d72eba1cf8cecd76a2b4c1dd7673c2dc775f514
Author:        Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
AuthorDate:    Tue, 30 Jul 2024 04:49:18 
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Thu, 05 Sep 2024 12:02:14 +02:00

perf/x86/rapl: Fix the energy-pkg event for AMD CPUs

After commit:

  63edbaa48a57 ("x86/cpu/topology: Add support for the AMD 0x80000026 leaf")

... on AMD processors that support extended CPUID leaf 0x80000026, the
topology_die_cpumask() and topology_logical_die_id() macros no longer
return the package cpumask and package ID, instead they return the CCD
(Core Complex Die) mask and ID respectively.

This leads to the energy-pkg event scope to be modified to CCD instead of package.

So, change the PMU scope for AMD and Hygon back to package.

On a 12 CCD 1 Package AMD Zen4 Genoa machine:

  Before:

  $ cat /sys/devices/power/cpumask
  0,8,16,24,32,40,48,56,64,72,80,88.

The expected cpumask here is supposed to be just "0", as it is a package
scope event, only one CPU will be collecting the event for all the CPUs in
the package.

  After:

  $ cat /sys/devices/power/cpumask
  0

[ mingo: Cleaned up the changelog ]

Signed-off-by: Dhananjay Ugwekar <Dhananjay.Ugwekar@amd.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Link: https://lore.kernel.org/r/20240904100934.3260-1-Dhananjay.Ugwekar@amd.com
---
 arch/x86/events/rapl.c | 47 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 42 insertions(+), 5 deletions(-)

diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
index b985ca7..a481a93 100644
--- a/arch/x86/events/rapl.c
+++ b/arch/x86/events/rapl.c
@@ -103,6 +103,19 @@ static struct perf_pmu_events_attr event_attr_##v = {				\
 	.event_str	= str,							\
 };
 
+/*
+ * RAPL Package energy counter scope:
+ * 1. AMD/HYGON platforms have a per-PKG package energy counter
+ * 2. For Intel platforms
+ *	2.1. CLX-AP is multi-die and its RAPL MSRs are die-scope
+ *	2.2. Other Intel platforms are single die systems so the scope can be
+ *	     considered as either pkg-scope or die-scope, and we are considering
+ *	     them as die-scope.
+ */
+#define rapl_pmu_is_pkg_scope()				\
+	(boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||	\
+	 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
+
 struct rapl_pmu {
 	raw_spinlock_t		lock;
 	int			n_active;
@@ -140,9 +153,25 @@ static unsigned int rapl_cntr_mask;
 static u64 rapl_timer_ms;
 static struct perf_msr *rapl_msrs;
 
+/*
+ * Helper functions to get the correct topology macros according to the
+ * RAPL PMU scope.
+ */
+static inline unsigned int get_rapl_pmu_idx(int cpu)
+{
+	return rapl_pmu_is_pkg_scope() ? topology_logical_package_id(cpu) :
+					 topology_logical_die_id(cpu);
+}
+
+static inline const struct cpumask *get_rapl_pmu_cpumask(int cpu)
+{
+	return rapl_pmu_is_pkg_scope() ? topology_core_cpumask(cpu) :
+					 topology_die_cpumask(cpu);
+}
+
 static inline struct rapl_pmu *cpu_to_rapl_pmu(unsigned int cpu)
 {
-	unsigned int rapl_pmu_idx = topology_logical_die_id(cpu);
+	unsigned int rapl_pmu_idx = get_rapl_pmu_idx(cpu);
 
 	/*
 	 * The unsigned check also catches the '-1' return value for non
@@ -552,7 +581,7 @@ static int rapl_cpu_offline(unsigned int cpu)
 
 	pmu->cpu = -1;
 	/* Find a new cpu to collect rapl events */
-	target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
+	target = cpumask_any_but(get_rapl_pmu_cpumask(cpu), cpu);
 
 	/* Migrate rapl events to the new target */
 	if (target < nr_cpu_ids) {
@@ -565,6 +594,11 @@ static int rapl_cpu_offline(unsigned int cpu)
 
 static int rapl_cpu_online(unsigned int cpu)
 {
+	s32 rapl_pmu_idx = get_rapl_pmu_idx(cpu);
+	if (rapl_pmu_idx < 0) {
+		pr_err("topology_logical_(package/die)_id() returned a negative value");
+		return -EINVAL;
+	}
 	struct rapl_pmu *pmu = cpu_to_rapl_pmu(cpu);
 	int target;
 
@@ -579,14 +613,14 @@ static int rapl_cpu_online(unsigned int cpu)
 		pmu->timer_interval = ms_to_ktime(rapl_timer_ms);
 		rapl_hrtimer_init(pmu);
 
-		rapl_pmus->pmus[topology_logical_die_id(cpu)] = pmu;
+		rapl_pmus->pmus[rapl_pmu_idx] = pmu;
 	}
 
 	/*
 	 * Check if there is an online cpu in the package which collects rapl
 	 * events already.
 	 */
-	target = cpumask_any_and(&rapl_cpu_mask, topology_die_cpumask(cpu));
+	target = cpumask_any_and(&rapl_cpu_mask, get_rapl_pmu_cpumask(cpu));
 	if (target < nr_cpu_ids)
 		return 0;
 
@@ -675,7 +709,10 @@ static const struct attribute_group *rapl_attr_update[] = {
 
 static int __init init_rapl_pmus(void)
 {
-	int nr_rapl_pmu = topology_max_packages() * topology_max_dies_per_package();
+	int nr_rapl_pmu = topology_max_packages();
+
+	if (!rapl_pmu_is_pkg_scope())
+		nr_rapl_pmu *= topology_max_dies_per_package();
 
 	rapl_pmus = kzalloc(struct_size(rapl_pmus, pmus, nr_rapl_pmu), GFP_KERNEL);
 	if (!rapl_pmus)