Consolidated KVM vPMU support for x86
v5 cover Consolidated KVM vPMU support for x86
Currently KVM only supports vPMU for Intel CPUs. This patchset enables KVM vPMU support for AMD platform by creating a common PMU interface for x86. By refractoring, PMU related MSR accesses from guest VMs are dispatched to corresponding functions defined in arch specific files.
This patchset is directlyh applicable on kvm.git/queue.
V5:
* Remove the get_pmu_ops from sub_arch; instead define pmu dispatcher in kvm_x86_ops->pmu_ops. The dispatcher is initialized in sub-arch. The PMU interface functions are changed accordingly (suggested by Joerg Rodel).
V4:
* Change vPMU API function names to further clarify their functionality
* Small fix for switch statement index in EVNTSELn and PERFCTRn
(patch 4)
* Add Tested-by and Reviewed-by
V3:
* Rebase the code to the latest of KVM tree (queue branch);
* Branch out the Intel specific code from pmu.c to pmu_intel.c, in
order to reflect the change history more accurately;
* Name the parameters/variables more consistently (use msr, idx,
pmc_idx) across files;
* Fix issues (whitespaces, macro names, ...) based on Radim's V2
comments; * Fix the MSR_K7_PERFCTRn and MSR_K7_EVNTSELn access code (in patch 4);
V2:
* Create a generic pmu.c file which is shared by Intel and AMD CPUs;
* pmu.c code becomes part of kvm.ko module. Similarly pmu_intel.c and
pmu_amd.c are linked to kvm-intel.ko and kvm-amd.ko respectively;
* Re-define kvm_pmu_ops function pointers. Per Radim Krcmar's comments, a large portion of Intel vPMU code are now consolidated and moved to pmu.c;
* Polish pmu_amd.c code to comply with new definition of kvm_pmu_ops;
V1:
* Adopt the file layout suggested by Radim Krcmar
* Link arch module with its specific PMU file
RFC:
* Initial version for RFC
Wei Huang (4):
KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch
KVM: x86/vPMU: Create vPMU interface for VMX and SVM
KVM: x86/vPMU: Implement AMD vPMU code for KVM
KVM: x86/vPMU: Enable PMU handling for AMD PERFCTRn and EVNTSELn MSRs
arch/x86/include/asm/kvm_host.h | 48 ++-- arch/x86/kvm/Makefile | 4 +- arch/x86/kvm/cpuid.c | 3 +- arch/x86/kvm/pmu.c | 550 ++++++++++------------------------------ arch/x86/kvm/pmu.h | 96 +++++++ arch/x86/kvm/pmu_amd.c | 207 +++++++++++++++ arch/x86/kvm/pmu_intel.c | 360 ++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 3 + arch/x86/kvm/vmx.c | 3 + arch/x86/kvm/x86.c | 70 ++--- 10 files changed, 863 insertions(+), 481 deletions(-) create mode 100644 arch/x86/kvm/pmu.h create mode 100644 arch/x86/kvm/pmu_amd.c create mode 100644 arch/x86/kvm/pmu_intel.c
Signature · 1 line
1.8.3.1
PATCH v5 1/4 KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch
This patch defines a new function pointer struct (kvm_pmu_ops) to support vPMU for both Intel and AMD. The functions pointers defined in this new struct will be linked with Intel and AMD functions later. In the meanwhile the struct that maps from event_sel bits to PERF_TYPE_HARDWARE events is renamed and moved from Intel specific code to kvm_host.h as a common struct.
--- arch/x86/include/asm/kvm_host.h | 34 ++++++++++++++++++++++++++++------ arch/x86/kvm/pmu.c | 21 ++++++++------------- 2 files changed, 36 insertions(+), 19 deletions(-)
arch/x86/include/asm/kvm_host.h +28 -6
@@ -304,6 +304,12 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; +struct msr_data { +bool host_initiated; +u32 index; +u64 data; +}; +enum pmc_type {KVM_PMC_GP =0 ,KVM_PMC_FIXED ,
@@ -318,6 +324,12 @@ struct kvm_pmc {struct kvm_vcpu *vcpu; }; +struct kvm_event_hw_type_mapping { +u8 eventsel; +u8 unit_mask; +unsigned event_type; +}; +struct kvm_pmu {unsigned nr_arch_gp_counters;unsigned nr_arch_fixed_counters;
@@ -336,6 +348,22 @@ struct kvm_pmu {u64 reprogram_pmi; }; +struct kvm_pmu_ops { +unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, +u8 unit_mask); +unsigned (*find_fixed_event)(int idx); +bool (*pmc_enabled)(struct kvm_pmc *pmc); +struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); +struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); +int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); +bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); +int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); +int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); +void (*refresh)(struct kvm_vcpu *vcpu); +void (*init)(struct kvm_vcpu *vcpu); +void (*reset)(struct kvm_vcpu *vcpu); +}; +enum {KVM_DEBUGREG_BP_ENABLED =1 ,KVM_DEBUGREG_WONT_EXIT =2 ,
@@ -683,12 +711,6 @@ struct kvm_vcpu_stat {struct x86_instruction_info ; -struct msr_data { -bool host_initiated; -u32 index; -u64 data; -}; -struct kvm_lapic_irq {u32 vector;u16 delivery_mode;
arch/x86/kvm/pmu.c +8 -13
@@ -20,12 +20,7 @@#include "cpuid.h" #include "lapic.h" -static struct kvm_arch_event_perf_mapping { -u8 eventsel; -u8 unit_mask; -unsigned event_type; -bool inexact; -} arch_events[] = { +static struct kvm_event_hw_type_mapping intel_arch_events[] = {/* Index must match CPUID 0x0A.EBX bit vector */ [0 ] = {0x3c ,0x00 ,PERF_COUNT_HW_CPU_CYCLES }, [1 ] = {0xc0 ,0x00 ,PERF_COUNT_HW_INSTRUCTIONS },
@@ -37,7 +32,7 @@ static struct kvm_arch_event_perf_mapping { [7] = { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES }, }; -/* mapping between fixed pmc index and arch_events array */ +/* mapping between fixed pmc index and intel_arch_events array */ static int fixed_pmc_events[] = {1 ,0 ,7 };static bool pmc_is_gp (struct kvm_pmc *pmc)
@@ -202,16 +197,16 @@ static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select, {int i; -for (i =0 ; i <ARRAY_SIZE (arch_events); i++) -if (arch_events[i].eventsel == event_select - && arch_events[i].unit_mask == unit_mask +for (i =0 ; i <ARRAY_SIZE (intel_arch_events); i++) +if (intel_arch_events[i].eventsel == event_select + && intel_arch_events[i].unit_mask == unit_mask && (pmu->available_event_types & (1 << i)))break ; -if (i ==ARRAY_SIZE (arch_events)) +if (i ==ARRAY_SIZE (intel_arch_events))return PERF_COUNT_HW_MAX ; -return arch_events[i].event_type; +return intel_arch_events[i].event_type; }static void reprogram_gp_counter (struct kvm_pmc *pmc,u64 eventsel)
@@ -265,7 +260,7 @@ static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx)return ;reprogram_counter (pmc,PERF_TYPE_HARDWARE , - arch_events[fixed_pmc_events[idx]].event_type, + intel_arch_events[fixed_pmc_events[idx]].event_type, !(en &0x2 ),/* exclude user */ !(en &0x1 ),/* exclude kernel */ pmi, false, false);
Signature · 1 line
1.8.3.1
Paolo Bonzini
maintainerRe: [PATCH V5 1/4] KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch
Quoted 10 lines · Wei Huang
This patch defines a new function pointer struct (
kvm_pmu_ops) to support vPMU for both Intel and AMD. The functions pointers defined in this new struct will be linked with Intel and AMD functions later. In the meanwhile the struct that maps fromevent_selbits toPERF_TYPE_HARDWAREevents is renamed and moved from Intel specific code tokvm_host.has a common struct.Reviewed-by Joerg Roedel <[email protected]>Tested-by Joerg Roedel <[email protected]>Signed-off-by Wei Huang <[email protected]>
The kvm_pmu_ops are unused and would belong in patch 2... the code is good anyway, and I'm not going to be fussy about it because there's no semantic change anyway in this patch.
Paolo
Quoted 122 lines
--- arch/x86/include/asm/kvm_host.h | 34 ++++++++++++++++++++++++++++------ arch/x86/kvm/pmu.c | 21 ++++++++------------- 2 files changed, 36 insertions(+), 19 deletions(-)arch/x86/include/asm/kvm_host.h +28
-6@@ -304,6 +304,12 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; +struct msr_data { +bool host_initiated; +u32 index; +u64 data; +}; +enum pmc_type {KVM_PMC_GP =0 ,KVM_PMC_FIXED ,@@ -318,6 +324,12 @@ struct kvm_pmc {struct kvm_vcpu *vcpu; }; +struct kvm_event_hw_type_mapping { +u8 eventsel; +u8 unit_mask; +unsigned event_type; +}; +struct kvm_pmu {unsigned nr_arch_gp_counters;unsigned nr_arch_fixed_counters;@@ -336,6 +348,22 @@ struct kvm_pmu {u64 reprogram_pmi; }; +struct kvm_pmu_ops { +unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, +u8 unit_mask); +unsigned (*find_fixed_event)(int idx); +bool (*pmc_enabled)(struct kvm_pmc *pmc); +struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); +struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); +int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); +bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); +int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); +int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); +void (*refresh)(struct kvm_vcpu *vcpu); +void (*init)(struct kvm_vcpu *vcpu); +void (*reset)(struct kvm_vcpu *vcpu); +}; +enum {KVM_DEBUGREG_BP_ENABLED =1 ,KVM_DEBUGREG_WONT_EXIT =2 ,@@ -683,12 +711,6 @@ struct kvm_vcpu_stat {struct x86_instruction_info ; -struct msr_data { -bool host_initiated; -u32 index; -u64 data; -}; -struct kvm_lapic_irq {u32 vector;u16 delivery_mode;arch/x86/kvm/pmu.c +7
-12@@ -20,12 +20,7 @@#include "cpuid.h" #include "lapic.h" -static struct kvm_arch_event_perf_mapping { -u8 eventsel; -u8 unit_mask; -unsigned event_type; -bool inexact; -} arch_events[] = { +static struct kvm_event_hw_type_mapping intel_arch_events[] = {/* Index must match CPUID 0x0A.EBX bit vector */ [0 ] = {0x3c ,0x00 ,PERF_COUNT_HW_CPU_CYCLES }, [1 ] = {0xc0 ,0x00 ,PERF_COUNT_HW_INSTRUCTIONS },@@ -37,7 +32,7 @@ static struct kvm_arch_event_perf_mapping { [7] = { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES }, }; -/* mapping between fixed pmc index and arch_events array */ +/* mapping between fixed pmc index and intel_arch_events array */ static int fixed_pmc_events[] = {1 ,0 ,7 };static bool pmc_is_gp (struct kvm_pmc *pmc)@@ -202,16 +197,16 @@ static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select, {int i; -for (i =0 ; i <ARRAY_SIZE (arch_events); i++) -if (arch_events[i].eventsel == event_select - && arch_events[i].unit_mask == unit_mask +for (i =0 ; i <ARRAY_SIZE (intel_arch_events); i++) +if (intel_arch_events[i].eventsel == event_select + && intel_arch_events[i].unit_mask == unit_mask && (pmu->available_event_types & (1 << i)))break ; -if (i ==ARRAY_SIZE (arch_events)) +if (i ==ARRAY_SIZE (intel_arch_events))return PERF_COUNT_HW_MAX ; -return arch_events[i].event_type; +return intel_arch_events[i].event_type; }static void reprogram_gp_counter (struct kvm_pmc *pmc,u64 eventsel)
arch/x86/kvm/pmu.c static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx) · line 266
Quoted 8 lines
arch/x86/kvm/pmu.c +1
-1@@ -265,7 +260,7 @@ static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx)return ;reprogram_counter (pmc,PERF_TYPE_HARDWARE , - arch_events[fixed_pmc_events[idx]].event_type, + intel_arch_events[fixed_pmc_events[idx]].event_type, !(en &0x2 ),/* exclude user */ !(en &0x1 ),/* exclude kernel */ pmi, false, false);
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
Re: [PATCH V5 1/4] KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch
Quoted 10 lines · Paolo Bonzini
Quoted 7 lines · Wei Huang
This patch defines a new function pointer struct (
kvm_pmu_ops) to support vPMU for both Intel and AMD. The functions pointers defined in this new struct will be linked with Intel and AMD functions later. In the meanwhile the struct that maps fromevent_selbits toPERF_TYPE_HARDWAREevents is renamed and moved from Intel specific code tokvm_host.has a common struct.
Quoted 7 lines
Quoted 3 lines
Reviewed-by Joerg Roedel <[email protected]>Tested-by Joerg Roedel <[email protected]>Signed-off-by Wei Huang <[email protected]>The
kvm_pmu_opsare unused and would belong in patch 2... the code is good anyway, and I'm not going to be fussy about it because there's no semantic change anyway in this patch.
OK. This is should be relatively easy.
-Wei
Quoted 126 lines
Paolo
Quoted 123 lines
--- arch/x86/include/asm/kvm_host.h | 34 ++++++++++++++++++++++++++++------ arch/x86/kvm/pmu.c | 21 ++++++++------------- 2 files changed, 36 insertions(+), 19 deletions(-)arch/x86/include/asm/kvm_host.h +28
-6@@ -304,6 +304,12 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; +struct msr_data { +bool host_initiated; +u32 index; +u64 data; +}; +enum pmc_type {KVM_PMC_GP =0 ,KVM_PMC_FIXED ,@@ -318,6 +324,12 @@ struct kvm_pmc {struct kvm_vcpu *vcpu; }; +struct kvm_event_hw_type_mapping { +u8 eventsel; +u8 unit_mask; +unsigned event_type; +}; +struct kvm_pmu {unsigned nr_arch_gp_counters;unsigned nr_arch_fixed_counters;@@ -336,6 +348,22 @@ struct kvm_pmu {u64 reprogram_pmi; }; +struct kvm_pmu_ops { +unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, +u8 unit_mask); +unsigned (*find_fixed_event)(int idx); +bool (*pmc_enabled)(struct kvm_pmc *pmc); +struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); +struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); +int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); +bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); +int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); +int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); +void (*refresh)(struct kvm_vcpu *vcpu); +void (*init)(struct kvm_vcpu *vcpu); +void (*reset)(struct kvm_vcpu *vcpu); +}; +enum {KVM_DEBUGREG_BP_ENABLED =1 ,KVM_DEBUGREG_WONT_EXIT =2 ,@@ -683,12 +711,6 @@ struct kvm_vcpu_stat {struct x86_instruction_info ; -struct msr_data { -bool host_initiated; -u32 index; -u64 data; -}; -struct kvm_lapic_irq {u32 vector;u16 delivery_mode;arch/x86/kvm/pmu.c +7
-12@@ -20,12 +20,7 @@#include "cpuid.h" #include "lapic.h" -static struct kvm_arch_event_perf_mapping { -u8 eventsel; -u8 unit_mask; -unsigned event_type; -bool inexact; -} arch_events[] = { +static struct kvm_event_hw_type_mapping intel_arch_events[] = {/* Index must match CPUID 0x0A.EBX bit vector */ [0 ] = {0x3c ,0x00 ,PERF_COUNT_HW_CPU_CYCLES }, [1 ] = {0xc0 ,0x00 ,PERF_COUNT_HW_INSTRUCTIONS },@@ -37,7 +32,7 @@ static struct kvm_arch_event_perf_mapping { [7] = { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES }, }; -/* mapping between fixed pmc index and arch_events array */ +/* mapping between fixed pmc index and intel_arch_events array */ static int fixed_pmc_events[] = {1 ,0 ,7 };static bool pmc_is_gp (struct kvm_pmc *pmc)@@ -202,16 +197,16 @@ static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select, {int i; -for (i =0 ; i <ARRAY_SIZE (arch_events); i++) -if (arch_events[i].eventsel == event_select - && arch_events[i].unit_mask == unit_mask +for (i =0 ; i <ARRAY_SIZE (intel_arch_events); i++) +if (intel_arch_events[i].eventsel == event_select + && intel_arch_events[i].unit_mask == unit_mask && (pmu->available_event_types & (1 << i)))break ; -if (i ==ARRAY_SIZE (arch_events)) +if (i ==ARRAY_SIZE (intel_arch_events))return PERF_COUNT_HW_MAX ; -return arch_events[i].event_type; +return intel_arch_events[i].event_type; }static void reprogram_gp_counter (struct kvm_pmc *pmc,u64 eventsel)@@ -265,7 +260,7 @@ static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx)return ;
Quoted 7 lines
arch/x86/kvm/pmu.c static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 en_pmi, int idx) · line 266
Quoted 7 lines
arch/x86/kvm/pmu.c +1
-1reprogram_counter (pmc,PERF_TYPE_HARDWARE , - arch_events[fixed_pmc_events[idx]].event_type, + intel_arch_events[fixed_pmc_events[idx]].event_type, !(en &0x2 ),/* exclude user */ !(en &0x1 ),/* exclude kernel */ pmi, false, false);
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
PATCH v5 2/4 KVM: x86/vPMU: Create vPMU interface for VMX and SVM
In reply to Wei HuangThis patch splits existing vPMU code into a common vPMU interface (pmc.c) and Intel specific vPMU code (pmu_intel.c) using the following steps:
- Part of arechitectural vPMU code is extracted and moved to pmu_intel.c file. They are hooked up with the newly-defined intel_pmu_ops, which will be called from pmu interface;
- Create a dummy pmu_amd.c file for AMD SVM with empty functions;
All architectural vPMU functions are now called via PMU function dispatcher. This function dispatcher is defined in kvm_x86_ops->pmu_ops, which is initialized by sub-arch. Also note that Intel and AMD modules are now generated by combinig their corresponding arch files (vmx.c/svm.c) and pmu files (pmu_intel.c/pmu_amd.c).
--- arch/x86/include/asm/kvm_host.h | 14 +- arch/x86/kvm/Makefile | 4 +- arch/x86/kvm/cpuid.c | 3 +- arch/x86/kvm/pmu.c | 545 +++++++++++----------------------------- arch/x86/kvm/pmu.h | 96 +++++++ arch/x86/kvm/pmu_amd.c | 97 +++++++ arch/x86/kvm/pmu_intel.c | 360 ++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 3 + arch/x86/kvm/vmx.c | 3 + arch/x86/kvm/x86.c | 19 +- 10 files changed, 716 insertions(+), 428 deletions(-) create mode 100644 arch/x86/kvm/pmu.h create mode 100644 arch/x86/kvm/pmu_amd.c create mode 100644 arch/x86/kvm/pmu_intel.c
arch/x86/include/asm/kvm_host.h +2 -12
@@ -862,6 +862,8 @@ struct kvm_x86_ops {void (*enable_log_dirty_pt_masked)(struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t offset,unsigned long mask); +/* pmu operations of sub-arch */ +const struct kvm_pmu_ops *pmu_ops; };struct kvm_arch_async_pf {
@@ -1204,18 +1206,6 @@ 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,struct msr_data *msr_info); -int kvm_pmu_check_pmc (struct kvm_vcpu *vcpu,unsigned pmc); -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); -int __x86_set_memory_region (struct kvm *kvm,const struct kvm_userspace_memory_region *mem); int x86_set_memory_region(struct kvm *kvm,
arch/x86/kvm/Makefile +2 -2
@@ -14,8 +14,8 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += assigned-dev.o iommu.o -kvm-intel-y += vmx.o -kvm-amd-y += svm.o +kvm-intel-y += vmx.o pmu_intel.o +kvm-amd-y += svm.o pmu_amd.o obj-$(CONFIG_KVM) += kvm.o obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
arch/x86/kvm/cpuid.c +2 -1
@@ -24,6 +24,7 @@#include "lapic.h" #include "mmu.h" #include "trace.h" +#include "pmu.h" static u32 xstate_required_size (u64 xstate_bv,bool compacted) {
@@ -111,7 +112,7 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu)/* Update physical-address width */ vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); -kvm_pmu_cpuid_update (vcpu); +kvm_pmu_refresh (vcpu);return 0 ; }
arch/x86/kvm/pmu.c +141 -404
688 lines not on this page · raw message
arch/x86/kvm/pmu.h new file +96 -0
96 lines not on this page · raw message
arch/x86/kvm/pmu_amd.c new file +97 -0
97 lines not on this page · raw message
arch/x86/kvm/pmu_intel.c new file +360 -0
360 lines not on this page · raw message
arch/x86/kvm/svm.c +3 -0
@@ -21,6 +21,7 @@#include "kvm_cache_regs.h" #include "x86.h" #include "cpuid.h" +#include "pmu.h" #include <linux/module.h> #include <linux/mod_devicetable.h>
@@ -4453,6 +4454,8 @@ static struct kvm_x86_ops svm_x86_ops = { .handle_external_intr = svm_handle_external_intr, .sched_in = svm_sched_in, + + .pmu_ops = &amd_pmu_ops, };static int __init svm_init (void )
arch/x86/kvm/vmx.c +3 -0
@@ -48,6 +48,7 @@#include <asm/apic.h> #include "trace.h" +#include "pmu.h" #define __ex (x) __kvm_handle_fault_on_reboot(x)#define __ex_clear (x, reg) \
@@ -10407,6 +10408,8 @@ static struct kvm_x86_ops vmx_x86_ops = { .slot_disable_log_dirty = vmx_slot_disable_log_dirty, .flush_log_dirty = vmx_flush_log_dirty, .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, + + .pmu_ops = &intel_pmu_ops, };static int __init vmx_init (void )
arch/x86/kvm/x86.c +10 -9
@@ -28,6 +28,7 @@#include "x86.h" #include "cpuid.h" #include "assigned-dev.h" +#include "pmu.h" #include <linux/clocksource.h> #include <linux/interrupt.h>
@@ -910,7 +911,7 @@ bool kvm_rdpmc(struct kvm_vcpu *vcpu)u64 data;int err; - err =kvm_pmu_read_pmc (vcpu, ecx, &data); + err =kvm_pmu_rdpmc (vcpu, ecx, &data);if (err)return err;kvm_register_write (vcpu,VCPU_REGS_RAX , (u32 )data);
@@ -2401,7 +2402,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) pr = true;case MSR_P6_EVNTSEL0 :case MSR_P6_EVNTSEL1 : -if (kvm_pmu_msr (vcpu, msr)) +if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info);if (pr || data !=0 )
@@ -2447,7 +2448,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)default :if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))return xen_hvm_config (vcpu, data); -if (kvm_pmu_msr (vcpu, msr)) +if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info);if (!ignore_msrs) { vcpu_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
@@ -2641,7 +2642,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_P6_PERFCTR1 :case MSR_P6_EVNTSEL0 :case MSR_P6_EVNTSEL1 : -if (kvm_pmu_msr (vcpu, msr_info->index)) +if (kvm_pmu_is_valid_msr (vcpu, msr_info->index))return kvm_pmu_get_msr (vcpu, msr_info->index, &msr_info->data); msr_info->data =0 ;break ;
@@ -2769,7 +2770,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = vcpu->arch.osvw.status;break ;default : -if (kvm_pmu_msr (vcpu, msr_info->index)) +if (kvm_pmu_is_valid_msr (vcpu, msr_info->index))return kvm_pmu_get_msr (vcpu, msr_info->index, &msr_info->data);if (!ignore_msrs) {vcpu_unimpl (vcpu,"unhandled rdmsr: 0x%x\n" , msr_info->index);
@@ -5174,13 +5175,13 @@ static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)static int emulator_check_pmc (struct x86_emulate_ctxt *ctxt,u32 pmc) { -return kvm_pmu_check_pmc (emul_to_vcpu (ctxt), pmc); +return kvm_pmu_is_valid_msr_idx (emul_to_vcpu (ctxt), pmc); }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); +return kvm_pmu_rdpmc (emul_to_vcpu (ctxt), pmc, pdata); }static void emulator_halt (struct x86_emulate_ctxt *ctxt)
@@ -6750,9 +6751,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_NMI , vcpu))process_nmi (vcpu);if (kvm_check_request (KVM_REQ_PMU , vcpu)) -kvm_handle_pmu_event (vcpu); +kvm_pmu_handle_event (vcpu);if (kvm_check_request (KVM_REQ_PMI , vcpu)) -kvm_deliver_pmi (vcpu); +kvm_pmu_deliver_pmi (vcpu);if (kvm_check_request (KVM_REQ_SCAN_IOAPIC , vcpu))vcpu_scan_ioapic (vcpu);if (kvm_check_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu))
Signature · 1 line
1.8.3.1
Re: [PATCH V5 2/4] KVM: x86/vPMU: Create vPMU interface for VMX and SVM
Quoted 8 lines · Wei Huang
This patch splits existing vPMU code into a common vPMU interface (
pmc.c) and Intel specific vPMU code (pmu_intel.c) using the following steps:- Part of arechitectural vPMU code is extracted and moved to
pmu_intel.cfile. They are hooked up with the newly-definedintel_pmu_ops, which will be called from pmu interface;
- Create a dummypmu_amd.cfile for AMD SVM with empty functions;
Quoted 8 lines
All architectural vPMU functions are now called via PMU function dispatcher. This function dispatcher is defined in
kvm_x86_ops->pmu_ops, which is initialized by sub-arch. Also note that Intel and AMD modules are now generated by combinig their corresponding arch files (vmx.c/svm.c) and pmu files (pmu_intel.c/pmu_amd.c).Tested-by Radim Krčmář <[email protected]>Signed-off-by Wei Huang <[email protected]>
Paolo Bonzini
maintainerRe: [PATCH V5 2/4] KVM: x86/vPMU: Create vPMU interface for VMX and SVM
In reply to Wei HuangQuoted 4 lines · Wei Huang
arch/x86/kvm/pmu.c +1
-1-static void trigger_pmi (struct irq_work *irq_work) +static void kvm_pmi_trigger_fn (struct irq_work *irq_work) {
arch/x86/kvm/pmu.c line 54
Quoted 10 lines
arch/x86/kvm/pmu.c +3
-5- struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, - irq_work); - struct kvm_vcpu *vcpu = container_of(pmu, struct kvm_vcpu, - arch.pmu); + struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work); +struct kvm_vcpu *vcpu =pmu_to_vcpu (pmu); -kvm_deliver_pmi (vcpu); +kvm_pmu_deliver_pmi (vcpu); }
On the other hand, this patch really should have been split further.
For example, just looking at this snippet you can see that the introduction of pmu_to_vcpu and the function renames should have been separate from the creation of the vPMU interface.
Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
Re: [PATCH V5 2/4] KVM: x86/vPMU: Create vPMU interface for VMX and SVM
Quoted 20 lines · Paolo Bonzini
Quoted 4 lines · Wei Huang
arch/x86/kvm/pmu.c +1
-1-static void trigger_pmi (struct irq_work *irq_work) +static void kvm_pmi_trigger_fn (struct irq_work *irq_work) {arch/x86/kvm/pmu.c line 54
Quoted 10 lines
arch/x86/kvm/pmu.c +3
-5- struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, - irq_work); - struct kvm_vcpu *vcpu = container_of(pmu, struct kvm_vcpu, - arch.pmu); + struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work); +struct kvm_vcpu *vcpu =pmu_to_vcpu (pmu); -kvm_deliver_pmi (vcpu); +kvm_pmu_deliver_pmi (vcpu); }On the other hand, this patch really should have been split further.
Quoted 3 lines
For example, just looking at this snippet you can see that the introduction of
pmu_to_vcpuand the function renames should have been separate from the creation of the vPMU interface.
Drew Jones brought up in a separate email. I will split it into a smaller pieces, less intrusive in each step.
Thanks,
-Wei
Quoted 3 lines
Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
PATCH v5 3/4 KVM: x86/vPMU: Implement AMD vPMU code for KVM
In reply to Wei HuangThis patch replaces the empty AMD vPMU functions (in pmu_amd.c) with real implementation.
--- arch/x86/kvm/pmu_amd.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 6 deletions(-)
arch/x86/kvm/pmu_amd.c +116 -6
@@ -19,11 +19,33 @@#include "lapic.h" #include "pmu.h" +/* duplicated from amd_perfmon_event_map, K7 and above should work. */ +static struct kvm_event_hw_type_mapping amd_event_mapping[] = { + [0 ] = {0x76 ,0x00 ,PERF_COUNT_HW_CPU_CYCLES }, + [1 ] = {0xc0 ,0x00 ,PERF_COUNT_HW_INSTRUCTIONS }, + [2 ] = {0x80 ,0x00 ,PERF_COUNT_HW_CACHE_REFERENCES }, + [3 ] = {0x81 ,0x00 ,PERF_COUNT_HW_CACHE_MISSES }, + [4 ] = {0xc2 ,0x00 ,PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, + [5 ] = {0xc3 ,0x00 ,PERF_COUNT_HW_BRANCH_MISSES }, + [6 ] = {0xd0 ,0x00 ,PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, + [7 ] = {0xd1 ,0x00 ,PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, +}; +static unsigned amd_find_arch_event (struct kvm_pmu *pmu,u8 event_select,u8 unit_mask) { -return PERF_COUNT_HW_MAX ; +int i; + +for (i =0 ; i <ARRAY_SIZE (amd_event_mapping); i++) +if (amd_event_mapping[i].eventsel == event_select + && amd_event_mapping[i].unit_mask == unit_mask) +break ; + +if (i ==ARRAY_SIZE (amd_event_mapping)) +return PERF_COUNT_HW_MAX ; + +return amd_event_mapping[i].event_type; }/* return PERF_COUNT_HW_MAX as AMD doesn't have fixed events */
@@ -32,53 +54,141 @@ static unsigned amd_find_fixed_event(int idx)return PERF_COUNT_HW_MAX ; } +/* check if a PMC is enabled by comparing it against global_ctrl bits. Because +* AMD CPU doesn't have global_ctrl MSR, all PMCs are enabled (return TRUE). +*/ static bool amd_pmc_enabled (struct kvm_pmc *pmc) { -return false; +return true; }static struct kvm_pmc *amd_pmc_idx_to_pmc (struct kvm_pmu *pmu,int pmc_idx) { -return NULL ; +return get_gp_pmc (pmu,MSR_K7_EVNTSEL0 + pmc_idx,MSR_K7_EVNTSEL0 ); }/* returns 0 if idx's corresponding MSR exists; otherwise returns 1. */ static int amd_is_valid_msr_idx (struct kvm_vcpu *vcpu,unsigned idx) { -return 1 ; +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + + idx &= ~(3u <<30 ); + +return (idx >= pmu->nr_arch_gp_counters); }/* idx is the ECX register of RDPMC instruction */ static struct kvm_pmc *amd_msr_idx_to_pmc (struct kvm_vcpu *vcpu,unsigned idx) { -return NULL ; +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct kvm_pmc *counters; + + idx &= ~(3u <<30 ); +if (idx >= pmu->nr_arch_gp_counters) +return NULL ; + counters = pmu->gp_counters; + +return &counters[idx]; }static bool amd_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr) { -return false; +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +int ret = false; + + ret =get_gp_pmc (pmu, msr,MSR_K7_PERFCTR0 ) || +get_gp_pmc (pmu, msr,MSR_K7_EVNTSEL0 ); + +return ret; }static int amd_pmu_get_msr (struct kvm_vcpu *vcpu,u32 msr,u64 *data) { +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct kvm_pmc *pmc; + +/* MSR_K7_PERFCTRn */ + pmc =get_gp_pmc (pmu, msr,MSR_K7_PERFCTR0 ); +if (pmc) { + *data =pmc_read_counter (pmc); +return 0 ; + } +/* MSR_K7_EVNTSELn */ + pmc =get_gp_pmc (pmu, msr,MSR_K7_EVNTSEL0 ); +if (pmc) { + *data = pmc->eventsel; +return 0 ; + } +return 1 ; }static int amd_pmu_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) { +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +struct kvm_pmc *pmc; +u32 msr = msr_info->index; +u64 data = msr_info->data; + +/* MSR_K7_PERFCTRn */ + pmc =get_gp_pmc (pmu, msr,MSR_K7_PERFCTR0 ); +if (pmc) { +if (!msr_info->host_initiated) + data = (s64 )data; + pmc->counter += data -pmc_read_counter (pmc); +return 0 ; + } +/* MSR_K7_EVNTSELn */ + pmc =get_gp_pmc (pmu, msr,MSR_K7_EVNTSEL0 ); +if (pmc) { +if (data == pmc->eventsel) +return 0 ; +if (!(data & pmu->reserved_bits)) { +reprogram_gp_counter (pmc, data); +return 0 ; + } + } +return 1 ; }static void amd_pmu_refresh (struct kvm_vcpu *vcpu) { +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + + pmu->nr_arch_gp_counters =AMD64_NUM_COUNTERS ; + pmu->counter_bitmask[KVM_PMC_GP ] = ((u64 )1 <<48 ) -1 ; + pmu->reserved_bits =0xffffffff00200000ull ; +/* not applicable to AMD; but clean them to prevent any fall out */ + pmu->counter_bitmask[KVM_PMC_FIXED ] =0 ; + pmu->nr_arch_fixed_counters =0 ; + pmu->version =0 ; + pmu->global_status =0 ; }static void amd_pmu_init (struct kvm_vcpu *vcpu) { +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +int i; + +for (i =0 ; i <AMD64_NUM_COUNTERS ; i++) { + pmu->gp_counters[i].type =KVM_PMC_GP ; + pmu->gp_counters[i].vcpu = vcpu; + pmu->gp_counters[i].idx = i; + } }static void amd_pmu_reset (struct kvm_vcpu *vcpu) { +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); +int i; + +for (i =0 ; i <AMD64_NUM_COUNTERS ; i++) { +struct kvm_pmc *pmc = &pmu->gp_counters[i]; + +pmc_stop_counter (pmc); + pmc->counter = pmc->eventsel =0 ; + } }struct kvm_pmu_ops amd_pmu_ops = {
Signature · 1 line
1.8.3.1
PATCH v5 4/4 KVM: x86/vPMU: Enable PMU handling for AMD PERFCTRn and EVNTSELn MSRs
In reply to Wei HuangThis patch enables AMD guest VM to access (R/W) PMU related MSRs, which include PERFCTR[0..3] and EVNTSEL[0..3].
--- arch/x86/kvm/x86.c | 51 +++++++++------------------------------------------ 1 file changed, 9 insertions(+), 42 deletions(-)
arch/x86/kvm/x86.c +9 -42
@@ -2372,36 +2372,11 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:return set_msr_mce (vcpu, msr, data); -/* Performance counters are not protected by a CPUID bit, -* so we should check all of them in the generic path for the sake of -* cross vendor migration. -* Writing a zero into the event select MSRs disables them, -* which we perfectly emulate ;-). Any other value should be at least -* reported, some guests depend on them. -*/ -case MSR_K7_EVNTSEL0 : -case MSR_K7_EVNTSEL1 : -case MSR_K7_EVNTSEL2 : -case MSR_K7_EVNTSEL3 : -if (data !=0 ) -vcpu_unimpl (vcpu,"unimplemented perfctr wrmsr: " -"0x%x data 0x%llx\n" , msr, data); -break ; -/* at least RHEL 4 unconditionally writes to the perfctr registers, -* so we ignore writes to make it happy. -*/ -case MSR_K7_PERFCTR0 : -case MSR_K7_PERFCTR1 : -case MSR_K7_PERFCTR2 : -case MSR_K7_PERFCTR3 : -vcpu_unimpl (vcpu,"unimplemented perfctr wrmsr: " -"0x%x data 0x%llx\n" , msr, data); -break ; -case MSR_P6_PERFCTR0 : -case MSR_P6_PERFCTR1 : - pr = true; -case MSR_P6_EVNTSEL0 : -case MSR_P6_EVNTSEL1 : + case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: + case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: + pr = true;/* fall through */ + case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: + case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info);
@@ -2624,24 +2599,16 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)case MSR_K8_SYSCFG :case MSR_K7_HWCR :case MSR_VM_HSAVE_PA : -case MSR_K7_EVNTSEL0 : -case MSR_K7_EVNTSEL1 : -case MSR_K7_EVNTSEL2 : -case MSR_K7_EVNTSEL3 : -case MSR_K7_PERFCTR0 : -case MSR_K7_PERFCTR1 : -case MSR_K7_PERFCTR2 : -case MSR_K7_PERFCTR3 :case MSR_K8_INT_PENDING_MSG :case MSR_AMD64_NB_CFG :case MSR_FAM10H_MMIO_CONF_BASE :case MSR_AMD64_BU_CFG2 : msr_info->data =0 ;break ; -case MSR_P6_PERFCTR0 : -case MSR_P6_PERFCTR1 : -case MSR_P6_EVNTSEL0 : -case MSR_P6_EVNTSEL1 : + case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: + case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: + case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: + case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:if (kvm_pmu_is_valid_msr (vcpu, msr_info->index))return kvm_pmu_get_msr (vcpu, msr_info->index, &msr_info->data); msr_info->data =0 ;
Signature · 1 line
1.8.3.1
Quoted 7 lines · Wei Huang
This patchset is directlyh applicable on kvm.git/queue.
V5:
* Remove theget_pmu_opsfromsub_arch; instead define pmu dispatcher inkvm_x86_ops->pmu_ops. The dispatcher is initialized in sub-arch. The PMU interface functions are changed accordingly (suggested by Joerg Rodel).
Tested it again, works like a charm on my AMD system :) So for all patches again:
Thanks,
Joerg
Paolo Bonzini
maintainerWhile the code looks great, the splitting of patches 1 and 2 left something to be desired. I've redone the split altogether and I'll place it soon in kvm/queue because I needed to do it myself to figure out some suggestions; and after 4 hours it would have been pointless for you to do the work again. :) Nevertheless, do take some time to review the single patches in order to understand the point of splitting the patches this way.
Here is the new structuring of the patch series:
* KVM: x86/vPMU: rename a few PMU functions
* KVM: x86/vPMU: introduce pmu.h header
* KVM: x86/vPMU: use the new macros to go between PMC, PMU and VCPU
* KVM: x86/vPMU: whitespace and stylistic adjustments in PMU code
* KVM: x86/vPMU: reorder PMU functions
* KVM: x86/vPMU: introduce kvm_pmu_msr_idx_to_pmc
* KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch
* KVM: x86/vPMU: Implement AMD vPMU code for KVM
* KVM: x86/vPMU: Enable PMU handling for AMD PERFCTRn and EVNTSELn MSRs
The code changes compared to your patches are very minor, mostly undoing parts of patch 1:
arch/x86/include/asm/kvm_host.h +7 -27
@@ -304,12 +304,6 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; -struct msr_data { -bool host_initiated; -u32 index; -u64 data; -}; -enum pmc_type {KVM_PMC_GP =0 ,KVM_PMC_FIXED ,
@@ -324,12 +318,6 @@ struct kvm_pmc {struct kvm_vcpu *vcpu; }; -struct kvm_event_hw_type_mapping { -u8 eventsel; -u8 unit_mask; -unsigned event_type; -}; -struct kvm_pmu {unsigned nr_arch_gp_counters;unsigned nr_arch_fixed_counters;
@@ -348,21 +336,7 @@ struct kvm_pmu {u64 reprogram_pmi; }; -struct kvm_pmu_ops { -unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, -u8 unit_mask); -unsigned (*find_fixed_event)(int idx); -bool (*pmc_enabled)(struct kvm_pmc *pmc); -struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); -struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); -int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); -bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); -int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); -int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); -void (*refresh)(struct kvm_vcpu *vcpu); -void (*init)(struct kvm_vcpu *vcpu); -void (*reset)(struct kvm_vcpu *vcpu); -}; +struct kvm_pmu_ops ;enum {KVM_DEBUGREG_BP_ENABLED =1 ,
@@ -725,6 +699,12 @@ struct kvm_vcpu_stat {struct x86_instruction_info ; +struct msr_data { +bool host_initiated; +u32 index; +u64 data; +}; +struct kvm_lapic_irq {u32 vector;u16 delivery_mode;
arch/x86/kvm/pmu.c +5 -4
@@ -10,7 +10,9 @@* * This work is licensed under the terms of the GNU GPL, version 2. See * the COPYING file in the top-level directory. +* */ +#include <linux/types.h> #include <linux/kvm_host.h> #include <linux/perf_event.h>
@@ -88,7 +90,7 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event,* NMI context. Do it from irq work instead. */ if (!kvm_is_in_guest ()) -irq_work_queue (&pmc->vcpu->arch.pmu.irq_work); +irq_work_queue (&pmc_to_pmu (pmc)->irq_work);else kvm_make_request (KVM_REQ_PMI , pmc->vcpu); }
@@ -128,7 +130,7 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, } pmc->perf_event = event; -clear_bit (pmc->idx, (unsigned long *)&pmc->vcpu->arch.pmu.reprogram_pmi); +clear_bit (pmc->idx, (unsigned long *)&pmc_to_pmu (pmc)->reprogram_pmi); }void reprogram_gp_counter (struct kvm_pmc *pmc,u64 eventsel)
@@ -154,7 +156,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) ARCH_PERFMON_EVENTSEL_CMASK | HSW_IN_TX | HSW_IN_TX_CHECKPOINTED))) { - config = kvm_x86_ops->pmu_ops->find_arch_event (&pmc->vcpu->arch.pmu, + config = kvm_x86_ops->pmu_ops->find_arch_event (pmc_to_pmu (pmc), event_select, unit_mask);if (config !=PERF_COUNT_HW_MAX )
@@ -305,4 +307,3 @@ void kvm_pmu_destroy(struct kvm_vcpu *vcpu) {kvm_pmu_reset (vcpu); } -
arch/x86/kvm/pmu.h +24 -2
@@ -5,9 +5,31 @@#define pmu_to_vcpu (pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))#define pmc_to_pmu (pmc) (&(pmc)->vcpu->arch.pmu) -/* retrieve the control fields of IA32_FIXED_CTR_CTRL */ +/* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */ #define fixed_ctrl_field (ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf) +struct kvm_event_hw_type_mapping { +u8 eventsel; +u8 unit_mask; +unsigned event_type; +}; + +struct kvm_pmu_ops { +unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, +u8 unit_mask); +unsigned (*find_fixed_event)(int idx); +bool (*pmc_is_enabled)(struct kvm_pmc *pmc); +struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); +struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); +int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); +bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); +int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); +int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); +void (*refresh)(struct kvm_vcpu *vcpu); +void (*init)(struct kvm_vcpu *vcpu); +void (*reset)(struct kvm_vcpu *vcpu); +}; +static inline u64 pmc_bitmask (struct kvm_pmc *pmc) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);
@@ -48,7 +70,7 @@ static inline bool pmc_is_fixed(struct kvm_pmc *pmc)static inline bool pmc_is_enabled (struct kvm_pmc *pmc) { -return kvm_x86_ops->pmu_ops->pmc_enabled (pmc); +return kvm_x86_ops->pmu_ops->pmc_is_enabled (pmc); }/* returns general purpose PMC with the specified MSR. Note that it can be
arch/x86/kvm/pmu_amd.c +2 -2
@@ -57,7 +57,7 @@ static unsigned amd_find_fixed_event(int idx)/* check if a PMC is enabled by comparing it against global_ctrl bits. Because * AMD CPU doesn't have global_ctrl MSR, all PMCs are enabled (return TRUE). */ -static bool amd_pmc_enabled (struct kvm_pmc *pmc) +static bool amd_pmc_is_enabled (struct kvm_pmc *pmc) {return true; }
@@ -194,7 +194,7 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu)struct kvm_pmu_ops amd_pmu_ops = { .find_arch_event = amd_find_arch_event, .find_fixed_event = amd_find_fixed_event, - .pmc_enabled = amd_pmc_enabled, + .pmc_is_enabled = amd_pmc_is_enabled, .pmc_idx_to_pmc = amd_pmc_idx_to_pmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc, .is_valid_msr_idx = amd_is_valid_msr_idx,
arch/x86/kvm/pmu_intel.c +3 -5
@@ -63,9 +63,8 @@ static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) pmu->global_ctrl = data; -for_each_set_bit (bit, (unsigned long *)&diff,X86_PMC_IDX_MAX ) { +for_each_set_bit (bit, (unsigned long *)&diff,X86_PMC_IDX_MAX )reprogram_counter (pmu, bit); - } } static unsigned intel_find_arch_event(struct kvm_pmu *pmu,
@@ -88,7 +87,6 @@ static unsigned intel_find_arch_event(struct kvm_pmu *pmu,static unsigned intel_find_fixed_event (int idx) { -if (idx >=ARRAY_SIZE (fixed_pmc_events))return PERF_COUNT_HW_MAX ;
@@ -96,7 +94,7 @@ static unsigned intel_find_fixed_event(int idx) }/* check if a PMC is enabled by comparising it with globl_ctrl bits. */ -static bool intel_pmc_enabled (struct kvm_pmc *pmc) +static bool intel_pmc_is_enabled (struct kvm_pmc *pmc) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);
@@ -347,7 +345,7 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)struct kvm_pmu_ops intel_pmu_ops = { .find_arch_event = intel_find_arch_event, .find_fixed_event = intel_find_fixed_event, - .pmc_enabled = intel_pmc_enabled, + .pmc_is_enabled = intel_pmc_is_enabled, .pmc_idx_to_pmc = intel_pmc_idx_to_pmc, .msr_idx_to_pmc = intel_msr_idx_to_pmc, .is_valid_msr_idx = intel_is_valid_msr_idx,
Note how, by splitting patches, I was able to find:
* some coding style violations (global_ctrl_changed, intel_find_fixed_event)
* a function naming inconsistency (pmc_is_enabled calls pmu_ops->pmc_enabled)
* some missed conversions to pmc_to_pmu
* the unnecessary changes in patch 1 I already mentioned
Interestingly, after the split git does not show anymore the "big" patch as rewriting pmu.c; the diffstat for that file is a pretty tame
1 file changed, 47 insertions(+), 336 deletions(-)
where most of the insertions are the nice new header comment.
The trick is to treat these kind of style changes/refactorings as a warning sign that you should be committing something soon. You can develop a git workflow, based for example on "git add -p" and possibly "git stash", that lets you commit them quickly without getting out of your previous line of thoughts. Just get them out of the way, you can later group them or otherwise reorder them as you see fit.
For other examples of splitting patches, check out my SMM series and Xiao's patches in kvm/next.
Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
I understand that the original patch2 was huge. Thanks for taking care of it (and the tips). I reviewed the diff below and they are reasonable.
-Wei
Quoted 272 lines · Paolo Bonzini
While the code looks great, the splitting of patches 1 and 2 left something to be desired. I've redone the split altogether and I'll place it soon in kvm/queue because I needed to do it myself to figure out some suggestions; and after 4 hours it would have been pointless for you to do the work again. :) Nevertheless, do take some time to review the single patches in order to understand the point of splitting the patches this way.
Here is the new structuring of the patch series:
* KVM: x86/vPMU: rename a few PMU functions
* KVM: x86/vPMU: introducepmu.hheader
* KVM: x86/vPMU: use the new macros to go between PMC, PMU and VCPU
* KVM: x86/vPMU: whitespace and stylistic adjustments in PMU code
* KVM: x86/vPMU: reorder PMU functions
* KVM: x86/vPMU: introducekvm_pmu_msr_idx_to_pmc
* KVM: x86/vPMU: Definekvm_pmu_opsto support vPMU function dispatch
* KVM: x86/vPMU: Implement AMD vPMU code for KVM
* KVM: x86/vPMU: Enable PMU handling for AMD PERFCTRn and EVNTSELn MSRsThe code changes compared to your patches are very minor, mostly undoing parts of patch 1:
arch/x86/include/asm/kvm_host.h +7
-27@@ -304,12 +304,6 @@ struct kvm_mmu {u64 pdptrs[4 ];/* pae */ }; -struct msr_data { -bool host_initiated; -u32 index; -u64 data; -}; -enum pmc_type {KVM_PMC_GP =0 ,KVM_PMC_FIXED ,@@ -324,12 +318,6 @@ struct kvm_pmc {struct kvm_vcpu *vcpu; }; -struct kvm_event_hw_type_mapping { -u8 eventsel; -u8 unit_mask; -unsigned event_type; -}; -struct kvm_pmu {unsigned nr_arch_gp_counters;unsigned nr_arch_fixed_counters;@@ -348,21 +336,7 @@ struct kvm_pmu {u64 reprogram_pmi; }; -struct kvm_pmu_ops { -unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, -u8 unit_mask); -unsigned (*find_fixed_event)(int idx); -bool (*pmc_enabled)(struct kvm_pmc *pmc); -struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); -struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); -int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); -bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); -int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); -int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); -void (*refresh)(struct kvm_vcpu *vcpu); -void (*init)(struct kvm_vcpu *vcpu); -void (*reset)(struct kvm_vcpu *vcpu); -}; +struct kvm_pmu_ops ;enum {KVM_DEBUGREG_BP_ENABLED =1 ,@@ -725,6 +699,12 @@ struct kvm_vcpu_stat {struct x86_instruction_info ; +struct msr_data { +bool host_initiated; +u32 index; +u64 data; +}; +struct kvm_lapic_irq {u32 vector;u16 delivery_mode;arch/x86/kvm/pmu.c +5
-4@@ -10,7 +10,9 @@* * This work is licensed under the terms of the GNU GPL, version 2. See * the COPYING file in the top-level directory. +* */ +#include <linux/types.h> #include <linux/kvm_host.h> #include <linux/perf_event.h> @@ -88,7 +90,7 @@ static void kvm_perf_overflow_intr(struct perf_event *perf_event,* NMI context. Do it from irq work instead. */ if (!kvm_is_in_guest ()) -irq_work_queue (&pmc->vcpu->arch.pmu.irq_work); +irq_work_queue (&pmc_to_pmu (pmc)->irq_work);else kvm_make_request (KVM_REQ_PMI , pmc->vcpu); }@@ -128,7 +130,7 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, } pmc->perf_event = event; -clear_bit (pmc->idx, (unsigned long *)&pmc->vcpu->arch.pmu.reprogram_pmi); +clear_bit (pmc->idx, (unsigned long *)&pmc_to_pmu (pmc)->reprogram_pmi); }void reprogram_gp_counter (struct kvm_pmc *pmc,u64 eventsel)@@ -154,7 +156,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) ARCH_PERFMON_EVENTSEL_CMASK | HSW_IN_TX | HSW_IN_TX_CHECKPOINTED))) { - config = kvm_x86_ops->pmu_ops->find_arch_event (&pmc->vcpu->arch.pmu, + config = kvm_x86_ops->pmu_ops->find_arch_event (pmc_to_pmu (pmc), event_select, unit_mask);if (config !=PERF_COUNT_HW_MAX )@@ -305,4 +307,3 @@ void kvm_pmu_destroy(struct kvm_vcpu *vcpu) {kvm_pmu_reset (vcpu); } -arch/x86/kvm/pmu.h +24
-2@@ -5,9 +5,31 @@#define pmu_to_vcpu (pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))#define pmc_to_pmu (pmc) (&(pmc)->vcpu->arch.pmu) -/* retrieve the control fields of IA32_FIXED_CTR_CTRL */ +/* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */ #define fixed_ctrl_field (ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf) +struct kvm_event_hw_type_mapping { +u8 eventsel; +u8 unit_mask; +unsigned event_type; +}; + +struct kvm_pmu_ops { +unsigned (*find_arch_event)(struct kvm_pmu *pmu,u8 event_select, +u8 unit_mask); +unsigned (*find_fixed_event)(int idx); +bool (*pmc_is_enabled)(struct kvm_pmc *pmc); +struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu,int pmc_idx); +struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,unsigned idx); +int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu,unsigned idx); +bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr); +int (*get_msr)(struct kvm_vcpu *vcpu,u32 msr,u64 *data); +int (*set_msr)(struct kvm_vcpu *vcpu,struct msr_data *msr_info); +void (*refresh)(struct kvm_vcpu *vcpu); +void (*init)(struct kvm_vcpu *vcpu); +void (*reset)(struct kvm_vcpu *vcpu); +}; +static inline u64 pmc_bitmask (struct kvm_pmc *pmc) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);@@ -48,7 +70,7 @@ static inline bool pmc_is_fixed(struct kvm_pmc *pmc)static inline bool pmc_is_enabled (struct kvm_pmc *pmc) { -return kvm_x86_ops->pmu_ops->pmc_enabled (pmc); +return kvm_x86_ops->pmu_ops->pmc_is_enabled (pmc); }/* returns general purpose PMC with the specified MSR. Note that it can be arch/x86/kvm/pmu_amd.c +2
-2@@ -57,7 +57,7 @@ static unsigned amd_find_fixed_event(int idx)/* check if a PMC is enabled by comparing it against global_ctrl bits. Because * AMD CPU doesn't have global_ctrl MSR, all PMCs are enabled (return TRUE). */ -static bool amd_pmc_enabled (struct kvm_pmc *pmc) +static bool amd_pmc_is_enabled (struct kvm_pmc *pmc) {return true; }@@ -194,7 +194,7 @@ static void amd_pmu_reset(struct kvm_vcpu *vcpu)struct kvm_pmu_ops amd_pmu_ops = { .find_arch_event = amd_find_arch_event, .find_fixed_event = amd_find_fixed_event, - .pmc_enabled = amd_pmc_enabled, + .pmc_is_enabled = amd_pmc_is_enabled, .pmc_idx_to_pmc = amd_pmc_idx_to_pmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc, .is_valid_msr_idx = amd_is_valid_msr_idx,arch/x86/kvm/pmu_intel.c +3
-5@@ -63,9 +63,8 @@ static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) pmu->global_ctrl = data; -for_each_set_bit (bit, (unsigned long *)&diff,X86_PMC_IDX_MAX ) { +for_each_set_bit (bit, (unsigned long *)&diff,X86_PMC_IDX_MAX )reprogram_counter (pmu, bit); - } } static unsigned intel_find_arch_event(struct kvm_pmu *pmu,@@ -88,7 +87,6 @@ static unsigned intel_find_arch_event(struct kvm_pmu *pmu,static unsigned intel_find_fixed_event (int idx) { -if (idx >=ARRAY_SIZE (fixed_pmc_events))return PERF_COUNT_HW_MAX ;@@ -96,7 +94,7 @@ static unsigned intel_find_fixed_event(int idx) }/* check if a PMC is enabled by comparising it with globl_ctrl bits. */ -static bool intel_pmc_enabled (struct kvm_pmc *pmc) +static bool intel_pmc_is_enabled (struct kvm_pmc *pmc) {struct kvm_pmu *pmu =pmc_to_pmu (pmc);@@ -347,7 +345,7 @@ static void intel_pmu_reset(struct kvm_vcpu *vcpu)struct kvm_pmu_ops intel_pmu_ops = { .find_arch_event = intel_find_arch_event, .find_fixed_event = intel_find_fixed_event, - .pmc_enabled = intel_pmc_enabled, + .pmc_is_enabled = intel_pmc_is_enabled, .pmc_idx_to_pmc = intel_pmc_idx_to_pmc, .msr_idx_to_pmc = intel_msr_idx_to_pmc, .is_valid_msr_idx = intel_is_valid_msr_idx,Note how, by splitting patches, I was able to find:
* some coding style violations (
global_ctrl_changed,intel_find_fixed_event)* a function naming inconsistency (
pmc_is_enabledcallspmu_ops->pmc_enabled)* some missed conversions to
pmc_to_pmu* the unnecessary changes in patch 1 I already mentioned
Interestingly, after the split git does not show anymore the "big" patch as rewriting
pmu.c; the diffstat for that file is a pretty tame1 file changed, 47 insertions(+), 336 deletions(-)where most of the insertions are the nice new header comment.
The trick is to treat these kind of style changes/refactorings as a warning sign that you should be committing something soon. You can develop a git workflow, based for example on "git add -p" and possibly "git stash", that lets you commit them quickly without getting out of your previous line of thoughts. Just get them out of the way, you can later group them or otherwise reorder them as you see fit.
Quoted 5 lines
For other examples of splitting patches, check out my SMM series and Xiao's patches in kvm/next.
Paolo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in