Introduce Architectural LBR for vPMU
Subsystems (4)
v9 cover Introduce Architectural LBR for vPMU
The Architectural Last Branch Records (LBRs) is published in release Intel Architecture Instruction Set Extensions and Future Features Programming Reference[0].
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 KVM series, we impose one restriction for guest Arch LBR:
Guest can only set the same LBR record depth as host, 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.
But this restriction won't impact guest perf tool usage.
[0] https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf
[1] https://lore.kernel.org/lkml/[email protected]/
Qemu patch:
https://patchwork.ozlabs.org/project/qemu-devel/cover/[email protected]/
Previous version:
v8: https://lkml.kernel.org/kvm/[email protected]/
Changes in v9:
1. Added Arch LBR MSR access interface for userspace.
2. Refactored XSS related dependent patches so that xsaves/xrstors can work for guest.
3. Refactored Arch LBR CTL and DEPTH MSR handling in KVM.
4. Rebased and tested on kernel base-commit: c5d9ae265b10
Like Xu (6):
perf/x86/intel: Fix the comment about guest LBR support on KVM 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: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
KVM: x86: Refine the matching and clearing logic for supported_xss
KVM: x86: Add XSAVE Support for Architectural LBR
Sean Christopherson (2):
KVM: x86: Report XSS as an MSR to be saved if there are supported
features
KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES
Yang Weijiang (9):
KVM: x86: Refresh CPUID on writes to MSR_IA32_XSS
KVM: x86: Add Arch LBR MSRs to msrs_to_save_all list
KVM: x86/pmu: Refactor code to support guest Arch LBR
KVM: x86/vmx: Check Arch LBR config when return perf capabilities
KVM: nVMX: Add necessary Arch LBR settings for nested VM
KVM: x86/vmx: Clear Arch LBREn bit before inject #DB to guest
KVM: x86/vmx: Flip Arch LBREn bit on guest state change
KVM: x86: Add Arch LBR MSR access interface
KVM: x86/cpuid: Advertise Arch LBR feature in CPUID
arch/x86/events/intel/core.c | 3 +- arch/x86/events/intel/lbr.c | 6 +- arch/x86/include/asm/kvm_host.h | 7 ++ arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 4 + arch/x86/kvm/cpuid.c | 54 ++++++++++- arch/x86/kvm/vmx/capabilities.h | 8 ++ arch/x86/kvm/vmx/nested.c | 7 +- arch/x86/kvm/vmx/pmu_intel.c | 155 ++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 3 +- arch/x86/kvm/vmx/vmx.c | 65 ++++++++++++- arch/x86/kvm/x86.c | 78 +++++++++++++++- 13 files changed, 356 insertions(+), 36 deletions(-)
Signature · 1 line
2.27.0
PATCH v9 1/17 KVM: x86: Report XSS as an MSR to be saved if there are supported features
From: 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 | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/x86.c +7 -0
@@ -1390,6 +1390,7 @@ static const u32 msrs_to_save_all[] = {MSR_F15H_PERF_CTR0 ,MSR_F15H_PERF_CTR1 ,MSR_F15H_PERF_CTR2 ,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)];
@@ -6585,6 +6586,10 @@ static void kvm_init_msr_list(void)if (!kvm_cpu_cap_has (X86_FEATURE_XFD ))continue ;break ; +case MSR_IA32_XSS : +if (!supported_xss) +continue ; +break ;default :break ; }
@@ -11481,6 +11486,8 @@ int kvm_arch_hardware_setup(void *opaque)if (!kvm_cpu_cap_has (X86_FEATURE_XSAVES )) supported_xss =0 ; +else + supported_xss &= host_xss;#define __kvm_cpu_cap_has (UNUSED_ , f) kvm_cpu_cap_has(f) cr4_reserved_bits =__cr4_reserved_bits (__kvm_cpu_cap_has,UNUSED_ );
Signature · 1 line
2.27.0
PATCH v9 2/17 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/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 21 ++++++++++++++++++--- arch/x86/kvm/x86.c | 8 +++++--- 3 files changed, 24 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -704,6 +704,7 @@ struct kvm_vcpu_arch {u64 xcr0;u64 guest_supported_xcr0; +u64 guest_supported_xss;struct kvm_pio_request pio;void *pio_data;
arch/x86/kvm/cpuid.c +18 -3
@@ -239,9 +239,24 @@ 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 ; + } + vcpu->arch.guest_supported_xss = + (((u64 )best->edx <<32 ) | best->ecx) & supported_xss; + + }else { + vcpu->arch.guest_supported_xss =0 ; + } best =__kvm_find_kvm_cpuid_features (vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best &&
arch/x86/kvm/x86.c +5 -3
@@ -3535,10 +3535,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than * XSAVES/XRSTORS to save/restore PT MSRs. */ -if (data & ~supported_xss) +if (data & ~vcpu->arch.guest_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 v9 3/17 KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVES
In reply to Yang WeijiangFrom: Sean Christopherson <[email protected]>
If new feature MSRs are supported in XSS and passed through to the guest they are saved and restored by XSAVES/XRSTORS, i.e. in the guest's FPU state.
Load the guest's FPU state if userspace is accessing MSRs whose values are managed by XSAVES so that the MSR helper, e.g. kvm_{get,set}_xsave_msr(), can simply do {RD,WR}MSR to access the guest's value.
Because it's also used for the KVM_GET_MSRS device ioctl(), explicitly check that @vcpu is non-null before attempting to load guest state. The XSS supporting MSRs cannot be retrieved via the device ioctl() without loading guest FPU state (which doesn't exist).
MSRs that are switched through XSAVES are especially annoying due to the possibility of the kernel's FPU being used in IRQ context. Disable IRQs and ensure the guest's FPU state is loaded when accessing such MSRs.
Note that guest_cpuid_has() is not queried as host userspace is allowed to access MSRs that have not been exposed to the guest, e.g. it might do KVM_SET_MSRS prior to KVM_SET_CPUID2.
--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/x86.c | 44 ++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1731,6 +1731,9 @@ int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu);int kvm_get_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr);int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr); +void kvm_get_xsave_msr (struct msr_data *msr_info); +void kvm_set_xsave_msr (struct msr_data *msr_info); +unsigned long kvm_get_rflags (struct kvm_vcpu *vcpu);void kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags);int kvm_emulate_rdpmc (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +43 -1
66 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v9 4/17 perf/x86/intel: Fix the comment about guest LBR support on KVM
In reply to Yang WeijiangFrom: Like Xu <[email protected]>
Starting from v5.12, KVM reports guest LBR and extra_regs support when the host has relevant support. Just delete this part of the comment and fix a typo incidentally.
--- arch/x86/events/intel/core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/events/intel/core.c +1 -2
@@ -6353,8 +6353,7 @@ __init int intel_pmu_init(void) x86_pmu.intel_ctrl);/* * Access LBR MSR may cause #GP under certain circumstances. -* E.g. KVM doesn't support LBR MSR -* Check all LBT MSR here. +* Check all LBR MSR here. * Disable LBR access if any LBR MSRs can not be accessed. */ if (x86_pmu.lbr_tos && !check_msr (x86_pmu.lbr_tos,0x3UL ))
Signature · 1 line
2.27.0
PATCH v9 5/17 perf/x86/lbr: Simplify the exposure check for the LBR_INFO registers
In reply to Yang WeijiangFrom: 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
@@ -1874,12 +1874,10 @@ void __init intel_pmu_arch_lbr_init(void)*/ int 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;return 0 ; }
Signature · 1 line
2.27.0
PATCH v9 6/17 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 | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kvm/x86.c +6 -0
@@ -1394,6 +1394,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 , + MSR_ARCH_LBR_CTL, MSR_ARCH_LBR_DEPTH, };static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_all)];
@@ -6634,6 +6635,11 @@ static void kvm_init_msr_list(void)if (!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
PATCH v9 7/17 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 | 48 ++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -528,6 +528,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 +47 -1
94 lines not on this page · raw message
Signature · 1 line
2.27.0
PATCH v9 8/17 KVM: vmx/pmu: Emulate MSR_ARCH_LBR_CTL for guest Arch LBR
In reply to Yang WeijiangFrom: Like Xu <[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 | 58 +++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmx.c | 12 +++++++ 5 files changed, 68 insertions(+), 7 deletions(-)
arch/x86/events/intel/lbr.c +0 -2
@@ -160,8 +160,6 @@ enum { 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
@@ -169,6 +169,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
@@ -245,6 +245,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 +53 -5
95 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +12 -0
@@ -2034,6 +2034,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))
@@ -4563,6 +4570,11 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu);vpid_sync_context (vmx->vpid); + +if (!init_event) { +if (static_cpu_has (X86_FEATURE_ARCH_LBR )) +vmcs_write64 (GUEST_IA32_LBR_CTL ,0 ); + } }static void vmx_enable_irq_window (struct kvm_vcpu *vcpu)
Signature · 1 line
2.27.0
PATCH v9 9/17 KVM: x86/pmu: Refactor code to support guest Arch LBR
In reply to Yang WeijiangTake account of Arch LBR when do sanity checks before program vPMU for guest. Pass through Arch LBR recording MSRs to guest to gain better performance. Note, Arch LBR and Legacy LBR support are mutually exclusive, i.e., they're not both available on one platform.
--- arch/x86/kvm/vmx/pmu_intel.c | 37 +++++++++++++++++++++++++++++------- arch/x86/kvm/vmx/vmx.c | 3 +++ 2 files changed, 33 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +30 -7
@@ -170,12 +170,16 @@ static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr)bool intel_pmu_lbr_is_compatible (struct kvm_vcpu *vcpu) { +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +return guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR ); +/* * As a first step, a guest could only enable LBR feature if its * cpu model is the same as the host because the LBR registers * would be pass-through to the guest and they're model specific. */ -return boot_cpu_data.x86_model ==guest_cpuid_model (vcpu); +return !boot_cpu_has (X86_FEATURE_ARCH_LBR ) && + boot_cpu_data.x86_model ==guest_cpuid_model (vcpu); }bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu)
@@ -193,12 +197,19 @@ static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index)if (!intel_pmu_lbr_is_enabled (vcpu))return ret; - 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 )) + ret = (index ==MSR_LBR_SELECT ) || (index ==MSR_LBR_TOS ); + +if (!ret) { + ret = (index >= records->from && + index < records->from + records->nr) || + (index >= records->to && + index < records->to + records->nr); + }if (!ret && records->info) - ret = (index >= records->info && index < records->info + records->nr); + ret = (index >= records->info && + index < records->info + records->nr);return ret; }
@@ -738,6 +749,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); }
@@ -778,10 +792,13 @@ 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_LBR_CTL ) &ARCH_LBR_CTL_LBREN ) : + (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;
@@ -798,13 +815,19 @@ void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)return ; warn: +if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) +wrmsrl (MSR_ARCH_LBR_DEPTH , lbr_desc->records.nr);pr_warn_ratelimited ("kvm: vcpu-%d: fail to passthrough LBR.\n" , vcpu->vcpu_id); }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_LBR_CTL ) &ARCH_LBR_CTL_LBREN ) : + (vmcs_read64 (GUEST_IA32_DEBUGCTL ) &DEBUGCTLMSR_LBR ); + +if (!lbr_enable)intel_pmu_release_guest_lbr_event (vcpu); }
arch/x86/kvm/vmx/vmx.c +3 -0
@@ -578,6 +578,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 v9 10/17 KVM: x86: Refine the matching and clearing logic for supported_xss
In reply to Yang WeijiangFrom: Like Xu <[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
@@ -7434,9 +7434,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_set (X86_FEATURE_UMIP );/* CPUID 0xD.1 */ - supported_xss =0 ; -if (!cpu_has_vmx_xsaves ()) +if (!cpu_has_vmx_xsaves ()) {kvm_cpu_cap_clear (X86_FEATURE_XSAVES ); + supported_xss =0 ; + }/* CPUID 0x80000001 and 0x7 (RDPID) */ if (!cpu_has_vmx_rdtscp ()) {
arch/x86/kvm/x86.c +5 -1
@@ -223,6 +223,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);
@@ -11522,8 +11524,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); + supported_xss = host_xss &KVM_SUPPORTED_XSS ; + } r = ops->hardware_setup ();if (r !=0 )
Signature · 1 line
2.27.0
PATCH v9 11/17 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
@@ -7422,6 +7422,10 @@ static __init void vmx_set_cpu_caps(void)kvm_cpu_cap_clear (X86_FEATURE_INVPCID );if (vmx_pt_mode_is_host_guest ())kvm_cpu_cap_check_and_set (X86_FEATURE_INTEL_PT ); +if (!cpu_has_vmx_arch_lbr ()) { +kvm_cpu_cap_clear (X86_FEATURE_ARCH_LBR ); + supported_xss &= ~XFEATURE_MASK_LBR ; + }if (!enable_sgx) {kvm_cpu_cap_clear (X86_FEATURE_SGX );
arch/x86/kvm/x86.c +1 -1
@@ -223,7 +223,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 v9 12/17 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.
--- arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/capabilities.h | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 10 ++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-)
arch/x86/include/asm/vmx.h +2 -0
@@ -95,6 +95,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
@@ -108,6 +109,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 +8 -0
@@ -385,6 +385,12 @@ static inline bool vmx_pt_mode_is_host_guest(void)return pt_mode ==PT_MODE_HOST_GUEST ; } +static inline bool cpu_has_vmx_arch_lbr (void ) +{ +return (vmcs_config.vmexit_ctrl &VM_EXIT_CLEAR_IA32_LBR_CTL ) && + (vmcs_config.vmentry_ctrl &VM_ENTRY_LOAD_IA32_LBR_CTL ); +} +static inline u64 vmx_get_perf_capabilities (void ) {u64 perf_cap =0 ;
@@ -396,6 +402,8 @@ static inline u64 vmx_get_perf_capabilities(void)rdmsrl (MSR_IA32_PERF_CAPABILITIES , perf_cap); perf_cap &=PMU_CAP_LBR_FMT ; +if (boot_cpu_has (X86_FEATURE_ARCH_LBR ) && !cpu_has_vmx_arch_lbr ()) + perf_cap &= ~PMU_CAP_LBR_FMT ;/* * Since counters are virtualized, KVM would support full
arch/x86/kvm/vmx/vmx.c +8 -2
@@ -2544,7 +2544,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,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 ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_EXIT_CTLS , &_vmexit_control) <0 )return -EIO ;
@@ -2568,7 +2569,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,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 ;if (adjust_vmx_controls (min, opt,MSR_IA32_VMX_ENTRY_CTLS , &_vmentry_control) <0 )return -EIO ;
@@ -5933,6 +5935,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",
Signature · 1 line
2.27.0
PATCH v9 13/17 KVM: nVMX: Add necessary Arch LBR settings for nested VM
In reply to Yang WeijiangArch LBR is not supported in nested VM now. This patch is to add necessary settings to make it pass host KVM checks before L2 VM is launched and also to avoid some warnings reported from L1.
--- arch/x86/kvm/vmx/nested.c | 7 +++++-- arch/x86/kvm/vmx/pmu_intel.c | 2 ++ arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +5 -2
@@ -6552,7 +6552,9 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)VM_EXIT_HOST_ADDR_SPACE_SIZE | #endifVM_EXIT_LOAD_IA32_PAT |VM_EXIT_SAVE_IA32_PAT | -VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL ; +VM_EXIT_CLEAR_BNDCFGS |VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | +VM_EXIT_CLEAR_IA32_LBR_CTL ; + msrs->exit_ctls_high |=VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
@@ -6572,7 +6574,8 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps)VM_ENTRY_IA32E_MODE | #endifVM_ENTRY_LOAD_IA32_PAT |VM_ENTRY_LOAD_BNDCFGS | -VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL ; +VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |VM_ENTRY_LOAD_IA32_LBR_CTL ; + msrs->entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |VM_ENTRY_LOAD_IA32_EFER );
arch/x86/kvm/vmx/pmu_intel.c +2 -0
@@ -228,6 +228,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)break ;case MSR_ARCH_LBR_DEPTH :case MSR_ARCH_LBR_CTL : +if (is_guest_mode (vcpu)) +break ;if (kvm_cpu_cap_has (X86_FEATURE_ARCH_LBR )) ret =guest_cpuid_has (vcpu,X86_FEATURE_ARCH_LBR );break ;
arch/x86/kvm/vmx/vmcs12.c +1 -0
@@ -65,6 +65,7 @@ const unsigned short vmcs12_field_offsets[] = {FIELD64 (HOST_IA32_PAT , host_ia32_pat),FIELD64 (HOST_IA32_EFER , host_ia32_efer),FIELD64 (HOST_IA32_PERF_GLOBAL_CTRL , host_ia32_perf_global_ctrl), +FIELD64 (GUEST_IA32_LBR_CTL , guest_lbr_ctl),FIELD (PIN_BASED_VM_EXEC_CONTROL , pin_based_vm_exec_control),FIELD (CPU_BASED_VM_EXEC_CONTROL , cpu_based_vm_exec_control), FIELD(EXCEPTION_BITMAP, exception_bitmap),
arch/x86/kvm/vmx/vmcs12.h +2 -1
@@ -71,7 +71,7 @@ struct __packed vmcs12 {u64 pml_address;u64 encls_exiting_bitmap;u64 tsc_multiplier; -u64 padding64[1 ];/* room for future expansion */ +u64 guest_lbr_ctl;/* * To allow migration of L1 (complete with its L2 guests) between * machines of different natural widths (32 or 64 bit), we cannot have
@@ -254,6 +254,7 @@ static inline void vmx_check_vmcs12_offsets(void)CHECK_OFFSET (pml_address,312 );CHECK_OFFSET (encls_exiting_bitmap,320 );CHECK_OFFSET (tsc_multiplier,328 ); +CHECK_OFFSET (guest_lbr_ctl,336 );CHECK_OFFSET (cr0_guest_host_mask,344 );CHECK_OFFSET (cr4_guest_host_mask,352 );CHECK_OFFSET (cr0_read_shadow,360 );
Signature · 1 line
2.27.0
PATCH v9 14/17 KVM: x86/vmx: Clear Arch LBREn bit before inject #DB to guest
In reply to Yang WeijiangOn a debug breakpoint event (#DB), IA32_LBR_CTL.LBREn is cleared. So need to clear the bit manually before inject #DB.
--- arch/x86/kvm/vmx/vmx.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
arch/x86/kvm/vmx/vmx.c +27 -0
@@ -1617,6 +1617,27 @@ static void vmx_clear_hlt(struct kvm_vcpu *vcpu)vmcs_write32 (GUEST_ACTIVITY_STATE ,GUEST_ACTIVITY_ACTIVE ); } +static void flip_arch_lbr_ctl (struct kvm_vcpu *vcpu,bool on) +{ +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 old =vmcs_read64 (GUEST_IA32_LBR_CTL ); +u64 new; + +if (on) + new = old |ARCH_LBR_CTL_LBREN ; +else + new = old & ~ARCH_LBR_CTL_LBREN ; + +if (old != new) +vmcs_write64 (GUEST_IA32_LBR_CTL , new); + } +} +static void vmx_queue_exception (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
@@ -1652,6 +1673,9 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu)vmcs_write32 (VM_ENTRY_INTR_INFO_FIELD , intr_info);vmx_clear_hlt (vcpu); + +if (nr ==DB_VECTOR ) +flip_arch_lbr_ctl (vcpu, false); } static void vmx_setup_uret_msr(struct vcpu_vmx *vmx, unsigned int msr,
@@ -4655,6 +4679,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.nr ==DB_VECTOR ) +flip_arch_lbr_ctl (vcpu, false); }bool vmx_get_nmi_mask (struct kvm_vcpu *vcpu)
Signature · 1 line
2.27.0
PATCH v9 15/17 KVM: x86/vmx: Flip Arch LBREn bit on guest state change
In reply to Yang WeijiangPer spec:"IA32_LBR_CTL.LBREn is saved and cleared on #SMI, and restored on RSM. 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." So clear Arch LBREn bit on #SMI and restore it on RSM manully, also clear the bit when guest does warm reset.
--- arch/x86/kvm/vmx/vmx.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -4603,6 +4603,8 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)if (!init_event) {if (static_cpu_has (X86_FEATURE_ARCH_LBR ))vmcs_write64 (GUEST_IA32_LBR_CTL ,0 ); + }else { +flip_arch_lbr_ctl (vcpu, false); } }
@@ -7694,6 +7696,7 @@ static int vmx_enter_smm(struct kvm_vcpu *vcpu, char *smstate) vmx->nested.smm.vmxon = vmx->nested.vmxon; vmx->nested.vmxon = false;vmx_clear_hlt (vcpu); +flip_arch_lbr_ctl (vcpu, false);return 0 ; }
@@ -7715,6 +7718,7 @@ static int vmx_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) vmx->nested.nested_run_pending =1 ; vmx->nested.smm.guest_mode = false; } +flip_arch_lbr_ctl (vcpu, true);return 0 ; }
Signature · 1 line
2.27.0
PATCH v9 16/17 KVM: x86: Add Arch LBR MSR access interface
In reply to Yang WeijiangWhen userspace wants to access guest Arch LBR data MSRs, these MSRs actually reside in guest FPU area, so need to load them to HW before RDMSR and save them back into FPU area after WRMSR.
--- arch/x86/kvm/vmx/pmu_intel.c | 10 ++++++++++ arch/x86/kvm/x86.c | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +10 -0
@@ -431,6 +431,11 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_ARCH_LBR_CTL : 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: +kvm_get_xsave_msr (msr_info); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {
@@ -511,6 +516,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: +kvm_set_xsave_msr (msr_info); +return 0 ;default :if ((pmc =get_gp_pmc (pmu, msr,MSR_IA32_PERFCTR0 )) || (pmc =get_gp_pmc (pmu, msr,MSR_IA32_PMC0 ))) {
arch/x86/kvm/x86.c +8 -1
@@ -4105,7 +4105,14 @@ EXPORT_SYMBOL_GPL(kvm_set_xsave_msr);*/ static bool is_xsaves_msr (u32 index) { -return false; +bool xsaves_msr = (index >=MSR_ARCH_LBR_FROM_0 && + index <=MSR_ARCH_LBR_FROM_0 +31 ) || + (index >=MSR_ARCH_LBR_TO_0 && + index <=MSR_ARCH_LBR_TO_0 +31 ) || + (index >=MSR_ARCH_LBR_INFO_0 && + index <=MSR_ARCH_LBR_INFO_0 +31 ); + +return xsaves_msr; }/*
Signature · 1 line
2.27.0
PATCH v9 17/17 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 | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +32 -1
@@ -102,6 +102,16 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,if (vaddr_bits !=48 && vaddr_bits !=57 && vaddr_bits !=0 )return -EINVAL ; } + 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 ((best->eax &0xff ) !=BIT (fls (eax &0xff ) -1 )) +return -EINVAL ; + }/* * Exposing dynamic xfeatures to the guest requires additional
@@ -600,7 +610,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. */
@@ -1023,6 +1033,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
Ping...
Hi, Paolo and other maintainers,
Arch LBR is the enhancement and replacement of Legacy LBR, this patch-set is
the necessity for guest Arch LBR usage on new Intel platforms starting from
Sapphire Rapids.
We're appreciated for your review and comments on these patches, thanks a lot!
Quoted 77 lines · Yang Weijiang
The Architectural Last Branch Records (LBRs) is published in release Intel Architecture Instruction Set Extensions and Future Features Programming Reference[0].
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 KVM series, we impose one restriction for guest Arch LBR:
Guest can only set the same LBR record depth as host, this is due to the special behavior ofMSR_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.But this restriction won't impact guest perf tool usage.
[0]https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf [1]https://lore.kernel.org/lkml/[email protected]/
Qemu patch:
https://patchwork.ozlabs.org/project/qemu-devel/cover/[email protected]/Previous version:
v8:https://lkml.kernel.org/kvm/[email protected]/Changes in v9:
1. Added Arch LBR MSR access interface for userspace.
2. Refactored XSS related dependent patches so that xsaves/xrstors can work for guest.
3. Refactored Arch LBR CTL and DEPTH MSR handling in KVM.
4. Rebased and tested on kernel base-commit:c5d9ae265b10Like Xu (6):
perf/x86/intel: Fix the comment about guest LBR support on KVM perf/x86/lbr: Simplify the exposure check for theLBR_INFOregisters
KVM: vmx/pmu: EmulateMSR_ARCH_LBR_DEPTHfor guest Arch LBR
KVM: vmx/pmu: EmulateMSR_ARCH_LBR_CTLfor guest Arch LBR
KVM: x86: Refine the matching and clearing logic forsupported_xss
KVM: x86: Add XSAVE Support for Architectural LBRSean Christopherson (2):
KVM: x86: Report XSS as an MSR to be saved if there are supported
features
KVM: x86: Load guest fpu state when accessing MSRs managed by XSAVESYang Weijiang (9):
KVM: x86: Refresh CPUID on writes toMSR_IA32_XSS
KVM: x86: Add Arch LBR MSRs tomsrs_to_save_alllist
KVM: x86/pmu: Refactor code to support guest Arch LBR
KVM: x86/vmx: Check Arch LBR config when return perf capabilities
KVM: nVMX: Add necessary Arch LBR settings for nested VM
KVM: x86/vmx: Clear Arch LBREn bit before inject #DB to guest
KVM: x86/vmx: Flip Arch LBREn bit on guest state change
KVM: x86: Add Arch LBR MSR access interface
KVM: x86/cpuid: Advertise Arch LBR feature in CPUIDarch/x86/events/intel/core.c | 3 +- arch/x86/events/intel/lbr.c | 6 +- arch/x86/include/asm/kvm_host.h | 7 ++ arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/vmx.h | 4 + arch/x86/kvm/cpuid.c | 54 ++++++++++- arch/x86/kvm/vmx/capabilities.h | 8 ++ arch/x86/kvm/vmx/nested.c | 7 +- arch/x86/kvm/vmx/pmu_intel.c | 155 ++++++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 3 +- arch/x86/kvm/vmx/vmx.c | 65 ++++++++++++- arch/x86/kvm/x86.c | 78 +++++++++++++++- 13 files changed, 356 insertions(+), 36 deletions(-)