diff mbox series

[1/2] KVM: x86/pmu: Prevent the PMU from counting disallowed events

Message ID 20221209194957.2774423-2-aaronlewis@google.com (mailing list archive)
State New, archived
Headers show
Series Fix "Instructions Retired" from incorrectly counting | expand

Commit Message

Aaron Lewis Dec. 9, 2022, 7:49 p.m. UTC
When counting "Instructions Retired" (0xc0) in a guest, KVM will
occasionally increment the PMU counter regardless of if that event is
being filtered. This is because some PMU events are incremented via
kvm_pmu_trigger_event(), which doesn't know about the event filter. Add
the event filter to kvm_pmu_trigger_event(), so events that are
disallowed do not increment their counters.

Fixes: 9cd803d496e7 ("KVM: x86: Update vPMCs when retiring instructions")
Signed-off-by: Aaron Lewis <aaronlewis@google.com>
---
 arch/x86/kvm/pmu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Sean Christopherson Jan. 4, 2023, 5:20 p.m. UTC | #1
On Fri, Dec 09, 2022, Aaron Lewis wrote:
> When counting "Instructions Retired" (0xc0) in a guest, KVM will
> occasionally increment the PMU counter regardless of if that event is
> being filtered. This is because some PMU events are incremented via
> kvm_pmu_trigger_event(), which doesn't know about the event filter. Add
> the event filter to kvm_pmu_trigger_event(), so events that are
> disallowed do not increment their counters.
> 
> Fixes: 9cd803d496e7 ("KVM: x86: Update vPMCs when retiring instructions")
> Signed-off-by: Aaron Lewis <aaronlewis@google.com>
> ---
>  arch/x86/kvm/pmu.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
> index 684393c22105..b87cf35a38b7 100644
> --- a/arch/x86/kvm/pmu.c
> +++ b/arch/x86/kvm/pmu.c
> @@ -581,7 +581,9 @@ void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id)
>  	for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) {
>  		pmc = static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, i);
>  
> -		if (!pmc || !pmc_is_enabled(pmc) || !pmc_speculative_in_use(pmc))
> +		if (!pmc || !pmc_is_enabled(pmc) ||
> +		    !pmc_speculative_in_use(pmc) ||
> +		    !check_pmu_event_filter(pmc))

reprogram_counter() has the same three checks, seems like we should combine them
into a common helper.  No idea what to call it though.  Maybe?

		if (!pmc || !pmc_is_fully_enabled(pmc))

>  			continue;
>  
>  		/* Ignore checks for edge detect, pin control, invert and CMASK bits */
> -- 
> 2.39.0.rc1.256.g54fd8350bd-goog
>
diff mbox series

Patch

diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 684393c22105..b87cf35a38b7 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -581,7 +581,9 @@  void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, u64 perf_hw_id)
 	for_each_set_bit(i, pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX) {
 		pmc = static_call(kvm_x86_pmu_pmc_idx_to_pmc)(pmu, i);
 
-		if (!pmc || !pmc_is_enabled(pmc) || !pmc_speculative_in_use(pmc))
+		if (!pmc || !pmc_is_enabled(pmc) ||
+		    !pmc_speculative_in_use(pmc) ||
+		    !check_pmu_event_filter(pmc))
 			continue;
 
 		/* Ignore checks for edge detect, pin control, invert and CMASK bits */