KVM: x86: Provide a capability to disable APERF/MPERF read intercepts
Subsystems (5)
v3 cover KVM: x86: Provide a capability to disable APERF/MPERF read intercepts
Allow guest reads of IA32_APERF and IA32_MPERF, so that it can determine the effective frequency multiplier for the physical LPU.
Commit b51700632e0e ("KVM: X86: Provide a capability to disable cstate msr read intercepts") allowed the userspace VMM to grant a guest read access to four core C-state residency MSRs. Do the same for IA32_APERF and IA32_MPERF.
While this isn't sufficient to claim support for CPUID.6:ECX.APERFMPERF[bit 0], it may suffice in a sufficiently restricted environment (i.e. vCPUs pinned to LPUs, no TSC multiplier, and no suspend/resume).
v1 -> v2: Add {IA32_APERF,IA32_MPERF} to vmx_possible_passthrough_msrs[] v2 -> v3: Add a selftest
Jim Mattson (2):
KVM: x86: Provide a capability to disable APERF/MPERF read intercepts
KVM: selftests: Test behavior of KVM_X86_DISABLE_EXITS_APERFMPERF
Documentation/virt/kvm/api.rst | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 7 + arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/vmx/vmx.c | 6 + arch/x86/kvm/vmx/vmx.h | 2 +- arch/x86/kvm/x86.c | 8 +- arch/x86/kvm/x86.h | 5 + include/uapi/linux/kvm.h | 1 + tools/include/uapi/linux/kvm.h | 4 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/aperfmperf_test.c | 162 ++++++++++++++++++ 12 files changed, 196 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/aperfmperf_test.c
Signature · 1 line
2.49.0.395.g12beb8f557-goog
PATCH v3 1/2 KVM: x86: Provide a capability to disable APERF/MPERF read intercepts
Allow a guest to read the physical IA32_APERF and IA32_MPERF MSRs without interception.
The IA32_APERF and IA32_MPERF MSRs are not virtualized. Writes are not handled at all. The MSR values are not zeroed on vCPU creation, saved on suspend, or restored on resume. No accommodation is made for processor migration or for sharing a logical processor with other tasks. No adjustments are made for non-unit TSC multipliers. The MSRs do not account for time the same way as the comparable PMU events, whether the PMU is virtualized by the traditional emulation method or the new mediated pass-through approach.
Nonetheless, in a properly constrained environment, this capability can be combined with a guest CPUID table that advertises support for CPUID.6:ECX.APERFMPERF[bit 0] to induce a Linux guest to report the effective physical CPU frequency in /proc/cpuinfo. Moreover, there is no performance cost for this capability.
--- Documentation/virt/kvm/api.rst | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/svm.c | 7 +++++++ arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/vmx/vmx.c | 6 ++++++ arch/x86/kvm/vmx/vmx.h | 2 +- arch/x86/kvm/x86.c | 8 +++++++- arch/x86/kvm/x86.h | 5 +++++ include/uapi/linux/kvm.h | 1 + tools/include/uapi/linux/kvm.h | 4 +++- 10 files changed, 33 insertions(+), 4 deletions(-)
Documentation/virt/kvm/api.rst +1 -0
@@ -7684,6 +7684,7 @@ Valid bits in args[0] are:: #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) + #define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4) Enabling this capability on a VM provides userspace with a way to no longer intercept some instructions for improved latency in some
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1381,6 +1381,7 @@ struct kvm_arch {bool hlt_in_guest;bool pause_in_guest;bool cstate_in_guest; +bool aperfmperf_in_guest;unsigned long irq_sources_bitmap;s64 kvmclock_offset;
arch/x86/kvm/svm/svm.c +7 -0
@@ -111,6 +111,8 @@ static const struct svm_direct_access_msrs { { .index = MSR_IA32_CR_PAT, .always = false }, { .index = MSR_AMD64_SEV_ES_GHCB, .always = true }, { .index = MSR_TSC_AUX, .always = false }, + { .index = MSR_IA32_APERF, .always = false }, + { .index = MSR_IA32_MPERF, .always = false }, { .index = X2APIC_MSR(APIC_ID), .always = false }, { .index = X2APIC_MSR(APIC_LVR), .always = false }, { .index = X2APIC_MSR(APIC_TASKPRI), .always = false },
@@ -1359,6 +1361,11 @@ static void init_vmcb(struct kvm_vcpu *vcpu)if (boot_cpu_has (X86_FEATURE_V_SPEC_CTRL ))set_msr_interception (vcpu, svm->msrpm,MSR_IA32_SPEC_CTRL ,1 ,1 ); +if (kvm_aperfmperf_in_guest (vcpu->kvm)) { +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_APERF ,1 ,0 ); +set_msr_interception (vcpu, svm->msrpm,MSR_IA32_MPERF ,1 ,0 ); + } +if (kvm_vcpu_apicv_active (vcpu))avic_init_vmcb (svm, vmcb);
arch/x86/kvm/svm/svm.h +1 -1
@@ -44,7 +44,7 @@ static inline struct page *__sme_pa_to_page(unsigned long pa)#define IOPM_SIZE PAGE_SIZE * 3#define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 48 +#define MAX_DIRECT_ACCESS_MSRS 50#define MSRPM_OFFSETS 32extern u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;extern bool npt_enabled;
arch/x86/kvm/vmx/vmx.c +6 -0
@@ -186,6 +186,8 @@ static u32 vmx_possible_passthrough_msrs[MAX_POSSIBLE_PASSTHROUGH_MSRS] = {MSR_CORE_C3_RESIDENCY ,MSR_CORE_C6_RESIDENCY ,MSR_CORE_C7_RESIDENCY , +MSR_IA32_APERF , + MSR_IA32_MPERF, };/*
@@ -7593,6 +7595,10 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)vmx_disable_intercept_for_msr (vcpu,MSR_CORE_C6_RESIDENCY ,MSR_TYPE_R );vmx_disable_intercept_for_msr (vcpu,MSR_CORE_C7_RESIDENCY ,MSR_TYPE_R ); } +if (kvm_aperfmperf_in_guest (vcpu->kvm)) { +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_APERF ,MSR_TYPE_R ); +vmx_disable_intercept_for_msr (vcpu,MSR_IA32_MPERF ,MSR_TYPE_R ); + } vmx->loaded_vmcs = &vmx->vmcs01;
arch/x86/kvm/vmx/vmx.h +1 -1
@@ -356,7 +356,7 @@ struct vcpu_vmx {struct lbr_desc lbr_desc;/* Save desired MSR intercept (read: pass-through) state */ -#define MAX_POSSIBLE_PASSTHROUGH_MSRS 16 +#define MAX_POSSIBLE_PASSTHROUGH_MSRS 18struct { DECLARE_BITMAP(read, MAX_POSSIBLE_PASSTHROUGH_MSRS); DECLARE_BITMAP(write, MAX_POSSIBLE_PASSTHROUGH_MSRS);
arch/x86/kvm/x86.c +7 -1
@@ -4535,6 +4535,9 @@ static u64 kvm_get_allowed_disable_exits(void) {u64 r =KVM_X86_DISABLE_EXITS_PAUSE ; +if (boot_cpu_has (X86_FEATURE_APERFMPERF )) + r |=KVM_X86_DISABLE_EXITS_APERFMPERF ; +if (!mitigate_smt_rsb) { r |=KVM_X86_DISABLE_EXITS_HLT |KVM_X86_DISABLE_EXITS_CSTATE ;
@@ -6543,7 +6546,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,if (!mitigate_smt_rsb &&boot_cpu_has_bug (X86_BUG_SMT_RSB ) &&cpu_smt_possible () && - (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) + (cap->args[0 ] & ~(KVM_X86_DISABLE_EXITS_PAUSE | +KVM_X86_DISABLE_EXITS_APERFMPERF )))pr_warn_once (SMT_RSB_MSG );if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_PAUSE )
@@ -6554,6 +6558,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, kvm->arch.hlt_in_guest = true;if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_CSTATE ) kvm->arch.cstate_in_guest = true; +if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_APERFMPERF ) + kvm->arch.aperfmperf_in_guest = true; r =0 ; disable_exits_unlock:mutex_unlock (&kvm->lock);
arch/x86/kvm/x86.h +5 -0
@@ -488,6 +488,11 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } +static inline bool kvm_aperfmperf_in_guest (struct kvm *kvm) +{ +return kvm->arch.aperfmperf_in_guest; +} +static inline bool kvm_notify_vmexit_enabled (struct kvm *kvm) {return kvm->arch.notify_vmexit_flags &KVM_X86_NOTIFY_VMEXIT_ENABLED ;
include/uapi/linux/kvm.h +1 -0
@@ -617,6 +617,7 @@ struct kvm_ioeventfd {#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) +#define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4)/* for KVM_ENABLE_CAP */ struct kvm_enable_cap {
tools/include/uapi/linux/kvm.h +3 -1
@@ -617,10 +617,12 @@ struct kvm_ioeventfd {#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) +#define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4)#define KVM_X86_DISABLE_VALID_EXITS (KVM_X86_DISABLE_EXITS_MWAIT | \ KVM_X86_DISABLE_EXITS_HLT | \ KVM_X86_DISABLE_EXITS_PAUSE | \ - KVM_X86_DISABLE_EXITS_CSTATE) + KVM_X86_DISABLE_EXITS_CSTATE | \ + KVM_X86_DISABLE_EXITS_APERFMPERF)/* for KVM_ENABLE_CAP */ struct kvm_enable_cap {
Signature · 1 line
2.49.0.395.g12beb8f557-goog
Sean Christopherson
maintainerDocumentation/virt/kvm/api.rst Valid bits in args[0] are:: · line 7687
Quoted 9 lines · Jim Mattson
Documentation/virt/kvm/api.rst +1
-0@@ -7684,6 +7684,7 @@ Valid bits in args[0] are:: #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) + #define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4)
Might be pre-existing with C-states, but I think the documentation needs to call out that userspace is responsible for enumerating APERFMPERF in guest CPUID.
And more importantly, KVM either needs to honor APERFMPERF in each vCPU's CPUID, or the documentation needs to call out that KVM doesn't honor guest CPUID for APERF/MPERF MSRs. I don't have a strong preference either way, but I'm leaning toward having KVM honor CPUID so that if someone copy+pastes the KVM selftest code for the host enabling, it'll do the right thing. On the other hand, KVM doesn't (and shouldn't) fully emulate the MSRs, so I'm a-ok if we ignore CPUID entirely (but document it).
Ignoring CPUID entirely would also make it easier to document that KVM doesn't upport loading/saving C-state or APERF/MPERF MSRs via load/store lists on VM-Enter and VM-Exit. E.g. we can simply say KVM doesn't emulate the MSRs in any capacity, and that the capability disable the exit/interception, no more no less.
Heh, I guess maybe I've talked myself into having KVM ignore guest CPUID :-)
arch/x86/kvm/svm/svm.h static inline struct page *__sme_pa_to_page(unsigned long pa) · line 47
Quoted 10 lines
arch/x86/kvm/svm/svm.h +1
-1@@ -44,7 +44,7 @@ static inline struct page *__sme_pa_to_page(unsigned long pa)#define IOPM_SIZE PAGE_SIZE * 3#define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 48 +#define MAX_DIRECT_ACCESS_MSRS 50
Ugh, I really need to get the MSR interception cleanup series posted.
Quoted 25 lines
arch/x86/kvm/x86.c +5
-1@@ -4535,6 +4535,9 @@ static u64 kvm_get_allowed_disable_exits(void) {u64 r =KVM_X86_DISABLE_EXITS_PAUSE ; +if (boot_cpu_has (X86_FEATURE_APERFMPERF )) + r |=KVM_X86_DISABLE_EXITS_APERFMPERF ; +if (!mitigate_smt_rsb) { r |=KVM_X86_DISABLE_EXITS_HLT |KVM_X86_DISABLE_EXITS_CSTATE ;@@ -6543,7 +6546,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,if (!mitigate_smt_rsb &&boot_cpu_has_bug (X86_BUG_SMT_RSB ) &&cpu_smt_possible () && - (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) + (cap->args[0 ] & ~(KVM_X86_DISABLE_EXITS_PAUSE | +KVM_X86_DISABLE_EXITS_APERFMPERF )))pr_warn_once (SMT_RSB_MSG );if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_PAUSE )
arch/x86/kvm/x86.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 6562
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0@@ -6554,6 +6558,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, kvm->arch.hlt_in_guest = true;if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_CSTATE ) kvm->arch.cstate_in_guest = true; +if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_APERFMPERF ) + kvm->arch.aperfmperf_in_guest = true;
Rather that an ever-growing stream of a booleans, what about tracing the flags as a u64 and providing a builder macro to generate the helper? The latter is a bit gratuitous, but this seems like the type of boilerplate that would be embarassingly easy to screw up without anyone noticing.
Very lightly tested...
--
From: Sean Christopherson <[email protected]>
Date: Mon, 28 Apr 2025 11:35:47 -0700
Subject: [PATCH] KVM: x86: Consolidate DISABLE_EXITS_xxx handling into a
single kvm_arch field
Replace the individual xxx_in_guest booleans with a single field to track exits that have been disabled for a VM. To further cut down on the amount of boilerplate needed for each disabled exit, add a builder macro to generate the accessor.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 5 +---- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 25 ++++++++----------------- arch/x86/kvm/x86.h | 28 +++++++++------------------- 5 files changed, 20 insertions(+), 42 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -4
@@ -1389,10 +1389,7 @@ struct kvm_arch {gpa_t wall_clock; -bool mwait_in_guest; -bool hlt_in_guest; -bool pause_in_guest; -bool cstate_in_guest; +u64 disabled_exits;unsigned long irq_sources_bitmap;s64 kvmclock_offset;
arch/x86/kvm/svm/svm.c +1 -1
@@ -5053,7 +5053,7 @@ static int svm_vm_init(struct kvm *kvm) }if (!pause_filter_count || !pause_filter_thresh) - kvm->arch.pause_in_guest = true; + kvm->arch.disabled_exits |=KVM_X86_DISABLE_EXITS_PAUSE ;if (enable_apicv) {int ret =avic_vm_init (kvm);
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -7613,7 +7613,7 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)int vmx_vm_init (struct kvm *kvm) {if (!ple_gap) - kvm->arch.pause_in_guest = true; + kvm->arch.disabled_exits |=KVM_X86_DISABLE_EXITS_PAUSE ;if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {
arch/x86/kvm/x86.c +8 -17
@@ -6591,27 +6591,18 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;mutex_lock (&kvm->lock); -if (kvm->created_vcpus) - goto disable_exits_unlock; - +if (!kvm->created_vcpus) {#define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerability. " \ "KVM_CAP_X86_DISABLE_EXITS should only be used with SMT disabled or trusted guests." -if (!mitigate_smt_rsb &&boot_cpu_has_bug (X86_BUG_SMT_RSB ) && -cpu_smt_possible () && - (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) -pr_warn_once (SMT_RSB_MSG ); +if (!mitigate_smt_rsb &&cpu_smt_possible () && +boot_cpu_has_bug (X86_BUG_SMT_RSB ) && + (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) +pr_warn_once (SMT_RSB_MSG ); -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_PAUSE ) - kvm->arch.pause_in_guest = true; -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_MWAIT ) - kvm->arch.mwait_in_guest = true; -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_HLT ) - kvm->arch.hlt_in_guest = true; -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_CSTATE ) - kvm->arch.cstate_in_guest = true; - r =0 ; -disable_exits_unlock: + kvm->arch.disabled_exits |= cap->args[0 ]; + r =0 ; + }mutex_unlock (&kvm->lock);break ;case KVM_CAP_MSR_PLATFORM_INFO :
arch/x86/kvm/x86.h +9 -19
@@ -481,25 +481,15 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) __rem; \ }) -static inline bool kvm_mwait_in_guest (struct kvm *kvm) -{ -return kvm->arch.mwait_in_guest; -} - -static inline bool kvm_hlt_in_guest (struct kvm *kvm) -{ -return kvm->arch.hlt_in_guest; -} - -static inline bool kvm_pause_in_guest (struct kvm *kvm) -{ -return kvm->arch.pause_in_guest; -} - -static inline bool kvm_cstate_in_guest (struct kvm *kvm) -{ -return kvm->arch.cstate_in_guest; -} +#define BUILD_DISABLED_EXITS_HELPER (lname, uname) \ +static inline bool kvm_##lname##_in_guest(struct kvm *kvm) \ +{ \ + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_##uname; \ +} +BUILD_DISABLED_EXITS_HELPER (hlt,HLT ); +BUILD_DISABLED_EXITS_HELPER (pause,PAUSE ); +BUILD_DISABLED_EXITS_HELPER (mwait,MWAIT ); +BUILD_DISABLED_EXITS_HELPER (cstate,CSTATE );static inline bool kvm_notify_vmexit_enabled (struct kvm *kvm) {
base-commit: 45eb29140e68ffe8e93a5471006858a018480a45 --
Quoted 23 lines · Sean Christopherson
Documentation/virt/kvm/api.rst Valid bits in args[0] are:: · line 7687
Quoted 9 lines · Jim Mattson
Documentation/virt/kvm/api.rst +1
-0@@ -7684,6 +7684,7 @@ Valid bits in args[0] are:: #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) + #define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4)Might be pre-existing with C-states, but I think the documentation needs to call out that userspace is responsible for enumerating APERFMPERF in guest CPUID.
And more importantly, KVM either needs to honor APERFMPERF in each vCPU's CPUID, or the documentation needs to call out that KVM doesn't honor guest CPUID for APERF/MPERF MSRs. I don't have a strong preference either way, but I'm leaning toward having KVM honor CPUID so that if someone copy+pastes the KVM selftest code for the host enabling, it'll do the right thing. On the other hand, KVM doesn't (and shouldn't) fully emulate the MSRs, so I'm a-ok if we ignore CPUID entirely (but document it).
Quoted 6 lines
Ignoring CPUID entirely would also make it easier to document that KVM doesn't upport loading/saving C-state or APERF/MPERF MSRs via load/store lists on VM-Enter and VM-Exit. E.g. we can simply say KVM doesn't emulate the MSRs in any capacity, and that the capability disable the exit/interception, no more no less.
Heh, I guess maybe I've talked myself into having KVM ignore guest CPUID :-)
I concur. I will add a note to that effect.
Quoted 181 lines
arch/x86/kvm/svm/svm.h static inline struct page *__sme_pa_to_page(unsigned long pa) · line 47
Quoted 10 lines
arch/x86/kvm/svm/svm.h +1
-1@@ -44,7 +44,7 @@ static inline struct page *__sme_pa_to_page(unsigned long pa)#define IOPM_SIZE PAGE_SIZE * 3#define MSRPM_SIZE PAGE_SIZE * 2 -#define MAX_DIRECT_ACCESS_MSRS 48 +#define MAX_DIRECT_ACCESS_MSRS 50Ugh, I really need to get the MSR interception cleanup series posted.
Quoted 25 lines
arch/x86/kvm/x86.c +5
-1@@ -4535,6 +4535,9 @@ static u64 kvm_get_allowed_disable_exits(void) {u64 r =KVM_X86_DISABLE_EXITS_PAUSE ; +if (boot_cpu_has (X86_FEATURE_APERFMPERF )) + r |=KVM_X86_DISABLE_EXITS_APERFMPERF ; +if (!mitigate_smt_rsb) { r |=KVM_X86_DISABLE_EXITS_HLT |KVM_X86_DISABLE_EXITS_CSTATE ;@@ -6543,7 +6546,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,if (!mitigate_smt_rsb &&boot_cpu_has_bug (X86_BUG_SMT_RSB ) &&cpu_smt_possible () && - (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) + (cap->args[0 ] & ~(KVM_X86_DISABLE_EXITS_PAUSE | +KVM_X86_DISABLE_EXITS_APERFMPERF )))pr_warn_once (SMT_RSB_MSG );if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_PAUSE )arch/x86/kvm/x86.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 6562
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0@@ -6554,6 +6558,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, kvm->arch.hlt_in_guest = true;if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_CSTATE ) kvm->arch.cstate_in_guest = true; +if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_APERFMPERF ) + kvm->arch.aperfmperf_in_guest = true;Rather that an ever-growing stream of a booleans, what about tracing the flags as a u64 and providing a builder macro to generate the helper? The latter is a bit gratuitous, but this seems like the type of boilerplate that would be embarassingly easy to screw up without anyone noticing.
Very lightly tested...
--
From: Sean Christopherson <[email protected]>
Date: Mon, 28 Apr 2025 11:35:47 -0700
Subject: [PATCH] KVM: x86: ConsolidateDISABLE_EXITS_xxxhandling into a
singlekvm_archfieldReplace the individual
xxx_in_guestbooleans with a single field to track exits that have been disabled for a VM. To further cut down on the amount of boilerplate needed for each disabled exit, add a builder macro to generate the accessor.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 5 +---- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 25 ++++++++----------------- arch/x86/kvm/x86.h | 28 +++++++++------------------- 5 files changed, 20 insertions(+), 42 deletions(-)arch/x86/include/asm/kvm_host.h +1
-4@@ -1389,10 +1389,7 @@ struct kvm_arch {gpa_t wall_clock; -bool mwait_in_guest; -bool hlt_in_guest; -bool pause_in_guest; -bool cstate_in_guest; +u64 disabled_exits;unsigned long irq_sources_bitmap;s64 kvmclock_offset;arch/x86/kvm/svm/svm.c +1
-1@@ -5053,7 +5053,7 @@ static int svm_vm_init(struct kvm *kvm) }if (!pause_filter_count || !pause_filter_thresh) - kvm->arch.pause_in_guest = true; + kvm->arch.disabled_exits |=KVM_X86_DISABLE_EXITS_PAUSE ;if (enable_apicv) {int ret =avic_vm_init (kvm);arch/x86/kvm/vmx/vmx.c +1
-1@@ -7613,7 +7613,7 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)int vmx_vm_init (struct kvm *kvm) {if (!ple_gap) - kvm->arch.pause_in_guest = true; + kvm->arch.disabled_exits |=KVM_X86_DISABLE_EXITS_PAUSE ;if (boot_cpu_has (X86_BUG_L1TF ) && enable_ept) {switch (l1tf_mitigation) {arch/x86/kvm/x86.c +8
-17@@ -6591,27 +6591,18 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;mutex_lock (&kvm->lock); -if (kvm->created_vcpus) - goto disable_exits_unlock; - +if (!kvm->created_vcpus) {#define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerability. " \ "KVM_CAP_X86_DISABLE_EXITS should only be used with SMT disabled or trusted guests." -if (!mitigate_smt_rsb &&boot_cpu_has_bug (X86_BUG_SMT_RSB ) && -cpu_smt_possible () && - (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) -pr_warn_once (SMT_RSB_MSG ); +if (!mitigate_smt_rsb &&cpu_smt_possible () && +boot_cpu_has_bug (X86_BUG_SMT_RSB ) && + (cap->args[0 ] & ~KVM_X86_DISABLE_EXITS_PAUSE )) +pr_warn_once (SMT_RSB_MSG ); -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_PAUSE ) - kvm->arch.pause_in_guest = true; -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_MWAIT ) - kvm->arch.mwait_in_guest = true; -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_HLT ) - kvm->arch.hlt_in_guest = true; -if (cap->args[0 ] &KVM_X86_DISABLE_EXITS_CSTATE ) - kvm->arch.cstate_in_guest = true; - r =0 ; -disable_exits_unlock: + kvm->arch.disabled_exits |= cap->args[0 ]; + r =0 ; + }mutex_unlock (&kvm->lock);break ;case KVM_CAP_MSR_PLATFORM_INFO :arch/x86/kvm/x86.h +0
-18@@ -481,25 +481,15 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) __rem; \ }) -static inline bool kvm_mwait_in_guest (struct kvm *kvm) -{ -return kvm->arch.mwait_in_guest; -} - -static inline bool kvm_hlt_in_guest (struct kvm *kvm) -{ -return kvm->arch.hlt_in_guest; -} - -static inline bool kvm_pause_in_guest (struct kvm *kvm) -{ -return kvm->arch.pause_in_guest; -} - -static inline bool kvm_cstate_in_guest (struct kvm *kvm) -{ -return kvm->arch.cstate_in_guest;
arch/x86/kvm/x86.h static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) · line 492
Quoted 10 lines
arch/x86/kvm/x86.h +9
-1-} +#define BUILD_DISABLED_EXITS_HELPER (lname, uname) \ +static inline bool kvm_##lname##_in_guest(struct kvm *kvm) \ +{ \ + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_##uname; \ +} +BUILD_DISABLED_EXITS_HELPER (hlt,HLT ); +BUILD_DISABLED_EXITS_HELPER (pause,PAUSE ); +BUILD_DISABLED_EXITS_HELPER (mwait,MWAIT ); +BUILD_DISABLED_EXITS_HELPER (cstate,CSTATE );
The boilerplate is bad, but that's abhorrent.
Quoted 5 lines
arch/x86/kvm/x86.h +0
-0static inline bool kvm_notify_vmexit_enabled (struct kvm *kvm) {base-commit:
45eb29140e68ffe8e93a5471006858a018480a45--
Sean Christopherson
maintainerQuoted 31 lines · Jim Mattson
Quoted 30 lines · Sean Christopherson
arch/x86/kvm/x86.h +3
-19@@ -481,25 +481,15 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) __rem; \ }) -static inline bool kvm_mwait_in_guest (struct kvm *kvm) -{ -return kvm->arch.mwait_in_guest; -} - -static inline bool kvm_hlt_in_guest (struct kvm *kvm) -{ -return kvm->arch.hlt_in_guest; -} - -static inline bool kvm_pause_in_guest (struct kvm *kvm) -{ -return kvm->arch.pause_in_guest; -} - -static inline bool kvm_cstate_in_guest (struct kvm *kvm) -{ -return kvm->arch.cstate_in_guest; -} +#define BUILD_DISABLED_EXITS_HELPER (lname, uname) \ +static inline bool kvm_##lname##_in_guest(struct kvm *kvm) \ +{ \
Quoted 8 lines
arch/x86/kvm/x86.h static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) · line 492
Quoted 6 lines
arch/x86/kvm/x86.h +6
-0+ return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_##uname; \ +} +BUILD_DISABLED_EXITS_HELPER (hlt,HLT ); +BUILD_DISABLED_EXITS_HELPER (pause,PAUSE ); +BUILD_DISABLED_EXITS_HELPER (mwait,MWAIT ); +BUILD_DISABLED_EXITS_HELPER (cstate,CSTATE );The boilerplate is bad, but that's abhorrent.
Assuming it's the macros you hate, keep the "u64 disabled_exits" change but manually code all of the getters?
Quoted 42 lines · Sean Christopherson
Quoted 31 lines · Jim Mattson
Quoted 30 lines · Sean Christopherson
arch/x86/kvm/x86.h +3
-19@@ -481,25 +481,15 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) __rem; \ }) -static inline bool kvm_mwait_in_guest (struct kvm *kvm) -{ -return kvm->arch.mwait_in_guest; -} - -static inline bool kvm_hlt_in_guest (struct kvm *kvm) -{ -return kvm->arch.hlt_in_guest; -} - -static inline bool kvm_pause_in_guest (struct kvm *kvm) -{ -return kvm->arch.pause_in_guest; -} - -static inline bool kvm_cstate_in_guest (struct kvm *kvm) -{ -return kvm->arch.cstate_in_guest; -} +#define BUILD_DISABLED_EXITS_HELPER (lname, uname) \ +static inline bool kvm_##lname##_in_guest(struct kvm *kvm) \ +{ \Quoted 8 lines
arch/x86/kvm/x86.h static inline u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec) · line 492
Quoted 6 lines
arch/x86/kvm/x86.h +6
-0+ return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_##uname; \ +} +BUILD_DISABLED_EXITS_HELPER (hlt,HLT ); +BUILD_DISABLED_EXITS_HELPER (pause,PAUSE ); +BUILD_DISABLED_EXITS_HELPER (mwait,MWAIT ); +BUILD_DISABLED_EXITS_HELPER (cstate,CSTATE );The boilerplate is bad, but that's abhorrent.
Quoted 2 lines
Assuming it's the macros you hate, keep the "u64
disabled_exits" change but manually code all of the getters?
Sounds good. I'll try to get a V4 out this week.
PATCH v3 2/2 KVM: selftests: Test behavior of KVM_X86_DISABLE_EXITS_APERFMPERF
In reply to Jim MattsonFor a VCPU thread pinned to a single LPU, verify that interleaved host and guest reads of IA32_[AM]PERF return strictly increasing values when APERFMPERF exiting is disabled.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/aperfmperf_test.c | 162 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/aperfmperf_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/amx_test TEST_GEN_PROGS_x86 += x86/max_vcpuid_cap_test TEST_GEN_PROGS_x86 += x86/triple_fault_event_test TEST_GEN_PROGS_x86 += x86/recalc_apic_map_test +TEST_GEN_PROGS_x86 += x86/aperfmperf_test TEST_GEN_PROGS_x86 += access_tracking_perf_test TEST_GEN_PROGS_x86 += coalesced_io_test TEST_GEN_PROGS_x86 += demand_paging_test
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.49.0.395.g12beb8f557-goog
Sean Christopherson
maintainerRe: [PATCH v3 2/2] KVM: selftests: Test behavior of KVM_X86_DISABLE_EXITS_APERFMPERF
Quoted 16 lines · Jim Mattson
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +16
-0+#include <fcntl.h> +#include <limits.h> +#include <pthread.h> +#include <sched.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <asm/msr-index.h> + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +#define NUM_ITERATIONS 100 +
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 39
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+static void pin_thread (int cpu) +{ +cpu_set_t cpuset; +int rc; + +CPU_ZERO (&cpuset); +CPU_SET (cpu, &cpuset); + + rc =pthread_setaffinity_np (pthread_self (),sizeof (cpuset), &cpuset); +TEST_ASSERT (rc ==0 ,"%s: Can't set thread affinity" , __func__);
Heh, you copy-pasted this from hardware_disable_test.c, didn't you? :-)
Would it make sense to turn this into a generic API that takes care of the entire sched_getcpu() => pthread_setaffinity_np()? E.g. kvm_pin_task_to_current_cpu(). I suspect there are other (potential) tests that don't care about what CPU they run on, so long as the test is pinned.
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 49
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+} + +static int open_dev_msr (int cpu) +{ +char path[PATH_MAX ]; +int msr_fd; + +snprintf (path,sizeof (path),"/dev/cpu/%d/msr" , cpu); + msr_fd =open (path,O_RDONLY ); +__TEST_REQUIRE (msr_fd >=0 ,"Can't open %s for read" , path);
Please use open_path_or_exit().
Hmm, and I'm planning on posting a small series to add a variant that takes an ENOENT message, and spits out a (hopefully) helpful message for the EACCES case. It would be nice to have this one spit out something like "Is msk.ko loaded?", but I would say don't worry about trying to coordinate anything. Worst case scenario we can add a help message when the dust settles.
Quoted 13 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +13
-0+return msr_fd; +} + +static uint64_t read_dev_msr (int msr_fd,uint32_t msr) +{ +uint64_t data; +ssize_t rc; + + rc =pread (msr_fd, &data,sizeof (data), msr); +TEST_ASSERT (rc ==sizeof (data),"Read of MSR 0x%x failed" , msr); + +return data; +}
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 73
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+ +static void guest_code (void ) +{ +int i; + +for (i =0 ; i <NUM_ITERATIONS ; i++) { +uint64_t aperf =rdmsr (MSR_IA32_APERF ); +uint64_t mperf =rdmsr (MSR_IA32_MPERF ); + +GUEST_SYNC2 (aperf, mperf);
Does the test generate multiple RDMSR per MSR if you do:
GUEST_SYNC2(rdmsr(MSR_IA32_APERF), rdmsr(MSR_IA32_MPERF));
If the code generation comes out
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 84
Quoted 11 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +11
-0+ } + +GUEST_DONE (); +} + +static bool kvm_can_disable_aperfmperf_exits (struct kvm_vm *vm) +{ +int flags =vm_check_cap (vm,KVM_CAP_X86_DISABLE_EXITS ); + +return flags &KVM_X86_DISABLE_EXITS_APERFMPERF ; +}
Please don't add one-off helpers like this, especially when they're the condition for TEST_REQUIRE(). I *want* the gory details if the test is skipped, so that I don't have to go look at the source code to figure out what's missing.
And it's literally more code.
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +8
-0+ +int main (int argc,char *argv[]) +{ +uint64_t host_aperf_before, host_mperf_before; +int cpu =sched_getcpu (); +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +int msr_fd;
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 102
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+int i; + +pin_thread (cpu); + + msr_fd =open_dev_msr (cpu); + +/* +* This test requires a non-standard VM initialization, because +* KVM_ENABLE_CAP cannot be used on a VM file descriptor after +* a VCPU has been created.
Hrm, we should really sort this out. Every test that needs to enable a capability is having to copy+paste this pattern. I don't love the idea of expanding __vm_create_with_one_vcpu(), but there's gotta be a solution that isn't horrible, and anything is better than endly copy paste.
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 106
Quoted 4 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +4
-0+*/ + vm =vm_create (1 ); + +TEST_REQUIRE (kvm_can_disable_aperfmperf_exits (vm));
TEST_REQUIRE(vm_check_cap(vm, KVM_CAP_X86_DISABLE_EXITS) & KVM_X86_DISABLE_EXITS_APERFMPERF);
Quoted 22 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +22
-0+ +vm_enable_cap (vm,KVM_CAP_X86_DISABLE_EXITS , +KVM_X86_DISABLE_EXITS_APERFMPERF ); + + vcpu =vm_vcpu_add (vm,0 , guest_code); + + host_aperf_before =read_dev_msr (msr_fd,MSR_IA32_APERF ); + host_mperf_before =read_dev_msr (msr_fd,MSR_IA32_MPERF ); + +for (i =0 ; i <NUM_ITERATIONS ; i++) { +uint64_t host_aperf_after, host_mperf_after; +uint64_t guest_aperf, guest_mperf; +struct ucall uc; + +vcpu_run (vcpu); +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_IO ); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc);
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 138
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+case UCALL_SYNC : + guest_aperf = uc.args[0 ]; + guest_mperf = uc.args[1 ]; + + host_aperf_after =read_dev_msr (msr_fd,MSR_IA32_APERF ); + host_mperf_after =read_dev_msr (msr_fd,MSR_IA32_MPERF ); + +TEST_ASSERT (host_aperf_before < guest_aperf, +"APERF: host_before (%lu) >= guest (%lu)" , + host_aperf_before, guest_aperf);
Honest question, is decimal really better than hex for these?
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +8
-0+TEST_ASSERT (guest_aperf < host_aperf_after, +"APERF: guest (%lu) >= host_after (%lu)" , + guest_aperf, host_aperf_after); +TEST_ASSERT (host_mperf_before < guest_mperf, +"MPERF: host_before (%lu) >= guest (%lu)" , + host_mperf_before, guest_mperf); + TEST_ASSERT(guest_mperf < host_mperf_after, + "MPERF: guest (%lu) >= host_after (%lu)",
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 156
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+ guest_mperf, host_mperf_after); + + host_aperf_before = host_aperf_after; + host_mperf_before = host_mperf_after; + +break ; + } + } + +TEST_ASSERT_EQ (i,NUM_ITERATIONS );
Why?
Re: [PATCH v3 2/2] KVM: selftests: Test behavior of KVM_X86_DISABLE_EXITS_APERFMPERF
Quoted 22 lines · Sean Christopherson
Quoted 20 lines · Jim Mattson
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +20
-0+#include <fcntl.h> +#include <limits.h> +#include <pthread.h> +#include <sched.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <asm/msr-index.h> + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +#define NUM_ITERATIONS 100 + +static void pin_thread (int cpu) +{ +cpu_set_t cpuset; +int rc;
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 39
Quoted 6 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +6
-0+ +CPU_ZERO (&cpuset); +CPU_SET (cpu, &cpuset); + + rc =pthread_setaffinity_np (pthread_self (),sizeof (cpuset), &cpuset); +TEST_ASSERT (rc ==0 ,"%s: Can't set thread affinity" , __func__);Heh, you copy-pasted this from
hardware_disable_test.c, didn't you? :-)
Probably.
Quoted 4 lines
Would it make sense to turn this into a generic API that takes care of the entire
sched_getcpu()=>pthread_setaffinity_np()? E.g.kvm_pin_task_to_current_cpu(). I suspect there are other (potential) tests that don't care about what CPU they run on, so long as the test is pinned.
Sure.
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +4
-0+} + +static int open_dev_msr (int cpu) +{
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 49
Quoted 6 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +6
-0+char path[PATH_MAX ]; +int msr_fd; + +snprintf (path,sizeof (path),"/dev/cpu/%d/msr" , cpu); + msr_fd =open (path,O_RDONLY ); +__TEST_REQUIRE (msr_fd >=0 ,"Can't open %s for read" , path);Please use
open_path_or_exit().
TIL.
Quoted 32 lines
Hmm, and I'm planning on posting a small series to add a variant that takes an ENOENT message, and spits out a (hopefully) helpful message for the EACCES case. It would be nice to have this one spit out something like "Is msk.ko loaded?", but I would say don't worry about trying to coordinate anything. Worst case scenario we can add a help message when the dust settles.
Quoted 13 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +13
-0+return msr_fd; +} + +static uint64_t read_dev_msr (int msr_fd,uint32_t msr) +{ +uint64_t data; +ssize_t rc; + + rc =pread (msr_fd, &data,sizeof (data), msr); +TEST_ASSERT (rc ==sizeof (data),"Read of MSR 0x%x failed" , msr); + +return data; +}tools/testing/selftests/kvm/x86/aperfmperf_test.c line 73
Quoted 10 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +10
-0+ +static void guest_code (void ) +{ +int i; + +for (i =0 ; i <NUM_ITERATIONS ; i++) { +uint64_t aperf =rdmsr (MSR_IA32_APERF ); +uint64_t mperf =rdmsr (MSR_IA32_MPERF ); + +GUEST_SYNC2 (aperf, mperf);Does the test generate multiple RDMSR per MSR if you do:
Quoted 3 lines
GUEST_SYNC2(rdmsr(MSR_IA32_APERF), rdmsr(MSR_IA32_MPERF));If the code generation comes out
I'll have to check.
Quoted 17 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 84
Quoted 12 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +11
-0+ } + +GUEST_DONE (); +} + +static bool kvm_can_disable_aperfmperf_exits (struct kvm_vm *vm) +{ +int flags =vm_check_cap (vm,KVM_CAP_X86_DISABLE_EXITS ); + +return flags &KVM_X86_DISABLE_EXITS_APERFMPERF ; +}Please don't add one-off helpers like this, especially when they're the condition for
TEST_REQUIRE(). I *want* the gory details if the test is skipped, so that I don't have to go look at the source code to figure out what's missing.
Quoted 1 line
And it's literally more code.
Okay.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +15
-0+ +int main (int argc,char *argv[]) +{ +uint64_t host_aperf_before, host_mperf_before; +int cpu =sched_getcpu (); +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +int msr_fd; +int i; + +pin_thread (cpu); + + msr_fd =open_dev_msr (cpu); + +/*
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 102
Quoted 3 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +3
-0+* This test requires a non-standard VM initialization, because +* KVM_ENABLE_CAP cannot be used on a VM file descriptor after +* a VCPU has been created. Hrm, we should really sort this out. Every test that needs to enable a capability is having to copy+paste this pattern. I don't love the idea of expanding
__vm_create_with_one_vcpu(), but there's gotta be a solution that isn't horrible, and anything is better than endly copy paste.
This is all your fault, I believe. But, I'll see what I can do.
Quoted 33 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 106
Quoted 4 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +4
-0+*/ + vm =vm_create (1 ); + +TEST_REQUIRE (kvm_can_disable_aperfmperf_exits (vm));TEST_REQUIRE(vm_check_cap(vm,
KVM_CAP_X86_DISABLE_EXITS) &
KVM_X86_DISABLE_EXITS_APERFMPERF);Quoted 26 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +26
-0+ +vm_enable_cap (vm,KVM_CAP_X86_DISABLE_EXITS , +KVM_X86_DISABLE_EXITS_APERFMPERF ); + + vcpu =vm_vcpu_add (vm,0 , guest_code); + + host_aperf_before =read_dev_msr (msr_fd,MSR_IA32_APERF ); + host_mperf_before =read_dev_msr (msr_fd,MSR_IA32_MPERF ); + +for (i =0 ; i <NUM_ITERATIONS ; i++) { +uint64_t host_aperf_after, host_mperf_after; +uint64_t guest_aperf, guest_mperf; +struct ucall uc; + +vcpu_run (vcpu); +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_IO ); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +case UCALL_SYNC : + guest_aperf = uc.args[0 ]; + guest_mperf = uc.args[1 ]; +
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 138
Quoted 6 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +6
-0+ host_aperf_after =read_dev_msr (msr_fd,MSR_IA32_APERF ); + host_mperf_after =read_dev_msr (msr_fd,MSR_IA32_MPERF ); + +TEST_ASSERT (host_aperf_before < guest_aperf, +"APERF: host_before (%lu) >= guest (%lu)" , + host_aperf_before, guest_aperf);Honest question, is decimal really better than hex for these?
They are just numbers, so any base should be fine. I guess it depends on which base you're most comfortable with. I could add a command-line parameter.
Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +12
-0+TEST_ASSERT (guest_aperf < host_aperf_after, +"APERF: guest (%lu) >= host_after (%lu)" , + guest_aperf, host_aperf_after); +TEST_ASSERT (host_mperf_before < guest_mperf, +"MPERF: host_before (%lu) >= guest (%lu)" , + host_mperf_before, guest_mperf); +TEST_ASSERT (guest_mperf < host_mperf_after, +"MPERF: guest (%lu) >= host_after (%lu)" , + guest_mperf, host_mperf_after); + + host_aperf_before = host_aperf_after; + host_mperf_before = host_mperf_after;
Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 156
Quoted 6 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +6
-0+ +break ; + } + } + +TEST_ASSERT_EQ (i,NUM_ITERATIONS );Why?
I think this was leftover from a version where it was possible to break out of the loop early. I'll get rid of it.
V4 this week, I hope.
Sean Christopherson
maintainerRe: [PATCH v3 2/2] KVM: selftests: Test behavior of KVM_X86_DISABLE_EXITS_APERFMPERF
Quoted 6 lines · Jim Mattson
Quoted 5 lines · Sean Christopherson
Quoted 4 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +4
-0+/* +* This test requires a non-standard VM initialization, because +* KVM_ENABLE_CAP cannot be used on a VM file descriptor after +* a VCPU has been created.
Quoted 6 lines
Quoted 4 lines
Hrm, we should really sort this out. Every test that needs to enable a capability is having to copy+paste this pattern. I don't love the idea of expanding
__vm_create_with_one_vcpu(), but there's gotta be a solution that isn't horrible, and anything is better than endly copy paste.This is all your fault, I believe. But, I'll see what I can do.
Ha, that it is, both on the KVM and the selftests side.
Unless you already have something clever in hand, just keep what you have. I poked at this a bit today, and came to the conclusion that trying to save two lives of "manual" effort isn't worth the explosion in APIs and complexity. I was thinking that the only additional input would be the capability to enable, but most usage also needs to specify a payload, and this pattern is used in a few places where a selftest does more than toggle a capability.
What I really want is the ability to provide a closure to all of the "create with vCPUs" APIs, e.g.
vm = vm_create_with_one_vcpu(&vcpu, guest_code, magic() {
vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS,
KVM_X86_DISABLE_EXITS_APERFMPERF);
});But even if we managed to make something work, I'm not sure it'd be worth the plumbing.
One thing that would make me less annoyed would be to eliminate the @vcpu_id param, e.g.
static inline struct kvm_vcpu *vm_vcpu_add(struct kvm_vm *vm, void *guest_code)
{
return __vm_vcpu_add(vm, vm->nr_vcpus++, guest_code);}
so that at least this pattern doesn't have '0' hardcoded everywhere. But that's an annoying cleanup due to __vm_vcpu_add() not being a strict superset of vm_vcpu_add(), i.e. would require a lot of churn.
So for this series, just keep the copy+pasted pattern.
Quoted 42 lines
Quoted 33 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 106
Quoted 4 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +4
-0+*/ + vm =vm_create (1 ); + +TEST_REQUIRE (kvm_can_disable_aperfmperf_exits (vm));TEST_REQUIRE(vm_check_cap(vm,
KVM_CAP_X86_DISABLE_EXITS) &
KVM_X86_DISABLE_EXITS_APERFMPERF);Quoted 26 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +26
-0+ +vm_enable_cap (vm,KVM_CAP_X86_DISABLE_EXITS , +KVM_X86_DISABLE_EXITS_APERFMPERF ); + + vcpu =vm_vcpu_add (vm,0 , guest_code); + + host_aperf_before =read_dev_msr (msr_fd,MSR_IA32_APERF ); + host_mperf_before =read_dev_msr (msr_fd,MSR_IA32_MPERF ); + +for (i =0 ; i <NUM_ITERATIONS ; i++) { +uint64_t host_aperf_after, host_mperf_after; +uint64_t guest_aperf, guest_mperf; +struct ucall uc; + +vcpu_run (vcpu); +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_IO ); + +switch (get_ucall (vcpu, &uc)) { +case UCALL_DONE : +break ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +case UCALL_SYNC : + guest_aperf = uc.args[0 ]; + guest_mperf = uc.args[1 ]; +Quoted 8 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c line 138
Quoted 6 lines
tools/testing/selftests/kvm/x86/aperfmperf_test.c new file +6
-0+ host_aperf_after =read_dev_msr (msr_fd,MSR_IA32_APERF ); + host_mperf_after =read_dev_msr (msr_fd,MSR_IA32_MPERF ); + +TEST_ASSERT (host_aperf_before < guest_aperf, +"APERF: host_before (%lu) >= guest (%lu)" , + host_aperf_before, guest_aperf);Honest question, is decimal really better than hex for these?
Quoted 3 lines
They are just numbers, so any base should be fine. I guess it depends on which base you're most comfortable with. I could add a command-line parameter.
Nah, don't bother, pick whatever you like. I was genuinely curious if one format or another made it easier to understand the output.