Skip to contentSkip to conversation

KVM: x86/pmu: Avoid ternary operator by directly referring to counters->type

PATCH In review Like Xu 2 participants 6 messages Dec 5, 2022 – Dec 7, 2022

Subsystems (3)
lore
Share & tools
lore

Like Xu

author
#0 lore raw

PATCH 1/1 KVM: x86/pmu: Avoid ternary operator by directly referring to counters->type

From: Like Xu <[email protected]>

In either case, the counters will point to fixed or gp pmc array, and taking advantage of the C pointer, it's reasonable to use an almost known mem load operation directly without disturbing the branch predictor.

---
 arch/x86/kvm/vmx/pmu_intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index e5cec07ca8d9..28b0a784f6e9 100644
@@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 	}
 	if (idx >= num_counters)
 		return NULL;
-	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
+	*mask &= pmu->counter_bitmask[counters->type];
 	return &counters[array_index_nospec(idx, num_counters)];
 }
 
Signature · 1 line
2.38.1
#1 lore raw
Quoted 5 lines · Like Xu

From: Like Xu <[email protected]>

In either case, the counters will point to fixed or gp pmc array, and taking advantage of the C pointer, it's reasonable to use an almost known mem load operation directly without disturbing the branch predictor.

The compiler is extremely unlikely to generate a branch for this, e.g. gcc-12 uses setne and clang-14 shifts "fixed" by 30. FWIW, clang is also clever enough to use a cmov to load the address of counters, i.e. the happy path will have no taken branches for either type of counter.

Quoted 10 lines
---
 arch/x86/kvm/vmx/pmu_intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index e5cec07ca8d9..28b0a784f6e9 100644

arch/x86/kvm/vmx/pmu_intel.c static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, · line 145

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 	}
 	if (idx >= num_counters)
 		return NULL;
-	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
+	*mask &= pmu->counter_bitmask[counters->type];

In terms of readability, I have a slight preference for the current code as I don't have to look at counters->type to understand its possible values.

author
#2 lore raw
Quoted 11 lines · Sean Christopherson
Quoted 5 lines · Like Xu

From: Like Xu <[email protected]>

In either case, the counters will point to fixed or gp pmc array, and taking advantage of the C pointer, it's reasonable to use an almost known mem load operation directly without disturbing the branch predictor.

The compiler is extremely unlikely to generate a branch for this, e.g. gcc-12 uses setne and clang-14 shifts "fixed" by 30. FWIW, clang is also clever enough to use a cmov to load the address of counters, i.e. the happy path will have no taken branches for either type of counter.

If so, good news for users of the new tool chain. I assume our Linux project is also to be commended when it comes to supporting legacy issues even if just a little.

Quoted 6 lines
Quoted 6 lines
---
  arch/x86/kvm/vmx/pmu_intel.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

arch/x86/kvm/vmx/pmu_intel.c static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, · line 145

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index e5cec07ca8d9..28b0a784f6e9 100644
@@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
  	}
  	if (idx >= num_counters)
  		return NULL;
-	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
+	*mask &= pmu->counter_bitmask[counters->type];

In terms of readability, I have a slight preference for the current code as I don't have to look at counters->type to understand its possible values.

When someone tries to add a new type of pmc type, the code bugs up. And, this one will make all usage of pmu->counter_bitmask[] more consistent.

Please reconsider this minor diff if it does no harm.

#3 lore raw
Quoted 13 lines · Like Xu
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index e5cec07ca8d9..28b0a784f6e9 100644
@@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
Quoted 8 lines

arch/x86/kvm/vmx/pmu_intel.c static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, · line 145

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
  	}
  	if (idx >= num_counters)
  		return NULL;
-	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
+	*mask &= pmu->counter_bitmask[counters->type];

In terms of readability, I have a slight preference for the current code as I don't have to look at counters->type to understand its possible values.

Quoted 1 line

When someone tries to add a new type of pmc type, the code bugs up.

Are there new types coming along? If so, I definitely would not object to refactoring this code in the context of a series that adds a new type(s). But "fixing" this one case is not sufficient to support a new type, e.g. intel_is_valid_rdpmc_ecx() also needs to be updated. Actually, even this function would need additional updates to perform a similar sanity check.

	if (fixed) {
		counters = pmu->fixed_counters;
		num_counters = pmu->nr_arch_fixed_counters;
	} else {
		counters = pmu->gp_counters;
		num_counters = pmu->nr_arch_gp_counters;
	}
	if (idx >= num_counters)
		return NULL;
Quoted 1 line

And, this one will make all usage of pmu->counter_bitmask[] more consistent.

How's that? There's literally one instance of using ->type

static inline u64 pmc_bitmask(struct kvm_pmc *pmc)

  {
	struct kvm_pmu *pmu = pmc_to_pmu(pmc);

	return pmu->counter_bitmask[pmc->type];

}

