[kvm-unit-tests PATCH] x86/pmu: Verify all available GP counters in check_counters_many()

Dapeng Mi posted 1 patch 4 months ago
x86/pmu.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
[kvm-unit-tests PATCH] x86/pmu: Verify all available GP counters in check_counters_many()
Posted by Dapeng Mi 4 months ago
The intent of check_counters_many() is to verify all available counters
can count correctly at the same time. So an alternative event should be
picked to verify the avaialbe GP counter instead of skiping the counter
if the initial event is not available.

Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
---
Please notice this patch is based on Sean's "x86: Add CPUID properties,
 clean up related code" v2 patchset (https://lore.kernel.org/all/20250610195415.115404-1-seanjc@google.com/).

 x86/pmu.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/x86/pmu.c b/x86/pmu.c
index 3987311c..a6b0cfcc 100644
--- a/x86/pmu.c
+++ b/x86/pmu.c
@@ -457,18 +457,34 @@ static void check_fixed_counters(void)
 	}
 }
 
+static struct pmu_event *get_one_event(int idx)
+{
+	int i;
+
+	if (pmu_arch_event_is_available(idx))
+		return &gp_events[idx % gp_events_size];
+
+	for (i = 0; i < gp_events_size; i++) {
+		if (pmu_arch_event_is_available(i))
+			return &gp_events[i];
+	}
+
+	return NULL;
+}
+
 static void check_counters_many(void)
 {
+	struct pmu_event *evt;
 	pmu_counter_t cnt[48];
 	int i, n;
 
 	for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) {
-		if (!pmu_arch_event_is_available(i))
+		evt = get_one_event(i);
+		if (!evt)
 			continue;
 
 		cnt[n].ctr = MSR_GP_COUNTERx(n);
-		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
-			gp_events[i % gp_events_size].unit_sel;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel;
 		n++;
 	}
 	for (i = 0; i < fixed_counters_num; i++) {
-- 
2.43.0
Re: [kvm-unit-tests PATCH] x86/pmu: Verify all available GP counters in check_counters_many()
Posted by Sean Christopherson 3 months, 2 weeks ago
On Wed, 11 Jun 2025 07:58:42 +0000, Dapeng Mi wrote:
> The intent of check_counters_many() is to verify all available counters
> can count correctly at the same time. So an alternative event should be
> picked to verify the avaialbe GP counter instead of skiping the counter
> if the initial event is not available.

Applied to kvm-x86 next, thanks!

[1/1] x86/pmu: Verify all available GP counters in check_counters_many()
      https://github.com/kvm-x86/kvm-unit-tests/commit/525bdb5d65d5

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next
Re: [kvm-unit-tests PATCH] x86/pmu: Verify all available GP counters in check_counters_many()
Posted by Sandipan Das 4 months ago
On 6/11/2025 1:28 PM, Dapeng Mi wrote:
> The intent of check_counters_many() is to verify all available counters
> can count correctly at the same time. So an alternative event should be
> picked to verify the avaialbe GP counter instead of skiping the counter
> if the initial event is not available.
> 
> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
> ---
> Please notice this patch is based on Sean's "x86: Add CPUID properties,
>  clean up related code" v2 patchset (https://lore.kernel.org/all/20250610195415.115404-1-seanjc@google.com/).
> 
>  x86/pmu.c | 22 +++++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
> 

For AMD processors
Tested-by: Sandipan Das <sandipan.das@amd.com>

> diff --git a/x86/pmu.c b/x86/pmu.c
> index 3987311c..a6b0cfcc 100644
> --- a/x86/pmu.c
> +++ b/x86/pmu.c
> @@ -457,18 +457,34 @@ static void check_fixed_counters(void)
>  	}
>  }
>  
> +static struct pmu_event *get_one_event(int idx)
> +{
> +	int i;
> +
> +	if (pmu_arch_event_is_available(idx))
> +		return &gp_events[idx % gp_events_size];
> +
> +	for (i = 0; i < gp_events_size; i++) {
> +		if (pmu_arch_event_is_available(i))
> +			return &gp_events[i];
> +	}
> +
> +	return NULL;
> +}
> +
>  static void check_counters_many(void)
>  {
> +	struct pmu_event *evt;
>  	pmu_counter_t cnt[48];
>  	int i, n;
>  
>  	for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) {
> -		if (!pmu_arch_event_is_available(i))
> +		evt = get_one_event(i);
> +		if (!evt)
>  			continue;
>  
>  		cnt[n].ctr = MSR_GP_COUNTERx(n);
> -		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
> -			gp_events[i % gp_events_size].unit_sel;
> +		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel;
>  		n++;
>  	}
>  	for (i = 0; i < fixed_counters_num; i++) {