Skip to contentSkip to conversation

KVM: x86/pmu: Don't retry a counter whose config was rejected

PATCH Applied Luka Absandze 4 participants 5 messages Sep 20 – Oct 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: x86/pmu: Don't retry a counter whose config was rejected

kvm_pmu_handle_event() re-arms the reprogram bit for every failed reprogram, on the assumption that the failure is transient and a later refresh will succeed. That is true for contention, e.g. the -EBUSY from x86_reserve_hardware(), but not for a configuration the host PMU driver rejects outright. A rejected config can never succeed on retry, so the counter is reprogrammed on every PMU refresh for as long as the guest leaves it enabled, and every attempt fails the same way.

Re-arm only for the errnos that indicate a transient condition, i.e. -EBUSY and -ENOMEM.

---
v2:
- Use an allow-list of transient errnos (-EBUSY, -ENOMEM) rather than excluding -EINVAL, and hoist 'r' out of the loop. Diff taken from Sean's suggestion verbatim. [Sean]
- Reword the comment to say "fails on a transient condition". [Sean]
- v1: https://lore.kernel.org/all/[email protected]/

arch/x86/kvm/pmu.c +8 -6
index a7d60c8785cd..03a470c49a74 100644
@@ -662,7 +662,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 	DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct kvm_pmc *pmc;
-	int bit;
+	int bit, r;
 
 	bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX);
 
@@ -676,12 +676,14 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 
 	kvm_for_each_pmc(pmu, pmc, bit, bitmap) {
 		/*
-		 * If reprogramming fails, e.g. due to contention, re-set the
-		 * reprogram bit, i.e. opportunistically try again on the next
-		 * PMU refresh.  Don't make a new request as doing so can stall
-		 * the guest if reprogramming repeatedly fails.
+		 * If reprogramming fails on a transient condition, e.g. due to
+		 * contention, re-set the reprogram bit, i.e. opportunistically
+		 * try again on the next PMU refresh.  Don't make a new request
+		 * as doing so can stall the guest if reprogramming repeatedly
+		 * fails.
 		 */
-		if (reprogram_counter(pmc))
+		r = reprogram_counter(pmc);
+		if (r == -EBUSY || r == -ENOMEM)
 			set_bit(pmc->idx, pmu->reprogram_pmi);
 	}
 
Signature · 1 line
2.47.3
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

kvm_pmu_handle_event() re-arms the reprogram bit for every failed reprogram, on the assumption that the failure is transient and a later refresh will succeed. That is true for contention, e.g. the -EBUSY from x86_reserve_hardware(), but not for a configuration the host PMU driver rejects outright. A rejected config can never succeed on retry, so the counter is reprogrammed on every PMU refresh for as long as the guest leaves it enabled, and every attempt fails the same way.

Quoted 6 lines

Re-arm only for the errnos that indicate a transient condition, i.e. -EBUSY and -ENOMEM.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v2 1/1
#2 lore raw
In reply to Luka Absandze
Quoted 45 lines · Luka Absandze

kvm_pmu_handle_event() re-arms the reprogram bit for every failed reprogram, on the assumption that the failure is transient and a later refresh will succeed. That is true for contention, e.g. the -EBUSY from x86_reserve_hardware(), but not for a configuration the host PMU driver rejects outright. A rejected config can never succeed on retry, so the counter is reprogrammed on every PMU refresh for as long as the guest leaves it enabled, and every attempt fails the same way.

Re-arm only for the errnos that indicate a transient condition, i.e. -EBUSY and -ENOMEM.

---
v2:
- Use an allow-list of transient errnos (-EBUSY, -ENOMEM) rather than excluding -EINVAL, and hoist 'r' out of the loop. Diff taken from Sean's suggestion verbatim. [Sean]
- Reword the comment to say "fails on a transient condition". [Sean]
- v1: https://lore.kernel.org/all/[email protected]/

arch/x86/kvm/pmu.c +3 -5
index a7d60c8785cd..03a470c49a74 100644
@@ -662,7 +662,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 	DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct kvm_pmc *pmc;
-	int bit;
+	int bit, r;
 
 	bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX);
 
