kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: vPMU: Don't program counter for interrupt-based event sampling w/o lapic_in_kernel
@ 2021-10-22  9:17 Wanpeng Li
  2021-10-25 16:31 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Wanpeng Li @ 2021-10-22  9:17 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

From: Wanpeng Li <wanpengli@tencent.com>

vPMU depends on in-kernel lapic to deliver pmi interrupt, there is a
lot of overhead when creating/maintaining perf_event object, 
locking/unlocking perf_event_ctx etc for vPMU. It silently fails to 
deliver pmi interrupt if w/o in-kernel lapic currently. Let's not 
program counter for interrupt-based event sampling w/o in-kernel 
lapic support to avoid the whole bothering. 

Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/pmu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 0772bad9165c..fa5cd33af10d 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -179,6 +179,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
 	struct kvm_pmu_event_filter *filter;
 	int i;
 	bool allow_event = true;
+	bool intr = eventsel & ARCH_PERFMON_EVENTSEL_INT;
 
 	if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
 		printk_once("kvm pmu: pin control bit is ignored\n");
@@ -187,7 +188,8 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
 
 	pmc_pause_counter(pmc);
 
-	if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc))
+	if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc)
+	    || (intr && !lapic_in_kernel(pmc->vcpu)))
 		return;
 
 	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
@@ -233,7 +235,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
 	pmc_reprogram_counter(pmc, type, config,
 			      !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
 			      !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
-			      eventsel & ARCH_PERFMON_EVENTSEL_INT,
+			      intr,
 			      (eventsel & HSW_IN_TX),
 			      (eventsel & HSW_IN_TX_CHECKPOINTED));
 }
@@ -248,7 +250,7 @@ void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx)
 
 	pmc_pause_counter(pmc);
 
-	if (!en_field || !pmc_is_enabled(pmc))
+	if (!en_field || !pmc_is_enabled(pmc) || (pmi && !lapic_in_kernel(pmc->vcpu)))
 		return;
 
 	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
-- 
2.25.1


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

* Re: [PATCH] KVM: vPMU: Don't program counter for interrupt-based event sampling w/o lapic_in_kernel
  2021-10-22  9:17 [PATCH] KVM: vPMU: Don't program counter for interrupt-based event sampling w/o lapic_in_kernel Wanpeng Li
@ 2021-10-25 16:31 ` Sean Christopherson
  2021-10-25 16:44   ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2021-10-25 16:31 UTC (permalink / raw)
  To: Wanpeng Li
  Cc: linux-kernel, kvm, Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

On Fri, Oct 22, 2021, Wanpeng Li wrote:
> From: Wanpeng Li <wanpengli@tencent.com>
> 
> vPMU depends on in-kernel lapic to deliver pmi interrupt, there is a
> lot of overhead when creating/maintaining perf_event object, 
> locking/unlocking perf_event_ctx etc for vPMU. It silently fails to 
> deliver pmi interrupt if w/o in-kernel lapic currently. Let's not 
> program counter for interrupt-based event sampling w/o in-kernel 
> lapic support to avoid the whole bothering. 

This feels all kinds of wrong.  AFAIK, there's no way for KVM to enumerate to
the guest that the vPMU isn't capable of generating interrupts.  I.e. any setup
that exposes a vPMU to the guest without an in-kernel local APIC is either
inherently broken or requires a paravirtualized guest.  I don't think KVM's bugs
should be optimized.

> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> ---
>  arch/x86/kvm/pmu.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
> index 0772bad9165c..fa5cd33af10d 100644
> --- a/arch/x86/kvm/pmu.c
> +++ b/arch/x86/kvm/pmu.c
> @@ -179,6 +179,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
>  	struct kvm_pmu_event_filter *filter;
>  	int i;
>  	bool allow_event = true;
> +	bool intr = eventsel & ARCH_PERFMON_EVENTSEL_INT;
>  
>  	if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
>  		printk_once("kvm pmu: pin control bit is ignored\n");
> @@ -187,7 +188,8 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
>  
>  	pmc_pause_counter(pmc);
>  
> -	if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc))
> +	if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc)
> +	    || (intr && !lapic_in_kernel(pmc->vcpu)))
>  		return;
>  
>  	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
> @@ -233,7 +235,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
>  	pmc_reprogram_counter(pmc, type, config,
>  			      !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
>  			      !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
> -			      eventsel & ARCH_PERFMON_EVENTSEL_INT,
> +			      intr,
>  			      (eventsel & HSW_IN_TX),
>  			      (eventsel & HSW_IN_TX_CHECKPOINTED));
>  }
> @@ -248,7 +250,7 @@ void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx)
>  
>  	pmc_pause_counter(pmc);
>  
> -	if (!en_field || !pmc_is_enabled(pmc))
> +	if (!en_field || !pmc_is_enabled(pmc) || (pmi && !lapic_in_kernel(pmc->vcpu)))
>  		return;
>  
>  	filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
> -- 
> 2.25.1
> 

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

* Re: [PATCH] KVM: vPMU: Don't program counter for interrupt-based event sampling w/o lapic_in_kernel
  2021-10-25 16:31 ` Sean Christopherson
@ 2021-10-25 16:44   ` Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2021-10-25 16:44 UTC (permalink / raw)
  To: Sean Christopherson, Wanpeng Li
  Cc: linux-kernel, kvm, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel

On 25/10/21 18:31, Sean Christopherson wrote:
>> vPMU depends on in-kernel lapic to deliver pmi interrupt, there is a
>> lot of overhead when creating/maintaining perf_event object,
>> locking/unlocking perf_event_ctx etc for vPMU. It silently fails to
>> deliver pmi interrupt if w/o in-kernel lapic currently. Let's not
>> program counter for interrupt-based event sampling w/o in-kernel
>> lapic support to avoid the whole bothering.
>
> This feels all kinds of wrong.  AFAIK, there's no way for KVM to enumerate to
> the guest that the vPMU isn't capable of generating interrupts.  I.e. any setup
> that exposes a vPMU to the guest without an in-kernel local APIC is either
> inherently broken or requires a paravirtualized guest.  I don't think KVM's bugs
> should be optimized.

Yeah, if it simplified the code it would be a different story, but here 
there's even not one but two new checks.

Paolo


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

end of thread, other threads:[~2021-10-25 16:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-22  9:17 [PATCH] KVM: vPMU: Don't program counter for interrupt-based event sampling w/o lapic_in_kernel Wanpeng Li
2021-10-25 16:31 ` Sean Christopherson
2021-10-25 16:44   ` 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).