linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
@ 2020-01-27 21:22 Eric Hankland
  2020-01-28 21:59 ` Jim Mattson
  2020-01-29 10:36 ` Paolo Bonzini
  0 siblings, 2 replies; 7+ messages in thread
From: Eric Hankland @ 2020-01-27 21:22 UTC (permalink / raw)
  To: Paolo Bonzini, Jim Mattson, Peter Shier; +Cc: linux-kernel, kvm, 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.

Signed-off-by: Eric Hankland <ehankland@google.com>
---
 arch/x86/kvm/vmx/pmu_intel.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 34a3a17bb6d7..9bdbe05b599c 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -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)


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
  2020-01-27 21:22 [PATCH] KVM: x86: Fix perfctr WRMSR for running counters Eric Hankland
@ 2020-01-28 21:59 ` Jim Mattson
  2020-01-29 10:36 ` Paolo Bonzini
  1 sibling, 0 replies; 7+ messages in thread
From: Jim Mattson @ 2020-01-28 21:59 UTC (permalink / raw)
  To: Eric Hankland; +Cc: Paolo Bonzini, Peter Shier, LKML, kvm list

On Mon, Jan 27, 2020 at 1:23 PM Eric Hankland <ehankland@google.com> wrote:
>
> 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.
>
> Signed-off-by: Eric Hankland <ehankland@google.com>

Fixes: 2924b52117b2 ("KVM: x86/pmu: do not mask the value that is
written to fixed PMUs")

Reviewed-by: Jim Mattson <jmattson@google.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
  2020-01-27 21:22 [PATCH] KVM: x86: Fix perfctr WRMSR for running counters Eric Hankland
  2020-01-28 21:59 ` Jim Mattson
@ 2020-01-29 10:36 ` Paolo Bonzini
  2020-01-30  1:09   ` Eric Hankland
  1 sibling, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2020-01-29 10:36 UTC (permalink / raw)
  To: Eric Hankland, Jim Mattson, Peter Shier; +Cc: linux-kernel, kvm

On 27/01/20 22:22, Eric Hankland wrote:
> 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.
> 
> Signed-off-by: Eric Hankland <ehankland@google.com>
> ---
>  arch/x86/kvm/vmx/pmu_intel.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
> index 34a3a17bb6d7..9bdbe05b599c 100644
> --- a/arch/x86/kvm/vmx/pmu_intel.c
> +++ b/arch/x86/kvm/vmx/pmu_intel.c
> @@ -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

>  			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)
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
  2020-01-29 10:36 ` Paolo Bonzini
@ 2020-01-30  1:09   ` Eric Hankland
  2020-02-01 18:51     ` Paolo Bonzini
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Hankland @ 2020-01-30  1:09 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Jim Mattson, Peter Shier, linux-kernel, kvm

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

> 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.

> 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?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
  2020-01-30  1:09   ` Eric Hankland
@ 2020-02-01 18:51     ` Paolo Bonzini
  2020-02-07 22:15       ` Eric Hankland
  0 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2020-02-01 18:51 UTC (permalink / raw)
  To: Eric Hankland; +Cc: Jim Mattson, Peter Shier, linux-kernel, kvm

On 30/01/20 02:09, Eric Hankland wrote:
> Sorry - I forgot to switch to plain text mode on my first reply.
> 
>> 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.
> 
>> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
  2020-02-01 18:51     ` Paolo Bonzini
@ 2020-02-07 22:15       ` Eric Hankland
  2020-02-12 12:03         ` Paolo Bonzini
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Hankland @ 2020-02-07 22:15 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Jim Mattson, Peter Shier, linux-kernel, kvm

> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] KVM: x86: Fix perfctr WRMSR for running counters
  2020-02-07 22:15       ` Eric Hankland
@ 2020-02-12 12:03         ` Paolo Bonzini
  0 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2020-02-12 12:03 UTC (permalink / raw)
  To: Eric Hankland; +Cc: Jim Mattson, Peter Shier, linux-kernel, kvm

On 07/02/20 23:15, Eric Hankland wrote:
>> 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-02-12 12:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-27 21:22 [PATCH] KVM: x86: Fix perfctr WRMSR for running counters Eric Hankland
2020-01-28 21:59 ` Jim Mattson
2020-01-29 10:36 ` Paolo Bonzini
2020-01-30  1:09   ` Eric Hankland
2020-02-01 18:51     ` Paolo Bonzini
2020-02-07 22:15       ` Eric Hankland
2020-02-12 12:03         ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).