Re-apply the LBR MSR intercepts in vmx_recalc_msr_intercepts() if the LBRs are currently passed through, so that changes to userspace MSR filters are reflected in the MSR bitmap. Unlike SVM, which can simply rewrite the intercepts in either direction, leave the bitmap alone if the LBRs aren't passed through, as a filter can only force interception, never prevent it, i.e. the bitmap is already correct. Note, clearing msr_passthrough to force vmx_passthrough_lbr_msrs() to re-apply the intercepts on the next VM-Enter isn't an option, as doing so would leave stale passthrough bits behind if the guest LBR event is no longer active. vmx_recalc_msr_intercepts() is invoked in response to KVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates, but it skips the LBR MSRs, and vmx_enable_lbr_msrs_passthrough() does nothing if lbr_desc->msr_passthrough is already set. If the LBR MSRs are passed through when userspace installs a filter that denies them, they remain passed through, and the guest can access them without exiting to userspace. Conversely, if a filter forced interception when the guest LBR event became active, relaxing the filter leaves the MSRs needlessly intercepted. Either way, the bitmap remains stale until the guest LBR event is torn down and recreated. Only x2APIC MSRs are exempt from userspace filtering (see kvm_msr_allowed() and the KVM_X86_SET_MSR_FILTER documentation), and the filter is honored when LBR passthrough is first enabled, so KVM's behavior shouldn't depend on whether the filter is set before or after the guest enables LBRs. Drop the LBR MSRs from the comment claiming that they can't be filtered by userspace, and clarify that lbr_desc.msr_passthrough tracks KVM's desired interception, which may not match the MSR bitmap when userspace filters are in play. Fixes: 9254beaafd12 ("KVM: vmx/pmu: Reduce the overhead of LBR pass-through or cancellation") Cc: stable@vger.kernel.org Signed-off-by: Jim Mattson --- arch/x86/kvm/vmx/pmu_intel.c | 20 +++++++++++++++++++- arch/x86/kvm/vmx/pmu_intel.h | 7 ++++++- arch/x86/kvm/vmx/vmx.c | 6 ++++-- arch/x86/kvm/vmx/vmx.h | 1 + 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 70a8c4816135..b945213f0b68 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -517,7 +517,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) memset(&lbr_desc->records, 0, sizeof(lbr_desc->records)); /* - * Setting passthrough of LBR MSRs is done only in the VM-Entry loop, + * LBR MSRs are initially passed through only in the VM-Entry loop (a + * full MSR intercept recalc merely refreshes an existing passthrough), * and PMU refresh is disallowed after the vCPU has run, i.e. this code * should never be reached while KVM is passing through MSRs. */ @@ -704,6 +705,23 @@ static inline void vmx_enable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu) lbr_desc->msr_passthrough = true; } +/* + * Refresh the LBR MSR intercepts if the LBRs are passed through, so that + * changes to userspace MSR filters are reflected in the MSR bitmap. There's + * nothing to do if the LBRs are intercepted, as a filter can only force + * interception, never prevent it. + */ +void vmx_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) +{ + struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); + + if (WARN_ON_ONCE(!lbr_desc)) + return; + + if (lbr_desc->msr_passthrough) + vmx_update_intercept_for_lbr_msrs(vcpu, false); +} + /* * Higher priority host perf events (e.g. cpu pinned) could reclaim the * pmu resources (e.g. LBR) that were assigned to the guest. This is diff --git a/arch/x86/kvm/vmx/pmu_intel.h b/arch/x86/kvm/vmx/pmu_intel.h index 5d9357640aa1..b553f63e19a6 100644 --- a/arch/x86/kvm/vmx/pmu_intel.h +++ b/arch/x86/kvm/vmx/pmu_intel.h @@ -34,7 +34,12 @@ struct lbr_desc { */ struct perf_event *event; - /* True if LBRs are marked as not intercepted in the MSR bitmap */ + /* + * True if KVM wants the LBR MSRs to be passed through, i.e. not + * intercepted. Note, userspace MSR filters may force interception of + * some or all LBR MSRs regardless, i.e. this doesn't necessarily + * reflect the state of the MSR bitmap. + */ bool msr_passthrough; }; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 612ab07d4100..1c190a6303a3 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4347,11 +4347,13 @@ static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu) vmx_set_intercept_for_msr(vcpu, MSR_IA32_S_CET, MSR_TYPE_RW, intercept); } + vmx_recalc_lbr_msr_intercepts(vcpu); + vmx_recalc_pmu_msr_intercepts(vcpu); /* - * x2APIC and LBR MSR intercepts are modified on-demand and cannot be - * filtered by userspace. + * x2APIC intercepts are modified on-demand and cannot be filtered by + * userspace. */ } diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index dc8517f15bc4..ec6104b66009 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -667,6 +667,7 @@ static __always_inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu); int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu); void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu); +void vmx_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu); struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags); void free_vmcs(struct vmcs *vmcs); -- 2.56.0.212.g129ede20d9-goog