KVM: x86: Honor userspace MSR filter changes for passed-through LBR MSRs
Subsystems (5)
cover KVM: x86: Honor userspace MSR filter changes for passed-through LBR MSRs
When userspace installs a new MSR filter, KVM raises KVM_REQ_RECALC_INTERCEPTS to rebuild the MSR intercept bitmaps. However, both SVM and VMX cache the desired interception state for the LBR MSRs, and skip updating the bitmap when the cached state hasn't changed. The cached state doesn't account for the filter, so a filter that denies access to LBR MSRs that are already passed through never takes effect, i.e. the guest can continue to read and write the MSRs without exiting to userspace. Conversely, if a filter forced interception when the LBRs were passed through, relaxing the filter leaves the MSRs needlessly intercepted.
On SVM, the bug was introduced by commit 3fa05f96fc08 ("KVM: SVM: Fix redundant updates of LBR MSR intercepts"). On VMX, it goes all the way back to commit 9254beaafd12 ("KVM: vmx/pmu: Reduce the overhead of LBR pass-through or cancellation"). The comment in vmx_recalc_msr_intercepts() claiming that LBR MSRs "cannot be filtered by userspace" is at odds with both kvm_msr_allowed() and the KVM_X86_SET_MSR_FILTER documentation, which exempt only the x2APIC MSRs. It's also at odds with the filter actually being applied when the LBRs are first passed through.
Patches 1 and 2 fix SVM and VMX, respectively. Patch 3 adds a selftests helper to request permission for a dynamic XSAVE feature without skipping the test if the feature is unavailable. Patch 4 adds a test that primes as many of KVM's passthrough paths as is practical (LBRs, SPEC_CTRL, XFD, APERF/MPERF, etc.), and then cycles through every possible transition between "allow all," "deny reads," "deny writes," and "deny both," verifying that every denied access (and only denied accesses) exits to userspace.
Without patch 1, the new test fails on an AMD Genoa host with lbrv=1:
both RDMSR and WRMSR of all four LBR MSRs (0x1db-0x1de) are still passed through after the first deny-all filter is installed. Similarly, without patch 2, the test fails on an Intel Ice Lake host: both RDMSR and WRMSR of MSR_LBR_SELECT, MSR_LBR_TOS, MSR_LBR_NHM_FROM, MSR_LBR_NHM_TO, and MSR_LBR_INFO_0 are still passed through after the first deny-all filter is installed. With the full series, the test passes on both hosts.
The selftest is intentionally broader than the LBR MSRs, in the hope of catching future "skip the update if nothing changed" optimizations that forget about userspace MSR filters. Intel PT MSRs aren't covered, as priming them requires the guest to be actively tracing.
Jim Mattson (4):
KVM: SVM: Always update LBR MSR intercepts on full MSR intercept
recalc
KVM: VMX: Refresh LBR MSR intercepts on full MSR intercept recalc
KVM: selftests: Add a helper to request XSAVE permission without
skipping
KVM: selftests: Verify MSR filter changes apply to passed-through MSRs
arch/x86/kvm/svm/svm.c | 28 +- arch/x86/kvm/svm/svm.h | 6 + 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 + tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/processor.h | 4 + .../testing/selftests/kvm/lib/x86/processor.c | 33 +- .../kvm/x86/msr_filter_update_test.c | 392 ++++++++++++++++++ 10 files changed, 478 insertions(+), 20 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/msr_filter_update_test.c
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.56.0.212.g129ede20d9-goog
PATCH 1/4 KVM: SVM: Always update LBR MSR intercepts on full MSR intercept recalc
Split the "already up-to-date" check out of the code that writes the LBR MSR intercepts, and write the intercepts unconditionally from svm_recalc_msr_intercepts(), so that changes to userspace MSR filters are reflected in the MSR permissions map.
svm_recalc_msr_intercepts() is invoked in response to KVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates. But svm->lbr_msrs_intercepted tracks only the interception KVM wants, not what is actually in the bitmap after the filter is applied. If the LBR MSRs are passed through when userspace installs a filter that denies them, the early return leaves them passed through, and the guest can access them without exiting to userspace. Conversely, if a filter forced interception when LBR virtualization was enabled, relaxing the filter leaves the MSRs needlessly intercepted. Either way, the bitmap remains stale until the next change to LBR_CTL.
Full intercept recalculations are rare (e.g. on RESET/INIT, EFER.SVME toggles, and CPUID, PERF_CAPABILITIES, and MSR filter updates), and never happen on nested VMRUN or #VMEXIT, so writing the intercepts there does not reintroduce the redundant per-VMRUN bitmap updates that the check was added to avoid. And because svm_recalc_msr_intercepts() already unconditionally writes the intercepts for many other MSRs (and thus marks Hyper-V's nested enlightenments dirty), writing the LBR MSR intercepts there doesn't introduce any new invalidations.
Add a comment to clarify that lbr_msrs_intercepted reflects KVM's desired interception, not necessarily the state of the bitmap.
--- arch/x86/kvm/svm/svm.c | 28 +++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 6 ++++++ 2 files changed, 29 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/svm.c +23 -5
@@ -721,14 +721,11 @@ void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)return pm; } -static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +static void svm_set_lbr_msr_intercepts (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); -if (intercept == svm->lbr_msrs_intercepted) -return ; -svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHFROMIP ,MSR_TYPE_RW , intercept);svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHTOIP ,MSR_TYPE_RW , intercept);svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTINTFROMIP ,MSR_TYPE_RW , intercept);
@@ -740,6 +737,22 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) svm->lbr_msrs_intercepted = intercept; } +static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); + +/* +* Skip redundant updates from svm_update_lbrv(), e.g. on every nested +* VMRUN and #VMEXIT. Note, svm_recalc_msr_intercepts() updates the +* bitmap unconditionally, as userspace MSR filters may have changed. +*/ +if (intercept == svm->lbr_msrs_intercepted) +return ; + +svm_set_lbr_msr_intercepts (vcpu); +} +void svm_vcpu_free_msrpm (void *msrpm) {__free_pages (virt_to_page (msrpm),get_order (MSRPM_SIZE ));
@@ -795,8 +808,13 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)svm_disable_intercept_for_msr (vcpu,MSR_SYSCALL_MASK ,MSR_TYPE_RW );#endif +/* +* Unconditionally refresh the LBR MSR intercepts, as userspace MSR +* filters may have changed, in which case the bitmap may be stale even +* if KVM's desired interception hasn't changed. +*/ if (lbrv) -svm_recalc_lbr_msr_intercepts (vcpu); +svm_set_lbr_msr_intercepts (vcpu);if (cpu_feature_enabled (X86_FEATURE_IBPB )) svm_set_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W,
arch/x86/kvm/svm/svm.h +6 -0
@@ -364,6 +364,12 @@ struct vcpu_svm {bool avic_irq_window;bool x2avic_msrs_intercepted; + +/* +* True if KVM wants the LBR MSRs to be intercepted. Note, userspace MSR +* filters may force interception regardless, i.e. this doesn't +* necessarily reflect the state of the MSR bitmap. +*/ bool lbr_msrs_intercepted;/* Guest GIF value, used when vGIF is not enabled */
Signature · 1 line
2.56.0.212.g129ede20d9-goog
Re: [PATCH 1/4] KVM: SVM: Always update LBR MSR intercepts on full MSR intercept recalc
Quoted 58 lines · Jim Mattson
Split the "already up-to-date" check out of the code that writes the LBR MSR intercepts, and write the intercepts unconditionally from
svm_recalc_msr_intercepts(), so that changes to userspace MSR filters are reflected in the MSR permissions map.
svm_recalc_msr_intercepts()is invoked in response toKVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates. Butsvm->lbr_msrs_interceptedtracks only the interception KVM wants, not what is actually in the bitmap after the filter is applied. If the LBR MSRs are passed through when userspace installs a filter that denies them, the early return leaves them passed through, and the guest can access them without exiting to userspace. Conversely, if a filter forced interception when LBR virtualization was enabled, relaxing the filter leaves the MSRs needlessly intercepted. Either way, the bitmap remains stale until the next change toLBR_CTL.Full intercept recalculations are rare (e.g. on RESET/INIT, EFER.SVME toggles, and CPUID,
PERF_CAPABILITIES, and MSR filter updates), and never happen on nested VMRUN or #VMEXIT, so writing the intercepts there does not reintroduce the redundant per-VMRUN bitmap updates that the check was added to avoid. And becausesvm_recalc_msr_intercepts()already unconditionally writes the intercepts for many other MSRs (and thus marks Hyper-V's nested enlightenments dirty), writing the LBR MSR intercepts there doesn't introduce any new invalidations.Add a comment to clarify that
lbr_msrs_interceptedreflects KVM's desired interception, not necessarily the state of the bitmap.Fixes 3fa05f96fc08 ("KVM: SVM: Fix redundant updates of LBR MSR intercepts")Signed-off-by Jim Mattson <[email protected]>--- arch/x86/kvm/svm/svm.c | 28 +++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 6 ++++++ 2 files changed, 29 insertions(+), 5 deletions(-)arch/x86/kvm/svm/svm.c +1
-4@@ -721,14 +721,11 @@ void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)return pm; } -static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +static void svm_set_lbr_msr_intercepts (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); -if (intercept == svm->lbr_msrs_intercepted) -return ; -svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHFROMIP ,MSR_TYPE_RW , intercept);svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHTOIP ,MSR_TYPE_RW , intercept);svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTINTFROMIP ,MSR_TYPE_RW , intercept);
arch/x86/kvm/svm/svm.c static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) · line 737
Quoted 1 line
arch/x86/kvm/svm/svm.c +0
-0@@ -740,6 +737,22 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) svm->lbr_msrs_intercepted = intercept;
Hmm I was going to ask if we can set svm->lbr_msrs_intercepted based on whether or not svm_set_intercept_for_msr() actually set the intercept, so that it also captures intercepts due to MSR filtering. I assume that we didn't do this because MSR filters can result in different interception status for different LBR MSRs, and we don't want to track them individually?
Hmm even if we do track them individually (e.g. as a bitmap), it would add a bit of complexity here because we'll need to plumb that into svm_set_intercept_for_msr(), right?
I suppose if we go with "lbr_msrs_intercepted just means KVM wants to intercept", then we should probably rename it accordingly?
Quoted 56 lines
arch/x86/kvm/svm/svm.c +22
-1} +static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); + +/* +* Skip redundant updates from svm_update_lbrv(), e.g. on every nested +* VMRUN and #VMEXIT. Note, svm_recalc_msr_intercepts() updates the +* bitmap unconditionally, as userspace MSR filters may have changed. +*/ +if (intercept == svm->lbr_msrs_intercepted) +return ; + +svm_set_lbr_msr_intercepts (vcpu); +} +void svm_vcpu_free_msrpm (void *msrpm) {__free_pages (virt_to_page (msrpm),get_order (MSRPM_SIZE ));@@ -795,8 +808,13 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)svm_disable_intercept_for_msr (vcpu,MSR_SYSCALL_MASK ,MSR_TYPE_RW );#endif +/* +* Unconditionally refresh the LBR MSR intercepts, as userspace MSR +* filters may have changed, in which case the bitmap may be stale even +* if KVM's desired interception hasn't changed. +*/ if (lbrv) -svm_recalc_lbr_msr_intercepts (vcpu); +svm_set_lbr_msr_intercepts (vcpu);if (cpu_feature_enabled (X86_FEATURE_IBPB )) svm_set_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W,arch/x86/kvm/svm/svm.h +6
-1@@ -364,6 +364,12 @@ struct vcpu_svm {bool avic_irq_window;bool x2avic_msrs_intercepted; + +/* +* True if KVM wants the LBR MSRs to be intercepted. Note, userspace MSR +* filters may force interception regardless, i.e. this doesn't +* necessarily reflect the state of the MSR bitmap. +*/ bool lbr_msrs_intercepted;/* Guest GIF value, used when vGIF is not enabled */ --2.56.0.212.g129ede20d9-goog
Sean Christopherson
maintainerRe: [PATCH 1/4] KVM: SVM: Always update LBR MSR intercepts on full MSR intercept recalc
Quoted 15 lines · Yosry Ahmed
Quoted 14 lines · Jim Mattson
arch/x86/kvm/svm/svm.c +1
-1@@ -721,14 +721,11 @@ void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)return pm; } -static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +static void svm_set_lbr_msr_intercepts (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR );
Quoted 15 lines
arch/x86/kvm/svm/svm.c static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) · line 737
Quoted 8 lines
arch/x86/kvm/svm/svm.c +0
-3-if (intercept == svm->lbr_msrs_intercepted) -return ; -svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHFROMIP ,MSR_TYPE_RW , intercept);svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHTOIP ,MSR_TYPE_RW , intercept);svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTINTFROMIP ,MSR_TYPE_RW , intercept);@@ -740,6 +737,22 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) svm->lbr_msrs_intercepted = intercept;Hmm I was going to ask if we can set
svm->lbr_msrs_interceptedbased on whether or notsvm_set_intercept_for_msr()actually set the intercept, so that it also captures intercepts due to MSR filtering. I assume that we didn't do this because MSR filters can result in different interception status for different LBR MSRs, and we don't want to track them individually?
NAK. In the past, KVM had a bitmap to do exactly that, and it was a trainwreck.
Quoted 6 lines
Hmm even if we do track them individually (e.g. as a bitmap), it would add a bit of complexity here because we'll need to plumb that into
svm_set_intercept_for_msr(), right?I suppose if we go with "
lbr_msrs_interceptedjust means KVM wants to intercept", then we should probably rename it accordingly?
Probably, but that can be done on top. And I'd like to change all of these flags (all two of them?) to be consistent.
Quoted 6 lines
arch/x86/kvm/svm/svm.c static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) · line 743
Quoted 6 lines
arch/x86/kvm/svm/svm.c +4
-0} +static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR );
Eww. The naming is all kinda of funky, and I strongly dislike the duplicate code. E.g. svm_set_lbr_msr_intercepts() recalcs, it's not a pure "set". I'd much rather pass a @force param, e.g.
arch/x86/kvm/svm/svm.c +8 -3
@@ -728,12 +728,12 @@ void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)return pm; } -static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +static void __svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu,bool force) {struct vcpu_svm *svm =to_svm (vcpu);bool intercept = !(svm->vmcb->control.misc_ctl2 &SVM_MISC2_ENABLE_V_LBR ); -if (intercept == svm->lbr_msrs_intercepted) +if (intercept == svm->lbr_msrs_intercepted && !force)return ;svm_set_intercept_for_msr (vcpu,MSR_IA32_LASTBRANCHFROMIP ,MSR_TYPE_RW , intercept);
@@ -747,6 +747,11 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu) svm->lbr_msrs_intercepted = intercept; } +static void svm_recalc_lbr_msr_intercepts (struct kvm_vcpu *vcpu) +{ +__svm_recalc_lbr_msr_intercepts (vcpu, false); +} +void svm_vcpu_free_msrpm (void *msrpm) {__free_pages (virt_to_page (msrpm),get_order (MSRPM_SIZE ));
@@ -803,7 +808,7 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)#endif if (lbrv) -svm_recalc_lbr_msr_intercepts (vcpu); +__svm_recalc_lbr_msr_intercepts (vcpu, true);if (cpu_feature_enabled (X86_FEATURE_IBPB )) svm_set_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W,
PATCH 2/4 KVM: VMX: Refresh LBR MSR intercepts on full MSR intercept recalc
In reply to Jim MattsonRe-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.
--- 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(-)
arch/x86/kvm/vmx/pmu_intel.c +19 -1
@@ -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
arch/x86/kvm/vmx/pmu_intel.h +6 -1
@@ -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; };
arch/x86/kvm/vmx/vmx.c +4 -2
@@ -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. */ }
arch/x86/kvm/vmx/vmx.h +1 -0
@@ -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);
Signature · 1 line
2.56.0.212.g129ede20d9-goog
Sean Christopherson
maintainerRe: [PATCH 2/4] KVM: VMX: Refresh LBR MSR intercepts on full MSR intercept recalc
Quoted 31 lines · Jim Mattson
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, clearingmsr_passthroughto forcevmx_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 toKVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates, but it skips the LBR MSRs, andvmx_enable_lbr_msrs_passthrough()does nothing iflbr_desc->msr_passthroughis 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 theKVM_X86_SET_MSR_FILTERdocumentation), 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.
Quoted 2 lines
Fixes 9254beaafd12 ("KVM: vmx/pmu: Reduce the overhead of LBR pass-through or cancellation")
Meh, this isn't stable material. Unlike SVM, where LBR virtualization is tied to a pile of other crud, if userspace wants to filter LBRs on Intel it should just not expose LBRs in the first place.
If it weren't for SVM, and the fact that avoiding the filters is actually more work, I probably would vote to fix the docs.
Quoted 23 lines
Signed-off-by Jim Mattson <[email protected]>--- 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(-)arch/x86/kvm/vmx/pmu_intel.c +2
-1@@ -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. */
arch/x86/kvm/vmx/pmu_intel.c static inline void vmx_enable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu) · line 712
Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c +5
-0@@ -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.
Hmm, I don't love assuming that recalcs are only done for filter updates. On the other hand, taking action if lbr_desc->msr_passthrough=false is nonsensical. Rather than immediately refresh the updates, what if we just disable passthrough and let vmx_passthrough_lbr_msrs() do the actual update?
void vmx_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu)
{
if (WARN_ON_ONCE(!vcpu_to_lbr_desc(vcpu)))
return;
/* Disable passthrough to force a recalc on the next VM-Entry. */
vmx_disable_lbr_msrs_passthrough(vcpu);
}Re: [PATCH 2/4] KVM: VMX: Refresh LBR MSR intercepts on full MSR intercept recalc
Quoted 33 lines · Sean Christopherson
Quoted 31 lines · Jim Mattson
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, clearingmsr_passthroughto forcevmx_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 toKVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates, but it skips the LBR MSRs, andvmx_enable_lbr_msrs_passthrough()does nothing iflbr_desc->msr_passthroughis 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 theKVM_X86_SET_MSR_FILTERdocumentation), 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.
Quoted 6 lines
Quoted 2 lines
Fixes 9254beaafd12 ("KVM: vmx/pmu: Reduce the overhead of LBR pass-through or cancellation")Meh, this isn't stable material. Unlike SVM, where LBR virtualization is tied to a pile of other crud, if userspace wants to filter LBRs on Intel it should just not expose LBRs in the first place.
The functionality is not equivalent. For whatever reason, MSR filtering can be modified dynamically.
Re: [PATCH 2/4] KVM: VMX: Refresh LBR MSR intercepts on full MSR intercept recalc
Quoted 38 lines · Jim Mattson
Quoted 36 lines · Sean Christopherson
Quoted 33 lines · Jim Mattson
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, clearingmsr_passthroughto forcevmx_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 toKVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates, but it skips the LBR MSRs, andvmx_enable_lbr_msrs_passthrough()does nothing iflbr_desc->msr_passthroughis 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 theKVM_X86_SET_MSR_FILTERdocumentation), 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")
Quoted 6 lines
Quoted 3 lines
Meh, this isn't stable material. Unlike SVM, where LBR virtualization is tied to a pile of other crud, if userspace wants to filter LBRs on Intel it should just not expose LBRs in the first place.
The functionality is not equivalent. For whatever reason, MSR filtering can be modified dynamically.
Oh, and, duh, MSR filtering doesn't disallow the LBRs; it just reflects them to userspace.
Sean Christopherson
maintainerRe: [PATCH 2/4] KVM: VMX: Refresh LBR MSR intercepts on full MSR intercept recalc
Quoted 43 lines · Jim Mattson
Quoted 42 lines · Jim Mattson
Quoted 39 lines · Sean Christopherson
Quoted 31 lines · Jim Mattson
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, clearingmsr_passthroughto forcevmx_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 toKVM_REQ_RECALC_INTERCEPTS, which is raised on MSR filter updates, but it skips the LBR MSRs, andvmx_enable_lbr_msrs_passthrough()does nothing iflbr_desc->msr_passthroughis 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 theKVM_X86_SET_MSR_FILTERdocumentation), 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.Quoted 2 lines
Fixes 9254beaafd12 ("KVM: vmx/pmu: Reduce the overhead of LBR pass-through or cancellation")Meh, this isn't stable material. Unlike SVM, where LBR virtualization is tied to a pile of other crud, if userspace wants to filter LBRs on Intel it should just not expose LBRs in the first place.
Quoted 5 lines
Quoted 2 lines
The functionality is not equivalent. For whatever reason, MSR filtering can be modified dynamically.
Oh, and, duh, MSR filtering doesn't disallow the LBRs; it just reflects them to userspace.
Right, but how many real world deployments actually enable vLBR *and* want to intercept accesses *and* want to do so dynamically? This isn't a particularly dangerous change by any means, but sending things to stable@ does incur some amount of downstream work. Unless there's an actual end user that's affected by this flaw, I'd much prefer not to create more work.
PATCH 3/4 KVM: selftests: Add a helper to request XSAVE permission without skipping
In reply to Jim MattsonSplit the core of __vm_xsave_require_permission() into a new helper, vm_xsave_request_permission(), which returns false instead of skipping the test if KVM doesn't support the requested dynamic XSAVE feature, or if the request for permission is denied. This allows tests to opt into dynamic features opportunistically, e.g. to exercise additional KVM paths when AMX is available, while retaining the sanity check that the request is made before KVM_GET_SUPPORTED_CPUID is first queried.
Like vm_xsave_require_permission(), wrap the new helper in a macro that stringifies the feature mask for use in assertion messages.
Use __kvm_device_attr_get() instead of open-coding KVM_GET_DEVICE_ATTR.
No functional change intended for existing users, other than collapsing the reasons for skipping into a single message.
--- .../selftests/kvm/include/x86/processor.h | 4 +++ .../testing/selftests/kvm/lib/x86/processor.c | 33 ++++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +4 -0
@@ -1530,8 +1530,12 @@ static inline void cli(void) asmvolatile ("cli" ); } +bool __vm_xsave_request_permission (u64 xfeature,const char *name);void __vm_xsave_require_permission (u64 xfeature,const char *name); +#define vm_xsave_request_permission (xfeature) \ + __vm_xsave_request_permission(xfeature, #xfeature) +#define vm_xsave_require_permission (xfeature) \ __vm_xsave_require_permission(xfeature, #xfeature)
tools/testing/selftests/kvm/lib/x86/processor.c +22 -11
@@ -975,16 +975,18 @@ u64 kvm_get_feature_msr(u64 msr_index)return buffer.entry.data; } -void __vm_xsave_require_permission (u64 xfeature,const char *name) +/* +* Request permission for guests to use the dynamic XSAVE feature @xfeature. +* Returns false, without skipping the test, if KVM doesn't support the +* feature or if the request is denied. Must be called before +* KVM_GET_SUPPORTED_CPUID is first queried, as KVM enumerates dynamic +* features only if userspace has permission to use them. +*/ +bool __vm_xsave_request_permission (u64 xfeature,const char *name) {int kvm_fd;u64 bitmask;long rc; -struct kvm_device_attr attr = { - .group =0 , - .attr =KVM_X86_XCOMP_GUEST_SUPP , - .addr = (unsigned long ) &bitmask, - };TEST_ASSERT (!kvm_supported_cpuid,"kvm_get_supported_cpuid() cannot be used before ARCH_REQ_XCOMP_GUEST_PERM" );
@@ -993,24 +995,33 @@ void __vm_xsave_require_permission(u64 xfeature, const char *name) "Dynamic XFeatures must be enabled one at a time"); kvm_fd =open_kvm_dev_path_or_exit (); - rc =__kvm_ioctl (kvm_fd,KVM_GET_DEVICE_ATTR , &attr); + rc =__kvm_device_attr_get (kvm_fd,0 ,KVM_X86_XCOMP_GUEST_SUPP , &bitmask);close (kvm_fd);if (rc ==-1 && (errno ==ENXIO || errno ==EINVAL )) -__TEST_REQUIRE (0 ,"KVM_X86_XCOMP_GUEST_SUPP not supported" ); +return false;TEST_ASSERT (rc ==0 ,"KVM_GET_DEVICE_ATTR(0, KVM_X86_XCOMP_GUEST_SUPP) error: %ld" , rc); -__TEST_REQUIRE (bitmask & xfeature, -"Required XSAVE feature '%s' not supported" , name); +if (!(bitmask & xfeature)) +return false; -TEST_REQUIRE (!syscall (SYS_arch_prctl,ARCH_REQ_XCOMP_GUEST_PERM ,ilog2 (xfeature))); +if (syscall (SYS_arch_prctl,ARCH_REQ_XCOMP_GUEST_PERM ,ilog2 (xfeature))) +return false; rc =syscall (SYS_arch_prctl,ARCH_GET_XCOMP_GUEST_PERM , &bitmask);TEST_ASSERT (rc ==0 ,"prctl(ARCH_GET_XCOMP_GUEST_PERM) error: %ld" , rc);TEST_ASSERT (bitmask & xfeature,"'%s' (0x%lx) not permitted after prctl(ARCH_REQ_XCOMP_GUEST_PERM) permitted=0x%lx" , name, xfeature, bitmask); +return true; +} + +void __vm_xsave_require_permission (u64 xfeature,const char *name) +{ +__TEST_REQUIRE (__vm_xsave_request_permission (xfeature, name), +"Dynamic XSAVE feature '%s' not supported or not permitted" , + name); }void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid)
Signature · 1 line
2.56.0.212.g129ede20d9-goog
PATCH 4/4 KVM: selftests: Verify MSR filter changes apply to passed-through MSRs
In reply to Jim MattsonAdd a test to verify that userspace MSR filter changes take effect for MSRs that KVM has already passed through to the guest. KVM tracks the desired interception state for some MSRs to avoid redundant bitmap updates, and such optimizations can easily cause KVM to skip refreshing the MSR bitmaps when only the filter has changed, leaving MSRs passed through despite userspace denying access. This is a regression test for the SVM and VMX LBR MSR fixes that precede it.
Prime as many passthrough paths as is practical: disable APERF/MPERF and C-state exits if allowed, request AMX permission so that the guest can write XFD, and have the guest enable LBRs, set SPEC_CTRL, and set XFD. Then cycle through a sequence of filters that allow everything, or deny reads, writes, or both for every MSR of interest, and verify that every denied access (and only denied accesses) exits to userspace. The sequence covers every possible transition between the four filter states, to catch stale state tracking from any prior state.
Add a new test rather than extending userspace_msr_exit_test, as the existing test focuses on the semantics of individual filters, whereas this test needs to prime KVM's on-demand passthrough paths before installing filters, and then cycle through many filters on a single vCPU.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/msr_filter_update_test.c | 392 ++++++++++++++++++ 2 files changed, 393 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/msr_filter_update_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -92,6 +92,7 @@ TEST_GEN_PROGS_x86 += x86/kvm_clock_test TEST_GEN_PROGS_x86 += x86/kvm_pv_test TEST_GEN_PROGS_x86 += x86/kvm_buslock_test TEST_GEN_PROGS_x86 += x86/monitor_mwait_test +TEST_GEN_PROGS_x86 += x86/msr_filter_update_test TEST_GEN_PROGS_x86 += x86/msrs_test TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test TEST_GEN_PROGS_x86 += x86/nested_dirty_log_test
tools/testing/selftests/kvm/x86/msr_filter_update_test.c new file +392 -0
392 lines not on this page · raw message
Signature · 1 line
2.56.0.212.g129ede20d9-goog