KVM: SVM: Support for PMC virtualization
Subsystems (4)
cover KVM: SVM: Support for PMC virtualization
AMD Zen 5 processors introduced PMC virtualization. When the feature is enabled, hardware can restore and save the guest PMU state at VM-Entry and VM-Exit respectively. The host PMU state still needs to be managed by software and this is done with the help of the Mediated PMU framework which can schedule the host perf events in or out.
This feature is documented under Section 15.39 in Volume 2 of the AMD64 Architecture Programmer’s Manual which can be found below.
https://bugzilla.kernel.org/attachment.cgi?id=306250
The guest PMU state is saved in the VMCB. Hence, struct kvm_pmu goes out of sync and does not have the latest PMU state. In some cases, such as during event filtering or instruction emulation, the VMCB needs to be accessed for reading or writing the current register state. This is done using host-initiated MSR accesses but the method may not be ideal and perhaps requires additional PMU ops to be introduced. The goal is to have data in vendor-specific structures like VMCB functions accessible to some of the common KVM x86 PMU functions.
Any feedback is appreciated.
Patch Summary
* The first three patches add new feature bits and capability flags to detect and advertise support for hardware PMU virtualization.
* The fourth patch extends Mediated PMU to make use of hardware PMU virtualization.
* The remaining patches add the vendor-specific changes for PMC virtualization.
TODOs
* Add appropriate KVM PMU ops to access MSR states from the VMCB.
* Make MSR states in the VMCB accessible via KVM_{GET,SET}_MSRS.
* Support for SEV-ES and SEV-SNP guests.
* Support for nested guests (nSVM).
* Support for the IRPerfCount register which is a free-running instructions retired counter.
Testing
* Used three different guest PMU configurations.
* PerfMonV2 capable - 6 counters with Global Control and Status registers (Zen 4 and later).
* PerfCtrExtCore capable - 6 counters but no Global Control and Status registers (Zen 3 and older).
* Legacy Guest - 4 counters only (pre-Zen, older than Family 15h).
* KVM Unit Tests passed.
* Perf Fuzzer passed.
The patches should be applied over v5 of the Mediated PMU series [1] along with an RDPMC interception fix [2].
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Sandipan Das (7):
perf: Add a capability for hardware virtualized PMUs x86/cpufeatures: Add PerfCtrVirt feature bit perf/x86/amd/core: Set PERF_PMU_CAP_VIRTUALIZED_VPMU
KVM: x86/pmu: Add support for hardware virtualized PMUs
KVM: SVM: Add VMCB fields for PMC virtualization
KVM: SVM: Add support for PMC virtualization
KVM: SVM: Adjust MSR index for legacy guests
arch/x86/events/amd/core.c | 3 + arch/x86/events/core.c | 1 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/perf_event.h | 1 + arch/x86/include/asm/svm.h | 12 ++- arch/x86/kvm/pmu.c | 94 ++++++++++++++++++----- arch/x86/kvm/pmu.h | 6 ++ arch/x86/kvm/svm/pmu.c | 115 +++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 52 +++++++++++++ arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 4 + arch/x86/kvm/x86.h | 1 + include/linux/perf_event.h | 1 + 14 files changed, 273 insertions(+), 20 deletions(-)
Signature · 1 line
2.43.0
RFC PATCH 1/7 perf: Add a capability for hardware virtualized PMUs
Introduce PERF_PMU_CAP_VIRTUALIZED_VPMU as an extension to the existing PERF_PMU_CAP_MEDIATED_VPMU to indicate support for hardware virtualized PMU where the guest counter states are automatically saved and restored during world switches.
Pass on the new capability through x86_pmu_cap so that any other entity, such as KVM, can enquire if the host has hardware PMU virtualization.
--- arch/x86/events/core.c | 1 + arch/x86/include/asm/perf_event.h | 1 + include/linux/perf_event.h | 1 + 3 files changed, 3 insertions(+)
arch/x86/events/core.c +1 -0
@@ -3131,6 +3131,7 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->events_mask_len = x86_pmu.events_mask_len; cap->pebs_ept = x86_pmu.pebs_ept; cap->mediated = !!(pmu.capabilities &PERF_PMU_CAP_MEDIATED_VPMU ); + cap->virtualized = !!(pmu.capabilities &PERF_PMU_CAP_VIRTUALIZED_VPMU ); }EXPORT_SYMBOL_GPL (perf_get_x86_pmu_capability);
arch/x86/include/asm/perf_event.h +1 -0
@@ -293,6 +293,7 @@ struct x86_pmu_capability {int events_mask_len; unsigned int pebs_ept :1; unsigned int mediated :1; + unsigned int virtualized :1; };/*
include/linux/perf_event.h +1 -0
@@ -306,6 +306,7 @@ struct perf_event_pmu_context;#define PERF_PMU_CAP_AUX_PAUSE 0x0200#define PERF_PMU_CAP_AUX_PREFER_LARGE 0x0400#define PERF_PMU_CAP_MEDIATED_VPMU 0x0800 +#define PERF_PMU_CAP_VIRTUALIZED_VPMU 0x1000/** * pmu::scope
Signature · 1 line
2.43.0
RFC PATCH 2/7 x86/cpufeatures: Add PerfCtrVirt feature bit
In reply to Sandipan DasDefine a feature flag for bit 8 of CPUID leaf 0x8000000A EDX which indicates support for virtualization of core performance monitoring counters.
When this feature is available, a hypervisor can rely on hardware to restore and save guest counter state when entering or exiting from guest context.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -370,6 +370,7 @@#define X86_FEATURE_VMCBCLEAN (15*32+ 5)/* "vmcb_clean" VMCB clean bits support */ #define X86_FEATURE_FLUSHBYASID (15*32+ 6)/* "flushbyasid" Flush-by-ASID support */ #define X86_FEATURE_DECODEASSISTS (15*32+ 7)/* "decodeassists" Decode Assists support */ +#define X86_FEATURE_PERFCTR_VIRT (15*32+ 8)/* "perfctr_virt" PMC virtualization support */ #define X86_FEATURE_PAUSEFILTER (15*32+10)/* "pausefilter" Filtered pause intercept */ #define X86_FEATURE_PFTHRESHOLD (15*32+12)/* "pfthreshold" Pause filter threshold */ #define X86_FEATURE_AVIC (15*32+13)/* "avic" Virtual Interrupt Controller */
Signature · 1 line
2.43.0
RFC PATCH 3/7 perf/x86/amd/core: Set PERF_PMU_CAP_VIRTUALIZED_VPMU
In reply to Sandipan DasSet the PERF_PMU_CAP_VIRTUALIZED_VPMU flag for processors that support X86_FEATURE_PERFCTR_VIRT.
--- arch/x86/events/amd/core.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/events/amd/core.c +3 -0
@@ -1443,6 +1443,9 @@ static int __init amd_core_pmu_init(void)static_call_update (amd_pmu_test_overflow, amd_pmu_test_overflow_status); } +if (cpu_feature_enabled (X86_FEATURE_PERFCTR_VIRT )) +x86_get_pmu (smp_processor_id ())->capabilities |=PERF_PMU_CAP_VIRTUALIZED_VPMU ; +/* * AMD Core perfctr has separate MSRs for the NB events, see * the amd/uncore.c driver.
Signature · 1 line
2.43.0
RFC PATCH 4/7 KVM: x86/pmu: Add support for hardware virtualized PMUs
In reply to Sandipan DasExtend the Mediated PMU framework to support hardware virtualized PMUs. The key differences with Mediated PMU are listed below.
* Hardware saves and restores the guest PMU state on world switches.
* The guest PMU state is saved in vendor-specific structures (such as VMCB or VMCS) instead of struct kvm_pmu.
* Hardware relies on interrupt virtualization (such as VNMI or AVIC) to notify guests about counter overflows instead of receiving interrupts in host context after switching the delivery mode in LVTPC and then injecting them back in to the guest (KVM_REQ_PMI).
Parts of the original PMU load and put functionality are reused as the active host events still need to be scheduled in and out in preparation for world switches.
Event filtering and instruction emulation require the ability to change the guest PMU state in software. Since struct kvm_pmu no longer has the correct state, make use of host-initiated MSR accesses for accessing MSR states directly from vendor-specific structures.
RDPMC is intercepted for legacy guests which do not have access to all counters. Host-initiated MSR accesses are also used in such cases to read the latest counter value from vendor-specific structures.
--- arch/x86/kvm/pmu.c | 94 +++++++++++++++++++++++++++++------- arch/x86/kvm/pmu.h | 6 +++ arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 4 ++ arch/x86/kvm/x86.h | 1 + 6 files changed, 89 insertions(+), 18 deletions(-)
arch/x86/kvm/pmu.c +76 -18
@@ -168,6 +168,43 @@ void kvm_handle_guest_mediated_pmi(void)kvm_make_request (KVM_REQ_PMI , vcpu); } +static __always_inline u32 fixed_counter_msr(u32 idx) +{ +return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static __always_inline u32 gp_counter_msr(u32 idx) +{ +return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static __always_inline u32 gp_eventsel_msr(u32 idx) +{ +return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static void kvm_pmu_get_msr_state (struct kvm_vcpu *vcpu,u32 index,u64 *data) +{ +struct msr_data msr_info; + + msr_info.index = index; + msr_info.host_initiated = true; + +KVM_BUG_ON (kvm_pmu_call (get_msr)(vcpu, &msr_info), vcpu->kvm); + *data = msr_info.data; +} + +static void kvm_pmu_set_msr_state (struct kvm_vcpu *vcpu,u32 index,u64 data) +{ +struct msr_data msr_info; + + msr_info.data = data; + msr_info.index = index; + msr_info.host_initiated = true; + +KVM_BUG_ON (kvm_pmu_call (set_msr)(vcpu, &msr_info), vcpu->kvm); +} +static inline void __kvm_perf_overflow (struct kvm_pmc *pmc,bool in_pmi) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);
@@ -520,19 +557,22 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) { -bool allowed =pmc_is_event_allowed (pmc);struct kvm_pmu *pmu =pmc_to_pmu (pmc); +struct kvm_vcpu *vcpu = pmc->vcpu;if (pmc_is_gp (pmc)) { pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE ; -if (allowed) +if (pmc_is_event_allowed (pmc)) pmc->eventsel_hw |= pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ; + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu,gp_eventsel_msr (pmc->idx), pmc->eventsel_hw); }else {u64 mask =intel_fixed_bits_by_idx (pmc->idx -KVM_FIXED_PMC_BASE_IDX ,0xf ); pmu->fixed_ctr_ctrl_hw &= ~mask; -if (allowed) +if (pmc_is_event_allowed (pmc)) pmu->fixed_ctr_ctrl_hw |= pmu->fixed_ctr_ctrl & mask; } }
@@ -740,6 +780,9 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))return 1 ; +if (kvm_vcpu_has_virtualized_pmu (pmc->vcpu)) +kvm_pmu_get_msr_state (pmc->vcpu,gp_counter_msr (pmc->idx), &pmc->counter); + *data =pmc_read_counter (pmc) & mask;return 0 ; }
@@ -974,6 +1017,9 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) (kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu))) pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0); +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL, pmu->global_ctrl); +if (kvm_vcpu_has_mediated_pmu (vcpu))kvm_pmu_call (write_global_ctrl)(pmu->global_ctrl);
@@ -1099,6 +1145,11 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,if (bitmap_empty (event_pmcs,X86_PMC_IDX_MAX ))return ; +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +kvm_pmu_get_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL, &pmu->global_ctrl); +kvm_pmu_get_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS, &pmu->global_status); + } +if (!kvm_pmu_has_perf_global_ctrl (pmu))bitmap_copy (bitmap, event_pmcs,X86_PMC_IDX_MAX ); else if (!bitmap_and(bitmap, event_pmcs,
@@ -1107,11 +1158,21 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, idx =srcu_read_lock (&vcpu->kvm->srcu);kvm_for_each_pmc (pmu, pmc, i, bitmap) { +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_get_msr_state (vcpu,gp_counter_msr (pmc->idx), &pmc->counter); +if (!pmc_is_event_allowed (pmc) || !cpl_is_matched (pmc))continue ;kvm_pmu_incr_counter (pmc); + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu,gp_counter_msr (pmc->idx), pmc->counter); } + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS, pmu->global_status); +srcu_read_unlock (&vcpu->kvm->srcu, idx); }
@@ -1270,21 +1331,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)return r; } -static __always_inline u32 fixed_counter_msr(u32 idx) -{ -return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} - -static __always_inline u32 gp_counter_msr(u32 idx) -{ -return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} - -static __always_inline u32 gp_eventsel_msr(u32 idx) -{ -return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} -static void kvm_pmu_load_guest_pmcs (struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);
@@ -1319,6 +1365,12 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)lockdep_assert_irqs_disabled (); +/* Guest PMU state is restored by hardware at VM-Entry */ +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +perf_load_guest_context (0 ); +return ; + } +perf_load_guest_context (kvm_lapic_get_reg (vcpu->arch.apic,APIC_LVTPC ));/*
@@ -1372,6 +1424,12 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)lockdep_assert_irqs_disabled (); +/* Guest PMU state is saved by hardware at VM-Exit */ +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +perf_put_guest_context (); +return ; + } +/* * Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's * atomically cleared on VM-Exit, i.e. doesn't need to be clear here.
arch/x86/kvm/pmu.h +6 -0
@@ -47,6 +47,7 @@ struct kvm_pmu_ops {const int MIN_NR_GP_COUNTERS ;const u32 PERF_GLOBAL_CTRL ; +const u32 PERF_GLOBAL_STATUS ;const u32 GP_EVENTSEL_BASE ;const u32 GP_COUNTER_BASE ;const u32 FIXED_COUNTER_BASE ;
@@ -76,6 +77,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)return enable_mediated_pmu &&vcpu_to_pmu (vcpu)->version; } +static inline bool kvm_vcpu_has_virtualized_pmu (struct kvm_vcpu *vcpu) +{ +return enable_virtualized_pmu &&vcpu_to_pmu (vcpu)->version; +} +/* * KVM tracks all counters in 64-bit bitmaps, with general purpose counters * mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0
arch/x86/kvm/svm/pmu.c +1 -0
@@ -278,6 +278,7 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS, .PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL, + .PERF_GLOBAL_STATUS = MSR_AMD64_PERF_CNTR_GLOBAL_STATUS, .GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0, .GP_COUNTER_BASE = MSR_F15H_PERF_CTR0, .FIXED_COUNTER_BASE = 0,
arch/x86/kvm/vmx/pmu_intel.c +1 -0
@@ -845,6 +845,7 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .MIN_NR_GP_COUNTERS = 1, .PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL, + .PERF_GLOBAL_STATUS = MSR_CORE_PERF_GLOBAL_STATUS, .GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0, .GP_COUNTER_BASE = MSR_IA32_PMC0, .FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,
arch/x86/kvm/x86.c +4 -0
@@ -191,6 +191,10 @@ module_param(enable_pmu, bool, 0444);bool __read_mostly enable_mediated_pmu;EXPORT_SYMBOL_GPL (enable_mediated_pmu); +/* Enable/disable hardware PMU virtualization. */ +bool __read_mostly enable_virtualized_pmu; +EXPORT_SYMBOL_GPL (enable_virtualized_pmu); +bool __read_mostly eager_page_split = true;module_param (eager_page_split, bool,0644 );
arch/x86/kvm/x86.h +1 -0
@@ -446,6 +446,7 @@ extern struct kvm_host_values kvm_host;extern bool enable_pmu;extern bool enable_mediated_pmu; +extern bool enable_virtualized_pmu;/* * Get a filtered version of KVM's supported XCR0 that strips out dynamic
Signature · 1 line
2.43.0
RFC PATCH 5/7 KVM: SVM: Add VMCB fields for PMC virtualization
In reply to Sandipan DasWhen PMC virtualization (X86_FEATURE_PERFCTR_VIRT) is supported and the feature is enabled, additional save slots are available in the VMCB for the following MSRs.
* Performance Counter Global Control (MSR 0xc0000301) (Swap Type C)
* Performance Counter Global Status (MSR 0xc0000300) (Swap Type A)
* Performance Event Select (MSR 0xc0010200..0xc001020a) (Swap Type C)
* Performance Event Counter (MSR 0xc0010201..0xc001020b) (Swap Type C)
Define the additional VMCB fields that will be used by hardware to save and restore the guest PMU state.
--- arch/x86/include/asm/svm.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/include/asm/svm.h +9 -2
@@ -327,7 +327,12 @@ struct vmcb_save_area {u8 cpl;u8 reserved_0xcc[4 ];u64 efer; -u8 reserved_0xd8[112 ]; +u8 reserved_0xd8[8 ]; +struct { +u64 perf_ctl; +u64 perf_ctr; + } __packed pmc[6]; +u8 reserved_0x140[8 ];u64 cr4;u64 cr3;u64 cr0;
@@ -335,7 +340,9 @@ struct vmcb_save_area {u64 dr6;u64 rflags;u64 rip; -u8 reserved_0x180[88 ]; +u8 reserved_0x180[72 ]; +u64 perf_cntr_global_status; +u64 perf_cntr_global_control;u64 rsp;u64 s_cet;u64 ssp;
Signature · 1 line
2.43.0
RFC PATCH 6/7 KVM: SVM: Add support for PMC virtualization
In reply to Sandipan DasMake use of the extended Mediated PMU framework to introduce support for PMC virtualization (X86_FEATURE_PERFCTR_VIRT). Since the guest PMU state is saved in the VMCB State Save Area, extend the MSR access PMU ops to handle host-initiated requests from code responsible for event filtering and incrementing counters due to instruction emulation.
The underlying implementation depends on the availability of either VNMI or AVIC for guest interrupt delivery. Synthesized overflows like those resulting from incrementing counters in software due to instruction emulation are still injected through the KVM_REQ_PMI path.
If both VNMI and AVIC are enabled, the hardware automatically chooses AVIC. The advantage of using AVIC is that it lets the guest change the delivery mode in the LVTPC. Unlike AVIC, VNMI ignores the LVTPC, as APIC is emulated, and will always present the overflow interrupts as NMIs. This is demonstrated by some failures in KUT's x86/pmu test.
The feature is enabled by default if the host supports it and also has Mediated PMU enabled but it can also be switched manually using the new "vpmc" parameter for the kvm_amd module.
--- arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/pmu.c | 100 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 52 +++++++++++++++++++ arch/x86/kvm/svm/svm.h | 1 + 4 files changed, 154 insertions(+)
arch/x86/include/asm/svm.h +1 -0
@@ -221,6 +221,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define LBR_CTL_ENABLE_MASK BIT_ULL(0)#define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1) +#define PMC_VIRTUALIZATION_ENABLE_MASK BIT_ULL(3)#define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0)#define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1)
arch/x86/kvm/svm/pmu.c +100 -0
142 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +52 -0
82 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -51,6 +51,7 @@ extern bool intercept_smi;extern bool x2avic_enabled;extern bool vnmi;extern int lbrv; +extern bool vpmc;/* * Clean bits in VMCB.
Signature · 1 line
2.43.0
RFC PATCH 7/7 KVM: SVM: Adjust MSR index for legacy guests
In reply to Sandipan DasFor legacy guests, the host-initiated requests from the hypervisor to access PMC-related MSRs will always be for MSR_F15H_PERF_CTLx and MSR_F15H_PERF_CTRx instead of MSR_K7_EVNTSELx and MSR_K7_PERFCTRx because of how GP_EVENTSEL_BASE and GP_COUNTER_BASE are set in the PMU ops. In such cases, translate the index to the equivalent legacy MSR as get_gp_pmc_amd() will otherwise return NULL.
--- arch/x86/kvm/svm/pmu.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arch/x86/kvm/svm/pmu.c +14 -0
@@ -124,6 +124,16 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)return amd_msr_idx_to_pmc (vcpu, msr); } +static inline u32 amd_pmu_adjust_msr_idx (struct kvm_vcpu *vcpu,u32 msr) +{ +if (!guest_cpu_cap_has (vcpu,X86_FEATURE_PERFCTR_CORE ) && + msr >=MSR_F15H_PERF_CTL0 && msr <=MSR_F15H_PERF_CTR5 ) + msr = ((msr &0x1 ) ?MSR_K7_PERFCTR0 :MSR_K7_EVNTSEL0 ) + + ((msr -MSR_F15H_PERF_CTL0 ) /2 ); + +return msr; +} +static int amd_virtualized_pmu_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {
@@ -142,6 +152,8 @@ static int amd_virtualized_pmu_get_msr(struct kvm_vcpu *vcpu,return 0 ; } + msr =amd_pmu_adjust_msr_idx (vcpu, msr); +/* MSR_PERFCTRn */ pmc =get_gp_pmc_amd (pmu, msr,PMU_TYPE_COUNTER );if (pmc) {
@@ -203,6 +215,8 @@ static int amd_virtualized_pmu_set_msr(struct kvm_vcpu *vcpu,return 0 ; } + msr =amd_pmu_adjust_msr_idx (vcpu, msr); +/* MSR_PERFCTRn */ pmc =get_gp_pmc_amd (pmu, msr,PMU_TYPE_COUNTER );if (pmc) {
Signature · 1 line
2.43.0
Re: [RFC PATCH 4/7] KVM: x86/pmu: Add support for hardware virtualized PMUs
In reply to Sandipan DasQuoted 67 lines · Sandipan Das
Extend the Mediated PMU framework to support hardware virtualized PMUs. The key differences with Mediated PMU are listed below.
* Hardware saves and restores the guest PMU state on world switches.
* The guest PMU state is saved in vendor-specific structures (such as VMCB or VMCS) instead of structkvm_pmu.
* Hardware relies on interrupt virtualization (such as VNMI or AVIC) to notify guests about counter overflows instead of receiving interrupts in host context after switching the delivery mode in LVTPC and then injecting them back in to the guest (KVM_REQ_PMI).Parts of the original PMU load and put functionality are reused as the active host events still need to be scheduled in and out in preparation for world switches.
Event filtering and instruction emulation require the ability to change the guest PMU state in software. Since struct
kvm_pmuno longer has the correct state, make use of host-initiated MSR accesses for accessing MSR states directly from vendor-specific structures.RDPMC is intercepted for legacy guests which do not have access to all counters. Host-initiated MSR accesses are also used in such cases to read the latest counter value from vendor-specific structures.
Signed-off-by Sandipan Das <[email protected]>--- arch/x86/kvm/pmu.c | 94 +++++++++++++++++++++++++++++------- arch/x86/kvm/pmu.h | 6 +++ arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 4 ++ arch/x86/kvm/x86.h | 1 + 6 files changed, 89 insertions(+), 18 deletions(-)arch/x86/kvm/pmu.c +26
-0@@ -168,6 +168,43 @@ void kvm_handle_guest_mediated_pmi(void)kvm_make_request (KVM_REQ_PMI , vcpu); } +static __always_inline u32 fixed_counter_msr(u32 idx) +{ +return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static __always_inline u32 gp_counter_msr(u32 idx) +{ +return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static __always_inline u32 gp_eventsel_msr(u32 idx) +{ +return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static void kvm_pmu_get_msr_state (struct kvm_vcpu *vcpu,u32 index,u64 *data) +{ +struct msr_data msr_info; + + msr_info.index = index; + msr_info.host_initiated = true; + +KVM_BUG_ON (kvm_pmu_call (get_msr)(vcpu, &msr_info), vcpu->kvm); + *data = msr_info.data; +} +
arch/x86/kvm/pmu.c void kvm_handle_guest_mediated_pmi(void) · line 206
Quoted 10 lines
arch/x86/kvm/pmu.c +10
-0+static void kvm_pmu_set_msr_state (struct kvm_vcpu *vcpu,u32 index,u64 data) +{ +struct msr_data msr_info; + + msr_info.data = data; + msr_info.index = index; + msr_info.host_initiated = true; + +KVM_BUG_ON (kvm_pmu_call (set_msr)(vcpu, &msr_info), vcpu->kvm); +}
With these 2 new helpers, suppose the helpers write_global_ctrl()/read_global_ctrl() could be retired.
Quoted 207 lines
arch/x86/kvm/pmu.c +40
-18+static inline void __kvm_perf_overflow (struct kvm_pmc *pmc,bool in_pmi) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);@@ -520,19 +557,22 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) { -bool allowed =pmc_is_event_allowed (pmc);struct kvm_pmu *pmu =pmc_to_pmu (pmc); +struct kvm_vcpu *vcpu = pmc->vcpu;if (pmc_is_gp (pmc)) { pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE ; -if (allowed) +if (pmc_is_event_allowed (pmc)) pmc->eventsel_hw |= pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ; + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu,gp_eventsel_msr (pmc->idx), pmc->eventsel_hw); }else {u64 mask =intel_fixed_bits_by_idx (pmc->idx -KVM_FIXED_PMC_BASE_IDX ,0xf ); pmu->fixed_ctr_ctrl_hw &= ~mask; -if (allowed) +if (pmc_is_event_allowed (pmc)) pmu->fixed_ctr_ctrl_hw |= pmu->fixed_ctr_ctrl & mask; } }@@ -740,6 +780,9 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))return 1 ; +if (kvm_vcpu_has_virtualized_pmu (pmc->vcpu)) +kvm_pmu_get_msr_state (pmc->vcpu,gp_counter_msr (pmc->idx), &pmc->counter); + *data =pmc_read_counter (pmc) & mask;return 0 ; }@@ -974,6 +1017,9 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) (kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu))) pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0); +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL, pmu->global_ctrl); +if (kvm_vcpu_has_mediated_pmu (vcpu))kvm_pmu_call (write_global_ctrl)(pmu->global_ctrl);@@ -1099,6 +1145,11 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,if (bitmap_empty (event_pmcs,X86_PMC_IDX_MAX ))return ; +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +kvm_pmu_get_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL, &pmu->global_ctrl); +kvm_pmu_get_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS, &pmu->global_status); + } +if (!kvm_pmu_has_perf_global_ctrl (pmu))bitmap_copy (bitmap, event_pmcs,X86_PMC_IDX_MAX ); else if (!bitmap_and(bitmap, event_pmcs,@@ -1107,11 +1158,21 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, idx =srcu_read_lock (&vcpu->kvm->srcu);kvm_for_each_pmc (pmu, pmc, i, bitmap) { +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_get_msr_state (vcpu,gp_counter_msr (pmc->idx), &pmc->counter); +if (!pmc_is_event_allowed (pmc) || !cpl_is_matched (pmc))continue ;kvm_pmu_incr_counter (pmc); + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu,gp_counter_msr (pmc->idx), pmc->counter); } + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS, pmu->global_status); +srcu_read_unlock (&vcpu->kvm->srcu, idx); }@@ -1270,21 +1331,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)return r; } -static __always_inline u32 fixed_counter_msr(u32 idx) -{ -return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} - -static __always_inline u32 gp_counter_msr(u32 idx) -{ -return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} - -static __always_inline u32 gp_eventsel_msr(u32 idx) -{ -return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} -static void kvm_pmu_load_guest_pmcs (struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);@@ -1319,6 +1365,12 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)lockdep_assert_irqs_disabled (); +/* Guest PMU state is restored by hardware at VM-Entry */ +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +perf_load_guest_context (0 ); +return ; + } +perf_load_guest_context (kvm_lapic_get_reg (vcpu->arch.apic,APIC_LVTPC ));/* @@ -1372,6 +1424,12 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)lockdep_assert_irqs_disabled (); +/* Guest PMU state is saved by hardware at VM-Exit */ +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +perf_put_guest_context (); +return ; + } +/* * Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's * atomically cleared on VM-Exit, i.e. doesn't need to be clear here. arch/x86/kvm/pmu.h +6
-0@@ -47,6 +47,7 @@ struct kvm_pmu_ops {const int MIN_NR_GP_COUNTERS ;const u32 PERF_GLOBAL_CTRL ; +const u32 PERF_GLOBAL_STATUS ;const u32 GP_EVENTSEL_BASE ;const u32 GP_COUNTER_BASE ;const u32 FIXED_COUNTER_BASE ;@@ -76,6 +77,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)return enable_mediated_pmu &&vcpu_to_pmu (vcpu)->version; } +static inline bool kvm_vcpu_has_virtualized_pmu (struct kvm_vcpu *vcpu) +{ +return enable_virtualized_pmu &&vcpu_to_pmu (vcpu)->version; +} +/* * KVM tracks all counters in 64-bit bitmaps, with general purpose counters * mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0 arch/x86/kvm/svm/pmu.c +1
-0@@ -278,6 +278,7 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS, .PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL, + .PERF_GLOBAL_STATUS = MSR_AMD64_PERF_CNTR_GLOBAL_STATUS, .GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0, .GP_COUNTER_BASE = MSR_F15H_PERF_CTR0, .FIXED_COUNTER_BASE = 0,arch/x86/kvm/vmx/pmu_intel.c +1
-0@@ -845,6 +845,7 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .MIN_NR_GP_COUNTERS = 1, .PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL, + .PERF_GLOBAL_STATUS = MSR_CORE_PERF_GLOBAL_STATUS, .GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0, .GP_COUNTER_BASE = MSR_IA32_PMC0, .FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,arch/x86/kvm/x86.c +4
-0@@ -191,6 +191,10 @@ module_param(enable_pmu, bool, 0444);bool __read_mostly enable_mediated_pmu;EXPORT_SYMBOL_GPL (enable_mediated_pmu); +/* Enable/disable hardware PMU virtualization. */ +bool __read_mostly enable_virtualized_pmu; +EXPORT_SYMBOL_GPL (enable_virtualized_pmu); +bool __read_mostly eager_page_split = true;module_param (eager_page_split, bool,0644 );arch/x86/kvm/x86.h +1
-0@@ -446,6 +446,7 @@ extern struct kvm_host_values kvm_host;extern bool enable_pmu;extern bool enable_mediated_pmu; +extern bool enable_virtualized_pmu;/* * Get a filtered version of KVM's supported XCR0 that strips out dynamic
Re: [RFC PATCH 4/7] KVM: x86/pmu: Add support for hardware virtualized PMUs
Quoted 74 lines · Dapeng
Quoted 72 lines · Sandipan Das
Extend the Mediated PMU framework to support hardware virtualized PMUs. The key differences with Mediated PMU are listed below.
* Hardware saves and restores the guest PMU state on world switches.
* The guest PMU state is saved in vendor-specific structures (such as VMCB or VMCS) instead of structkvm_pmu.
* Hardware relies on interrupt virtualization (such as VNMI or AVIC) to notify guests about counter overflows instead of receiving interrupts in host context after switching the delivery mode in LVTPC and then injecting them back in to the guest (KVM_REQ_PMI).Parts of the original PMU load and put functionality are reused as the active host events still need to be scheduled in and out in preparation for world switches.
Event filtering and instruction emulation require the ability to change the guest PMU state in software. Since struct
kvm_pmuno longer has the correct state, make use of host-initiated MSR accesses for accessing MSR states directly from vendor-specific structures.RDPMC is intercepted for legacy guests which do not have access to all counters. Host-initiated MSR accesses are also used in such cases to read the latest counter value from vendor-specific structures.
Signed-off-by Sandipan Das <[email protected]>--- arch/x86/kvm/pmu.c | 94 +++++++++++++++++++++++++++++------- arch/x86/kvm/pmu.h | 6 +++ arch/x86/kvm/svm/pmu.c | 1 + arch/x86/kvm/vmx/pmu_intel.c | 1 + arch/x86/kvm/x86.c | 4 ++ arch/x86/kvm/x86.h | 1 + 6 files changed, 89 insertions(+), 18 deletions(-)arch/x86/kvm/pmu.c +31
-0@@ -168,6 +168,43 @@ void kvm_handle_guest_mediated_pmi(void)kvm_make_request (KVM_REQ_PMI , vcpu); } +static __always_inline u32 fixed_counter_msr(u32 idx) +{ +return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static __always_inline u32 gp_counter_msr(u32 idx) +{ +return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static __always_inline u32 gp_eventsel_msr(u32 idx) +{ +return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE; +} + +static void kvm_pmu_get_msr_state (struct kvm_vcpu *vcpu,u32 index,u64 *data) +{ +struct msr_data msr_info; + + msr_info.index = index; + msr_info.host_initiated = true; + +KVM_BUG_ON (kvm_pmu_call (get_msr)(vcpu, &msr_info), vcpu->kvm); + *data = msr_info.data; +} + +static void kvm_pmu_set_msr_state (struct kvm_vcpu *vcpu,u32 index,u64 data) +{ +struct msr_data msr_info; + + msr_info.data = data;
Quoted 9 lines
arch/x86/kvm/pmu.c void kvm_handle_guest_mediated_pmi(void) · line 206
Quoted 5 lines
arch/x86/kvm/pmu.c +5
-0+ msr_info.index = index; + msr_info.host_initiated = true; + +KVM_BUG_ON (kvm_pmu_call (set_msr)(vcpu, &msr_info), vcpu->kvm); +}With these 2 new helpers, suppose the helpers write_global_ctrl()/read_global_ctrl() could be retired.
Agreed. It will be good to have a new set of PMU ops other than {get,set}_msr() that can be used to read/write any of the PMU-related MSRs.
Quoted 208 lines
Quoted 208 lines
arch/x86/kvm/pmu.c +40
-18+static inline void __kvm_perf_overflow (struct kvm_pmc *pmc,bool in_pmi) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);@@ -520,19 +557,22 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) { -bool allowed =pmc_is_event_allowed (pmc);struct kvm_pmu *pmu =pmc_to_pmu (pmc); +struct kvm_vcpu *vcpu = pmc->vcpu;if (pmc_is_gp (pmc)) { pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE ; -if (allowed) +if (pmc_is_event_allowed (pmc)) pmc->eventsel_hw |= pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ; + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu,gp_eventsel_msr (pmc->idx), pmc->eventsel_hw); }else {u64 mask =intel_fixed_bits_by_idx (pmc->idx -KVM_FIXED_PMC_BASE_IDX ,0xf ); pmu->fixed_ctr_ctrl_hw &= ~mask; -if (allowed) +if (pmc_is_event_allowed (pmc)) pmu->fixed_ctr_ctrl_hw |= pmu->fixed_ctr_ctrl & mask; } }@@ -740,6 +780,9 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data) kvm_is_cr0_bit_set(vcpu, X86_CR0_PE))return 1 ; +if (kvm_vcpu_has_virtualized_pmu (pmc->vcpu)) +kvm_pmu_get_msr_state (pmc->vcpu,gp_counter_msr (pmc->idx), &pmc->counter); + *data =pmc_read_counter (pmc) & mask;return 0 ; }@@ -974,6 +1017,9 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) (kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu))) pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0); +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL, pmu->global_ctrl); +if (kvm_vcpu_has_mediated_pmu (vcpu))kvm_pmu_call (write_global_ctrl)(pmu->global_ctrl);@@ -1099,6 +1145,11 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu,if (bitmap_empty (event_pmcs,X86_PMC_IDX_MAX ))return ; +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +kvm_pmu_get_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_CTRL, &pmu->global_ctrl); +kvm_pmu_get_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS, &pmu->global_status); + } +if (!kvm_pmu_has_perf_global_ctrl (pmu))bitmap_copy (bitmap, event_pmcs,X86_PMC_IDX_MAX ); else if (!bitmap_and(bitmap, event_pmcs,@@ -1107,11 +1158,21 @@ static void kvm_pmu_trigger_event(struct kvm_vcpu *vcpu, idx =srcu_read_lock (&vcpu->kvm->srcu);kvm_for_each_pmc (pmu, pmc, i, bitmap) { +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_get_msr_state (vcpu,gp_counter_msr (pmc->idx), &pmc->counter); +if (!pmc_is_event_allowed (pmc) || !cpl_is_matched (pmc))continue ;kvm_pmu_incr_counter (pmc); + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu,gp_counter_msr (pmc->idx), pmc->counter); } + +if (kvm_vcpu_has_virtualized_pmu (vcpu)) +kvm_pmu_set_msr_state (vcpu, kvm_pmu_ops.PERF_GLOBAL_STATUS, pmu->global_status); +srcu_read_unlock (&vcpu->kvm->srcu, idx); }@@ -1270,21 +1331,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)return r; } -static __always_inline u32 fixed_counter_msr(u32 idx) -{ -return kvm_pmu_ops.FIXED_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} - -static __always_inline u32 gp_counter_msr(u32 idx) -{ -return kvm_pmu_ops.GP_COUNTER_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} - -static __always_inline u32 gp_eventsel_msr(u32 idx) -{ -return kvm_pmu_ops.GP_EVENTSEL_BASE + idx * kvm_pmu_ops.MSR_STRIDE; -} -static void kvm_pmu_load_guest_pmcs (struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);@@ -1319,6 +1365,12 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)lockdep_assert_irqs_disabled (); +/* Guest PMU state is restored by hardware at VM-Entry */ +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +perf_load_guest_context (0 ); +return ; + } +perf_load_guest_context (kvm_lapic_get_reg (vcpu->arch.apic,APIC_LVTPC ));/* @@ -1372,6 +1424,12 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu)lockdep_assert_irqs_disabled (); +/* Guest PMU state is saved by hardware at VM-Exit */ +if (kvm_vcpu_has_virtualized_pmu (vcpu)) { +perf_put_guest_context (); +return ; + } +/* * Defer handling of PERF_GLOBAL_CTRL to vendor code. On Intel, it's * atomically cleared on VM-Exit, i.e. doesn't need to be clear here. arch/x86/kvm/pmu.h +6
-0@@ -47,6 +47,7 @@ struct kvm_pmu_ops {const int MIN_NR_GP_COUNTERS ;const u32 PERF_GLOBAL_CTRL ; +const u32 PERF_GLOBAL_STATUS ;const u32 GP_EVENTSEL_BASE ;const u32 GP_COUNTER_BASE ;const u32 FIXED_COUNTER_BASE ;@@ -76,6 +77,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)return enable_mediated_pmu &&vcpu_to_pmu (vcpu)->version; } +static inline bool kvm_vcpu_has_virtualized_pmu (struct kvm_vcpu *vcpu) +{ +return enable_virtualized_pmu &&vcpu_to_pmu (vcpu)->version; +} +/* * KVM tracks all counters in 64-bit bitmaps, with general purpose counters * mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0 arch/x86/kvm/svm/pmu.c +1
-0@@ -278,6 +278,7 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .MIN_NR_GP_COUNTERS = AMD64_NUM_COUNTERS, .PERF_GLOBAL_CTRL = MSR_AMD64_PERF_CNTR_GLOBAL_CTL, + .PERF_GLOBAL_STATUS = MSR_AMD64_PERF_CNTR_GLOBAL_STATUS, .GP_EVENTSEL_BASE = MSR_F15H_PERF_CTL0, .GP_COUNTER_BASE = MSR_F15H_PERF_CTR0, .FIXED_COUNTER_BASE = 0,arch/x86/kvm/vmx/pmu_intel.c +1
-0@@ -845,6 +845,7 @@ struct kvm_pmu_ops intel_pmu_ops __initdata = { .MIN_NR_GP_COUNTERS = 1, .PERF_GLOBAL_CTRL = MSR_CORE_PERF_GLOBAL_CTRL, + .PERF_GLOBAL_STATUS = MSR_CORE_PERF_GLOBAL_STATUS, .GP_EVENTSEL_BASE = MSR_P6_EVNTSEL0, .GP_COUNTER_BASE = MSR_IA32_PMC0, .FIXED_COUNTER_BASE = MSR_CORE_PERF_FIXED_CTR0,arch/x86/kvm/x86.c +4
-0@@ -191,6 +191,10 @@ module_param(enable_pmu, bool, 0444);bool __read_mostly enable_mediated_pmu;EXPORT_SYMBOL_GPL (enable_mediated_pmu); +/* Enable/disable hardware PMU virtualization. */ +bool __read_mostly enable_virtualized_pmu; +EXPORT_SYMBOL_GPL (enable_virtualized_pmu); +bool __read_mostly eager_page_split = true;module_param (eager_page_split, bool,0644 );arch/x86/kvm/x86.h +1
-0@@ -446,6 +446,7 @@ extern struct kvm_host_values kvm_host;extern bool enable_pmu;extern bool enable_mediated_pmu; +extern bool enable_virtualized_pmu;/* * Get a filtered version of KVM's supported XCR0 that strips out dynamic