everything else is hardcoded. And using pmc->type there make perfect sense in that case. But in intel_rdpmc_ecx_to_pmc(), there is already usage of "fixed", so IMO switching to ->type makes that function somewhat inconsistent with itself.

author
#4 lore raw
Quoted 13 lines · Sean Christopherson
Quoted 4 lines · Like Xu
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index e5cec07ca8d9..28b0a784f6e9 100644
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx/pmu_intel.c static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, · line 145

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
   	}
   	if (idx >= num_counters)
   		return NULL;
-	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
+	*mask &= pmu->counter_bitmask[counters->type];

In terms of readability, I have a slight preference for the current code as I

IMO, using counters->type directly just like pmc_bitmask() will add more readability and opportunistically helps some stale compilers behave better.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

don't have to look at counters->type to understand its possible values.

When someone tries to add a new type of pmc type, the code bugs up.

Are there new types coming along? If so, I definitely would not object to refactoring this code in the context of a series that adds a new type(s). But "fixing" this one case is not sufficient to support a new type, e.g. intel_is_valid_rdpmc_ecx() also needs to be updated. Actually, even this function would need additional updates to perform a similar sanity check.

True but this part of the change is semantically relevant, which should not be present in a harmless generic optimization like this one. Right ?

Quoted 22 lines
	if (fixed) {
		counters = pmu->fixed_counters;
		num_counters = pmu->nr_arch_fixed_counters;
	} else {
		counters = pmu->gp_counters;
		num_counters = pmu->nr_arch_gp_counters;
	}
	if (idx >= num_counters)
		return NULL;
Quoted 1 line

And, this one will make all usage of pmu->counter_bitmask[] more consistent.

How's that? There's literally one instance of using ->type

   static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
   {
	struct kvm_pmu *pmu = pmc_to_pmu(pmc);

	return pmu->counter_bitmask[pmc->type];
   }
Quoted 3 lines

everything else is hardcoded. And using pmc->type there make perfect sense in that case. But in intel_rdpmc_ecx_to_pmc(), there is already usage of "fixed", so IMO switching to ->type makes that function somewhat inconsistent with itself.

More, it's rare to see code like " [ a ? b : c] " in the world of both KVM and x86. Good practice (branchless) should be scattered everywhere and not the other way around.

I have absolutely no objection to your "slight preference". Thanks for your time in reviewing this.

#5 lore raw
Quoted 15 lines · Like Xu
Quoted 13 lines · Sean Christopherson
Quoted 4 lines · Like Xu
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index e5cec07ca8d9..28b0a784f6e9 100644
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/vmx/pmu_intel.c static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, · line 145

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
   	}
   	if (idx >= num_counters)
   		return NULL;
-	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
+	*mask &= pmu->counter_bitmask[counters->type];

In terms of readability, I have a slight preference for the current code as I

Quoted 2 lines

IMO, using counters->type directly just like pmc_bitmask() will add more readability and opportunistically helps some stale compilers behave better.

Anyone that cares about this level of micro-optimization absolutely should be using a toolchain that's at or near the bleeding edge.

Quoted 11 lines
Quoted 8 lines
Quoted 2 lines
Quoted 1 line

don't have to look at counters->type to understand its possible values.

When someone tries to add a new type of pmc type, the code bugs up.

Are there new types coming along? If so, I definitely would not object to refactoring this code in the context of a series that adds a new type(s). But "fixing" this one case is not sufficient to support a new type, e.g. intel_is_valid_rdpmc_ecx() also needs to be updated. Actually, even this function would need additional updates to perform a similar sanity check.

True but this part of the change is semantically relevant, which should not be present in a harmless generic optimization like this one. Right ?

For modern compilers, it's not an optimization.

Quoted 18 lines
Quoted 18 lines
	if (fixed) {
		counters = pmu->fixed_counters;
		num_counters = pmu->nr_arch_fixed_counters;
	} else {
		counters = pmu->gp_counters;
		num_counters = pmu->nr_arch_gp_counters;
	}
	if (idx >= num_counters)
		return NULL;
Quoted 1 line

And, this one will make all usage of pmu->counter_bitmask[] more consistent.

How's that? There's literally one instance of using ->type

   static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
   {
	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Quoted 8 lines
Quoted 6 lines
	return pmu->counter_bitmask[pmc->type];

}

everything else is hardcoded. And using pmc->type there make perfect sense in that case. But in intel_rdpmc_ecx_to_pmc(), there is already usage of "fixed", so IMO switching to ->type makes that function somewhat inconsistent with itself.

More, it's rare to see code like " [ a ? b : c] " in the world of both KVM and x86.

There are a few false positives here, but ternary operators are common.

  $ git grep ? arch/x86/kvm | wc -l
  292

If you're saying that indexing an array with a ternary operator is rare, then sure, but only because there is almost never anything that fits such a pattern, not because it's an inherently bad pattern.

Quoted 2 lines

Good practice (branchless) should be scattered everywhere and not the other way around.

Once again, modern compilers will not generate branches for this code.