KVM in-guest performance monitoring
Subsystems (4)
v3 cover KVM in-guest performance monitoring
This patchset exposes an emulated version 2 architectural performance monitoring unit to KVM guests. The PMU is emulated using perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.
The patches are against next branch on kvm.git.
If you want to try running perf in a guest you need to apply the patch below to qemu-kvm and use -cpu host on qemu command line. But DO NOT TRY those patches without applying [1][2] to the host kernel first. Don't tell me I didn't warn you!
[1] https://lkml.org/lkml/2011/10/18/390
[2] https://lkml.org/lkml/2011/10/23/163
Changelog:
v1->v2
- put index into struct kvm_pmc instead of calculating it
- use locked version of bitops
- inject pmi from irq work if vcpu was not in a guest mode during NMI
- providing stub for perf_get_x86_pmu_capability() for !PERF_EVENTS
v2->v3
- minor style change/comment clarification
- add perf patch to disable arch event not supported by a CPU
- create perf events as pinned
Avi Kivity (6):
KVM: Expose kvm_lapic_local_deliver()
KVM: Add generic RDPMC support
KVM: SVM: Intercept RDPMC
KVM: VMX: Intercept RDPMC
KVM: x86 emulator: fix RDPMC privilege check
KVM: x86 emulator: implement RDPMC (0F 33)
Gleb Natapov (4):
KVM: Expose a version 2 architectural PMU to a guests x86, perf: disable non available architectural events. perf, x86: expose perf capability to other modules.
KVM: Expose the architectural performance monitoring CPUID leaf
arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 49 +++ arch/x86/include/asm/perf_event.h | 29 ++ arch/x86/kernel/cpu/perf_event.c | 11 + arch/x86/kernel/cpu/perf_event.h | 5 + arch/x86/kernel/cpu/perf_event_intel.c | 29 ++- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/emulate.c | 13 +- arch/x86/kvm/lapic.c | 2 +- arch/x86/kvm/lapic.h | 1 + arch/x86/kvm/pmu.c | 531 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 15 + arch/x86/kvm/vmx.c | 15 +- arch/x86/kvm/x86.c | 76 ++++- include/linux/kvm_host.h | 2 + 16 files changed, 763 insertions(+), 19 deletions(-) create mode 100644 arch/x86/kvm/pmu.c
target-i386/cpuid.c +14 -5
@@ -1178,11 +1178,20 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx =0 ;break ;case 0xA : -/* Architectural Performance Monitoring Leaf */ - *eax =0 ; - *ebx =0 ; - *ecx =0 ; - *edx =0 ; +if (kvm_enabled ()) { +KVMState *s = env->kvm_state; + + *eax =kvm_arch_get_supported_cpuid (s,0xA , count,R_EAX ); + *ebx =kvm_arch_get_supported_cpuid (s,0xA , count,R_EBX ); + *ecx =kvm_arch_get_supported_cpuid (s,0xA , count,R_ECX ); + *edx =kvm_arch_get_supported_cpuid (s,0xA , count,R_EDX ); + }else { +/* Architectural Performance Monitoring Leaf */ + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; + }break ;case 0xD :/* Processor Extended State */
Signature · 1 line
1.7.7.1
PATCH v3 1/10 KVM: Expose kvm_lapic_local_deliver()
From: Avi Kivity <[email protected]>
Needed to deliver performance monitoring interrupts.
--- arch/x86/kvm/lapic.c | 2 +- arch/x86/kvm/lapic.h | 1 + 2 files changed, 2 insertions(+), 1 deletions(-)
arch/x86/kvm/lapic.c +1 -1
@@ -1120,7 +1120,7 @@ int apic_has_pending_timer(struct kvm_vcpu *vcpu)return 0 ; } -static int kvm_apic_local_deliver (struct kvm_lapic *apic,int lvt_type) +int kvm_apic_local_deliver (struct kvm_lapic *apic,int lvt_type) {u32 reg =apic_get_reg (apic, lvt_type);int vector, mode, trig_mode;
arch/x86/kvm/lapic.h +1 -0
@@ -34,6 +34,7 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);int kvm_apic_match_physical_addr (struct kvm_lapic *apic,u16 dest);int kvm_apic_match_logical_addr (struct kvm_lapic *apic,u8 mda);int kvm_apic_set_irq (struct kvm_vcpu *vcpu,struct kvm_lapic_irq *irq); +int kvm_apic_local_deliver (struct kvm_lapic *apic,int lvt_type);u64 kvm_get_apic_base (struct kvm_vcpu *vcpu);void kvm_set_apic_base (struct kvm_vcpu *vcpu,u64 data);
Signature · 1 line
1.7.7.1
PATCH v3 2/10 KVM: Expose a version 2 architectural PMU to a guests
In reply to Gleb NatapovUse perf_events to emulate an architectural PMU, version 2.
Based on PMU version 1 emulation by Avi Kivity.
--- arch/x86/include/asm/kvm_host.h | 48 ++++ arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/pmu.c | 531 +++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 24 ++- include/linux/kvm_host.h | 2 + 6 files changed, 598 insertions(+), 10 deletions(-) create mode 100644 arch/x86/kvm/pmu.c
arch/x86/include/asm/kvm_host.h +48 -0
@@ -16,10 +16,12 @@#include <linux/mmu_notifier.h> #include <linux/tracepoint.h> #include <linux/cpumask.h> +#include <linux/irq_work.h> #include <linux/kvm.h> #include <linux/kvm_para.h> #include <linux/kvm_types.h> +#include <linux/perf_event.h> #include <asm/pvclock-abi.h> #include <asm/desc.h>
@@ -289,6 +291,37 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; +enum pmc_type { +KVM_PMC_GP =0 , +KVM_PMC_FIXED , +}; + +struct kvm_pmc { +enum pmc_type type; +u8 idx; +u64 counter; +u64 eventsel; +struct perf_event *perf_event; +struct kvm_vcpu *vcpu; +}; + +struct kvm_pmu { +unsigned nr_arch_gp_counters; +unsigned nr_arch_fixed_counters; +unsigned available_event_types; +u64 fixed_ctr_ctrl; +u64 global_ctrl; +u64 global_status; +u64 global_ovf_ctrl; +u64 counter_bitmask[2 ]; +u64 global_ctrl_mask; +u8 version; +struct kvm_pmc gp_counters[X86_PMC_MAX_GENERIC ]; +struct kvm_pmc fixed_counters[X86_PMC_MAX_FIXED ]; +struct irq_work irq_work; +u64 reprogram_pmi; +}; +struct kvm_vcpu_arch {/* * rip and regs accesses must go through
@@ -422,6 +455,8 @@ struct kvm_vcpu_arch {unsigned access;gfn_t mmio_gfn; +struct kvm_pmu pmu; +/* used for guest single stepping over the given code position */ unsigned long singlestep_rip;
@@ -881,4 +916,17 @@ extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);void kvm_complete_insn_gp (struct kvm_vcpu *vcpu,int err); +int kvm_is_in_guest (void ); + +void kvm_pmu_init (struct kvm_vcpu *vcpu); +void kvm_pmu_destroy (struct kvm_vcpu *vcpu); +void kvm_pmu_reset (struct kvm_vcpu *vcpu); +void kvm_pmu_cpuid_update (struct kvm_vcpu *vcpu); +bool kvm_pmu_msr (struct kvm_vcpu *vcpu,u32 msr); +int kvm_pmu_get_msr (struct kvm_vcpu *vcpu,u32 msr,u64 *data); +int kvm_pmu_set_msr (struct kvm_vcpu *vcpu,u32 msr,u64 data); +int kvm_pmu_read_pmc (struct kvm_vcpu *vcpu,unsigned pmc,u64 *data); +void kvm_handle_pmu_event (struct kvm_vcpu *vcpu); +void kvm_deliver_pmi (struct kvm_vcpu *vcpu); +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/Kconfig +1 -0
@@ -35,6 +35,7 @@ config KVM select KVM_MMIO select TASKSTATS select TASK_DELAY_ACCT + select PERF_EVENTS ---help--- Support hosting fully virtualized guest machines using hardware virtualization extensions. You will need a fairly recent
arch/x86/kvm/Makefile +1 -1
@@ -12,7 +12,7 @@ kvm-$(CONFIG_IOMMU_API) += $(addprefix ../../../virt/kvm/, iommu.o) kvm-$(CONFIG_KVM_ASYNC_PF) += $(addprefix ../../../virt/kvm/, async_pf.o) kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ - i8254.o timer.o + i8254.o timer.o pmu.o kvm-intel-y += vmx.o kvm-amd-y += svm.o
arch/x86/kvm/pmu.c new file +531 -0
531 lines not on this page · raw message
arch/x86/kvm/x86.c +15 -9
@@ -624,6 +624,8 @@ static void update_cpuid(struct kvm_vcpu *vcpu)if (apic) apic->lapic_timer.timer_mode_mask = timer_mode_mask; + +kvm_pmu_cpuid_update (vcpu); }int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4)
@@ -1656,8 +1658,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)* which we perfectly emulate ;-). Any other value should be at least * reported, some guests depend on them. */ -case MSR_P6_EVNTSEL0 : -case MSR_P6_EVNTSEL1 :case MSR_K7_EVNTSEL0 :case MSR_K7_EVNTSEL1 :case MSR_K7_EVNTSEL2 :
@@ -1669,8 +1669,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)/* at least RHEL 4 unconditionally writes to the perfctr registers, * so we ignore writes to make it happy. */ -case MSR_P6_PERFCTR0 : -case MSR_P6_PERFCTR1 :case MSR_K7_PERFCTR0 :case MSR_K7_PERFCTR1 :case MSR_K7_PERFCTR2 :
@@ -1707,6 +1705,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)default :if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))return xen_hvm_config (vcpu, data); +if (kvm_pmu_msr (vcpu, msr)) +return kvm_pmu_set_msr (vcpu, msr, data);if (!ignore_msrs) {pr_unimpl (vcpu,"unhandled wrmsr: 0x%x data %llx\n" , msr, data);
@@ -1869,10 +1869,6 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)case MSR_K8_SYSCFG :case MSR_K7_HWCR :case MSR_VM_HSAVE_PA : -case MSR_P6_PERFCTR0 : -case MSR_P6_PERFCTR1 : -case MSR_P6_EVNTSEL0 : -case MSR_P6_EVNTSEL1 :case MSR_K7_EVNTSEL0 :case MSR_K7_PERFCTR0 :case MSR_K8_INT_PENDING_MSG :
@@ -1983,6 +1979,8 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) data =0xbe702111 ;break ;default : +if (kvm_pmu_msr (vcpu, msr)) +return kvm_pmu_get_msr (vcpu, msr, pdata);if (!ignore_msrs) {pr_unimpl (vcpu,"unhandled rdmsr: 0x%x\n" , msr);return 1 ;
@@ -5136,7 +5134,7 @@ static void kvm_timer_init(void) static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); -static int kvm_is_in_guest (void ) +int kvm_is_in_guest (void ) {return percpu_read (current_vcpu) !=NULL ; }
@@ -5719,6 +5717,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)process_nmi (vcpu); req_immediate_exit =kvm_check_request (KVM_REQ_IMMEDIATE_EXIT , vcpu); +if (kvm_check_request (KVM_REQ_PMU , vcpu)) +kvm_handle_pmu_event (vcpu); +if (kvm_check_request (KVM_REQ_PMI , vcpu)) +kvm_deliver_pmi (vcpu); } r =kvm_mmu_reload (vcpu);
@@ -6459,6 +6461,8 @@ int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)kvm_async_pf_hash_reset (vcpu); vcpu->arch.apf.halted = false; +kvm_pmu_reset (vcpu); +return kvm_x86_ops->vcpu_reset (vcpu); }
@@ -6547,6 +6551,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) goto fail_free_mce_banks;kvm_async_pf_hash_reset (vcpu); +kvm_pmu_init (vcpu);return 0 ; fail_free_mce_banks:
@@ -6565,6 +6570,7 @@ void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {int idx; +kvm_pmu_destroy (vcpu);kfree (vcpu->arch.mce_banks);kvm_free_lapic (vcpu); idx =srcu_read_lock (&vcpu->kvm->srcu);
include/linux/kvm_host.h +2 -0
@@ -52,6 +52,8 @@#define KVM_REQ_STEAL_UPDATE 13#define KVM_REQ_NMI 14#define KVM_REQ_IMMEDIATE_EXIT 15 +#define KVM_REQ_PMU 16 +#define KVM_REQ_PMI 17#define KVM_USERSPACE_IRQ_SOURCE_ID 0
Signature · 1 line
1.7.7.1
PATCH v3 3/10 KVM: Add generic RDPMC support
In reply to Gleb NatapovFrom: Avi Kivity <[email protected]>
Add a helper function that emulates the RDPMC instruction operation.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -756,6 +756,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);unsigned long kvm_get_rflags (struct kvm_vcpu *vcpu);void kvm_set_rflags (struct kvm_vcpu *vcpu,unsigned long rflags); +bool kvm_rdpmc (struct kvm_vcpu *vcpu);void kvm_queue_exception (struct kvm_vcpu *vcpu,unsigned nr);void kvm_queue_exception_e (struct kvm_vcpu *vcpu,unsigned nr,u32 error_code);
arch/x86/kvm/x86.c +15 -0
@@ -815,6 +815,21 @@ int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) }EXPORT_SYMBOL_GPL (kvm_get_dr); +bool kvm_rdpmc (struct kvm_vcpu *vcpu) +{ +u32 ecx =kvm_register_read (vcpu,VCPU_REGS_RCX ); +u64 data; +int err; + + err =kvm_pmu_read_pmc (vcpu, ecx, &data); +if (err) +return err; +kvm_register_write (vcpu,VCPU_REGS_RAX , (u32 )data); +kvm_register_write (vcpu,VCPU_REGS_RDX , data >>32 ); +return err; +} +EXPORT_SYMBOL_GPL (kvm_rdpmc); +/* * List of msr numbers which we expose to userspace through KVM_GET_MSRS * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
Signature · 1 line
1.7.7.1
PATCH v3 4/10 KVM: SVM: Intercept RDPMC
In reply to Gleb NatapovFrom: Avi Kivity <[email protected]>
Intercept RDPMC and forward it to the PMU emulation code.
--- arch/x86/kvm/svm.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
arch/x86/kvm/svm.c +15 -0
@@ -1014,6 +1014,7 @@ static void init_vmcb(struct vcpu_svm *svm)set_intercept (svm,INTERCEPT_NMI );set_intercept (svm,INTERCEPT_SMI );set_intercept (svm,INTERCEPT_SELECTIVE_CR0 ); +set_intercept (svm,INTERCEPT_RDPMC );set_intercept (svm,INTERCEPT_CPUID );set_intercept (svm,INTERCEPT_INVD );set_intercept (svm,INTERCEPT_HLT );
@@ -2770,6 +2771,19 @@ static int emulate_on_interception(struct vcpu_svm *svm)return emulate_instruction (&svm->vcpu,0 ) ==EMULATE_DONE ; } +static int rdpmc_interception (struct vcpu_svm *svm) +{ +int err; + +if (!static_cpu_has (X86_FEATURE_NRIPS )) +return emulate_on_interception (svm); + + err =kvm_rdpmc (&svm->vcpu); +kvm_complete_insn_gp (&svm->vcpu, err); + +return 1 ; +} +bool check_selective_cr0_intercepted (struct vcpu_svm *svm,unsigned long val) {unsigned long cr0 = svm->vcpu.arch.cr0;
@@ -3190,6 +3204,7 @@ static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_SMI] = nop_on_interception, [SVM_EXIT_INIT] = nop_on_interception, [SVM_EXIT_VINTR] = interrupt_window_interception, + [SVM_EXIT_RDPMC] = rdpmc_interception, [SVM_EXIT_CPUID] = cpuid_interception, [SVM_EXIT_IRET] = iret_interception, [SVM_EXIT_INVD] = emulate_on_interception,
Signature · 1 line
1.7.7.1
PATCH v3 5/10 KVM: VMX: Intercept RDPMC
In reply to Gleb NatapovFrom: Avi Kivity <[email protected]>
Intercept RDPMC and forward it to the PMU emulation code.
--- arch/x86/kvm/vmx.c | 15 ++++++++++++++- 1 files changed, 14 insertions(+), 1 deletions(-)
arch/x86/kvm/vmx.c +14 -1
@@ -1956,6 +1956,7 @@ static __init void nested_vmx_setup_ctls_msrs(void)#endif CPU_BASED_MOV_DR_EXITING |CPU_BASED_UNCOND_IO_EXITING |CPU_BASED_USE_IO_BITMAPS |CPU_BASED_MONITOR_EXITING | +CPU_BASED_RDPMC_EXITING |CPU_BASED_ACTIVATE_SECONDARY_CONTROLS ;/* * We can allow some features even when not supported by the
@@ -2414,7 +2415,8 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)CPU_BASED_USE_TSC_OFFSETING |CPU_BASED_MWAIT_EXITING |CPU_BASED_MONITOR_EXITING | -CPU_BASED_INVLPG_EXITING ; +CPU_BASED_INVLPG_EXITING | +CPU_BASED_RDPMC_EXITING ;if (yield_on_hlt) min |=CPU_BASED_HLT_EXITING ;
@@ -4615,6 +4617,16 @@ static int handle_invlpg(struct kvm_vcpu *vcpu)return 1 ; } +static int handle_rdpmc (struct kvm_vcpu *vcpu) +{ +int err; + + err =kvm_rdpmc (vcpu); +kvm_complete_insn_gp (vcpu, err); + +return 1 ; +} +static int handle_wbinvd (struct kvm_vcpu *vcpu) {skip_emulated_instruction (vcpu);
@@ -5565,6 +5577,7 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { [EXIT_REASON_HLT] = handle_halt, [EXIT_REASON_INVD] = handle_invd, [EXIT_REASON_INVLPG] = handle_invlpg, + [EXIT_REASON_RDPMC] = handle_rdpmc, [EXIT_REASON_VMCALL] = handle_vmcall, [EXIT_REASON_VMCLEAR] = handle_vmclear, [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Signature · 1 line
1.7.7.1
PATCH v3 6/10 x86, perf: disable non available architectural events.
In reply to Gleb NatapovIntel CPUs report non-available architectural events in cpuid leaf 0AH.EBX. Use it to disable events that are not available according to CPU.
--- arch/x86/include/asm/perf_event.h | 14 ++++++++++++++ arch/x86/kernel/cpu/perf_event.h | 5 +++++ arch/x86/kernel/cpu/perf_event_intel.c | 29 ++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 5 deletions(-)
arch/x86/include/asm/perf_event.h +14 -0
@@ -57,6 +57,7 @@ (1 << (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX))#define ARCH_PERFMON_BRANCH_MISSES_RETIRED 6 +#define ARCH_PERFMON_EVENTS_COUNT 7/* * Intel "Architectural Performance Monitoring" CPUID
@@ -72,6 +73,19 @@ union cpuid10_eax {unsigned int full; }; +union cpuid10_ebx { +struct { +unsigned int no_unhalted_core_cycles:1 ; +unsigned int no_instructions_retired:1 ; +unsigned int no_unhalted_reference_cycles:1 ; +unsigned int no_llc_reference:1 ; +unsigned int no_llc_misses:1 ; +unsigned int no_branch_instruction_retired:1 ; +unsigned int no_branch_misses_retired:1 ; + } split; +unsigned int full; +}; +union cpuid10_edx {struct {unsigned int num_counters_fixed:5 ;
arch/x86/kernel/cpu/perf_event.h +5 -0
@@ -259,6 +259,11 @@ struct x86_pmu {int num_counters_fixed;int cntval_bits;u64 cntval_mask; +union { +unsigned long events_maskl; +unsigned long events_mask[BITS_TO_LONGS (ARCH_PERFMON_EVENTS_COUNT )]; + }; +int events_mask_len;int apic;u64 max_period; struct event_constraint *
arch/x86/kernel/cpu/perf_event_intel.c +24 -5
@@ -1544,13 +1544,23 @@ static void intel_clovertown_quirks(void) x86_pmu.pebs_constraints =NULL ; } +static int intel_event_id_to_hw_id[] = { +PERF_COUNT_HW_CPU_CYCLES , +PERF_COUNT_HW_INSTRUCTIONS , +PERF_COUNT_HW_BUS_CYCLES , +PERF_COUNT_HW_CACHE_REFERENCES , +PERF_COUNT_HW_CACHE_MISSES , +PERF_COUNT_HW_BRANCH_INSTRUCTIONS , +PERF_COUNT_HW_BRANCH_MISSES , +}; +__init int intel_pmu_init (void ) {union cpuid10_edx edx;union cpuid10_eax eax; +union cpuid10_ebx ebx;unsigned int unused; -unsigned int ebx; -int version; +int version, bit;if (!cpu_has (&boot_cpu_data,X86_FEATURE_ARCH_PERFMON )) {switch (boot_cpu_data.x86) {
@@ -1566,8 +1576,8 @@ __init int intel_pmu_init(void)* Check whether the Architectural PerfMon supports * Branch Misses Retired hw_event or not. */ -cpuid (10 , &eax.full, &ebx, &unused, &edx.full); -if (eax.split.mask_length <=ARCH_PERFMON_BRANCH_MISSES_RETIRED ) +cpuid (10 , &eax.full, &ebx.full, &unused, &edx.full); +if (eax.split.mask_length <ARCH_PERFMON_EVENTS_COUNT )return -ENODEV ; version = eax.split.version_id;
@@ -1643,7 +1653,7 @@ __init int intel_pmu_init(void)/* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */ intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND ] =0x1803fb1 ; -if (ebx &0x40 ) { +if (ebx.split.no_branch_misses_retired) {/* * Erratum AAJ80 detected, we work it around by using * the BR_MISP_EXEC.ANY event. This will over-count
@@ -1651,6 +1661,7 @@ __init int intel_pmu_init(void)* architectural event which is often completely bogus: */ intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES ] =0x7f89 ; + ebx.split.no_branch_misses_retired =0 ;pr_cont ("erratum AAJ80 worked around, " ); }
@@ -1729,5 +1740,13 @@ __init int intel_pmu_init(void)break ; } } + x86_pmu.events_maskl = ebx.full; + x86_pmu.events_mask_len = eax.split.mask_length; + +/* disable event that reported as not presend by cpuid */ +for_each_set_bit (bit, x86_pmu.events_mask, +min (x86_pmu.events_mask_len, x86_pmu.max_events)) + intel_perfmon_event_map[intel_event_id_to_hw_id[bit]] =0 ; +return 0 ; }
Signature · 1 line
1.7.7.1
Re: [PATCHv3 06/10] x86, perf: disable non available architectural events.
arch/x86/kernel/cpu/perf_event_intel.c __init int intel_pmu_init(void) · line 1749
Quoted 5 lines · Gleb Natapov
arch/x86/kernel/cpu/perf_event_intel.c +5
-0+ +/* disable event that reported as not presend by cpuid */ +for_each_set_bit (bit, x86_pmu.events_mask, +min (x86_pmu.events_mask_len, x86_pmu.max_events)) + intel_perfmon_event_map[intel_event_id_to_hw_id[bit]] =0 ;
I think that should read:
for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_event_id_to_hw_id))
intel_perfmon_event_map[intel_event_id_to_hw_id[bit]] = 0;There is no guarantee x86_pmu.max_events has any relation to the size of intel_event_id_to_hw_id.
Changed it for you.
[tip:perf/core] x86, perf: Disable non available architectural events
In reply to Gleb NatapovCommit-ID: ffb871bc9156ee2e5cf442f61250c5bd6aad17e3 Gitweb: http://git.kernel.org/tip/ffb871bc9156ee2e5cf442f61250c5bd6aad17e3 Author: Gleb Natapov <[email protected]> AuthorDate: Thu, 10 Nov 2011 14:57:26 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 6 Dec 2011 20:41:05 +0100
x86, perf: Disable non available architectural events
Intel CPUs report non-available architectural events in cpuid leaf 0AH.EBX. Use it to disable events that are not available according to CPU.
--- arch/x86/include/asm/perf_event.h | 14 ++++++++++++++ arch/x86/kernel/cpu/perf_event.h | 5 +++++ arch/x86/kernel/cpu/perf_event_intel.c | 28 +++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 5 deletions(-)
arch/x86/include/asm/perf_event.h +14 -0
@@ -57,6 +57,7 @@ (1 << (ARCH_PERFMON_UNHALTED_CORE_CYCLES_INDEX))#define ARCH_PERFMON_BRANCH_MISSES_RETIRED 6 +#define ARCH_PERFMON_EVENTS_COUNT 7/* * Intel "Architectural Performance Monitoring" CPUID
@@ -72,6 +73,19 @@ union cpuid10_eax {unsigned int full; }; +union cpuid10_ebx { +struct { +unsigned int no_unhalted_core_cycles:1 ; +unsigned int no_instructions_retired:1 ; +unsigned int no_unhalted_reference_cycles:1 ; +unsigned int no_llc_reference:1 ; +unsigned int no_llc_misses:1 ; +unsigned int no_branch_instruction_retired:1 ; +unsigned int no_branch_misses_retired:1 ; + } split; +unsigned int full; +}; +union cpuid10_edx {struct {unsigned int num_counters_fixed:5 ;
arch/x86/kernel/cpu/perf_event.h +5 -0
@@ -285,6 +285,11 @@ struct x86_pmu {int num_counters_fixed;int cntval_bits;u64 cntval_mask; +union { +unsigned long events_maskl; +unsigned long events_mask[BITS_TO_LONGS (ARCH_PERFMON_EVENTS_COUNT )]; + }; +int events_mask_len;int apic;u64 max_period; struct event_constraint *
arch/x86/kernel/cpu/perf_event_intel.c +23 -5
@@ -1552,13 +1552,23 @@ static void intel_sandybridge_quirks(void) x86_pmu.pebs_constraints =NULL ; } +static const int intel_event_id_to_hw_id[]__initconst = { +PERF_COUNT_HW_CPU_CYCLES , +PERF_COUNT_HW_INSTRUCTIONS , +PERF_COUNT_HW_BUS_CYCLES , +PERF_COUNT_HW_CACHE_REFERENCES , +PERF_COUNT_HW_CACHE_MISSES , +PERF_COUNT_HW_BRANCH_INSTRUCTIONS , +PERF_COUNT_HW_BRANCH_MISSES , +}; +__init int intel_pmu_init (void ) {union cpuid10_edx edx;union cpuid10_eax eax; +union cpuid10_ebx ebx;unsigned int unused; -unsigned int ebx; -int version; +int version, bit;if (!cpu_has (&boot_cpu_data,X86_FEATURE_ARCH_PERFMON )) {switch (boot_cpu_data.x86) {
@@ -1574,8 +1584,8 @@ __init int intel_pmu_init(void)* Check whether the Architectural PerfMon supports * Branch Misses Retired hw_event or not. */ -cpuid (10 , &eax.full, &ebx, &unused, &edx.full); -if (eax.split.mask_length <=ARCH_PERFMON_BRANCH_MISSES_RETIRED ) +cpuid (10 , &eax.full, &ebx.full, &unused, &edx.full); +if (eax.split.mask_length <ARCH_PERFMON_EVENTS_COUNT )return -ENODEV ; version = eax.split.version_id;
@@ -1651,7 +1661,7 @@ __init int intel_pmu_init(void)/* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */ intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND ] =0x1803fb1 ; -if (ebx &0x40 ) { +if (ebx.split.no_branch_misses_retired) {/* * Erratum AAJ80 detected, we work it around by using * the BR_MISP_EXEC.ANY event. This will over-count
@@ -1659,6 +1669,7 @@ __init int intel_pmu_init(void)* architectural event which is often completely bogus: */ intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES ] =0x7f89 ; + ebx.split.no_branch_misses_retired =0 ;pr_cont ("erratum AAJ80 worked around, " ); }
@@ -1738,5 +1749,12 @@ __init int intel_pmu_init(void)break ; } } + x86_pmu.events_maskl = ebx.full; + x86_pmu.events_mask_len = eax.split.mask_length; + +/* disable event that reported as not presend by cpuid */ +for_each_set_bit (bit, x86_pmu.events_mask,ARRAY_SIZE (intel_event_id_to_hw_id)) + intel_perfmon_event_map[intel_event_id_to_hw_id[bit]] =0 ; +return 0 ; }
PATCH v3 7/10 perf, x86: expose perf capability to other modules.
In reply to Gleb NatapovKVM needs to know perf capability to decide which PMU it can expose to a guest.
--- arch/x86/include/asm/perf_event.h | 15 +++++++++++++++ arch/x86/kernel/cpu/perf_event.c | 11 +++++++++++ 2 files changed, 26 insertions(+), 0 deletions(-)
arch/x86/include/asm/perf_event.h +15 -0
@@ -95,6 +95,15 @@ union cpuid10_edx {unsigned int full; }; +struct x86_pmu_capability { +int version; +int num_counters_gp; +int num_counters_fixed; +int bit_width_gp; +int bit_width_fixed; +unsigned int events_mask; +int events_mask_len; +};/* * Fixed-purpose performance events:
@@ -216,6 +225,7 @@ struct perf_guest_switch_msr { };extern struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr); +extern void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap);#else static inline perf_guest_switch_msr *perf_guest_get_msrs (int *nr) {
@@ -223,6 +233,11 @@ static inline perf_guest_switch_msr *perf_guest_get_msrs(int *nr) return NULL; } +static inline void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap) +{ +memset (cap,0 ,sizeof (*cap)); +} +static inline void perf_events_lapic_init (void ) { }#endif
arch/x86/kernel/cpu/perf_event.c +11 -0
@@ -1570,3 +1570,14 @@ unsigned long perf_misc_flags(struct pt_regs *regs)return misc; } + +void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap) +{ + cap->version = x86_pmu.version; + cap->num_counters_gp = x86_pmu.num_counters; + cap->num_counters_fixed = x86_pmu.num_counters_fixed; + cap->bit_width_gp = cap->bit_width_fixed = x86_pmu.cntval_bits; + cap->events_mask = (unsigned int )x86_pmu.events_maskl; + cap->events_mask_len = x86_pmu.events_mask_len; +} +EXPORT_SYMBOL_GPL (perf_get_x86_pmu_capability);
Signature · 1 line
1.7.7.1
[tip:perf/core] perf, x86: Expose perf capability to other modules
Commit-ID: b3d9468a8bd218a695e3a0ff112cd4efd27b670a Gitweb: http://git.kernel.org/tip/b3d9468a8bd218a695e3a0ff112cd4efd27b670a Author: Gleb Natapov <[email protected]> AuthorDate: Thu, 10 Nov 2011 14:57:27 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 6 Dec 2011 20:41:08 +0100
perf, x86: Expose perf capability to other modules
KVM needs to know perf capability to decide which PMU it can expose to a guest.
--- arch/x86/include/asm/perf_event.h | 15 +++++++++++++++ arch/x86/kernel/cpu/perf_event.c | 12 ++++++++++++ 2 files changed, 27 insertions(+), 0 deletions(-)
arch/x86/include/asm/perf_event.h +15 -0
@@ -95,6 +95,15 @@ union cpuid10_edx {unsigned int full; }; +struct x86_pmu_capability { +int version; +int num_counters_gp; +int num_counters_fixed; +int bit_width_gp; +int bit_width_fixed; +unsigned int events_mask; +int events_mask_len; +};/* * Fixed-purpose performance events:
@@ -216,6 +225,7 @@ struct perf_guest_switch_msr { };extern struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr); +extern void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap);#else static inline perf_guest_switch_msr *perf_guest_get_msrs (int *nr) {
@@ -223,6 +233,11 @@ static inline perf_guest_switch_msr *perf_guest_get_msrs(int *nr) return NULL; } +static inline void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap) +{ +memset (cap,0 ,sizeof (*cap)); +} +static inline void perf_events_lapic_init (void ) { }#endif
arch/x86/kernel/cpu/perf_event.c +12 -0
@@ -1696,3 +1696,15 @@ unsigned long perf_misc_flags(struct pt_regs *regs)return misc; } + +void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap) +{ + cap->version = x86_pmu.version; + cap->num_counters_gp = x86_pmu.num_counters; + cap->num_counters_fixed = x86_pmu.num_counters_fixed; + cap->bit_width_gp = x86_pmu.cntval_bits; + cap->bit_width_fixed = x86_pmu.cntval_bits; + cap->events_mask = (unsigned int )x86_pmu.events_maskl; + cap->events_mask_len = x86_pmu.events_mask_len; +} +EXPORT_SYMBOL_GPL (perf_get_x86_pmu_capability);
PATCH v3 8/10 KVM: Expose the architectural performance monitoring CPUID leaf
In reply to Gleb NatapovProvide a CPUID leaf that describes the emulated PMU.
--- arch/x86/kvm/x86.c | 30 +++++++++++++++++++++++++++++- 1 files changed, 29 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +29 -1
@@ -2544,6 +2544,35 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function, }case 9 :break ; +case 0xa : {/* Architectural Performance Monitoring */ +struct x86_pmu_capability cap; +union cpuid10_eax eax; +union cpuid10_edx edx; + +perf_get_x86_pmu_capability (&cap); + +/* +* Only support guest architectural pmu on a host +* with architectural pmu. +*/ +if (!cap.version) +memset (&cap,0 ,sizeof (cap)); + + eax.split.version_id =min (cap.version,2 ); + eax.split.num_counters = cap.num_counters_gp; + eax.split.bit_width = cap.bit_width_gp; + eax.split.mask_length = cap.events_mask_len; + + edx.split.num_counters_fixed = cap.num_counters_fixed; + edx.split.bit_width_fixed = cap.bit_width_fixed; + edx.split.reserved =0 ; + + entry->eax = eax.full; + entry->ebx = cap.events_mask; + entry->ecx =0 ; + entry->edx = edx.full; +break ; + }/* function 0xb has additional index. */ case 0xb : {int i, level_type;
@@ -2638,7 +2667,6 @@ static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,case 3 :/* Processor serial number */ case 5 :/* MONITOR/MWAIT */ case 6 :/* Thermal management */ -case 0xA :/* Architectural Performance Monitoring */ case 0x80000007 :/* Advanced power management */ case 0xC0000002 :case 0xC0000003 :
Signature · 1 line
1.7.7.1
PATCH v3 9/10 KVM: x86 emulator: fix RDPMC privilege check
In reply to Gleb NatapovFrom: Avi Kivity <[email protected]>
RDPMC is only privileged if CR4.PCE=0. check_rdpmc() already implements this, so all we need to do is drop the Priv flag.
--- arch/x86/kvm/emulate.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/emulate.c +1 -1
@@ -3254,7 +3254,7 @@ static struct opcode twobyte_table[256] = {DI (ImplicitOps | Priv, wrmsr),IIP (ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),DI (ImplicitOps | Priv, rdmsr), -DIP (ImplicitOps | Priv, rdpmc, check_rdpmc), +DIP (ImplicitOps, rdpmc, check_rdpmc),I (ImplicitOps | VendorSpecific, em_sysenter),I (ImplicitOps | Priv | VendorSpecific, em_sysexit), N, N,
Signature · 1 line
1.7.7.1
PATCH v3 10/10 KVM: x86 emulator: implement RDPMC (0F 33)
In reply to Gleb NatapovFrom: Avi Kivity <[email protected]>
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 13 ++++++++++++- arch/x86/kvm/x86.c | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -181,6 +181,7 @@ struct x86_emulate_ops {int (*set_dr)(struct x86_emulate_ctxt *ctxt,int dr,ulong value);int (*set_msr)(struct x86_emulate_ctxt *ctxt,u32 msr_index,u64 data);int (*get_msr)(struct x86_emulate_ctxt *ctxt,u32 msr_index,u64 *pdata); +int (*read_pmc)(struct x86_emulate_ctxt *ctxt,u32 pmc,u64 *pdata);void (*halt)(struct x86_emulate_ctxt *ctxt);void (*wbinvd)(struct x86_emulate_ctxt *ctxt);int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/emulate.c +12 -1
@@ -2623,6 +2623,17 @@ static int em_rdtsc(struct x86_emulate_ctxt *ctxt)return X86EMUL_CONTINUE ; } +static int em_rdpmc (struct x86_emulate_ctxt *ctxt) +{ +u64 pmc; + +if (ctxt->ops->read_pmc (ctxt, ctxt->regs[VCPU_REGS_RCX ], &pmc)) +return emulate_gp (ctxt,0 ); + ctxt->regs[VCPU_REGS_RAX ] = (u32 )pmc; + ctxt->regs[VCPU_REGS_RDX ] = pmc >>32 ; +return X86EMUL_CONTINUE ; +} +static int em_mov (struct x86_emulate_ctxt *ctxt) { ctxt->dst.val = ctxt->src.val;
@@ -3254,7 +3265,7 @@ static struct opcode twobyte_table[256] = {DI (ImplicitOps | Priv, wrmsr),IIP (ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),DI (ImplicitOps | Priv, rdmsr), -DIP (ImplicitOps, rdpmc, check_rdpmc), +IIP (ImplicitOps, em_rdpmc, rdpmc, check_rdpmc),I (ImplicitOps | VendorSpecific, em_sysenter),I (ImplicitOps | Priv | VendorSpecific, em_sysexit), N, N,
arch/x86/kvm/x86.c +7 -0
@@ -4662,6 +4662,12 @@ static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,return kvm_set_msr (emul_to_vcpu (ctxt), msr_index, data); } +static int emulator_read_pmc (struct x86_emulate_ctxt *ctxt, +u32 pmc,u64 *pdata) +{ +return kvm_pmu_read_pmc (emul_to_vcpu (ctxt), pmc, pdata); +} +static void emulator_halt (struct x86_emulate_ctxt *ctxt) {emul_to_vcpu (ctxt)->arch.halt_request =1 ;
@@ -4714,6 +4720,7 @@ static struct x86_emulate_ops emulate_ops = { .set_dr = emulator_set_dr, .set_msr = emulator_set_msr, .get_msr = emulator_get_msr, + .read_pmc = emulator_read_pmc, .halt = emulator_halt, .wbinvd = emulator_wbinvd, .fix_hypercall = emulator_fix_hypercall,
Signature · 1 line
1.7.7.1
Quoted 15 lines · Gleb Natapov
This patchset exposes an emulated version 2 architectural performance monitoring unit to KVM guests. The PMU is emulated using
perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.The patches are against next branch on kvm.git.
If you want to try running perf in a guest you need to apply the patch below to qemu-kvm and use -cpu host on qemu command line. But DO NOT TRY those patches without applying [1][2] to the host kernel first. Don't tell me I didn't warn you!
[1] https://lkml.org/lkml/2011/10/18/390
[2] https://lkml.org/lkml/2011/10/23/163
Quoted 10 lines
Changelog:
v1->v2
- put index into structkvm_pmcinstead of calculating it
- use locked version of bitops
- inject pmi from irq work if vcpu was not in a guest mode during NMI
- providing stub forperf_get_x86_pmu_capability()for !PERF_EVENTS
v2->v3
- minor style change/comment clarification
- add perf patch to disable arch event not supported by a CPU
- create perf events as pinned
On the pinned thing, do check event->state, if it hits PERF_EVENT_STATE_ERROR you're up shit creek and the counter didn't get scheduled. This can happen because eg. cpu-pinned events have higher precedence than task-pinned events.
I've taken the two perf patches from this (6 and 7), the rest I gather should go through the KVM tree. I've only cursory looked at the other patches but didn't spot anything very curious.
Avi Kivity
AppliedQuoted 8 lines · Gleb Natapov
This patchset exposes an emulated version 2 architectural performance monitoring unit to KVM guests. The PMU is emulated using
perf_events, so the host kernel can multiplex host-wide, host-user, and the guest on available resources.The patches are against next branch on kvm.git.
Thanks, applied.
Signature · 1 line
error compiling committee.c: too many arguments to function