Skip to contentSkip to conversation

KVM: x86: Fix perfctr WRMSR for running counters

PATCH In review Eric Hankland 3 participants 7 messages Jan 27, 2020 – Feb 12, 2020

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

PATCH 1/1 KVM: x86: Fix perfctr WRMSR for running counters

Correct the logic in intel_pmu_set_msr() for fixed and general purpose counters. This was recently changed to set pmc->counter without taking in to account the value of pmc_read_counter() which will be incorrect if the counter is currently running and non-zero; this changes back to the old logic which accounted for the value of currently running counters.

---
 arch/x86/kvm/vmx/pmu_intel.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +2 -1
index 34a3a17bb6d7..9bdbe05b599c 100644
@@ -264,9 +264,10 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 				pmc->counter = data;
 			else
 				pmc->counter = (s32)data;
+			pmc->counter += pmc->counter - pmc_read_counter(pmc);
 			return 0;
 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
-			pmc->counter = data;
+			pmc->counter += data - pmc_read_counter(pmc);
 			return 0;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
 			if (data == pmc->eventsel)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Eric Hankland

Correct the logic in intel_pmu_set_msr() for fixed and general purpose counters. This was recently changed to set pmc->counter without taking in to account the value of pmc_read_counter() which will be incorrect if the counter is currently running and non-zero; this changes back to the old logic which accounted for the value of currently running counters.

Fixes 2924b52117b2 ("KVM: x86/pmu: do not mask the value that is

written to fixed PMUs")

maintainer
#2 lore raw
In reply to Eric Hankland
Quoted 16 lines · Eric Hankland

Correct the logic in intel_pmu_set_msr() for fixed and general purpose counters. This was recently changed to set pmc->counter without taking in to account the value of pmc_read_counter() which will be incorrect if the counter is currently running and non-zero; this changes back to the old logic which accounted for the value of currently running counters.

---
 arch/x86/kvm/vmx/pmu_intel.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index 34a3a17bb6d7..9bdbe05b599c 100644

arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 267

Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -0
@@ -264,9 +264,10 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 				pmc->counter = data;
 			else
 				pmc->counter = (s32)data;
+			pmc->counter += pmc->counter - pmc_read_counter(pmc);

I think this best written as it was before commit 2924b52117:

			if (!msr_info->host_initiated)
				data = (s64)(s32)data;
			pmc->counter += data - pmc_read_counter(pmc);

Do you have a testcase?

Paolo

Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
 			return 0;
 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
-			pmc->counter = data;
+			pmc->counter += data - pmc_read_counter(pmc);
 			return 0;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
 			if (data == pmc->eventsel)
#3 lore raw

Sorry - I forgot to switch to plain text mode on my first reply.

Quoted 5 lines

I think this best written as it was before commit 2924b52117:

                        if (!msr_info->host_initiated)
                                data = (s64)(s32)data;
                        pmc->counter += data - pmc_read_counter(pmc);

Sounds good to me.

Quoted 1 line

Do you have a testcase?

I added a testcase to kvm-unit-tests/x86/pmu.c that fails without this patch and passes with it.
Should I send out that patch now?

maintainer
#4 lore raw
Quoted 4 lines · Eric Hankland

Sorry - I forgot to switch to plain text mode on my first reply.

Quoted 2 lines

I think this best written as it was before commit 2924b52117:

Quoted 8 lines
Quoted 3 lines
                        if (!msr_info->host_initiated)
                                data = (s64)(s32)data;
                        pmc->counter += data - pmc_read_counter(pmc);

Sounds good to me.

Quoted 1 line

Do you have a testcase?

I added a testcase to kvm-unit-tests/x86/pmu.c that fails without this patch and passes with it. Should I send out that patch now?

Yes, please send it! Thanks,

Paolo

#5 lore raw
Quoted 1 line

Yes, please send it! Thanks,

I sent out the test a couple days ago and you queued it (commit b9624f3f34bd "Test WRMSR on a running counter").
Are there any other changes that I should make?

Thanks,
Eric

maintainer
#6 lore raw
Quoted 5 lines · Eric Hankland
Quoted 1 line

Yes, please send it! Thanks,

I sent out the test a couple days ago and you queued it (commit b9624f3f34bd "Test WRMSR on a running counter").
Are there any other changes that I should make?

Nope, thanks!

Paolo