@@ -676,12 +676,14 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 
 	kvm_for_each_pmc(pmu, pmc, bit, bitmap) {
 		/*
-		 * If reprogramming fails, e.g. due to contention, re-set the
-		 * reprogram bit, i.e. opportunistically try again on the next
-		 * PMU refresh.  Don't make a new request as doing so can stall
-		 * the guest if reprogramming repeatedly fails.
+		 * If reprogramming fails on a transient condition, e.g. due to
+		 * contention, re-set the reprogram bit, i.e. opportunistically

arch/x86/kvm/pmu.c void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) · line 689

Quoted 10 lines
arch/x86/kvm/pmu.c +5 -1
+		 * try again on the next PMU refresh.  Don't make a new request
+		 * as doing so can stall the guest if reprogramming repeatedly
+		 * fails.
 		 */
-		if (reprogram_counter(pmc))
+		r = reprogram_counter(pmc);
+		if (r == -EBUSY || r == -ENOMEM)
 			set_bit(pmc->idx, pmu->reprogram_pmi);
 	}
 
Reviewed-by on v2 1/1
#3 lore raw
In reply to Luka Absandze
Quoted 45 lines · Luka Absandze

kvm_pmu_handle_event() re-arms the reprogram bit for every failed reprogram, on the assumption that the failure is transient and a later refresh will succeed. That is true for contention, e.g. the -EBUSY from x86_reserve_hardware(), but not for a configuration the host PMU driver rejects outright. A rejected config can never succeed on retry, so the counter is reprogrammed on every PMU refresh for as long as the guest leaves it enabled, and every attempt fails the same way.

Re-arm only for the errnos that indicate a transient condition, i.e. -EBUSY and -ENOMEM.

---
v2:
- Use an allow-list of transient errnos (-EBUSY, -ENOMEM) rather than excluding -EINVAL, and hoist 'r' out of the loop. Diff taken from Sean's suggestion verbatim. [Sean]
- Reword the comment to say "fails on a transient condition". [Sean]
- v1: https://lore.kernel.org/all/[email protected]/

arch/x86/kvm/pmu.c +3 -5
index a7d60c8785cd..03a470c49a74 100644
@@ -662,7 +662,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 	DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct kvm_pmc *pmc;
-	int bit;
+	int bit, r;
 
 	bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX);
 
@@ -676,12 +676,14 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
 
 	kvm_for_each_pmc(pmu, pmc, bit, bitmap) {
 		/*
-		 * If reprogramming fails, e.g. due to contention, re-set the
-		 * reprogram bit, i.e. opportunistically try again on the next
-		 * PMU refresh.  Don't make a new request as doing so can stall
-		 * the guest if reprogramming repeatedly fails.
+		 * If reprogramming fails on a transient condition, e.g. due to
+		 * contention, re-set the reprogram bit, i.e. opportunistically

arch/x86/kvm/pmu.c void kvm_pmu_handle_event(struct kvm_vcpu *vcpu) · line 689

Quoted 10 lines
arch/x86/kvm/pmu.c +5 -1
+		 * try again on the next PMU refresh.  Don't make a new request
+		 * as doing so can stall the guest if reprogramming repeatedly
+		 * fails.
 		 */
-		if (reprogram_counter(pmc))
+		r = reprogram_counter(pmc);
+		if (r == -EBUSY || r == -ENOMEM)
 			set_bit(pmc->idx, pmu->reprogram_pmi);
 	}
 
maintainer Applied
#4 lore raw
In reply to Luka Absandze
Quoted 9 lines · Luka Absandze

kvm_pmu_handle_event() re-arms the reprogram bit for every failed reprogram, on the assumption that the failure is transient and a later refresh will succeed. That is true for contention, e.g. the -EBUSY from x86_reserve_hardware(), but not for a configuration the host PMU driver rejects outright. A rejected config can never succeed on retry, so the counter is reprogrammed on every PMU refresh for as long as the guest leaves it enabled, and every attempt fails the same way.

[...]

Applied to kvm-x86 misc, thanks!

[1/1] KVM: x86/pmu: Don't retry a counter whose config was rejected

      https://github.com/kvm-x86/linux/commit/1c24893fdb4d

--
https://github.com/kvm-x86/linux/tree/next