After passthrough the LBR msrs to the guest, there is no need to call intel_pmu_intercept_lbr_msrs() again and again, and vice versa. If host reclaims the LBR between two availability checks, the interception state and LBR records can be safely preserved due to native save/restore support from guest LBR event. Signed-off-by: Like Xu <like.xu@xxxxxxxxxxxxxxx> --- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 57b281c4b196..dd51250c5688 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -511,6 +511,9 @@ struct kvm_pmu { * The records may be inaccurate if the host reclaims the LBR. */ struct perf_event *lbr_event; + + /* A flag to reduce the overhead of LBR pass-through or cancellation. */ + bool lbr_already_available; }; struct kvm_pmu_ops; diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index d0dece055605..583ecb5f5f7c 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -437,6 +437,7 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu) init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn); pmu->event_count = 0; pmu->need_cleanup = false; + pmu->lbr_already_available = false; kvm_pmu_refresh(vcpu); } diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index db185dca903d..408d80c9418b 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -316,6 +316,7 @@ static void intel_pmu_free_lbr_event(struct kvm_vcpu *vcpu) perf_event_release_kernel(event); intel_pmu_intercept_lbr_msrs(vcpu, true); + pmu->lbr_already_available = false; pmu->event_count--; pmu->lbr_event = NULL; } @@ -653,10 +654,18 @@ static bool intel_pmu_lbr_is_availabile(struct kvm_vcpu *vcpu) if (!pmu->lbr_event) return false; + if (pmu->lbr_already_available && event_is_oncpu(pmu->lbr_event)) + return true; + + if (!pmu->lbr_already_available && !event_is_oncpu(pmu->lbr_event)) + return false; + if (event_is_oncpu(pmu->lbr_event)) { intel_pmu_intercept_lbr_msrs(vcpu, false); + pmu->lbr_already_available = true; } else { intel_pmu_intercept_lbr_msrs(vcpu, true); + pmu->lbr_already_available = false; return false; } -- 2.21.3