public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Mingwei Zhang <mizhang@google.com>,
	Roman Kagan <rkagan@amazon.de>, Jim Mattson <jmattson@google.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Like Xu <like.xu.linux@gmail.com>
Subject: Re: [PATCH 6/6] KVM: x86/pmu: Track emulated counter events instead of previous counter
Date: Fri, 27 Oct 2023 13:55:11 -0700	[thread overview]
Message-ID: <ZTwjrxDodKVMK3PI@google.com> (raw)
In-Reply-To: <20231023234000.2499267-7-seanjc@google.com>

On Mon, Oct 23, 2023, Sean Christopherson wrote:
> @@ -226,13 +226,19 @@ static int pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, u64 config,
>  
>  static void pmc_pause_counter(struct kvm_pmc *pmc)
>  {
> -	u64 counter = pmc->counter;
> +	/*
> +	 * Accumulate emulated events, even if the PMC was already paused, e.g.
> +	 * if KVM emulated an event after a WRMSR, but before reprogramming, or
> +	 * if KVM couldn't create a perf event.
> +	 */
> +	u64 counter = pmc->counter + pmc->emulated_counter;
>  
> -	if (!pmc->perf_event || pmc->is_paused)
> -		return;
> +	pmc->emulated_counter = 0;

As pointed by Mingwei, who _very_ patiently explained to me what is broken, the
snapshot used to detect overflow due to emulated_counter events needs to be taken
_after_ pausing the perf event, i.e. the count from the perf event needs to be
excluded.  If overflow happens from pmc->counter => pmc->counter + pmc->perf_event,
then hardware (via perf) will detect overflow.  I.e. KVM is only responsible for
detecting overflow solely due to emulated_counter.  Include the count from the
perf event can lead to KVM generating multiple overflow events, where architecturally
only one should occur.

>  	/* update counter, reset event value to avoid redundant accumulation */
> -	counter += perf_event_pause(pmc->perf_event, true);
> +	if (pmc->perf_event && !pmc->is_paused)
> +		counter += perf_event_pause(pmc->perf_event, true);
> +
>  	pmc->counter = counter & pmc_bitmask(pmc);
>  	pmc->is_paused = true;
>  }

  reply	other threads:[~2023-10-27 20:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23 23:39 [PATCH 0/6] KVM: x86/pmu: Clean up emulated PMC event handling Sean Christopherson
2023-10-23 23:39 ` [PATCH 1/6] KVM: x86/pmu: Move PMU reset logic to common x86 code Sean Christopherson
2023-10-23 23:39 ` [PATCH 2/6] KVM: x86/pmu: Reset the PMU, i.e. stop counters, before refreshing Sean Christopherson
2023-10-23 23:39 ` [PATCH 3/6] KVM: x86/pmu: Stop calling kvm_pmu_reset() at RESET (it's redundant) Sean Christopherson
2023-10-23 23:39 ` [PATCH 4/6] KVM: x86/pmu: Remove manual clearing of fields in kvm_pmu_init() Sean Christopherson
2023-10-23 23:39 ` [PATCH 5/6] KVM: x86/pmu: Update sample period in pmc_write_counter() Sean Christopherson
2023-10-23 23:40 ` [PATCH 6/6] KVM: x86/pmu: Track emulated counter events instead of previous counter Sean Christopherson
2023-10-27 20:55   ` Sean Christopherson [this message]
2023-11-01  9:37 ` [PATCH 0/6] KVM: x86/pmu: Clean up emulated PMC event handling Mi, Dapeng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZTwjrxDodKVMK3PI@google.com \
    --to=seanjc@google.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=like.xu.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mizhang@google.com \
    --cc=pbonzini@redhat.com \
    --cc=rkagan@amazon.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox