Introduce Architectural LBR for vPMU
Subsystems (4)
v2 cover Introduce Architectural LBR for vPMU
Intel CPU model-specific LBR(Legacy LBR) has evolved to Architectural LBR(Arch LBR [0]), it's the replacement of legacy LBR on new platforms. The native support patches were merged into 5.9 kernel tree, and this patch series is to enable Arch LBR in vPMU so that guest can benefit from the feature.
The main advantages of Arch LBR are [1]:
- Faster context switching due to XSAVES support and faster reset of LBR MSRs via the new DEPTH MSR
- Faster LBR read for a non-PEBS event due to XSAVES support, which lowers the overhead of the NMI handler.
- Linux kernel can support the LBR features without knowing the model number of the current CPU.
From end user's point of view, the usage of Arch LBR is the same as the Legacy LBR that has been merged in the mainline.
Note, in this series, there's one restriction for guest Arch LBR, i.e., guest can only set its LBR record depth the same as host's. This is due to the special behavior of MSR_ARCH_LBR_DEPTH:
1) On write to the MSR, it'll reset all Arch LBR recording MSRs to 0s.
2) XRSTORS resets all record MSRs to 0s if the saved depth mismatches MSR_ARCH_LBR_DEPTH.
Enforcing the restriction keeps KVM Arch LBR vPMU working flow simple and straightforward.
Paolo refactored the old series and the resulting patches became the base of this new series, therefore he's the author of some patches.
[0] https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
[1] https://lore.kernel.org/lkml/[email protected]/
v1:
https://lore.kernel.org/all/[email protected]/
Changes v2:
1. Removed Paolo's SOBs from some patches. [Sean]
2. Modified some patches due to KVM changes, e.g., SMM/vPMU refactor.
3. Rebased to https://git.kernel.org/pub/scm/virt/kvm/kvm.git : queue branch.
Like Xu (3):
perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
KVM: x86: Add XSAVE Support for Architectural LBR
Paolo Bonzini (4):
KVM: PMU: disable LBR handling if architectural LBR is available
KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
KVM: VMX: Support passthrough of architectural LBRs
KVM: x86: Refine the matching and clearing logic for supported_xss
Sean Christopherson (1):
KVM: x86: Report XSS as an MSR to be saved if there are supported
features
Yang Weijiang (7):
KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
KVM: x86: Add Arch LBR MSRs to msrs_to_save_all list
KVM: x86/vmx: Check Arch LBR config when return perf capabilities
KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
KVM: x86/vmx: Save/Restore guest Arch LBR Ctrl msr at SMM entry/exit
KVM: x86: Add Arch LBR data MSR access interface
KVM: x86/cpuid: Advertise Arch LBR feature in CPUID
arch/x86/events/intel/lbr.c | 6 +- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 4 + arch/x86/kvm/cpuid.c | 52 +++++++++- arch/x86/kvm/smm.c | 1 + arch/x86/kvm/smm.h | 3 +- arch/x86/kvm/vmx/capabilities.h | 5 + arch/x86/kvm/vmx/nested.c | 8 ++ arch/x86/kvm/vmx/pmu_intel.c | 161 +++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.c | 74 +++++++++++++- arch/x86/kvm/vmx/vmx.h | 6 +- arch/x86/kvm/x86.c | 27 +++++- 13 files changed, 316 insertions(+), 35 deletions(-)
base-commit: da5f28e10aa7df1a925dbc10656cc89d9c061358
Signature · 1 line
2.27.0
PATCH v2 1/15 perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
From: Like Xu <[email protected]>
The x86_pmu.lbr_info is 0 unless explicitly initialized, so there's no point checking x86_pmu.intel_cap.lbr_format.
--- arch/x86/events/intel/lbr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/events/intel/lbr.c +1 -3
@@ -1606,12 +1606,10 @@ void __init intel_pmu_arch_lbr_init(void)*/ void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -int lbr_fmt = x86_pmu.intel_cap.lbr_format; - lbr->nr = x86_pmu.lbr_nr; lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; - lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; + lbr->info = x86_pmu.lbr_info; }EXPORT_SYMBOL_GPL (x86_perf_get_lbr);
Signature · 1 line
2.27.0
Re: [PATCH v2 01/15] perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
Hi Peter, would you help apply this one in your tip/perf tree, as it doesn't seem to be closely tied to the KVM changes. Thanks.
Quoted 32 lines · Yang Weijiang
From: Like Xu <[email protected]>
The x86_pmu.lbr_info is 0 unless explicitly initialized, so there's no point checking x86_pmu.intel_cap.lbr_format.
Cc Peter Zijlstra <[email protected]>Reviewed-by Kan Liang <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/events/intel/lbr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)arch/x86/events/intel/lbr.c +1
-3@@ -1606,12 +1606,10 @@ void __init intel_pmu_arch_lbr_init(void)*/ void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -int lbr_fmt = x86_pmu.intel_cap.lbr_format; - lbr->nr = x86_pmu.lbr_nr; lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; - lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; + lbr->info = x86_pmu.lbr_info; }EXPORT_SYMBOL_GPL (x86_perf_get_lbr);
Peter Zijlstra
maintainerRe: [PATCH v2 01/15] perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
Quoted 2 lines · Like Xu
Hi Peter, would you help apply this one in your tip/perf tree, as it doesn't seem to be closely tied to the KVM changes. Thanks.
OK, I'll go queue it for perf/core after -rc1
Sean Christopherson
maintainerRe: [PATCH v2 01/15] perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
In reply to Like XuQuoted 18 lines · Like Xu
Hi Peter, would you help apply this one in your tip/perf tree, as it doesn't seem to be closely tied to the KVM changes. Thanks.
Quoted 14 lines · Yang Weijiang
From: Like Xu <[email protected]>
The x86_pmu.lbr_info is 0 unless explicitly initialized, so there's no point checking x86_pmu.intel_cap.lbr_format.
Cc Peter Zijlstra <[email protected]>Reviewed-by Kan Liang <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/events/intel/lbr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 15 lines
Quoted 5 lines
arch/x86/events/intel/lbr.c +0
-0arch/x86/events/intel/lbr.c void __init intel_pmu_arch_lbr_init(void) · line 1612
Quoted 10 lines
arch/x86/events/intel/lbr.c +1
-3@@ -1606,12 +1606,10 @@ void __init intel_pmu_arch_lbr_init(void)*/ void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -int lbr_fmt = x86_pmu.intel_cap.lbr_format; - lbr->nr = x86_pmu.lbr_nr; lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; - lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; + lbr->info = x86_pmu.lbr_info;
This stable-worthy a bug fix, no? E.g. won't the existing code misreport lbr->info if the format is LBR_FORMAT_INFO2?
Quoted 2 lines
Quoted 2 lines
arch/x86/events/intel/lbr.c +0
-0}EXPORT_SYMBOL_GPL (x86_perf_get_lbr);
Re: [PATCH v2 01/15] perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
Quoted 29 lines · Sean Christopherson
Quoted 28 lines · Like Xu
Hi Peter, would you help apply this one in your tip/perf tree, as it doesn't seem to be closely tied to the KVM changes. Thanks.
Quoted 24 lines · Yang Weijiang
From: Like Xu <[email protected]>
The x86_pmu.lbr_info is 0 unless explicitly initialized, so there's no point checking x86_pmu.intel_cap.lbr_format.
Cc Peter Zijlstra <[email protected]>Reviewed-by Kan Liang <[email protected]>Reviewed-by Andi Kleen <[email protected]>Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/events/intel/lbr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)arch/x86/events/intel/lbr.c +0
-2@@ -1606,12 +1606,10 @@ void __init intel_pmu_arch_lbr_init(void)*/ void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -int lbr_fmt = x86_pmu.intel_cap.lbr_format; -
Quoted 8 lines
Quoted 5 lines
arch/x86/events/intel/lbr.c void __init intel_pmu_arch_lbr_init(void) · line 1612
Quoted 5 lines
arch/x86/events/intel/lbr.c +1
-1lbr->nr = x86_pmu.lbr_nr; lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; - lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; + lbr->info = x86_pmu.lbr_info;This stable-worthy a bug fix, no? E.g. won't the existing code misreport
lbr->infoif the format isLBR_FORMAT_INFO2?
Sure, we need "Cc: [email protected]" in order not to lose misprediction and cycles information on the LBR_FORMAT_INFO2 platforms like Goldmont plus.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/x86/events/intel/lbr.c +0
-0}EXPORT_SYMBOL_GPL (x86_perf_get_lbr);
[tip: perf/core] perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
In reply to Yang WeijiangThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 03c4c7f88709fac0e20b6a48357c73d6fc50e544 Gitweb: https://git.kernel.org/tip/03c4c7f88709fac0e20b6a48357c73d6fc50e544 Author: Like Xu <[email protected]> AuthorDate: Thu, 24 Nov 2022 23:05:50 -05:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 27 Dec 2022 12:52:07 +01:00
perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
The x86_pmu.lbr_info is 0 unless explicitly initialized, so there's no point checking x86_pmu.intel_cap.lbr_format.
--- arch/x86/events/intel/lbr.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
arch/x86/events/intel/lbr.c +1 -3
@@ -1606,12 +1606,10 @@ clear_arch_lbr:*/ void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -int lbr_fmt = x86_pmu.intel_cap.lbr_format; - lbr->nr = x86_pmu.lbr_nr; lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; - lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; + lbr->info = x86_pmu.lbr_info; }EXPORT_SYMBOL_GPL (x86_perf_get_lbr);
PATCH v2 2/15 KVM: x86: Report XSS as an MSR to be saved if there are supported features
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
Add MSR_IA32_XSS to the list of MSRs reported to userspace if supported_xss is non-zero, i.e. KVM supports at least one XSS based feature.
--- arch/x86/kvm/x86.c | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kvm/x86.c +5 -0
@@ -1465,6 +1465,7 @@ static const u32 msrs_to_save_all[] = {MSR_F15H_PERF_CTR3 ,MSR_F15H_PERF_CTR4 ,MSR_F15H_PERF_CTR5 ,MSR_IA32_XFD ,MSR_IA32_XFD_ERR , + MSR_IA32_XSS, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -7061,6 +7062,10 @@ static void kvm_init_msr_list(void)if (!kvm_cpu_cap_has (X86_FEATURE_XFD ))continue ;break ; +case MSR_IA32_XSS : +if (!kvm_caps.supported_xss) +continue ; +break ;default :break ; }
Signature · 1 line
2.27.0
PATCH v2 3/15 KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
In reply to Yang WeijiangUpdated CPUID.0xD.0x1, which reports the current required storage size of all features enabled via XCR0 | XSS, when the guest's XSS is modified.
Note, KVM does not yet support any XSS based features, i.e. supported_xss is guaranteed to be zero at this time.
--- arch/x86/kvm/cpuid.c | 16 +++++++++++++--- arch/x86/kvm/x86.c | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-)
arch/x86/kvm/cpuid.c +13 -3
@@ -272,9 +272,19 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e best->ebx =xstate_required_size (vcpu->arch.xcr0, false); best =cpuid_entry2_find (entries, nent,0xD ,1 ); -if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || -cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best) { +if (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || +cpuid_entry_has (best,X86_FEATURE_XSAVEC )) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true); + } + +if (!cpuid_entry_has (best,X86_FEATURE_XSAVES )) { + best->ecx =0 ; + best->edx =0 ; + } + } best =__kvm_find_kvm_cpuid_features (vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best &&
arch/x86/kvm/x86.c +4 -2
@@ -3685,8 +3685,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)*/ if (data & ~kvm_caps.supported_xss)return 1 ; - vcpu->arch.ia32_xss = data; -kvm_update_cpuid_runtime (vcpu); +if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; +kvm_update_cpuid_runtime (vcpu); + }break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 03/15] KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
Quoted 26 lines · Yang Weijiang
Updated CPUID.0xD.0x1, which reports the current required storage size of all features enabled via XCR0 | XSS, when the guest's XSS is modified.
Note, KVM does not yet support any XSS based features, i.e.
supported_xssis guaranteed to be zero at this time.Co-developed-by Zhang Yi Z <[email protected]>Signed-off-by Zhang Yi Z <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/cpuid.c | 16 +++++++++++++--- arch/x86/kvm/x86.c | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-)arch/x86/kvm/cpuid.c +1
-3@@ -272,9 +272,19 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e best->ebx =xstate_required_size (vcpu->arch.xcr0, false); best =cpuid_entry2_find (entries, nent,0xD ,1 ); -if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || -cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best) {
arch/x86/kvm/cpuid.c static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e · line 285
Quoted 10 lines
arch/x86/kvm/cpuid.c +10
-0+if (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || +cpuid_entry_has (best,X86_FEATURE_XSAVEC )) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true); + } + +if (!cpuid_entry_has (best,X86_FEATURE_XSAVES )) { + best->ecx =0 ; + best->edx =0 ;
ECX and EDX should be left alone, it is userspace's responsibility to provide a sane CPUID model. E.g. KVM doesn't clear EBX or EDX in CPUID.0xD.0x1 when XSAVE is unsupported.
Quoted 25 lines
arch/x86/kvm/cpuid.c +2
-0+ } + } best =__kvm_find_kvm_cpuid_features (vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best &&arch/x86/kvm/x86.c +4
-2@@ -3685,8 +3685,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)*/ if (data & ~kvm_caps.supported_xss)return 1 ; - vcpu->arch.ia32_xss = data; -kvm_update_cpuid_runtime (vcpu); +if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; +kvm_update_cpuid_runtime (vcpu); + }break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)Signature · 1 line
2.27.0
Re: [PATCH v2 03/15] KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
Quoted 32 lines · Sean Christopherson
Quoted 31 lines · Yang Weijiang
Updated CPUID.0xD.0x1, which reports the current required storage size of all features enabled via XCR0 | XSS, when the guest's XSS is modified.
Note, KVM does not yet support any XSS based features, i.e.
supported_xssis guaranteed to be zero at this time.Co-developed-by Zhang Yi Z <[email protected]>Signed-off-by Zhang Yi Z <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/cpuid.c | 16 +++++++++++++--- arch/x86/kvm/x86.c | 6 ++++-- 2 files changed, 17 insertions(+), 5 deletions(-)arch/x86/kvm/cpuid.c +6
-3@@ -272,9 +272,19 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e best->ebx =xstate_required_size (vcpu->arch.xcr0, false); best =cpuid_entry2_find (entries, nent,0xD ,1 ); -if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || -cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); +if (best) { +if (cpuid_entry_has (best,X86_FEATURE_XSAVES ) || +cpuid_entry_has (best,X86_FEATURE_XSAVEC )) { +u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx =xstate_required_size (xstate, true);
Quoted 8 lines
arch/x86/kvm/cpuid.c static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e · line 285
Quoted 5 lines
arch/x86/kvm/cpuid.c +5
-0+ } + +if (!cpuid_entry_has (best,X86_FEATURE_XSAVES )) { + best->ecx =0 ; + best->edx =0 ;ECX and EDX should be left alone, it is userspace's responsibility to provide a sane CPUID model. E.g. KVM doesn't clear EBX or EDX in CPUID.0xD.0x1 when XSAVE is unsupported.
Thanks for review!
Sure, I'll remove it.
Quoted 26 lines
Quoted 26 lines
arch/x86/kvm/cpuid.c +2
-0+ } + } best =__kvm_find_kvm_cpuid_features (vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best &&arch/x86/kvm/x86.c +4
-2@@ -3685,8 +3685,10 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)*/ if (data & ~kvm_caps.supported_xss)return 1 ; - vcpu->arch.ia32_xss = data; -kvm_update_cpuid_runtime (vcpu); +if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; +kvm_update_cpuid_runtime (vcpu); + }break ;case MSR_SMI_COUNT :if (!msr_info->host_initiated)Signature · 1 line
2.27.0
PATCH v2 4/15 KVM: PMU: disable LBR handling if architectural LBR is available
In reply to Yang WeijiangFrom: Paolo Bonzini <[email protected]>
Traditional LBR is absent on CPU models that have architectural LBR, so disable all processing of traditional LBR MSRs if they are not there.
--- arch/x86/kvm/vmx/pmu_intel.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +22 -10
@@ -170,19 +170,23 @@ static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)static bool intel_pmu_is_valid_lbr_msr (struct kvm_vcpu *vcpu,u32 index) {struct x86_pmu_lbr *records =vcpu_to_lbr_records (vcpu); -bool ret = false;if (!intel_pmu_lbr_is_enabled (vcpu)) -return ret; +return false; - ret = (index ==MSR_LBR_SELECT ) || (index ==MSR_LBR_TOS ) || - (index >= records->from && index < records->from + records->nr) || - (index >= records->to && index < records->to + records->nr); +if (!guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (index ==MSR_LBR_SELECT || index ==MSR_LBR_TOS )) +return true; -if (!ret && records->info) - ret = (index >= records->info && index < records->info + records->nr); +if ((index >= records->from && index < records->from + records->nr) || + (index >= records->to && index < records->to + records->nr)) +return true; -return ret; +if (records->info && index >= records->info && + index < records->info + records->nr) +return true; + +return false; }static bool intel_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr)
@@ -702,6 +706,9 @@ static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set)vmx_set_intercept_for_msr (vcpu, lbr->info + i,MSR_TYPE_RW , set); } +if (guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) +return ; +vmx_set_intercept_for_msr (vcpu,MSR_LBR_SELECT ,MSR_TYPE_RW , set);vmx_set_intercept_for_msr (vcpu,MSR_LBR_TOS ,MSR_TYPE_RW , set); }
@@ -742,10 +749,12 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR );if (!lbr_desc->event) {vmx_disable_lbr_msrs_passthrough (vcpu); -if (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ) +if (lbr_enable) goto warn;if (test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use)) goto warn;
@@ -768,7 +777,10 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)static void intel_pmu_cleanup (struct kvm_vcpu *vcpu) { -if (!(vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR )) +bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ); + +if (!lbr_enable)intel_pmu_release_guest_lbr_event (vcpu); }
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 04/15] KVM: PMU: disable LBR handling if architectural LBR is available
Quoted 19 lines · Yang Weijiang
From: Paolo Bonzini <[email protected]>
Traditional LBR is absent on CPU models that have architectural LBR, so disable all processing of traditional LBR MSRs if they are not there.
Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)arch/x86/kvm/vmx/pmu_intel.c +0
-0@@ -170,19 +170,23 @@ static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)static bool intel_pmu_is_valid_lbr_msr (struct kvm_vcpu *vcpu,u32 index) {struct x86_pmu_lbr *records =vcpu_to_lbr_records (vcpu);
arch/x86/kvm/vmx/pmu_intel.c static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr) · line 177
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-5-bool ret = false;if (!intel_pmu_lbr_is_enabled (vcpu)) -return ret; +return false; - ret = (index ==MSR_LBR_SELECT ) || (index ==MSR_LBR_TOS ) || - (index >= records->from && index < records->from + records->nr) || - (index >= records->to && index < records->to + records->nr); + if (!guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR) &&
IIUC, the MSRs flat out don't exist _and_ KVM expects to passthrough MSRs to the guest, i.e. KVM should check host support, not guest support. Probably a moot point from a functionality perspective since KVM shouldn't allow LBRs to shouldn't be enabled for the guest, but from a performance perspective, checking guest CPUID is slooow.
That brings me to point #2, which is that KVM needs to disallow enabling legacy LBRs on CPUs that support arch LBRs. Again, IIUC, because KVM doesn't have the option to fallback to legacy LBRs, that restriction needs to be treated as a bug fix. I'll post a separate patch unless my understanding is wrong.
Quoted 19 lines
arch/x86/kvm/vmx/pmu_intel.c +10
-3+ (index == MSR_LBR_SELECT || index == MSR_LBR_TOS)) +return true; -if (!ret && records->info) - ret = (index >= records->info && index < records->info + records->nr); +if ((index >= records->from && index < records->from + records->nr) || + (index >= records->to && index < records->to + records->nr)) +return true; -return ret; +if (records->info && index >= records->info && + index < records->info + records->nr) +return true; + +return false; }static bool intel_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr)
arch/x86/kvm/vmx/pmu_intel.c static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set) · line 709
Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +1
-0@@ -702,6 +706,9 @@ static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set)vmx_set_intercept_for_msr (vcpu, lbr->info + i,MSR_TYPE_RW , set); } +if (guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ))
Similar to above, I really don't want to query guest CPUID in the VM-Enter path. If we establish the rule that LBRs can be enabled if and only if the correct type is enabled (traditional/legacy vs. arch), then this can simply check host support.
arch/x86/kvm/vmx/pmu_intel.c void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) · line 753
Quoted 11 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0+return ; +vmx_set_intercept_for_msr (vcpu,MSR_LBR_SELECT ,MSR_TYPE_RW , set);vmx_set_intercept_for_msr (vcpu,MSR_LBR_TOS ,MSR_TYPE_RW , set); }@@ -742,10 +749,12 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR );
Unnecessary guest CPUID lookup and VMCS read, i.e. this can be deferred to the !lbr_desc->event path.
Quoted 23 lines
arch/x86/kvm/vmx/pmu_intel.c +5
-2if (!lbr_desc->event) {vmx_disable_lbr_msrs_passthrough (vcpu); -if (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ) +if (lbr_enable) goto warn;if (test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use)) goto warn;@@ -768,7 +777,10 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)static void intel_pmu_cleanup (struct kvm_vcpu *vcpu) { -if (!(vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR )) +bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ); + +if (!lbr_enable)intel_pmu_release_guest_lbr_event (vcpu); }Signature · 1 line
2.27.0
Re: [PATCH v2 04/15] KVM: PMU: disable LBR handling if architectural LBR is available
Quoted 26 lines · Sean Christopherson
Quoted 25 lines · Yang Weijiang
From: Paolo Bonzini <[email protected]>
Traditional LBR is absent on CPU models that have architectural LBR, so disable all processing of traditional LBR MSRs if they are not there.
Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)arch/x86/kvm/vmx/pmu_intel.c +1
-2@@ -170,19 +170,23 @@ static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)static bool intel_pmu_is_valid_lbr_msr (struct kvm_vcpu *vcpu,u32 index) {struct x86_pmu_lbr *records =vcpu_to_lbr_records (vcpu); -bool ret = false;if (!intel_pmu_lbr_is_enabled (vcpu)) -return ret; +return false;
Quoted 9 lines
arch/x86/kvm/vmx/pmu_intel.c static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr) · line 177
Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +1
-3- ret = (index ==MSR_LBR_SELECT ) || (index ==MSR_LBR_TOS ) || - (index >= records->from && index < records->from + records->nr) || - (index >= records->to && index < records->to + records->nr); + if (!guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR) &&IIUC, the MSRs flat out don't exist _and_ KVM expects to passthrough MSRs to the guest, i.e. KVM should check host support, not guest support. Probably a moot point from a functionality perspective since KVM shouldn't allow LBRs to shouldn't be enabled for the guest, but from a performance perspective, checking guest CPUID is slooow.
OK, I'll change the check.
Quoted 4 lines
That brings me to point #2, which is that KVM needs to disallow enabling legacy LBRs on CPUs that support arch LBRs. Again, IIUC, because KVM doesn't have the option to fallback to legacy LBRs,
Legacy LBR and Arch-lbr are exclusive on any platforms, on old platforms, legacy LBR is available,
on new platforms, e.g., SPR, arch-lbr is present, so we don't have fallback logic.
Quoted 25 lines
that restriction needs to be treated as a bug
fix. I'll post a separate patch unless my understanding is wrong.Quoted 22 lines
arch/x86/kvm/vmx/pmu_intel.c +10
-3+ (index == MSR_LBR_SELECT || index == MSR_LBR_TOS)) +return true; -if (!ret && records->info) - ret = (index >= records->info && index < records->info + records->nr); +if ((index >= records->from && index < records->from + records->nr) || + (index >= records->to && index < records->to + records->nr)) +return true; -return ret; +if (records->info && index >= records->info && + index < records->info + records->nr) +return true; + +return false; }static bool intel_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr)@@ -702,6 +706,9 @@ static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set)vmx_set_intercept_for_msr (vcpu, lbr->info + i,MSR_TYPE_RW , set); }
Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set) · line 709
Quoted 1 line
arch/x86/kvm/vmx/pmu_intel.c +1
-0+if (guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ))Similar to above, I really don't want to query guest CPUID in the VM-Enter path. If we establish the rule that LBRs can be enabled if and only if the correct type is enabled (traditional/legacy vs. arch), then this can simply check host support.
I understand your concerns, will try to use other efficient ways to check guest arch-lbr support.
Quoted 14 lines
arch/x86/kvm/vmx/pmu_intel.c void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) · line 753
Quoted 12 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0+return ; +vmx_set_intercept_for_msr (vcpu,MSR_LBR_SELECT ,MSR_TYPE_RW , set);vmx_set_intercept_for_msr (vcpu,MSR_LBR_TOS ,MSR_TYPE_RW , set); }@@ -742,10 +749,12 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR );Unnecessary guest CPUID lookup and VMCS read, i.e. this can be deferred to the !lbr_desc->event path.
OK
Quoted 24 lines
Quoted 24 lines
arch/x86/kvm/vmx/pmu_intel.c +5
-2if (!lbr_desc->event) {vmx_disable_lbr_msrs_passthrough (vcpu); -if (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ) +if (lbr_enable) goto warn;if (test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use)) goto warn;@@ -768,7 +777,10 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)static void intel_pmu_cleanup (struct kvm_vcpu *vcpu) { -if (!(vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR )) +bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ); + +if (!lbr_enable)intel_pmu_release_guest_lbr_event (vcpu); }Signature · 1 line
2.27.0
PATCH v2 5/15 KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
The number of Arch LBR entries available is determined by the value in host MSR_ARCH_LBR_DEPTH.DEPTH. The supported LBR depth values are enumerated in CPUID.(EAX=01CH, ECX=0):EAX[7:0]. For each bit "n" set in this field, the MSR_ARCH_LBR_DEPTH.DEPTH value of "8*(n+1)" is supported. In the first generation of Arch LBR, max entry size is 32, host configures the max size and guest always honors the setting.
Write to MSR_ARCH_LBR_DEPTH has side-effect, all LBR entries are reset to 0. Kernel PMU driver can leverage this effect to do fask reset to LBR record MSRs. KVM allows guest to achieve it when Arch LBR records MSRs are passed through to the guest.
--- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/vmx/pmu_intel.c | 58 +++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -571,6 +571,9 @@ struct kvm_pmu {* redundant check before cleanup if guest don't use vPMU at all. */ u8 event_count; + +/* Guest arch lbr depth supported by KVM. */ +u64 kvm_arch_lbr_depth; };struct kvm_pmu_ops ;
arch/x86/kvm/vmx/pmu_intel.c +55 -3
106 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [PATCH v2 05/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 576
Quoted 7 lines · Yang Weijiang
arch/x86/include/asm/kvm_host.h +3
-0@@ -571,6 +571,9 @@ struct kvm_pmu {* redundant check before cleanup if guest don't use vPMU at all. */ u8 event_count; + +/* Guest arch lbr depth supported by KVM. */ +u64 kvm_arch_lbr_depth;
Please drop the "kvm_" prefix for per-vcpu "struct kvm_pmu".
Quoted 2 lines
(quoted diff) +0
-0};
Re: [PATCH v2 05/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
Quoted 10 lines · Like Xu
arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 576
Quoted 7 lines · Yang Weijiang
arch/x86/include/asm/kvm_host.h +3
-0@@ -571,6 +571,9 @@ struct kvm_pmu {* redundant check before cleanup if guest don't use vPMU at all. */ u8 event_count; + + /* Guest arch lbr depth supported by KVM. */ + u64 kvm_arch_lbr_depth;Please drop the "
kvm_" prefix for per-vcpu "structkvm_pmu".
OK, will remove it.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +0
-0};
Re: [PATCH v2 05/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
In reply to Yang Weijiangarch/x86/kvm/vmx/pmu_intel.c static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu) · line 544
Quoted 9 lines · Yang Weijiang
arch/x86/kvm/vmx/pmu_intel.c +9
-0+static bool cpuid_enable_lbr (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct kvm_cpuid_entry2 *entry; +int depth_bit; + +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +return !static_cpu_has (X86_FEATURE_ARCH_LBR ) && +cpuid_model_is_consistent (vcpu);
Please add selftests to cover this incompatibility.
And running the SPR vcpu model on a GNR host, the guest arch lbr should be fully supported as long as both hosts have the same lbr_depth.
Sean Christopherson
maintainerRe: [PATCH v2 05/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
In reply to Yang WeijiangQuoted 18 lines · Yang Weijiang
Write to
MSR_ARCH_LBR_DEPTHhas side-effect, all LBR entries are reset to 0. Kernel PMU driver can leverage this effect to do fask reset to LBR record MSRs. KVM allows guest to achieve it when Arch LBR records MSRs are passed through to the guest.Signed-off-by Like Xu <[email protected]>Co-developed-by Yang Weijiang <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/vmx/pmu_intel.c | 58 +++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 576
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +3
-0@@ -571,6 +571,9 @@ struct kvm_pmu {* redundant check before cleanup if guest don't use vPMU at all. */ u8 event_count; + +/* Guest arch lbr depth supported by KVM. */ +u64 kvm_arch_lbr_depth;
There is zero reason to store this separately. KVM already records the allowed depth in kvm_vcpu.lbr_desc.records.nr.
arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index) · line 182
Quoted 13 lines
arch/x86/include/asm/kvm_host.h +0
-0};struct kvm_pmu_ops ;arch/x86/kvm/vmx/pmu_intel.c +2
-0@@ -178,6 +178,10 @@ static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index) (index == MSR_LBR_SELECT || index == MSR_LBR_TOS))return true; +if (index ==MSR_ARCH_LBR_DEPTH ) + return kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR) &&
Like the previous patch, since intel_pmu_lbr_is_enabled() effectively serves as a generic kvm_cpu_cap_has(LBRS) check, this can be distilled to:
if (cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {
if (index == MSR_ARCH_LBR_DEPTH || index == MSR_ARCH_LBR_CTL)
return true;
} else {
if (index == MSR_LBR_SELECT || index == MSR_LBR_TOS))
return true;
}Quoted 32 lines
arch/x86/kvm/vmx/pmu_intel.c +7
-0+guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ); +if ((index >= records->from && index < records->from + records->nr) || (index >= records->to && index < records->to + records->nr))return true;@@ -345,6 +349,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);u32 msr = msr_info->index;switch (msr) {@@ -369,6 +374,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_PEBS_DATA_CFG : msr_info->data = pmu->pebs_data_cfg;return 0 ; +case MSR_ARCH_LBR_DEPTH : + msr_info->data = lbr_desc->records.nr; +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -395,6 +403,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);u32 msr = msr_info->index;u64 data = msr_info->data;u64 reserved_bits, diff;
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 469
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-0@@ -456,6 +465,24 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; }break ; +case MSR_ARCH_LBR_DEPTH : +if (!pmu->kvm_arch_lbr_depth && !msr_info->host_initiated)
Don't invent a new check, just prevent KVM from reaching this path via the existing intel_pmu_lbr_is_enabled().
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 474
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +5
-0+return 1 ; +/* +* When guest/host depth are different, the handling would be tricky, +* so only max depth is supported for both host and guest. +*/
This semi-arbitrary restriction is fine because Intel's architecture allows KVM to enumerate support for a single depth, but somewhere in the changelog and/or code that actually needs to be state. This blurb
In the first generation of Arch LBR, max entry size is 32, host configures the max size and guest always honors the setting.
makes it sound like KVM is relying on the guest to do the right thing, and this code looks like KVM is making up it's own behavior.
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 483
Quoted 9 lines
arch/x86/kvm/vmx/pmu_intel.c +9
-0+if (data != pmu->kvm_arch_lbr_depth) +return 1 ; + + lbr_desc->records.nr = data; +/* +* Writing depth MSR from guest could either setting the +* MSR or resetting the LBR records with the side-effect. +*/ +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ))
Another check, really? KVM shouldn't reach this point if KVM doesn't support Arch LBRs. And if that isn't guarantee (honestly forgot what this series actually proposed at this point), then that's a bug, full stop.
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 484
Quoted 1 line
arch/x86/kvm/vmx/pmu_intel.c +1
-0+wrmsrl (MSR_ARCH_LBR_DEPTH , lbr_desc->records.nr);
IIUC, this is subtly broken. Piecing together all of the undocumented bits, my understanding is that arch LBRs piggyback KVM's existing LBR support, i.e. use a "virtual" perf event. And like traditional LBR support, the host can steal control of the LBRs in IRQ context by disabling the perf event via IPI. And since writes to MSR_ARCH_LBR_DEPTH purge LBR records, this needs to be treated as if it were a write to an LBR record, i.e. belongs in the IRQs disabled section of intel_pmu_handle_lbr_msrs_access().
If for some magical reason it's safe to access arch LBR MSRs without disabling IRQs and confirming perf event ownership, I want to see a very detailed changelog explaining exactly how that magic works.
Quoted 17 lines
arch/x86/kvm/vmx/pmu_intel.c +10
-0+return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -506,6 +533,32 @@ static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu) } } +static bool cpuid_enable_lbr (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct kvm_cpuid_entry2 *entry; +int depth_bit; + +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +return !static_cpu_has (X86_FEATURE_ARCH_LBR ) && +cpuid_model_is_consistent (vcpu);
arch/x86/kvm/vmx/pmu_intel.c static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu) · line 554
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10
-0+ + pmu->kvm_arch_lbr_depth =0 ; +if (!guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) +return false; + + entry =kvm_find_cpuid_entry (vcpu,0x1C ); +if (!entry) +return false; + + depth_bit =fls (cpuid_eax (0x1C ) &0xff );
This is unnecessarily fragile. Get the LBR depth from perf, don't read CPUID and assume perf will always configured the max depth.,
This enabling also belongs at the tail end of the series, i.e. KVM shouldn't let userspace enable LBRs until all the support pieces are in place.
Quoted 32 lines
arch/x86/kvm/vmx/pmu_intel.c +9
-3+if ((entry->eax &0xff ) != (1 << (depth_bit -1 ))) +return false; + + pmu->kvm_arch_lbr_depth = depth_bit *8 ; +return true; +} +static void intel_pmu_refresh (struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);@@ -589,9 +642,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)bitmap_set (pmu->all_valid_pmc_idx,INTEL_PMC_MAX_GENERIC , pmu->nr_arch_fixed_counters); - perf_capabilities =vcpu_get_perf_capabilities (vcpu); -if (cpuid_model_is_consistent (vcpu) && - (perf_capabilities &PMU_CAP_LBR_FMT )) +if (cpuid_enable_lbr (vcpu))x86_perf_get_lbr (&lbr_desc->records);else lbr_desc->records.nr =0 ;@@ -599,6 +650,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)if (lbr_desc->records.nr)bitmap_set (pmu->all_valid_pmc_idx,INTEL_PMC_IDX_FIXED_VLBR ,1 ); + perf_capabilities =vcpu_get_perf_capabilities (vcpu);if (perf_capabilities &PERF_CAP_PEBS_FORMAT ) {if (perf_capabilities &PERF_CAP_PEBS_BASELINE ) { pmu->pebs_enable_mask = counter_mask;Signature · 1 line
2.27.0
Re: [PATCH v2 05/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_DEPTH for guest Arch LBR
Quoted 12 lines · Sean Christopherson
arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 576
Quoted 9 lines · Yang Weijiang
[...]
+++b/arch/x86/include/asm/kvm_host.harch/x86/include/asm/kvm_host.h +3
-0@@ -571,6 +571,9 @@ struct kvm_pmu {* redundant check before cleanup if guest don't use vPMU at all. */ u8 event_count; + +/* Guest arch lbr depth supported by KVM. */ +u64 kvm_arch_lbr_depth;There is zero reason to store this separately. KVM already records the allowed depth in kvm_vcpu.lbr_desc.records.nr.
kvm_vcpu.lbr_desc.records.nr alone cannot tell whether it's legacy lbr or arch-lbr unless
binding host arch-lbr checking.
Quoted 17 lines
Quoted 9 lines
arch/x86/include/asm/kvm_host.h +0
-0};struct kvm_pmu_ops ;arch/x86/kvm/vmx/pmu_intel.c +0
-0arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index) · line 182
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-0@@ -178,6 +178,10 @@ static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index) (index == MSR_LBR_SELECT || index == MSR_LBR_TOS))return true; +if (index ==MSR_ARCH_LBR_DEPTH ) + return kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR) &&Like the previous patch, since
intel_pmu_lbr_is_enabled()effectively serves as a generickvm_cpu_cap_has(LBRS)check, this can be distilled to:
Quoted 7 lines
if (cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {
if (index == MSR_ARCH_LBR_DEPTH || index == MSR_ARCH_LBR_CTL)
return true;
} else {
if (index == MSR_LBR_SELECT || index == MSR_LBR_TOS))
return true;
}yes, exactly, thanks!
Quoted 31 lines
Quoted 31 lines
arch/x86/kvm/vmx/pmu_intel.c +7
-0+guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ); +if ((index >= records->from && index < records->from + records->nr) || (index >= records->to && index < records->to + records->nr))return true;@@ -345,6 +349,7 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);u32 msr = msr_info->index;switch (msr) {@@ -369,6 +374,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_PEBS_DATA_CFG : msr_info->data = pmu->pebs_data_cfg;return 0 ; +case MSR_ARCH_LBR_DEPTH : + msr_info->data = lbr_desc->records.nr; +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -395,6 +403,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct kvm_pmc *pmc; +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);u32 msr = msr_info->index;u64 data = msr_info->data;u64 reserved_bits, diff;
Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 469
Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-0@@ -456,6 +465,24 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 0 ; }break ; +case MSR_ARCH_LBR_DEPTH : +if (!pmu->kvm_arch_lbr_depth && !msr_info->host_initiated)Don't invent a new check, just prevent KVM from reaching this path via the existing
intel_pmu_lbr_is_enabled().
intel_pmu_lbr_is_enabled() only indicates LBR is on(either legacy or arch-lbr), but
MSR_ARCH_LBR_DEPTH is only for arch-lbr.
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 474
Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +5
-0+return 1 ; +/* +* When guest/host depth are different, the handling would be tricky, +* so only max depth is supported for both host and guest. +*/ This semi-arbitrary restriction is fine because Intel's architecture allows KVM to enumerate support for a single depth, but somewhere in the changelog and/or code that actually needs to be state. This blurb
Quoted 5 lines
In the first generation of Arch LBR, max entry size is 32, host configures the max size and guest always honors the setting.
makes it sound like KVM is relying on the guest to do the right thing, and this code looks like KVM is making up it's own behavior.
Will modify the change log.
Quoted 13 lines
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 483
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +9
-0+if (data != pmu->kvm_arch_lbr_depth) +return 1 ; + + lbr_desc->records.nr = data; +/* +* Writing depth MSR from guest could either setting the +* MSR or resetting the LBR records with the side-effect. +*/ +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ))Another check, really? KVM shouldn't reach this point if KVM doesn't support Arch LBRs. And if that isn't guarantee (honestly forgot what this series actually proposed at this point), then that's a bug, full stop.
Right, this check is unnecessary.
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 484
Quoted 2 lines
arch/x86/kvm/vmx/pmu_intel.c +1
-0+wrmsrl (MSR_ARCH_LBR_DEPTH , lbr_desc->records.nr);IIUC, this is subtly broken. Piecing together all of the undocumented bits, my understanding is that arch LBRs piggyback KVM's existing LBR support, i.e. use a "virtual" perf event.
Yes.
Quoted 5 lines
And like traditional LBR support, the host can steal control
of the LBRs in IRQ context by disabling the perf event via IPI. And since writes toMSR_ARCH_LBR_DEPTHpurge LBR records, this needs to be treated as if it were a write to an LBR record, i.e. belongs in the IRQs disabled section ofintel_pmu_handle_lbr_msrs_access().
I assume you're referring to host events preempt guest events. In that case, it's possible
guest operations interfere host events/data. But this series implementation focus on
"guest only" mode, i.e., it sets {Load|Clear}_LBR_CTL at VM entry/exit, that way, we don't
need to care about host preempt, the event data is saved/restored at event sched_{out|in}.
Quoted 4 lines
If for some magical reason it's safe to access arch LBR MSRs without disabling IRQs and confirming perf event ownership, I want to see a very detailed changelog explaining exactly how that magic works.
Will change the commit log to explain more.
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/vmx/pmu_intel.c +14
-0+return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -506,6 +533,32 @@ static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu) } } +static bool cpuid_enable_lbr (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct kvm_cpuid_entry2 *entry; +int depth_bit; + +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +return !static_cpu_has (X86_FEATURE_ARCH_LBR ) && +cpuid_model_is_consistent (vcpu); + + pmu->kvm_arch_lbr_depth =0 ; +if (!guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) +return false;
Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu) · line 554
Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +6
-0+ + entry =kvm_find_cpuid_entry (vcpu,0x1C ); +if (!entry) +return false; + + depth_bit =fls (cpuid_eax (0x1C ) &0xff );This is unnecessarily fragile. Get the LBR depth from perf, don't read CPUID and assume perf will always configured the max depth.,
Make sense, will refactor the function in next version.
Quoted 3 lines
This enabling also belongs at the tail end of the series, i.e. KVM shouldn't let userspace enable LBRs until all the support pieces are in place.
OK.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c +7
-0+if ((entry->eax &0xff ) != (1 << (depth_bit -1 ))) +return false; + + pmu->kvm_arch_lbr_depth = depth_bit *8 ; +return true; +} +
[...]
PATCH v2 6/15 KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
In reply to Yang WeijiangFrom: Paolo Bonzini <[email protected]>
Arch LBR is enabled by setting MSR_ARCH_LBR_CTL.LBREn to 1. A new guest state field named "Guest IA32_LBR_CTL" is added to enhance guest LBR usage. When guest Arch LBR is enabled, a guest LBR event will be created like the model-specific LBR does. Clear guest LBR enable bit on host PMI handling so guest can see expected config.
On processors that support Arch LBR, MSR_IA32_DEBUGCTLMSR[bit 0] has no meaning. It can be written to 0 or 1, but reads will always return 0. Like IA32_DEBUGCTL, IA32_ARCH_LBR_CTL msr is also preserved on INIT.
Regardless of the Arch LBR or legacy LBR, when the LBR_EN bit 0 of the corresponding control MSR is set to 1, LBR recording will be enabled.
--- arch/x86/events/intel/lbr.c | 2 - arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 2 + arch/x86/kvm/vmx/pmu_intel.c | 67 ++++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.c | 7 ++++ 5 files changed, 69 insertions(+), 10 deletions(-)
arch/x86/events/intel/lbr.c +0 -2
@@ -100,8 +100,6 @@ ARCH_LBR_RETURN |\ ARCH_LBR_OTHER_BRANCH) -#define ARCH_LBR_CTL_MASK 0x7f000e -static void intel_pmu_lbr_filter (struct cpu_hw_events *cpuc); static __always_inline bool is_lbr_call_stack_bit_set(u64 config)
arch/x86/include/asm/msr-index.h +1 -0
@@ -220,6 +220,7 @@#define LBR_INFO_BR_TYPE (0xfull << LBR_INFO_BR_TYPE_OFFSET)#define MSR_ARCH_LBR_CTL 0x000014ce +#define ARCH_LBR_CTL_MASK 0x7f000e#define ARCH_LBR_CTL_LBREN BIT(0)#define ARCH_LBR_CTL_CPL_OFFSET 1#define ARCH_LBR_CTL_CPL (0x3ull << ARCH_LBR_CTL_CPL_OFFSET)
arch/x86/include/asm/vmx.h +2 -0
@@ -257,6 +257,8 @@ enum vmcs_field {GUEST_BNDCFGS_HIGH =0x00002813 ,GUEST_IA32_RTIT_CTL =0x00002814 ,GUEST_IA32_RTIT_CTL_HIGH =0x00002815 , +GUEST_IA32_LBR_CTL =0x00002816 , +GUEST_IA32_LBR_CTL_HIGH =0x00002817 ,HOST_IA32_PAT =0x00002c00 ,HOST_IA32_PAT_HIGH =0x00002c01 , HOST_IA32_EFER = 0x00002c02,
arch/x86/kvm/vmx/pmu_intel.c +59 -8
116 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +7 -0
@@ -2120,6 +2120,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl = data; + /* + * For Arch LBR, IA32_DEBUGCTL[bit 0] has no meaning. + * It can be written to 0 or 1, but reads will always return 0. + */ + if (guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR)) + data &= ~DEBUGCTLMSR_LBR; + vmcs_write64(GUEST_IA32_DEBUGCTL, data); if (intel_pmu_lbr_is_enabled(vcpu) && !to_vmx(vcpu)->lbr_desc.event && (data & DEBUGCTLMSR_LBR))
Signature · 1 line
2.27.0
Re: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 407
Quoted 10 lines · Yang Weijiang
static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);arch/x86/kvm/vmx/pmu_intel.c +3
-0@@ -377,6 +402,14 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_ARCH_LBR_DEPTH : msr_info->data = lbr_desc->records.nr;return 0 ; +case MSR_ARCH_LBR_CTL : +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { +WARN_ON_ONCE (!msr_info->host_initiated);
Why we need this warning even in the format of WARN_ON_ONCE() ? And why not for MSR_ARCH_LBR_DEPTH msr ?
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +5
-0+ msr_info->data =0 ; + }else { + msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); + } +return 0 ;
Re: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
Quoted 15 lines · Like Xu
Quoted 11 lines · Yang Weijiang
static int
intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info){
structkvm_pmu*pmu =vcpu_to_pmu(vcpu);(quoted diff) +0
-0@@ -377,6 +402,14 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case
MSR_ARCH_LBR_DEPTH:
msr_info->data=lbr_desc->records.nr; return 0;
+ caseMSR_ARCH_LBR_CTL:
+ if (!kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR)) { +WARN_ON_ONCE(!msr_info->host_initiated);Why we need this warning even in the format of
WARN_ON_ONCE()? And why not forMSR_ARCH_LBR_DEPTHmsr ?
IMO, this is just to give some informative message. Not necessary for every
arch-lbr MSRs as MSR_ARCH_LBR_CTL is the master control msr.
Quoted 6 lines
Quoted 6 lines
+
msr_info->data= 0;
+ } else {
+msr_info->data=vmcs_read64(GUEST_IA32_LBR_CTL); + }
+ return 0;
Re: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
In reply to Yang WeijiangQuoted 12 lines · Yang Weijiang
arch/x86/kvm/vmx/pmu_intel.c +2
-5@@ -727,12 +772,16 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)*/ static void intel_pmu_legacy_freezing_lbrs_on_pmi (struct kvm_vcpu *vcpu) { -u64 data =vmcs_read64 (GUEST_IA32_DEBUGCTL ); +u32 lbr_ctl_field =GUEST_IA32_DEBUGCTL ; -if (data &DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ) { - data &= ~DEBUGCTLMSR_LBR ; -vmcs_write64 (GUEST_IA32_DEBUGCTL , data); - } +if (!(vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ))
arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_reset(struct kvm_vcpu *vcpu) · line 787
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +7
-0+return ; + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) + lbr_ctl_field =GUEST_IA32_LBR_CTL ; + +vmcs_write64 (lbr_ctl_field,vmcs_read64 (lbr_ctl_field) & ~0x1ULL ); }static void intel_pmu_deliver_pmi (struct kvm_vcpu *vcpu)
The legacy lbr test case in KUT does not cover this scenario, but arch lbr contributor should take the opportunity to fill this gap. Thanks.
Re: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
Quoted 17 lines · Like Xu
Quoted 16 lines · Yang Weijiang
arch/x86/kvm/vmx/pmu_intel.c +6
-4@@ -727,12 +772,16 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)*/ static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu) { - u64 data = vmcs_read64(GUEST_IA32_DEBUGCTL); + u32 lbr_ctl_field = GUEST_IA32_DEBUGCTL; - if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) { - data &= ~DEBUGCTLMSR_LBR; - vmcs_write64(GUEST_IA32_DEBUGCTL, data); - } + if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) &DEBUGCTLMSR_FREEZE_LBRS_ON_PMI )) + return; + + if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR) && + guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR))
Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_reset(struct kvm_vcpu *vcpu) · line 787
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +3
-0+ lbr_ctl_field = GUEST_IA32_LBR_CTL; + + vmcs_write64(lbr_ctl_field, vmcs_read64(lbr_ctl_field) & ~0x1ULL); } static void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu)The legacy lbr test case in KUT does not cover this scenario, but arch lbr contributor should take the opportunity to fill this gap. Thanks.
OK, will try to add this missing part check.
Re: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
In reply to Yang Weijiangarch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2126
Quoted 12 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +4
-0@@ -2120,6 +2120,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl = data; +/* +* For Arch LBR, IA32_DEBUGCTL[bit 0] has no meaning. +* It can be written to 0 or 1, but reads will always return 0. +*/
The comment looks good, please verify it with a test.
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0+if (guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) + data &= ~DEBUGCTLMSR_LBR ; +vmcs_write64 (GUEST_IA32_DEBUGCTL , data);if (intel_pmu_lbr_is_enabled (vcpu) && !to_vmx (vcpu)->lbr_desc.event && (data &DEBUGCTLMSR_LBR ))
Re: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
Quoted 15 lines · Like Xu
Quoted 8 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +0
-0@@ -2120,6 +2120,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl = data;Quoted 4 lines
+ /*
+ * For Arch LBR, IA32_DEBUGCTL[bit 0] has no meaning. + * It can be written to 0 or 1, but reads will always return 0. + */The comment looks good, please verify it with a test.
OK, I'll add the test into pmu_lbr KUT test together with arch-lbr test cases.
Quoted 7 lines
Quoted 7 lines
+ if (
guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR))
+ data &= ~DEBUGCTLMSR_LBR;
+
vmcs_write64(GUEST_IA32_DEBUGCTL, data);
if (intel_pmu_lbr_is_enabled(vcpu)&& !to_vmx(vcpu)->lbr_desc.event && (data &DEBUGCTLMSR_LBR))
Sean Christopherson
maintainerRe: [PATCH v2 06/15] KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
In reply to Yang WeijiangQuoted 6 lines · Yang Weijiang
arch/x86/kvm/vmx/pmu_intel.c +2
-0@@ -345,6 +346,30 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,return true; } +static bool arch_lbr_ctl_is_valid (struct kvm_vcpu *vcpu,u64 ctl) +{
arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 360
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10
-0+struct kvm_cpuid_entry2 *entry; +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (!pmu->kvm_arch_lbr_depth) +return false; + +if (ctl & ~KVM_ARCH_LBR_CTL_MASK ) +return false; + + entry =kvm_find_cpuid_entry (vcpu,0x1c );
Why!?!?! Why does this series reinvent the wheel so many times. We already have a huge pile of reserved bits masks that are computed in intel_pmu_refresh(), just do the easy thing and follow the established pattern.
Quoted 16 lines
arch/x86/kvm/vmx/pmu_intel.c +12
-0+if (!entry) +return false; + +if (!(entry->ebx &BIT (0 )) && (ctl &ARCH_LBR_CTL_CPL )) +return false; +if (!(entry->ebx &BIT (2 )) && (ctl &ARCH_LBR_CTL_STACK )) +return false; +if (!(entry->ebx &BIT (1 )) && (ctl &ARCH_LBR_CTL_FILTER )) +return false; +return true; +} +static int intel_pmu_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 406
Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-0@@ -377,6 +402,14 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_ARCH_LBR_DEPTH : msr_info->data = lbr_desc->records.nr;return 0 ; +case MSR_ARCH_LBR_CTL : +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) {
So I assume the point of this code is to allow reading MSR_ARCH_LBR_CTL from userspace before doing KVM_SET_CPUID2, but I would much rather do that in a generic way, i.e. build this series on
https://lore.kernel.org/all/[email protected]
Quoted 35 lines
arch/x86/kvm/vmx/pmu_intel.c +19
-1+WARN_ON_ONCE (!msr_info->host_initiated); + msr_info->data =0 ; + }else { + msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); + } +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -483,6 +516,18 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ))wrmsrl (MSR_ARCH_LBR_DEPTH , lbr_desc->records.nr);return 0 ; +case MSR_ARCH_LBR_CTL : +if (msr_info->host_initiated && !pmu->kvm_arch_lbr_depth) +return data !=0 ; + +if (!arch_lbr_ctl_is_valid (vcpu, data)) +break ; + +vmcs_write64 (GUEST_IA32_LBR_CTL , data); +if (intel_pmu_lbr_is_enabled (vcpu) && !lbr_desc->event && + (data &ARCH_LBR_CTL_LBREN )) +intel_pmu_create_guest_lbr_event (vcpu); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -727,12 +772,16 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)*/ static void intel_pmu_legacy_freezing_lbrs_on_pmi (struct kvm_vcpu *vcpu) { -u64 data =vmcs_read64 (GUEST_IA32_DEBUGCTL ); +u32 lbr_ctl_field =GUEST_IA32_DEBUGCTL ;
arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_reset(struct kvm_vcpu *vcpu) · line 782
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +6
-4-if (data &DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ) { - data &= ~DEBUGCTLMSR_LBR ; -vmcs_write64 (GUEST_IA32_DEBUGCTL , data); - } +if (!(vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_FREEZE_LBRS_ON_PMI )) +return ; + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR )) + lbr_ctl_field =GUEST_IA32_LBR_CTL ;
Similar to other comments, just rely on KVM not allowing LBRs to be enabled unless the guest is configured with the correct flavor.
Review static void intel_pmu_reset(struct kvm_vcpu *vcpu) · line 784
Quoted 2 lines
(quoted diff) +2
-0+ + vmcs_write64(lbr_ctl_field, vmcs_read64(lbr_ctl_field) & ~0x1ULL);
Open coding a bit just because it happens to be in the same position in both fields is ridiculous.
u64 debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); if (!debugctl & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI)) return; if (cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) vmcs_clear_bits64(GUEST_IA32_LBR_CTL, ARCH_LBR_CTL_LBREN); else vmcs_write64(GUEST_IA32_DEBUGCTL, debugctl & ~DEBUGCTLMSR_LBR);
Quoted 14 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-1}static void intel_pmu_deliver_pmi (struct kvm_vcpu *vcpu)@@ -801,7 +850,8 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); -bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && +bool lbr_enable =guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) ? + (vmcs_read64 (GUEST_IA32_LBR_CTL ) &ARCH_LBR_CTL_LBREN ) : (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR );if (!lbr_desc->event) {
arch/x86/kvm/vmx/pmu_intel.c void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) · line 884
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +2
-1@@ -829,7 +879,8 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)static void intel_pmu_cleanup (struct kvm_vcpu *vcpu) { -bool lbr_enable = !guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) && +bool lbr_enable =guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ) ? + (vmcs_read64 (GUEST_IA32_LBR_CTL ) &ARCH_LBR_CTL_LBREN ) : (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR );
Instead of open coding the same ugly ternary operator in multiple locations, add a helper.
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +0
-0if (!lbr_enable)arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2127
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +5
-0@@ -2120,6 +2120,13 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) VM_EXIT_SAVE_DEBUG_CONTROLS) get_vmcs12(vcpu)->guest_ia32_debugctl = data; + /* + * For Arch LBR, IA32_DEBUGCTL[bit 0] has no meaning. + * It can be written to 0 or 1, but reads will always return 0. + */ + if (guest_cpuid_has(vcpu, X86_FEATURE_ARCH_LBR))
This can be dependent solely on the host CPU. If LBRs are unsupported, the bit will be marked invalid and cleared above. And as mentioned multiple times, KVM needs to allow enabling LBRs iff the guest and host flavors match.
arch/x86/kvm/vmx/vmx.c static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 2128
Quoted 1 line
arch/x86/kvm/vmx/vmx.c +1
-0+ data &= ~DEBUGCTLMSR_LBR ;
This needs to be done before shoving the value into vmcs12.
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +1
-0+vmcs_write64 (GUEST_IA32_DEBUGCTL , data);if (intel_pmu_lbr_is_enabled (vcpu) && !to_vmx (vcpu)->lbr_desc.event && (data &DEBUGCTLMSR_LBR ))Signature · 1 line
2.27.0
PATCH v2 7/15 KVM: VMX: Support passthrough of architectural LBRs
In reply to Yang WeijiangFrom: Paolo Bonzini <[email protected]>
MSR_ARCH_LBR_* can be pointed to by records->from, records->to and records->info, so list them in is_valid_passthrough_msr.
--- arch/x86/kvm/vmx/vmx.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -638,6 +638,9 @@ static bool is_valid_passthrough_msr(u32 msr) case MSR_LBR_NHM_TO ... MSR_LBR_NHM_TO + 31: case MSR_LBR_CORE_FROM ... MSR_LBR_CORE_FROM + 8: case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8: + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31:/* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ return true; }
Signature · 1 line
2.27.0
PATCH v2 8/15 KVM: x86: Add Arch LBR MSRs to msrs_to_save_all list
In reply to Yang WeijiangArch LBR MSR_ARCH_LBR_DEPTH and MSR_ARCH_LBR_CTL are queried by userspace application before it wants to {save|restore} the Arch LBR data. Other LBR related data MSRs are omitted here intentionally due to lengthy list(32*3). Userspace can still use KVM_{GET|SET}_MSRS to access them if necessary.
--- arch/x86/kvm/x86.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/x86.c +10 -0
@@ -1466,6 +1466,7 @@ static const u32 msrs_to_save_all[] = {MSR_IA32_XFD ,MSR_IA32_XFD_ERR ,MSR_IA32_XSS , + MSR_ARCH_LBR_CTL, MSR_ARCH_LBR_DEPTH, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -3877,6 +3878,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_PEBS_ENABLE :case MSR_IA32_DS_AREA :case MSR_PEBS_DATA_CFG : +case MSR_ARCH_LBR_CTL : +case MSR_ARCH_LBR_DEPTH : case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info);
@@ -3980,6 +3983,8 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_IA32_PEBS_ENABLE :case MSR_IA32_DS_AREA :case MSR_PEBS_DATA_CFG : +case MSR_ARCH_LBR_CTL : +case MSR_ARCH_LBR_DEPTH : case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:if (kvm_pmu_is_valid_msr (vcpu, msr_info->index))return kvm_pmu_get_msr (vcpu, msr_info);
@@ -7068,6 +7073,11 @@ static void kvm_init_msr_list(void)if (!kvm_caps.supported_xss)continue ;break ; +case MSR_ARCH_LBR_DEPTH : +case MSR_ARCH_LBR_CTL : +if (!kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +continue ; +break ;default :break ; }
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 08/15] KVM: x86: Add Arch LBR MSRs to msrs_to_save_all list
Quoted 5 lines · Yang Weijiang
Arch LBR
MSR_ARCH_LBR_DEPTHandMSR_ARCH_LBR_CTLare queried by userspace application before it wants to {save|restore} the Arch LBR data. Other LBR related data MSRs are omitted here intentionally due to lengthylist(32*3). Userspace can still use KVM_{GET|SET}_MSRS to access them if necessary.
Absolutely not. "there are a lot of them" isn't sufficient justification. If the MSRs need to be migrated, then KVM needs to report them. If the expectation is that XSAVES will handle them, then KVM needs to take a dependency on XSAVES when enabling arch LBRs.
Re: [PATCH v2 08/15] KVM: x86: Add Arch LBR MSRs to msrs_to_save_all list
Quoted 10 lines · Sean Christopherson
Quoted 5 lines · Yang Weijiang
Arch LBR
MSR_ARCH_LBR_DEPTHandMSR_ARCH_LBR_CTLare queried by userspace application before it wants to {save|restore} the Arch LBR data. Other LBR related data MSRs are omitted here intentionally due to lengthylist(32*3). Userspace can still use KVM_{GET|SET}_MSRS to access them if necessary.Absolutely not. "there are a lot of them" isn't sufficient justification. If the MSRs need to be migrated, then KVM needs to report them. If the expectation is that XSAVES will handle them, then KVM needs to take a dependency on XSAVES when enabling arch LBRs.
OK, will add the full MSR list.
PATCH v2 9/15 KVM: x86: Refine the matching and clearing logic for supported_xss
In reply to Yang WeijiangFrom: Paolo Bonzini <[email protected]>
Refine the code path of the existing clearing of supported_xss in this way:
initialize the supported_xss with the filter of KVM_SUPPORTED_XSS mask and update its value in a bit clear manner (rather than bit setting).
--- arch/x86/kvm/vmx/vmx.c | 5 +++-- arch/x86/kvm/x86.c | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +3 -2
@@ -7738,9 +7738,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - kvm_caps.supported_xss =0 ; -if (!cpu_has_vmx_xsaves ()) +if (!cpu_has_vmx_xsaves ()) {kvm_cpu_cap_clear (X86_FEATURE_XSAVES ); + kvm_caps.supported_xss =0 ; + }/* CPUID 0x80000001 and 0x7 (RDPID) */ if (!cpu_has_vmx_rdtscp ()) {
arch/x86/kvm/x86.c +5 -1
@@ -217,6 +217,8 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) +#define KVM_SUPPORTED_XSS 0 +u64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);
@@ -11999,8 +12001,10 @@ int kvm_arch_hardware_setup(void *opaque)rdmsrl_safe (MSR_EFER , &host_efer); -if (boot_cpu_has (X86_FEATURE_XSAVES )) +if (boot_cpu_has (X86_FEATURE_XSAVES )) {rdmsrl (MSR_IA32_XSS , host_xss); + kvm_caps.supported_xss = host_xss &KVM_SUPPORTED_XSS ; + }kvm_init_pmu_capability ();
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 09/15] KVM: x86: Refine the matching and clearing logic for supported_xss
Quoted 20 lines · Yang Weijiang
From: Paolo Bonzini <[email protected]>
Refine the code path of the existing clearing of
supported_xssin this way:
initialize thesupported_xsswith the filter ofKVM_SUPPORTED_XSSmask and update its value in a bit clear manner (rather than bit setting).Suggested-by Sean Christopherson <[email protected]>Signed-off-by Like Xu <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 5 +++-- arch/x86/kvm/x86.c | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c static __init void vmx_set_cpu_caps(void) · line 7741
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +0
-1@@ -7738,9 +7738,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - kvm_caps.supported_xss =0 ;
This needs to stay until VMX actually supports something.
arch/x86/kvm/vmx/vmx.c static __init void vmx_set_cpu_caps(void) · line 7743
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +2
-1-if (!cpu_has_vmx_xsaves ()) +if (!cpu_has_vmx_xsaves ()) {kvm_cpu_cap_clear (X86_FEATURE_XSAVES ); + kvm_caps.supported_xss =0 ;
This is already handled in common KVM.
Quoted 32 lines
arch/x86/kvm/vmx/vmx.c +1
-0+ }/* CPUID 0x80000001 and 0x7 (RDPID) */ if (!cpu_has_vmx_rdtscp ()) {arch/x86/kvm/x86.c +5
-1@@ -217,6 +217,8 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) +#define KVM_SUPPORTED_XSS 0 +u64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);@@ -11999,8 +12001,10 @@ int kvm_arch_hardware_setup(void *opaque)rdmsrl_safe (MSR_EFER , &host_efer); -if (boot_cpu_has (X86_FEATURE_XSAVES )) +if (boot_cpu_has (X86_FEATURE_XSAVES )) {rdmsrl (MSR_IA32_XSS , host_xss); + kvm_caps.supported_xss = host_xss &KVM_SUPPORTED_XSS ; + }kvm_init_pmu_capability ();Signature · 1 line
2.27.0
Re: [PATCH v2 09/15] KVM: x86: Refine the matching and clearing logic for supported_xss
Quoted 23 lines · Sean Christopherson
Quoted 22 lines · Yang Weijiang
From: Paolo Bonzini <[email protected]>
Refine the code path of the existing clearing of
supported_xssin this way:
initialize thesupported_xsswith the filter ofKVM_SUPPORTED_XSSmask and update its value in a bit clear manner (rather than bit setting).Suggested-by Sean Christopherson <[email protected]>Signed-off-by Like Xu <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 5 +++-- arch/x86/kvm/x86.c | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0@@ -7738,9 +7738,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c static __init void vmx_set_cpu_caps(void) · line 7741
Quoted 2 lines
arch/x86/kvm/vmx/vmx.c +0
-1/* CPUID 0xD.1 */ - kvm_caps.supported_xss =0 ;This needs to stay until VMX actually supports something.
Will modify this patch.
Quoted 39 lines
arch/x86/kvm/vmx/vmx.c static __init void vmx_set_cpu_caps(void) · line 7743
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-1-if (!cpu_has_vmx_xsaves ()) +if (!cpu_has_vmx_xsaves ()) {kvm_cpu_cap_clear (X86_FEATURE_XSAVES ); + kvm_caps.supported_xss =0 ;This is already handled in common KVM.
Quoted 32 lines
arch/x86/kvm/vmx/vmx.c +1
-0+ }/* CPUID 0x80000001 and 0x7 (RDPID) */ if (!cpu_has_vmx_rdtscp ()) {arch/x86/kvm/x86.c +5
-1@@ -217,6 +217,8 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) +#define KVM_SUPPORTED_XSS 0 +u64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);@@ -11999,8 +12001,10 @@ int kvm_arch_hardware_setup(void *opaque)rdmsrl_safe (MSR_EFER , &host_efer); -if (boot_cpu_has (X86_FEATURE_XSAVES )) +if (boot_cpu_has (X86_FEATURE_XSAVES )) {rdmsrl (MSR_IA32_XSS , host_xss); + kvm_caps.supported_xss = host_xss &KVM_SUPPORTED_XSS ; + }kvm_init_pmu_capability ();Signature · 1 line
2.27.0
PATCH v2 10/15 KVM: x86/vmx: Check Arch LBR config when return perf capabilities
In reply to Yang WeijiangTwo new bit fields(VM_EXIT_CLEAR_IA32_LBR_CTL, VM_ENTRY_LOAD_IA32_LBR_CTL) are added to support guest Arch LBR. These two bits should be set in order to make Arch LBR workable in both guest and host. Since we don't support Arch LBR in nested guest, clear the two bits before run L2 VM.
--- arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/capabilities.h | 5 +++++ arch/x86/kvm/vmx/nested.c | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 11 +++++++++++ arch/x86/kvm/vmx/vmx.h | 6 ++++-- 5 files changed, 30 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +2 -0
@@ -102,6 +102,7 @@#define VM_EXIT_CLEAR_BNDCFGS 0x00800000#define VM_EXIT_PT_CONCEAL_PIP 0x01000000#define VM_EXIT_CLEAR_IA32_RTIT_CTL 0x02000000 +#define VM_EXIT_CLEAR_IA32_LBR_CTL 0x04000000#define VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR 0x00036dff
@@ -115,6 +116,7 @@#define VM_ENTRY_LOAD_BNDCFGS 0x00010000#define VM_ENTRY_PT_CONCEAL_PIP 0x00020000#define VM_ENTRY_LOAD_IA32_RTIT_CTL 0x00040000 +#define VM_ENTRY_LOAD_IA32_LBR_CTL 0x00200000#define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff
arch/x86/kvm/vmx/capabilities.h +5 -0
@@ -395,6 +395,11 @@ static inline bool vmx_pebs_supported(void)return boot_cpu_has (X86_FEATURE_PEBS ) && kvm_pmu_cap.pebs_ept; } +static inline bool cpu_has_vmx_arch_lbr (void ) +{ +return vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_IA32_LBR_CTL ; +} +static inline bool cpu_has_notify_vmexit (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/nested.c +8 -0
@@ -2360,6 +2360,10 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0if (guest_efer != host_efer) exec_control |=VM_ENTRY_LOAD_IA32_EFER ; } + +if (cpu_has_vmx_arch_lbr ()) + exec_control &= ~VM_ENTRY_LOAD_IA32_LBR_CTL ; +vm_entry_controls_set (vmx, exec_control);/*
@@ -2374,6 +2378,10 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 exec_control |=VM_EXIT_LOAD_IA32_EFER ; else exec_control &= ~VM_EXIT_LOAD_IA32_EFER; + +if (cpu_has_vmx_arch_lbr ()) + exec_control &= ~VM_EXIT_CLEAR_IA32_LBR_CTL ; +vm_exit_controls_set (vmx, exec_control);/*
arch/x86/kvm/vmx/vmx.c +11 -0
@@ -2599,6 +2599,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, { VM_ENTRY_LOAD_IA32_EFER, VM_EXIT_LOAD_IA32_EFER }, { VM_ENTRY_LOAD_BNDCFGS, VM_EXIT_CLEAR_BNDCFGS }, { VM_ENTRY_LOAD_IA32_RTIT_CTL, VM_EXIT_CLEAR_IA32_RTIT_CTL }, + { VM_ENTRY_LOAD_IA32_LBR_CTL, VM_EXIT_CLEAR_IA32_LBR_CTL }, };memset (vmcs_conf,0 ,sizeof (*vmcs_conf));
@@ -4794,6 +4795,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vpid_sync_context (vmx->vpid);vmx_update_fb_clear_dis (vcpu, vmx); + +if (!init_event &&cpu_has_vmx_arch_lbr ()) +vmcs_write64 (GUEST_IA32_LBR_CTL ,0 ); }static void vmx_enable_irq_window (struct kvm_vcpu *vcpu)
@@ -6191,6 +6195,10 @@ void dump_vmcs(struct kvm_vcpu *vcpu) vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)pr_err ("PerfGlobCtl = 0x%016llx\n" ,vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL )); +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && + vmentry_ctl &VM_ENTRY_LOAD_IA32_LBR_CTL ) +pr_err ("ArchLBRCtl = 0x%016llx\n" , +vmcs_read64 (GUEST_IA32_LBR_CTL ));if (vmentry_ctl &VM_ENTRY_LOAD_BNDCFGS )pr_err ("BndCfgS = 0x%016llx\n" ,vmcs_read64 (GUEST_BNDCFGS )); pr_err("Interruptibility = %08x ActivityState = %08x\n",
@@ -7700,6 +7708,9 @@ static u64 vmx_get_perf_capabilities(void) perf_cap &= ~PERF_CAP_PEBS_BASELINE ; } +if (boot_cpu_has (X86_FEATURE_ARCH_LBR ) && !cpu_has_vmx_arch_lbr ()) + perf_cap &= ~PMU_CAP_LBR_FMT ; +return perf_cap; }
arch/x86/kvm/vmx/vmx.h +4 -2
@@ -493,7 +493,8 @@ static inline u8 vmx_get_rvi(void) VM_ENTRY_LOAD_IA32_EFER | \ VM_ENTRY_LOAD_BNDCFGS | \ VM_ENTRY_PT_CONCEAL_PIP | \ - VM_ENTRY_LOAD_IA32_RTIT_CTL) + VM_ENTRY_LOAD_IA32_RTIT_CTL | \ + VM_ENTRY_LOAD_IA32_LBR_CTL)#define __KVM_REQUIRED_VMX_VM_EXIT_CONTROLS \ (VM_EXIT_SAVE_DEBUG_CONTROLS | \
@@ -515,7 +516,8 @@ static inline u8 vmx_get_rvi(void) VM_EXIT_LOAD_IA32_EFER | \ VM_EXIT_CLEAR_BNDCFGS | \ VM_EXIT_PT_CONCEAL_PIP | \ - VM_EXIT_CLEAR_IA32_RTIT_CTL) + VM_EXIT_CLEAR_IA32_RTIT_CTL | \ + VM_EXIT_CLEAR_IA32_LBR_CTL)#define KVM_REQUIRED_VMX_PIN_BASED_VM_EXEC_CONTROL \ (PIN_BASED_EXT_INTR_MASK | \
Signature · 1 line
2.27.0
Re: [PATCH v2 10/15] KVM: x86/vmx: Check Arch LBR config when return perf capabilities
arch/x86/kvm/vmx/nested.c static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 · line 2365
Quoted 11 lines · Yang Weijiang
arch/x86/kvm/vmx/nested.c +3
-0@@ -2360,6 +2360,10 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0if (guest_efer != host_efer) exec_control |=VM_ENTRY_LOAD_IA32_EFER ; } + +if (cpu_has_vmx_arch_lbr ()) + exec_control &= ~VM_ENTRY_LOAD_IA32_LBR_CTL ;
Please verify that (arch) lbr is not available in the nested test case.
Thus when we support nested lbr, the developer will be aware of the need for test case updates
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +1
-0+vm_entry_controls_set (vmx, exec_control);/*
Re: [PATCH v2 10/15] KVM: x86/vmx: Check Arch LBR config when return perf capabilities
Quoted 16 lines · Like Xu
Quoted 5 lines · Yang Weijiang
arch/x86/kvm/vmx/nested.c +0
-0@@ -2360,6 +2360,10 @@ static void prepare_vmcs02_early(structQuoted 7 lines
vcpu_vmx*vmx, structloaded_vmcs*vmcs0
if (guest_efer!=host_efer)exec_control|=VM_ENTRY_LOAD_IA32_EFER; }
+
+ if (cpu_has_vmx_arch_lbr())
+exec_control&= ~VM_ENTRY_LOAD_IA32_LBR_CTL;Please verify that (arch) lbr is not available in the nested test case.
Thus when we support nested lbr, the developer will be aware of the need for test case updates
It's not available in either KUT or selftest cases now.
Quoted 4 lines
Quoted 4 lines
+
vm_entry_controls_set(vmx, exec_control);
/*
Sean Christopherson
maintainerRe: [PATCH v2 10/15] KVM: x86/vmx: Check Arch LBR config when return perf capabilities
In reply to Yang WeijiangQuoted 17 lines · Yang Weijiang
arch/x86/kvm/vmx/capabilities.h +0
-0return vmcs_config.cpu_based_2nd_exec_ctrl &arch/x86/kvm/vmx/nested.c +4
-0@@ -2360,6 +2360,10 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0if (guest_efer != host_efer) exec_control |=VM_ENTRY_LOAD_IA32_EFER ; } + +if (cpu_has_vmx_arch_lbr ()) + exec_control &= ~VM_ENTRY_LOAD_IA32_LBR_CTL ; +vm_entry_controls_set (vmx, exec_control);/*
arch/x86/kvm/vmx/nested.c static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 · line 2383
Quoted 6 lines
arch/x86/kvm/vmx/nested.c +3
-0@@ -2374,6 +2378,10 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0 exec_control |=VM_EXIT_LOAD_IA32_EFER ; else exec_control &= ~VM_EXIT_LOAD_IA32_EFER; + +if (cpu_has_vmx_arch_lbr ()) + exec_control &= ~VM_EXIT_CLEAR_IA32_LBR_CTL ;
This is wrong. If KVM doesn't enumerate suport to L1, then L1's value needs to be preserved on entry/exit to/from L1<->L2. I.e. just delete these lines.
Quoted 16 lines
arch/x86/kvm/vmx/nested.c +0
-0vm_exit_controls_set (vmx, exec_control);/* arch/x86/kvm/vmx/vmx.c +1
-0@@ -2599,6 +2599,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, { VM_ENTRY_LOAD_IA32_EFER, VM_EXIT_LOAD_IA32_EFER }, { VM_ENTRY_LOAD_BNDCFGS, VM_EXIT_CLEAR_BNDCFGS }, { VM_ENTRY_LOAD_IA32_RTIT_CTL, VM_EXIT_CLEAR_IA32_RTIT_CTL }, + { VM_ENTRY_LOAD_IA32_LBR_CTL, VM_EXIT_CLEAR_IA32_LBR_CTL }, };memset (vmcs_conf,0 ,sizeof (*vmcs_conf));
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 4800
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -4794,6 +4795,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vpid_sync_context (vmx->vpid);vmx_update_fb_clear_dis (vcpu, vmx); + +if (!init_event &&cpu_has_vmx_arch_lbr ()) +vmcs_write64 (GUEST_IA32_LBR_CTL ,0 );
This belongs in init_vmcs().
arch/x86/kvm/vmx/vmx.c void dump_vmcs(struct kvm_vcpu *vcpu) · line 6198
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0}static void vmx_enable_irq_window (struct kvm_vcpu *vcpu)@@ -6191,6 +6195,10 @@ void dump_vmcs(struct kvm_vcpu *vcpu) vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)pr_err ("PerfGlobCtl = 0x%016llx\n" ,vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL )); + if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR) &&
Follow every other field and check the VMCS support, not the cap.
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0+ vmentry_ctl & VM_ENTRY_LOAD_IA32_LBR_CTL) +pr_err ("ArchLBRCtl = 0x%016llx\n" , +vmcs_read64 (GUEST_IA32_LBR_CTL ));if (vmentry_ctl &VM_ENTRY_LOAD_BNDCFGS )pr_err ("BndCfgS = 0x%016llx\n" ,vmcs_read64 (GUEST_BNDCFGS )); pr_err("Interruptibility = %08x ActivityState = %08x\n",
...
Quoted 15 lines
arch/x86/kvm/vmx/vmx.h +2
-1@@ -493,7 +493,8 @@ static inline u8 vmx_get_rvi(void) VM_ENTRY_LOAD_IA32_EFER | \ VM_ENTRY_LOAD_BNDCFGS | \ VM_ENTRY_PT_CONCEAL_PIP | \ - VM_ENTRY_LOAD_IA32_RTIT_CTL) + VM_ENTRY_LOAD_IA32_RTIT_CTL | \ + VM_ENTRY_LOAD_IA32_LBR_CTL)#define __KVM_REQUIRED_VMX_VM_EXIT_CONTROLS \ (VM_EXIT_SAVE_DEBUG_CONTROLS | \
arch/x86/kvm/vmx/vmx.h static inline u8 vmx_get_rvi(void) · line 520
Quoted 6 lines
arch/x86/kvm/vmx/vmx.h +2
-1@@ -515,7 +516,8 @@ static inline u8 vmx_get_rvi(void)VM_EXIT_LOAD_IA32_EFER | \VM_EXIT_CLEAR_BNDCFGS | \VM_EXIT_PT_CONCEAL_PIP | \ - VM_EXIT_CLEAR_IA32_RTIT_CTL) +VM_EXIT_CLEAR_IA32_RTIT_CTL | \ + VM_EXIT_CLEAR_IA32_LBR_CTL)
Enabling these flags by default is wrong. KVM will clear LBR_CTL on VM-Exit when arch LBRs are supported, and AFAICT, never restore the host's values. And that will happen regardless of whether or not the guest is using arch LBRs. I assume the correct approach is to toggle these fields, but I'll be damned if I can figure out what the intended behavior of the existing LBR suport is. I'll follow up in the cover letter.
PATCH v2 11/15 KVM: x86: Add XSAVE Support for Architectural LBR
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
On processors supporting XSAVES and XRSTORS, Architectural LBR XSAVE support is enumerated from CPUID.(EAX=0DH, ECX=1):ECX[bit 15]. The detailed sub-leaf for Arch LBR is enumerated in CPUID.(0DH, 0FH).
XSAVES provides a faster means than RDMSR for guest to read all LBRs. When guest IA32_XSS[bit 15] is set, the Arch LBR state can be saved using XSAVES and restored by XRSTORS with the appropriate RFBM.
--- arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -7733,6 +7733,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_check_and_set (X86_FEATURE_DS );kvm_cpu_cap_check_and_set (X86_FEATURE_DTES64 ); } +if (!cpu_has_vmx_arch_lbr ()) { +kvm_cpu_cap_clear (X86_FEATURE_ARCH_LBR ); + kvm_caps.supported_xss &= ~XFEATURE_MASK_LBR ; + }if (!enable_pmu)kvm_cpu_cap_clear (X86_FEATURE_PDCM );
arch/x86/kvm/x86.c +1 -1
@@ -217,7 +217,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS XFEATURE_MASK_LBRu64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 11/15] KVM: x86: Add XSAVE Support for Architectural LBR
Quoted 22 lines · Yang Weijiang
From: Like Xu <[email protected]>
On processors supporting XSAVES and XRSTORS, Architectural LBR XSAVE support is enumerated from CPUID.(EAX=0DH, ECX=1):ECX[bit 15]. The detailed sub-leaf for Arch LBR is enumerated in CPUID.(0DH, 0FH).
XSAVES provides a faster means than RDMSR for guest to read all LBRs. When guest IA32_XSS[bit 15] is set, the Arch LBR state can be saved using XSAVES and restored by XRSTORS with the appropriate RFBM.
Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c static __init void vmx_set_cpu_caps(void) · line 7737
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -7733,6 +7733,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_check_and_set (X86_FEATURE_DS );kvm_cpu_cap_check_and_set (X86_FEATURE_DTES64 ); } +if (!cpu_has_vmx_arch_lbr ()) { +kvm_cpu_cap_clear (X86_FEATURE_ARCH_LBR );
No, this needs to be opt-in, not opt-out. I.e. omit the flag from common CPUID code and set it if and only if it's fully supported. It's not out of the realm of possibilities that AMD might want to support arch LBRs, at which point those CPUs would explode.
Quoted 21 lines
arch/x86/kvm/vmx/vmx.c +2
-0+ kvm_caps.supported_xss &= ~XFEATURE_MASK_LBR ; + }if (!enable_pmu)kvm_cpu_cap_clear (X86_FEATURE_PDCM );arch/x86/kvm/x86.c +1
-1@@ -217,7 +217,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS XFEATURE_MASK_LBRu64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);Signature · 1 line
2.27.0
Re: [PATCH v2 11/15] KVM: x86: Add XSAVE Support for Architectural LBR
Quoted 18 lines · Sean Christopherson
Quoted 17 lines · Yang Weijiang
From: Like Xu <[email protected]>
On processors supporting XSAVES and XRSTORS, Architectural LBR XSAVE support is enumerated from CPUID.(EAX=0DH, ECX=1):ECX[bit 15]. The detailed sub-leaf for Arch LBR is enumerated in CPUID.(0DH, 0FH).
XSAVES provides a faster means than RDMSR for guest to read all LBRs. When guest IA32_XSS[bit 15] is set, the Arch LBR state can be saved using XSAVES and restored by XRSTORS with the appropriate RFBM.
Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-)
Quoted 14 lines
arch/x86/kvm/vmx/vmx.c static __init void vmx_set_cpu_caps(void) · line 7737
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -7733,6 +7733,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_check_and_set (X86_FEATURE_DS );kvm_cpu_cap_check_and_set (X86_FEATURE_DTES64 ); } +if (!cpu_has_vmx_arch_lbr ()) { +kvm_cpu_cap_clear (X86_FEATURE_ARCH_LBR );No, this needs to be opt-in, not opt-out. I.e. omit the flag from common CPUID code and set it if and only if it's fully supported. It's not out of the realm of possibilities that AMD might want to support arch LBRs, at which point those CPUs would explode.
Will modify this patch.
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/vmx/vmx.c +2
-0+ kvm_caps.supported_xss &= ~XFEATURE_MASK_LBR ; + }if (!enable_pmu)kvm_cpu_cap_clear (X86_FEATURE_PDCM );arch/x86/kvm/x86.c +1
-1@@ -217,7 +217,7 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs; | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) -#define KVM_SUPPORTED_XSS 0 +#define KVM_SUPPORTED_XSS XFEATURE_MASK_LBRu64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer);Signature · 1 line
2.27.0
PATCH v2 12/15 KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
In reply to Yang WeijiangPer SDM 3B, Chapter 18:
“On a debug breakpoint event (#DB), IA32_LBR_CTL.LBREn is cleared.” and "On a warm reset, all LBR MSRs, including IA32_LBR_DEPTH, have their values preserved. However, IA32_LBR_CTL.LBREn is cleared to 0, disabling LBRs.", clear the bit manually before inject #DB or when vCPU is in warm reset.
--- arch/x86/kvm/vmx/vmx.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +23 -1
@@ -1695,6 +1695,20 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)vmcs_write32 (GUEST_ACTIVITY_STATE ,GUEST_ACTIVITY_ACTIVE ); } +static void disable_arch_lbr_ctl (struct kvm_vcpu *vcpu) +{ +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use) && + lbr_desc->event) { +u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +} +static void vmx_inject_exception (struct kvm_vcpu *vcpu) {struct kvm_queued_exception *ex = &vcpu->arch.exception;
@@ -1738,6 +1752,9 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu)vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmx_clear_hlt (vcpu); + +if (ex->vector ==DB_VECTOR ) +disable_arch_lbr_ctl (vcpu); } static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr,
@@ -4796,7 +4813,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_update_fb_clear_dis (vcpu, vmx); -if (!init_event &&cpu_has_vmx_arch_lbr ()) +if (init_event) +disable_arch_lbr_ctl (vcpu); +else if (cpu_has_vmx_arch_lbr ())vmcs_write64 (GUEST_IA32_LBR_CTL ,0 ); }
@@ -4873,6 +4892,9 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu) INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);vmx_clear_hlt (vcpu); + +if (vcpu->arch.exception.vector ==DB_VECTOR ) +disable_arch_lbr_ctl (vcpu); }bool vmx_get_nmi_mask (struct kvm_vcpu *vcpu)
Signature · 1 line
2.27.0
Re: [PATCH v2 12/15] KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
Quoted 18 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +14
-0+static void disable_arch_lbr_ctl (struct kvm_vcpu *vcpu) +{ +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use) && + lbr_desc->event) { +u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +} +static void vmx_inject_exception (struct kvm_vcpu *vcpu) {struct kvm_queued_exception *ex = &vcpu->arch.exception;
arch/x86/kvm/vmx/vmx.c static void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1757
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -1738,6 +1752,9 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu)vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmx_clear_hlt (vcpu); + +if (ex->vector ==DB_VECTOR ) +disable_arch_lbr_ctl (vcpu);
Please verify this with KUT testcase, once I failed and did not confirm if it is a hardware issue. Good Luck.
Quoted 1 line
(quoted diff) +0
-0}
Re: [PATCH v2 12/15] KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
Quoted 20 lines · Like Xu
Quoted 19 lines · Yang Weijiang
arch/x86/kvm/vmx/vmx.c +14
-0+static void disable_arch_lbr_ctl (struct kvm_vcpu *vcpu) +{ + struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); + + if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR) && + test_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use) && + lbr_desc->event) { + u64 ctl = vmcs_read64(GUEST_IA32_LBR_CTL); + + vmcs_write64(GUEST_IA32_LBR_CTL, ctl & ~ARCH_LBR_CTL_LBREN); + } +} +static void
vmx_inject_exception(struct kvm_vcpu *vcpu){
structkvm_queued_exception*ex =&vcpu->arch.exception;(quoted diff) +0
-0@@ -1738,6 +1752,9 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu)
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static void vmx_inject_exception(struct kvm_vcpu *vcpu) · line 1757
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +3
-0vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); vmx_clear_hlt(vcpu); + + if (ex->vector == DB_VECTOR) + disable_arch_lbr_ctl(vcpu);Please verify this with KUT testcase, once I failed and did not confirm if it is a hardware issue. Good Luck.
Can you detail what you want to verify with?
Quoted 2 lines
Quoted 2 lines
(quoted diff) +0
-0}
Sean Christopherson
maintainerRe: [PATCH v2 12/15] KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
In reply to Yang WeijiangQuoted 18 lines · Yang Weijiang
Per SDM 3B, Chapter 18:
“On a debug breakpoint event (#DB), IA32_LBR_CTL.LBREn is cleared.” and "On a warm reset, all LBR MSRs, includingIA32_LBR_DEPTH, have their values preserved. However, IA32_LBR_CTL.LBREn is cleared to 0, disabling LBRs.", clear the bit manually before inject #DB or when vCPU is in warm reset.Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +0
-0@@ -1695,6 +1695,20 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)vmcs_write32 (GUEST_ACTIVITY_STATE ,GUEST_ACTIVITY_ACTIVE );
arch/x86/kvm/vmx/vmx.c static void vmx_clear_hlt(struct kvm_vcpu *vcpu) · line 1705
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +8
-0} +static void disable_arch_lbr_ctl (struct kvm_vcpu *vcpu) +{ +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use) && + lbr_desc->event) {
I don't see any reason to check that an event is actually assigned. The behavior is architectural, whether or not KVM is actively exposing LBRs to the guest is irrelevant.
Quoted 20 lines
arch/x86/kvm/vmx/vmx.c +9
-0+u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +} +static void vmx_inject_exception (struct kvm_vcpu *vcpu) {struct kvm_queued_exception *ex = &vcpu->arch.exception;@@ -1738,6 +1752,9 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu)vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmx_clear_hlt (vcpu); + +if (ex->vector ==DB_VECTOR ) +disable_arch_lbr_ctl (vcpu); } static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr,
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 4816
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -4796,7 +4813,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_update_fb_clear_dis (vcpu, vmx); -if (!init_event &&cpu_has_vmx_arch_lbr ()) +if (init_event)
INIT and warm RESET are not the same thing, i.e. this is flat out wrong.
arch/x86/kvm/vmx/vmx.c static void vmx_inject_nmi(struct kvm_vcpu *vcpu) · line 4896
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c +4
-0+disable_arch_lbr_ctl (vcpu); + else if (cpu_has_vmx_arch_lbr()) vmcs_write64(GUEST_IA32_LBR_CTL, 0); }@@ -4873,6 +4892,9 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu) INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);vmx_clear_hlt (vcpu); + +if (vcpu->arch.exception.vector ==DB_VECTOR )
Huh? This is _very_ obviously injecting NMIs, not #DBs.
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +1
-0+disable_arch_lbr_ctl (vcpu); }bool vmx_get_nmi_mask (struct kvm_vcpu *vcpu)Signature · 1 line
2.27.0
Re: [PATCH v2 12/15] KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Yang Weijiang
Per SDM 3B, Chapter 18:
“On a debug breakpoint event (#DB), IA32_LBR_CTL.LBREn is cleared.” and "On a warm reset, all LBR MSRs, includingIA32_LBR_DEPTH, have their values preserved. However, IA32_LBR_CTL.LBREn is cleared to 0, disabling LBRs.", clear the bit manually before inject #DB or when vCPU is in warm reset.Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/vmx/vmx.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)arch/x86/kvm/vmx/vmx.c +0
-0@@ -1695,6 +1695,20 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)vmcs_write32 (GUEST_ACTIVITY_STATE ,GUEST_ACTIVITY_ACTIVE ); }
Quoted 11 lines
arch/x86/kvm/vmx/vmx.c static void vmx_clear_hlt(struct kvm_vcpu *vcpu) · line 1705
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +8
-0+static void disable_arch_lbr_ctl (struct kvm_vcpu *vcpu) +{ +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu); +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +test_bit (INTEL_PMC_IDX_FIXED_VLBR , pmu->pmc_in_use) && + lbr_desc->event) {I don't see any reason to check that an event is actually assigned. The behavior is architectural, whether or not KVM is actively exposing LBRs to the guest is irrelevant
Agree, will remove them.
Quoted 19 lines
Quoted 19 lines
arch/x86/kvm/vmx/vmx.c +9
-0+u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +} +static void vmx_inject_exception (struct kvm_vcpu *vcpu) {struct kvm_queued_exception *ex = &vcpu->arch.exception;@@ -1738,6 +1752,9 @@ static void vmx_inject_exception(struct kvm_vcpu *vcpu)vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmx_clear_hlt (vcpu); + +if (ex->vector ==DB_VECTOR ) +disable_arch_lbr_ctl (vcpu); }
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) · line 4816
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +1
-1static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr,@@ -4796,7 +4813,9 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)vmx_update_fb_clear_dis (vcpu, vmx); -if (!init_event &&cpu_has_vmx_arch_lbr ()) +if (init_event)INIT and warm RESET are not the same thing, i.e. this is flat out wrong.
I was confused a bit. Is there's a way to intercept guest warm RESET so as to disable the bit?
Or what should be followed per spec.?
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-0+disable_arch_lbr_ctl (vcpu); + else if (cpu_has_vmx_arch_lbr()) vmcs_write64(GUEST_IA32_LBR_CTL, 0); }
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c static void vmx_inject_nmi(struct kvm_vcpu *vcpu) · line 4896
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -4873,6 +4892,9 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu) INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);vmx_clear_hlt (vcpu); + +if (vcpu->arch.exception.vector ==DB_VECTOR )Huh? This is _very_ obviously injecting NMIs, not #DBs.
My fault, will remove it.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1
-0+disable_arch_lbr_ctl (vcpu); }bool vmx_get_nmi_mask (struct kvm_vcpu *vcpu)Signature · 1 line
2.27.0
PATCH v2 13/15 KVM: x86/vmx: Save/Restore guest Arch LBR Ctrl msr at SMM entry/exit
In reply to Yang WeijiangPer SDM 3B Chapter 18: "IA32_LBR_CTL.LBREn is saved and cleared on #SMI, and restored on RSM", store guest IA32_LBR_CTL in SMRAM and clear LBREn in VMCS at SMM entry, and do reverse things at SMM exit.
--- arch/x86/kvm/smm.c | 1 + arch/x86/kvm/smm.h | 3 ++- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-)
arch/x86/kvm/smm.c +1 -0
@@ -86,6 +86,7 @@ static void check_smram_offsets(void)CHECK_SMRAM64_OFFSET (smm_revison,0xFEFC );CHECK_SMRAM64_OFFSET (smbase,0xFF00 );CHECK_SMRAM64_OFFSET (reserved4,0xFF04 ); +CHECK_SMRAM64_OFFSET (arch_lbr_ctl,0xFF10 );CHECK_SMRAM64_OFFSET (ssp,0xFF18 );CHECK_SMRAM64_OFFSET (svm_guest_pat,0xFF20 );CHECK_SMRAM64_OFFSET (svm_host_efer,0xFF28 );
arch/x86/kvm/smm.h +2 -1
@@ -114,7 +114,8 @@ struct kvm_smram_state_64 {u32 reserved3[3 ];u32 smm_revison;u32 smbase; -u32 reserved4[5 ]; +u32 reserved4[3 ]; +u64 arch_lbr_ctl;/* ssp and svm_* fields below are not implemented by KVM */ u64 ssp;
arch/x86/kvm/vmx/vmx.c +22 -0
@@ -8006,11 +8006,21 @@ static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) vmx->nested.smm.vmxon = vmx->nested.vmxon; vmx->nested.vmxon = false;vmx_clear_hlt (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_LM )) { +u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + + smram->smram64.arch_lbr_ctl = ctl; +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +return 0 ; }static int vmx_leave_smm (struct kvm_vcpu *vcpu,const union kvm_smram *smram) { +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);int ret;
@@ -8027,6 +8037,18 @@ static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) vmx->nested.nested_run_pending =1 ; vmx->nested.smm.guest_mode = false; } + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_LM )) { +u64 ctl = smram->smram64.arch_lbr_ctl; + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl &ARCH_LBR_CTL_LBREN ); + +if (intel_pmu_lbr_is_enabled (vcpu) && + (ctl &ARCH_LBR_CTL_LBREN ) && !lbr_desc->event) +intel_pmu_create_guest_lbr_event (vcpu); + } +return 0 ; }
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 13/15] KVM: x86/vmx: Save/Restore guest Arch LBR Ctrl msr at SMM entry/exit
Quoted 43 lines · Yang Weijiang
Per SDM 3B Chapter 18: "IA32_LBR_CTL.LBREn is saved and cleared on #SMI, and restored on RSM", store guest
IA32_LBR_CTLin SMRAM and clear LBREn in VMCS at SMM entry, and do reverse things at SMM exit.Suggested-by Paolo Bonzini <[email protected]>Signed-off-by Yang Weijiang <[email protected]>--- arch/x86/kvm/smm.c | 1 + arch/x86/kvm/smm.h | 3 ++- arch/x86/kvm/vmx/vmx.c | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-)arch/x86/kvm/smm.c +1
-0@@ -86,6 +86,7 @@ static void check_smram_offsets(void)CHECK_SMRAM64_OFFSET (smm_revison,0xFEFC );CHECK_SMRAM64_OFFSET (smbase,0xFF00 );CHECK_SMRAM64_OFFSET (reserved4,0xFF04 ); +CHECK_SMRAM64_OFFSET (arch_lbr_ctl,0xFF10 );CHECK_SMRAM64_OFFSET (ssp,0xFF18 );CHECK_SMRAM64_OFFSET (svm_guest_pat,0xFF20 );CHECK_SMRAM64_OFFSET (svm_host_efer,0xFF28 );arch/x86/kvm/smm.h +2
-1@@ -114,7 +114,8 @@ struct kvm_smram_state_64 {u32 reserved3[3 ];u32 smm_revison;u32 smbase; -u32 reserved4[5 ]; +u32 reserved4[3 ]; +u64 arch_lbr_ctl;/* ssp and svm_* fields below are not implemented by KVM */ u64 ssp;arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) · line 8011
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -8006,11 +8006,21 @@ static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) vmx->nested.smm.vmxon = vmx->nested.vmxon; vmx->nested.vmxon = false;vmx_clear_hlt (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_LM )) {
Uh, so this arbitrary dependency on 64-bit vCPUs needs to be factored into the enabling. And KVM should WARN if arch LBRs get enabled for a 32-bit vCPU.
Quoted 16 lines
arch/x86/kvm/vmx/vmx.c +7
-0+u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + + smram->smram64.arch_lbr_ctl = ctl; +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +return 0 ; }static int vmx_leave_smm (struct kvm_vcpu *vcpu,const union kvm_smram *smram) { +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);int ret;
arch/x86/kvm/vmx/vmx.c static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) · line 8045
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +6
-0@@ -8027,6 +8037,18 @@ static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) vmx->nested.nested_run_pending =1 ; vmx->nested.smm.guest_mode = false; } + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_LM )) { +u64 ctl = smram->smram64.arch_lbr_ctl; + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl &ARCH_LBR_CTL_LBREN );
IIUC, this should set only LBREn and preserve all other bits, not clobber the entire MSR.
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +6
-0+ +if (intel_pmu_lbr_is_enabled (vcpu) && + (ctl &ARCH_LBR_CTL_LBREN ) && !lbr_desc->event) +intel_pmu_create_guest_lbr_event (vcpu); + } +return 0 ; }Signature · 1 line
2.27.0
Re: [PATCH v2 13/15] KVM: x86/vmx: Save/Restore guest Arch LBR Ctrl msr at SMM entry/exit
Quoted 4 lines · Sean Christopherson
Quoted 3 lines · Yang Weijiang
Per SDM 3B Chapter 18: "IA32_LBR_CTL.LBREn is saved and cleared on #SMI, and restored on RSM", store guest
IA32_LBR_CTLin SMRAM and clear LBREn in VMCS at SMM entry, and do reverse things at SMM exit.
[...]
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) · line 8011
Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +3
-0@@ -8006,11 +8006,21 @@ static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) vmx->nested.smm.vmxon = vmx->nested.vmxon; vmx->nested.vmxon = false;vmx_clear_hlt (vcpu); + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_LM )) {Uh, so this arbitrary dependency on 64-bit vCPUs needs to be factored into the enabling. And KVM should WARN if arch LBRs get enabled for a 32-bit vCPU.
OK, will add the check while creating event.
Quoted 16 lines
Quoted 16 lines
arch/x86/kvm/vmx/vmx.c +7
-0+u64 ctl =vmcs_read64 (GUEST_IA32_LBR_CTL ); + + smram->smram64.arch_lbr_ctl = ctl; +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl & ~ARCH_LBR_CTL_LBREN ); + } +return 0 ; }static int vmx_leave_smm (struct kvm_vcpu *vcpu,const union kvm_smram *smram) { +struct lbr_desc *lbr_desc =vcpu_to_lbr_desc (vcpu);struct vcpu_vmx *vmx =to_vmx (vcpu);int ret;
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) · line 8045
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6
-0@@ -8027,6 +8037,18 @@ static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) vmx->nested.nested_run_pending =1 ; vmx->nested.smm.guest_mode = false; } + +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR ) && +guest_cpuid_has (vcpu,X86_FEATURE_LM )) { +u64 ctl = smram->smram64.arch_lbr_ctl; + +vmcs_write64 (GUEST_IA32_LBR_CTL , ctl &ARCH_LBR_CTL_LBREN );IIUC, this should set only LBREn and preserve all other bits, not clobber the entire MSR.
Oops, it's a typo, thanks!
Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/vmx/vmx.c +6
-0+ +if (intel_pmu_lbr_is_enabled (vcpu) && + (ctl &ARCH_LBR_CTL_LBREN ) && !lbr_desc->event) +intel_pmu_create_guest_lbr_event (vcpu); + } +return 0 ; }Signature · 1 line
2.27.0
PATCH v2 14/15 KVM: x86: Add Arch LBR data MSR access interface
In reply to Yang WeijiangArch LBR MSRs are xsave-supported, but they're operated as "independent" xsave feature by PMU code, i.e., during thread/process context switch, the MSRs are saved/restored with perf_event_task_sched_{in|out} instead of generic kernel fpu switch code, i.e.,save_fpregs_to_fpstate() and restore_fpregs_from_fpstate(). When vcpu guest/host fpu state swap happens, Arch LBR MSRs are retained so they can be accessed directly.
--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/kvm/vmx/pmu_intel.c +10 -0
@@ -410,6 +410,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); }return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +rdmsrl (msr_info->index, msr_info->data); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {
@@ -528,6 +533,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (data & ARCH_LBR_CTL_LBREN))intel_pmu_create_guest_lbr_event (vcpu);return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +wrmsrl (msr_info->index, msr_info->data); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {
Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 14/15] KVM: x86: Add Arch LBR data MSR access interface
Quoted 18 lines · Yang Weijiang
Arch LBR MSRs are xsave-supported, but they're operated as "independent" xsave feature by PMU code, i.e., during thread/process context switch, the MSRs are saved/restored with perf_event_task_sched_{in|out} instead of generic kernel fpu switch code, i.e.,save_fpregs_to_fpstate() and
restore_fpregs_from_fpstate(). When vcpu guest/host fpu state swap happens, Arch LBR MSRs are retained so they can be accessed directly.Signed-off-by Yang Weijiang <[email protected]>Reviewed-by Kan Liang <[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/vmx/pmu_intel.c +0
-0
arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 416
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0@@ -410,6 +410,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); }return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +rdmsrl (msr_info->index, msr_info->data);
I don't see how this is correct. As called out in patch 5:
: If for some magical reason it's safe to access arch LBR MSRs without disabling : IRQs and confirming perf event ownership, I want to see a very detailed changelog : explaining exactly how that magic works.
Quoted 19 lines
arch/x86/kvm/vmx/pmu_intel.c +6
-0+return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -528,6 +533,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (data & ARCH_LBR_CTL_LBREN))intel_pmu_create_guest_lbr_event (vcpu);return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +wrmsrl (msr_info->index, msr_info->data); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {Signature · 1 line
2.27.0
Re: [PATCH v2 14/15] KVM: x86: Add Arch LBR data MSR access interface
Quoted 28 lines · Sean Christopherson
Quoted 18 lines · Yang Weijiang
Arch LBR MSRs are xsave-supported, but they're operated as "independent" xsave feature by PMU code, i.e., during thread/process context switch, the MSRs are saved/restored with perf_event_task_sched_{in|out} instead of generic kernel fpu switch code, i.e.,save_fpregs_to_fpstate() and
restore_fpregs_from_fpstate(). When vcpu guest/host fpu state swap happens, Arch LBR MSRs are retained so they can be accessed directly.Signed-off-by Yang Weijiang <[email protected]>Reviewed-by Kan Liang <[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/vmx/pmu_intel.c +0
-0arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 416
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0@@ -410,6 +410,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); }return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +rdmsrl (msr_info->index, msr_info->data);I don't see how this is correct. As called out in patch 5:
Quoted 3 lines
: If for some magical reason it's safe to access arch LBR MSRs without disabling : IRQs and confirming perf event ownership, I want to see a very detailed changelog : explaining exactly how that magic works.
The MSR lists here are just for live migration. When arch-lbr is active, these MSRs are passed through
to guest.
Quoted 20 lines
Quoted 20 lines
arch/x86/kvm/vmx/pmu_intel.c +6
-0+return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {@@ -528,6 +533,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) (data & ARCH_LBR_CTL_LBREN))intel_pmu_create_guest_lbr_event (vcpu);return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +wrmsrl (msr_info->index, msr_info->data); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {Signature · 1 line
2.27.0
Sean Christopherson
maintainerRe: [PATCH v2 14/15] KVM: x86: Add Arch LBR data MSR access interface
Quoted 30 lines · Weijiang
Quoted 28 lines · Sean Christopherson
Quoted 18 lines · Yang Weijiang
Arch LBR MSRs are xsave-supported, but they're operated as "independent" xsave feature by PMU code, i.e., during thread/process context switch, the MSRs are saved/restored with perf_event_task_sched_{in|out} instead of generic kernel fpu switch code, i.e.,save_fpregs_to_fpstate() and
restore_fpregs_from_fpstate(). When vcpu guest/host fpu state swap happens, Arch LBR MSRs are retained so they can be accessed directly.Signed-off-by Yang Weijiang <[email protected]>Reviewed-by Kan Liang <[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/vmx/pmu_intel.c +0
-0arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 416
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0@@ -410,6 +410,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); }return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +rdmsrl (msr_info->index, msr_info->data);I don't see how this is correct. As called out in patch 5:
Quoted 6 lines
Quoted 3 lines
: If for some magical reason it's safe to access arch LBR MSRs without disabling : IRQs and confirming perf event ownership, I want to see a very detailed changelog : explaining exactly how that magic works.
The MSR lists here are just for live migration. When arch-lbr is active, these MSRs are passed through to guest.
None of that explains how the guest's MSR values are guaranteed to be resident in hardware.
Re: [PATCH v2 14/15] KVM: x86: Add Arch LBR data MSR access interface
Quoted 30 lines · Sean Christopherson
Quoted 29 lines · Weijiang
Quoted 28 lines · Sean Christopherson
Quoted 18 lines · Yang Weijiang
Arch LBR MSRs are xsave-supported, but they're operated as "independent" xsave feature by PMU code, i.e., during thread/process context switch, the MSRs are saved/restored with perf_event_task_sched_{in|out} instead of generic kernel fpu switch code, i.e.,save_fpregs_to_fpstate() and
restore_fpregs_from_fpstate(). When vcpu guest/host fpu state swap happens, Arch LBR MSRs are retained so they can be accessed directly.Signed-off-by Yang Weijiang<[email protected]>Reviewed-by Kan Liang<[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/vmx/pmu_intel.c +0
-0arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 416
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0@@ -410,6 +410,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); }return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +rdmsrl (msr_info->index, msr_info->data);I don't see how this is correct. As called out in patch 5:
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
: If for some magical reason it's safe to access arch LBR MSRs without disabling : IRQs and confirming perf event ownership, I want to see a very detailed changelog : explaining exactly how that magic works.
The MSR lists here are just for live migration. When arch-lbr is active, these MSRs are passed through to guest.
None of that explains how the guest's MSR values are guaranteed to be resident in hardware.
I ignored host *event* scheduling case in commit log.
My understanding is, host LBR *event* could break in at any point when the vCPU is running,
in this case disabling IRQs before read/write the MSRs is pointless because the HW context could have
been swapped. I need to do more investigation for the issue.
Sean Christopherson
maintainerRe: [PATCH v2 14/15] KVM: x86: Add Arch LBR data MSR access interface
Quoted 40 lines · Weijiang
Quoted 30 lines · Sean Christopherson
Quoted 29 lines · Weijiang
Quoted 28 lines · Sean Christopherson
Quoted 18 lines · Yang Weijiang
Arch LBR MSRs are xsave-supported, but they're operated as "independent" xsave feature by PMU code, i.e., during thread/process context switch, the MSRs are saved/restored with perf_event_task_sched_{in|out} instead of generic kernel fpu switch code, i.e.,save_fpregs_to_fpstate() and
restore_fpregs_from_fpstate(). When vcpu guest/host fpu state swap happens, Arch LBR MSRs are retained so they can be accessed directly.Signed-off-by Yang Weijiang<[email protected]>Reviewed-by Kan Liang<[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/x86/kvm/vmx/pmu_intel.c +0
-0arch/x86/kvm/vmx/pmu_intel.c static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 416
Quoted 7 lines
arch/x86/kvm/vmx/pmu_intel.c +4
-0@@ -410,6 +410,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data =vmcs_read64 (GUEST_IA32_LBR_CTL ); }return 0 ; + case MSR_ARCH_LBR_FROM_0 ... MSR_ARCH_LBR_FROM_0 + 31: + case MSR_ARCH_LBR_TO_0 ... MSR_ARCH_LBR_TO_0 + 31: + case MSR_ARCH_LBR_INFO_0 ... MSR_ARCH_LBR_INFO_0 + 31: +rdmsrl (msr_info->index, msr_info->data);I don't see how this is correct. As called out in patch 5:
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
: If for some magical reason it's safe to access arch LBR MSRs without disabling : IRQs and confirming perf event ownership, I want to see a very detailed changelog : explaining exactly how that magic works.
The MSR lists here are just for live migration. When arch-lbr is active, these MSRs are passed through to guest.
None of that explains how the guest's MSR values are guaranteed to be resident in hardware.
Quoted 8 lines
I ignored host *event* scheduling case in commit log.
My understanding is, host LBR *event* could break in at any point when the vCPU is running,
in this case disabling IRQs before read/write the MSRs is pointless because the HW context could have been swapped. I need to do more investigation for the issue.
Which is presumably why intel_pmu_handle_lbr_msrs_access() checks that the LBR perf event is active prior to accessing the MSRs, with IRQs disabled...
/*
* Disable irq to ensure the LBR feature doesn't get reclaimed by the
* host at the time the value is read from the msr, and this avoids the
* host LBR value to be leaked to the guest. If LBR has been reclaimed,
* return 0 on guest reads.
*/
local_irq_disable();
if (lbr_desc->event->state == PERF_EVENT_STATE_ACTIVE) {
if (read)
rdmsrl(index, msr_info->data);
else
wrmsrl(index, msr_info->data);
__set_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
local_irq_enable();
return true;
}
clear_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
local_irq_enable();PATCH v2 15/15 KVM: x86/cpuid: Advertise Arch LBR feature in CPUID
In reply to Yang WeijiangAdd Arch LBR feature bit in CPU cap-mask to expose the feature. Only max LBR depth is supported for guest, and it's consistent with host Arch LBR settings.
--- arch/x86/kvm/cpuid.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +35 -1
@@ -134,6 +134,19 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,if (vaddr_bits !=48 && vaddr_bits !=57 && vaddr_bits !=0 )return -EINVAL ; } +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { + best =cpuid_entry2_find (entries, nent,0x1c ,0 ); +if (best) { +unsigned int eax, ebx, ecx, edx; + +/* Reject user-space CPUID if depth is different from host's.*/ +cpuid_count (0x1c ,0 , &eax, &ebx, &ecx, &edx); + +if ((eax &0xff ) && + (best->eax &0xff ) !=BIT (fls (eax &0xff ) -1 )) +return -EINVAL ; + } + }/* * Exposing dynamic xfeatures to the guest requires additional
@@ -652,7 +665,7 @@ void kvm_set_cpu_caps(void)F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) |F (MD_CLEAR ) |F (AVX512_VP2INTERSECT ) |F (FSRM ) |F (SERIALIZE ) |F (TSXLDTRK ) |F (AVX512_FP16 ) | -F (AMX_TILE ) |F (AMX_INT8 ) |F (AMX_BF16 ) +F (AMX_TILE ) |F (AMX_INT8 ) |F (AMX_BF16 ) |F (ARCH_LBR ) );/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */
@@ -1074,6 +1087,27 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) goto out; }break ; +/* Architectural LBR */ +case 0x1c : { +u32 lbr_depth_mask = entry->eax &0xff ; + +if (!lbr_depth_mask || + !kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { + entry->eax = entry->ebx = entry->ecx = entry->edx =0 ; +break ; + } +/* +* KVM only exposes the maximum supported depth, which is the +* fixed value used on the host side. +* KVM doesn't allow VMM userspace to adjust LBR depth because +* guest LBR emulation depends on the configuration of host LBR +* driver. +*/ + lbr_depth_mask =BIT ((fls (lbr_depth_mask) -1 )); + entry->eax &= ~0xff ; + entry->eax |= lbr_depth_mask; +break ; + }/* Intel AMX TILE */ case 0x1d :if (!kvm_cpu_cap_has (X86_FEATURE_AMX_TILE )) {
Signature · 1 line
2.27.0
Re: [PATCH v2 15/15] KVM: x86/cpuid: Advertise Arch LBR feature in CPUID
Quoted 5 lines · Yang Weijiang
arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c static int kvm_check_cpuid(struct kvm_vcpu *vcpu, · line 142
Quoted 9 lines
arch/x86/kvm/cpuid.c +6
-0@@ -134,6 +134,19 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,if (vaddr_bits !=48 && vaddr_bits !=57 && vaddr_bits !=0 )return -EINVAL ; } +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { + best =cpuid_entry2_find (entries, nent,0x1c ,0 ); +if (best) { +unsigned int eax, ebx, ecx, edx; + +/* Reject user-space CPUID if depth is different from host's.*/
Try to verify this in the KVM selftest, as this behavior is different from the host.
Quoted 6 lines
arch/x86/kvm/cpuid.c +6
-0+ cpuid_count(0x1c, 0, &eax, &ebx, &ecx, &edx); > + + if ((eax & 0xff) && + (best->eax &0xff ) !=BIT (fls (eax &0xff ) -1 )) + return -EINVAL; + } + }
Re: [PATCH v2 15/15] KVM: x86/cpuid: Advertise Arch LBR feature in CPUID
Quoted 10 lines · Like Xu
Quoted 9 lines · Yang Weijiang
arch/x86/kvm/cpuid.c +1
-0@@ -134,6 +134,19 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu, if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0) return -EINVAL; } + if (kvm_cpu_cap_has(X86_FEATURE_ARCH_LBR)) {
Quoted 8 lines
Quoted 6 lines
arch/x86/kvm/cpuid.c +5
-0+ best = cpuid_entry2_find(entries, nent, 0x1c, 0); + if (best) { + unsigned int eax, ebx, ecx, edx; + + /* Reject user-space CPUID if depth is different fromhost's.*/
Try to verify this in the KVM selftest, as this behavior is different from the host.
Will do it.
Quoted 7 lines
Quoted 7 lines
+
cpuid_count(0x1c, 0, &eax, &ebx, &ecx, &edx); > + + if ((eax &0xff) &&
+ (best->eax&0xff) !=BIT(fls(eax & 0xff) - 1)) + return-EINVAL;
+ }
+ }
Sean Christopherson
maintainerRe: [PATCH v2 15/15] KVM: x86/cpuid: Advertise Arch LBR feature in CPUID
In reply to Yang WeijiangQuoted 23 lines · Yang Weijiang
Add Arch LBR feature bit in CPU cap-mask to expose the feature. Only max LBR depth is supported for guest, and it's consistent with host Arch LBR settings.
Co-developed-by Like Xu <[email protected]>Signed-off-by Like Xu <[email protected]>Signed-off-by Yang Weijiang <[email protected]>Reviewed-by Kan Liang <[email protected]>--- arch/x86/kvm/cpuid.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-)arch/x86/kvm/cpuid.c +3
-0@@ -134,6 +134,19 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,if (vaddr_bits !=48 && vaddr_bits !=57 && vaddr_bits !=0 )return -EINVAL ; } +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { + best =cpuid_entry2_find (entries, nent,0x1c ,0 ); +if (best) {
arch/x86/kvm/cpuid.c static int kvm_check_cpuid(struct kvm_vcpu *vcpu, · line 149
Quoted 10 lines
arch/x86/kvm/cpuid.c +10
-0+unsigned int eax, ebx, ecx, edx; + +/* Reject user-space CPUID if depth is different from host's.*/ +cpuid_count (0x1c ,0 , &eax, &ebx, &ecx, &edx); + +if ((eax &0xff ) && + (best->eax &0xff ) !=BIT (fls (eax &0xff ) -1 )) +return -EINVAL ; + } + }
Drop this. While I think everyone agrees that KVM's CPUID uAPI sucks, the status quo is to let userspace shoot itself in the foot. I.e. disallow enabling LBRs with a "bad" config, but don't reject the ioctl().
arch/x86/kvm/cpuid.c void kvm_set_cpu_caps(void) · line 668
Quoted 9 lines
arch/x86/kvm/cpuid.c +1
-1/* * Exposing dynamic xfeatures to the guest requires additional @@ -652,7 +665,7 @@ void kvm_set_cpu_caps(void)F (SPEC_CTRL_SSBD ) |F (ARCH_CAPABILITIES ) |F (INTEL_STIBP ) |F (MD_CLEAR ) |F (AVX512_VP2INTERSECT ) |F (FSRM ) |F (SERIALIZE ) |F (TSXLDTRK ) |F (AVX512_FP16 ) | -F (AMX_TILE ) |F (AMX_INT8 ) |F (AMX_BF16 ) +F (AMX_TILE ) |F (AMX_INT8 ) |F (AMX_BF16 ) |F (ARCH_LBR )
As mentioned earlier, omit this and make it opt-in.
Quoted 14 lines
arch/x86/kvm/cpuid.c +7
-0);/* TSC_ADJUST and ARCH_CAPABILITIES are emulated in software. */ @@ -1074,6 +1087,27 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) goto out; }break ; +/* Architectural LBR */ +case 0x1c : { +u32 lbr_depth_mask = entry->eax &0xff ; + +if (!lbr_depth_mask || + !kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) { + entry->eax = entry->ebx = entry->ecx = entry->edx =0 ;
arch/x86/kvm/cpuid.c static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) · line 1106
Quoted 10 lines
arch/x86/kvm/cpuid.c +10
-0+break ; + } +/* +* KVM only exposes the maximum supported depth, which is the +* fixed value used on the host side. +* KVM doesn't allow VMM userspace to adjust LBR depth because +* guest LBR emulation depends on the configuration of host LBR +* driver. +*/ + lbr_depth_mask =BIT ((fls (lbr_depth_mask) -1 ));
C'mon. More unnecessary dependencies on perf using the max depth.
Quoted 10 lines
arch/x86/kvm/cpuid.c +4
-0+ entry->eax &= ~0xff ; + entry->eax |= lbr_depth_mask; +break ; + }/* Intel AMX TILE */ case 0x1d :if (!kvm_cpu_cap_has (X86_FEATURE_AMX_TILE )) {Signature · 1 line
2.27.0
Hi, Sean,
Sorry to bother, do you have time to review this series? The feature has been pending
for a long time, and I want to move it forward.
Thanks!
Quoted 82 lines · Weijiang
Intel CPU model-specific LBR(Legacy LBR) has evolved to Architectural
LBR(Arch LBR [0]), it's the replacement of legacy LBR on new platforms. The native support patches were merged into 5.9 kernel tree, and this patch series is to enable Arch LBR in vPMU so that guest can benefit from the feature.The main advantages of Arch LBR are [1]:
- Faster context switching due to XSAVES support and faster reset of LBR MSRs via the new DEPTH MSR
- Faster LBR read for a non-PEBS event due to XSAVES support, which lowers the overhead of the NMI handler.
- Linux kernel can support the LBR features without knowing the model number of the current CPU.From end user's point of view, the usage of Arch LBR is the same as
the Legacy LBR that has been merged in the mainline.Note, in this series, there's one restriction for guest Arch LBR, i.e., guest can only set its LBR record depth the same as host's. This is due to the special behavior of
MSR_ARCH_LBR_DEPTH:
1) On write to the MSR, it'll reset all Arch LBR recording MSRs to 0s.
2) XRSTORS resets all record MSRs to 0s if the saved depth mismatchesMSR_ARCH_LBR_DEPTH.
Enforcing the restriction keeps KVM Arch LBR vPMU working flow simple and straightforward.Paolo refactored the old series and the resulting patches became the base of this new series, therefore he's the author of some patches.
[0] https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html
[1] https://lore.kernel.org/lkml/[email protected]/v1:
https://lore.kernel.org/all/[email protected]/Changes v2:
1. Removed Paolo's SOBs from some patches. [Sean]
2. Modified some patches due to KVM changes, e.g., SMM/vPMU refactor.
3. Rebased to https://git.kernel.org/pub/scm/virt/kvm/kvm.git : queue branch.Like Xu (3):
perf/x86/lbr: Simplify the exposure check for theLBR_INFOregisters
KVM: vmx/pmu: EmulateMSR_ARCH_LBR_DEPTHfor guest Arch LBR
KVM: x86: Add XSAVE Support for Architectural LBRPaolo Bonzini (4):
KVM: PMU: disable LBR handling if architectural LBR is available
KVM: vmx/pmu: EmulateMSR_ARCH_LBR_CTLfor guest Arch LBR
KVM: VMX: Support passthrough of architectural LBRs
KVM: x86: Refine the matching and clearing logic forsupported_xssSean Christopherson (1):
KVM: x86: Report XSS as an MSR to be saved if there are supported
featuresYang Weijiang (7):
KVM: x86: Refresh CPUID on writes toMSR_IA32_XSS
KVM: x86: Add Arch LBR MSRs tomsrs_to_save_alllist
KVM: x86/vmx: Check Arch LBR config when return perf capabilities
KVM: x86/vmx: Disable Arch LBREn bit in #DB and warm reset
KVM: x86/vmx: Save/Restore guest Arch LBR Ctrl msr at SMM entry/exit
KVM: x86: Add Arch LBR data MSR access interface
KVM: x86/cpuid: Advertise Arch LBR feature in CPUIDarch/x86/events/intel/lbr.c | 6 +- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 4 + arch/x86/kvm/cpuid.c | 52 +++++++++- arch/x86/kvm/smm.c | 1 + arch/x86/kvm/smm.h | 3 +- arch/x86/kvm/vmx/capabilities.h | 5 + arch/x86/kvm/vmx/nested.c | 8 ++ arch/x86/kvm/vmx/pmu_intel.c | 161 +++++++++++++++++++++++++++---- arch/x86/kvm/vmx/vmx.c | 74 +++++++++++++- arch/x86/kvm/vmx/vmx.h | 6 +- arch/x86/kvm/x86.c | 27 +++++- 13 files changed, 316 insertions(+), 35 deletions(-)base-commit:
da5f28e10aa7df1a925dbc10656cc89d9c061358
Sean Christopherson
maintainerQuoted 26 lines · Yang Weijiang
Intel CPU model-specific LBR(Legacy LBR) has evolved to Architectural
LBR(Arch LBR [0]), it's the replacement of legacy LBR on new platforms. The native support patches were merged into 5.9 kernel tree, and this patch series is to enable Arch LBR in vPMU so that guest can benefit from the feature.The main advantages of Arch LBR are [1]:
- Faster context switching due to XSAVES support and faster reset of LBR MSRs via the new DEPTH MSR
- Faster LBR read for a non-PEBS event due to XSAVES support, which lowers the overhead of the NMI handler.
- Linux kernel can support the LBR features without knowing the model number of the current CPU.From end user's point of view, the usage of Arch LBR is the same as the Legacy LBR that has been merged in the mainline.
Note, in this series, there's one restriction for guest Arch LBR, i.e., guest can only set its LBR record depth the same as host's. This is due to the special behavior of
MSR_ARCH_LBR_DEPTH:
1) On write to the MSR, it'll reset all Arch LBR recording MSRs to 0s.
2) XRSTORS resets all record MSRs to 0s if the saved depth mismatchesMSR_ARCH_LBR_DEPTH.
Enforcing the restriction keeps KVM Arch LBR vPMU working flow simple and straightforward.
Quoted 2 lines
Paolo refactored the old series and the resulting patches became the base of this new series, therefore he's the author of some patches.
To be very blunt, this series is a mess. I don't want to point fingers as there is plenty of blame to go around. The existing LBR support is a confusing mess, vPMU as a whole has been neglected for too long, review feedback has been relatively non-existent, and I'm sure some of the mess is due to Paolo trying to hastily fix things up back when this was temporarily queued.
However, for arch LBR support to be merged, things need to change.
First and foremost, the existing LBR support needs to be documented. Someone, I don't care who, needs to provide a detailed writeup of the contract between KVM and perf. Specifically, I want to know:
1. When exactly is perf allowed to take control of LBR MRS. Task switch? IRQ? NMI?
2. What is the expected behavior when perf is using LBRs? Is the guest supposed to be traced?
3. Why does KVM snapshot DEBUGCTL with IRQs enabled, but disables IRQs when accessing LBR MSRs?
It doesn't have to be polished, e.g. I'll happily wordsmith things into proper documentation, but I want to have a very clear understanding of how LBR support is _intended_ to function and how it all _actually_ functions without having to make guesses.
And depending on the answers, I want to revisit KVM's LBR implementation before tackling arch LBRs. Letting perf usurp LBRs while KVM has the vCPU loaded is frankly ridiculous. Just have perf set a flag telling KVM that it needs to take control of LBRs and have KVM service the flag as a request or something. Stealing the LBRs back in IRQ context adds a stupid amount of complexity without much value, e.g. waiting a few branches for KVM to get to a safe place isn't going to meaningfully change the traces. If that can't actually happen, then why on earth does KVM need to disable IRQs to read MSRs?
And AFAICT, since KVM unconditionally loads the guest's DEBUGCTL, whether or not guest branches show up in the LBRs when the host is tracing is completely up to the whims of the guest. If that's correct, then again, what's the point of the dance between KVM and perf?
Beyond the "how does this work" issues, there needs to be tests. At the absolute minimum, there needs to be selftests showing that this stuff actually works, that save/restore (migration) works, that the MSRs can/can't be accessed when guest CPUID is (in)correctly configured, etc. And I would really, really like to have tests that force contention between host and guests, e.g. to make sure that KVM isn't leaking host state or outright exploding, but I can understand that those types of tests would be very difficult to write.
I've pushed a heavily reworked, but definitely broken, version to
[email protected]:sean-jc/linux.git x86/arch_lbrs
It compiles, but it's otherwise untested and there are known gaps. E.g. I omitted toggling load+clear of ARCH_LBR_CTL because I couldn't figure out the intended behavior.
Quoted 77 lines · Sean Christopherson
Quoted 26 lines · Yang Weijiang
Intel CPU model-specific LBR(Legacy LBR) has evolved to Architectural
LBR(Arch LBR [0]), it's the replacement of legacy LBR on new platforms. The native support patches were merged into 5.9 kernel tree, and this patch series is to enable Arch LBR in vPMU so that guest can benefit from the feature.The main advantages of Arch LBR are [1]:
- Faster context switching due to XSAVES support and faster reset of LBR MSRs via the new DEPTH MSR
- Faster LBR read for a non-PEBS event due to XSAVES support, which lowers the overhead of the NMI handler.
- Linux kernel can support the LBR features without knowing the model number of the current CPU.From end user's point of view, the usage of Arch LBR is the same as
the Legacy LBR that has been merged in the mainline.Note, in this series, there's one restriction for guest Arch LBR, i.e., guest can only set its LBR record depth the same as host's. This is due to the special behavior of
MSR_ARCH_LBR_DEPTH:
1) On write to the MSR, it'll reset all Arch LBR recording MSRs to 0s.
2) XRSTORS resets all record MSRs to 0s if the saved depth mismatchesMSR_ARCH_LBR_DEPTH.
Enforcing the restriction keeps KVM Arch LBR vPMU working flow simple and straightforward.Quoted 2 lines
Paolo refactored the old series and the resulting patches became the base of this new series, therefore he's the author of some patches.
To be very blunt, this series is a mess. I don't want to point fingers as there is plenty of blame to go around. The existing LBR support is a confusing mess, vPMU as a whole has been neglected for too long, review feedback has been relatively non-existent, and I'm sure some of the mess is due to Paolo trying to hastily fix things up back when this was temporarily queued.
However, for arch LBR support to be merged, things need to change.
First and foremost, the existing LBR support needs to be documented. Someone, I don't care who, needs to provide a detailed writeup of the contract between KVM and perf. Specifically, I want to know:
1. When exactly is perf allowed to take control of LBR MRS. Task switch? IRQ? NMI?
2. What is the expected behavior when perf is using LBRs? Is the guest supposed to be traced?
3. Why does KVM snapshot DEBUGCTL with IRQs enabled, but disables IRQs when accessing LBR MSRs?
It doesn't have to be polished, e.g. I'll happily wordsmith things into proper documentation, but I want to have a very clear understanding of how LBR support is _intended_ to function and how it all _actually_ functions without having to make guesses.
And depending on the answers, I want to revisit KVM's LBR implementation before tackling arch LBRs. Letting perf usurp LBRs while KVM has the vCPU loaded is frankly ridiculous. Just have perf set a flag telling KVM that it needs to take control of LBRs and have KVM service the flag as a request or something. Stealing the LBRs back in IRQ context adds a stupid amount of complexity without much value, e.g. waiting a few branches for KVM to get to a safe place isn't going to meaningfully change the traces. If that can't actually happen, then why on earth does KVM need to disable IRQs to read MSRs?
And AFAICT, since KVM unconditionally loads the guest's DEBUGCTL, whether or not guest branches show up in the LBRs when the host is tracing is completely up to the whims of the guest. If that's correct, then again, what's the point of the dance between KVM and perf?
Beyond the "how does this work" issues, there needs to be tests. At the absolute minimum, there needs to be selftests showing that this stuff actually works, that save/restore (migration) works, that the MSRs can/can't be accessed when guest CPUID is (in)correctly configured, etc. And I would really, really like to have tests that force contention between host and guests, e.g. to make sure that KVM isn't leaking host state or outright exploding, but I can understand that those types of tests would be very difficult to write.
Quoted 7 lines
I've pushed a heavily reworked, but definitely broken, version to
[email protected]:sean-jc/linux.git x86/arch_lbrs
It compiles, but it's otherwise untested and there are known gaps. E.g. I omitted toggling load+clear of
ARCH_LBR_CTLbecause I couldn't figure out the intended behavior.
Appreciated for your elaborate review and comments!
I'll check your reworked version and discuss with stakeholders on how to move the work forward.
+xiongzha to follow up.
Quoted 48 lines · Sean Christopherson
Quoted 26 lines · Yang Weijiang
Intel CPU model-specific LBR(Legacy LBR) has evolved to Architectural
LBR(Arch LBR [0]), it's the replacement of legacy LBR on new platforms. The native support patches were merged into 5.9 kernel tree, and this patch series is to enable Arch LBR in vPMU so that guest can benefit from the feature.The main advantages of Arch LBR are [1]:
- Faster context switching due to XSAVES support and faster reset of LBR MSRs via the new DEPTH MSR
- Faster LBR read for a non-PEBS event due to XSAVES support, which lowers the overhead of the NMI handler.
- Linux kernel can support the LBR features without knowing the model number of the current CPU.From end user's point of view, the usage of Arch LBR is the same as
the Legacy LBR that has been merged in the mainline.Note, in this series, there's one restriction for guest Arch LBR, i.e., guest can only set its LBR record depth the same as host's. This is due to the special behavior of
MSR_ARCH_LBR_DEPTH:
1) On write to the MSR, it'll reset all Arch LBR recording MSRs to 0s.
2) XRSTORS resets all record MSRs to 0s if the saved depth mismatchesMSR_ARCH_LBR_DEPTH.
Enforcing the restriction keeps KVM Arch LBR vPMU working flow simple and straightforward.Quoted 2 lines
Paolo refactored the old series and the resulting patches became the base of this new series, therefore he's the author of some patches.
To be very blunt, this series is a mess. I don't want to point fingers as there is plenty of blame to go around. The existing LBR support is a confusing mess, vPMU as a whole has been neglected for too long, review feedback has been relatively non-existent, and I'm sure some of the mess is due to Paolo trying to hastily fix things up back when this was temporarily queued.
However, for arch LBR support to be merged, things need to change.
First and foremost, the existing LBR support needs to be documented. Someone, I don't care who, needs to provide a detailed writeup of the contract between KVM and perf. Specifically, I want to know:
1. When exactly is perf allowed to take control of LBR MRS. Task switch? IRQ? NMI?
2. What is the expected behavior when perf is using LBRs? Is the guest supposed to be traced?
Quoted 7 lines
3. Why does KVM snapshot DEBUGCTL with IRQs enabled, but disables IRQs when accessing LBR MSRs?
It doesn't have to be polished, e.g. I'll happily wordsmith things into proper documentation, but I want to have a very clear understanding of how LBR support is _intended_ to function and how it all _actually_ functions without having to make guesses.
This is a very good topic for LPC KVM Microconference.
Many thanks to Sean for ranting about something that only I was thinking about before. Having host and guest be able to use PMU at the same time in peace (hybrid profiling mode) is a very use-case worthy goal rather than introducing exclusivity, and it's clear that kvm+perf lacks reasonable and well-documented support when there is host perf user interference.
Ref: https://lpc.events/event/17/page/200-proposed-microconferences#kvm
Quoted 30 lines
And depending on the answers, I want to revisit KVM's LBR implementation before tackling arch LBRs. Letting perf usurp LBRs while KVM has the vCPU loaded is frankly ridiculous. Just have perf set a flag telling KVM that it needs to take control of LBRs and have KVM service the flag as a request or something. Stealing the LBRs back in IRQ context adds a stupid amount of complexity without much value, e.g. waiting a few branches for KVM to get to a safe place isn't going to meaningfully change the traces. If that can't actually happen, then why on earth does KVM need to disable IRQs to read MSRs?
And AFAICT, since KVM unconditionally loads the guest's DEBUGCTL, whether or not guest branches show up in the LBRs when the host is tracing is completely up to the whims of the guest. If that's correct, then again, what's the point of the dance between KVM and perf?
Beyond the "how does this work" issues, there needs to be tests. At the absolute minimum, there needs to be selftests showing that this stuff actually works, that save/restore (migration) works, that the MSRs can/can't be accessed when guest CPUID is (in)correctly configured, etc. And I would really, really like to have tests that force contention between host and guests, e.g. to make sure that KVM isn't leaking host state or outright exploding, but I can understand that those types of tests would be very difficult to write.
I've pushed a heavily reworked, but definitely broken, version to
[email protected]:sean-jc/linux.git x86/arch_lbrs
It compiles, but it's otherwise untested and there are known gaps. E.g. I omitted toggling load+clear of
ARCH_LBR_CTLbecause I couldn't figure out the intended behavior.