KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits
Subsystems (5)
v4 cover KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits
v4 of Jim's series adding support for AMD's Host-Only and Guest-Only performance counter eventsel bits in KVM's mediated PMU passthrough implementation.
These bits allow an nSVM-enabled guest to configure performance counters that count only during L1 execution (Host-Only) or only during L2 execution (Guest-Only).
KVM updates the hardware event selector ENABLE bit at the following state transitions to ensure counters only count in the appropriate mode:
- EFER.SVME changes: Enable/disable Guest-Only counters
- Nested VMRUN: Disable Host-Only, enable Guest-Only counters
- Nested VMEXIT: Enable Host-Only, disable Guest-Only counters
v3 -> v4:
- Dropped amd_pmu_set_eventsel_hw(), moved handling of Host-Only/Guest-Only bits to PMU counter reprogramming and funnelled all processing through KVM_REQ_PMU [Sean].
- For this to work, added a per-vendor callback for reprogramming PMU counters.
- Sorta restored the bitmask from v1, except now it's a single bitmask tracking all counters that need to be reprogrammed on nested transitions. The bitmask is used to avoid unnecessary KVM_REQ_PMU requests and only reprogram counters as needed [Jim].
- This is also needed to avoid directling calling vendor code from {enter/leave}_guest_mode() as requested by Sean.
- Added prep patches moving enable_pmu/enable_mediated_pmu and guest_mode helpers to facilitate following changes without circular dependencies and to avoid including new headers from leaf headers.
v3: https://lore.kernel.org/kvm/[email protected]/
Jim Mattson (2):
KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated
PMU
KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only
bits
Yosry Ahmed (4):
KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c
KVM: x86: Move guest_mode helpers to x86.h
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in
SVM
KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM
transitions
arch/x86/include/asm/kvm-x86-pmu-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 6 + arch/x86/include/asm/perf_event.h | 2 + arch/x86/kvm/kvm_cache_regs.h | 23 -- arch/x86/kvm/pmu.c | 12 ++ arch/x86/kvm/pmu.h | 17 ++ arch/x86/kvm/svm/pmu.c | 44 ++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/x86.c | 9 - arch/x86/kvm/x86.h | 31 ++- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../selftests/kvm/include/x86/processor.h | 2 + .../kvm/x86/svm_pmu_host_guest_test.c | 199 ++++++++++++++++++ 14 files changed, 319 insertions(+), 35 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
base-commit: 3d6cdcc8883b5726513d245eef0e91cabfc397f7
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v4 1/6 KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c
The declaration and definition of enable_pmu/enable_mediated_pmu semantically belongs in pmu.h and pmu.c, and more importantly, pmu.h uses enable_mediated_pmu and relies on the caller including x86.h.
There is already precedence for other module params defined outside of x86.c, so move enable_pmu/enable_mediated_pmu to pmu.c.
No functional change intended.
--- arch/x86/kvm/pmu.c | 10 ++++++++++ arch/x86/kvm/pmu.h | 3 +++ arch/x86/kvm/x86.c | 9 --------- arch/x86/kvm/x86.h | 3 --- 4 files changed, 13 insertions(+), 12 deletions(-)
arch/x86/kvm/pmu.c +10 -0
@@ -16,6 +16,7 @@#include <linux/perf_event.h> #include <linux/bsearch.h> #include <linux/sort.h> +#include <linux/moduleparam.h> #include <asm/perf_event.h> #include <asm/cpu_device_id.h> #include "x86.h"
@@ -33,6 +34,15 @@ static struct x86_pmu_capability __read_mostly kvm_host_pmu;struct x86_pmu_capability __read_mostly kvm_pmu_cap;EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_pmu_cap); +/* Enable/disable PMU virtualization */ +bool __read_mostly enable_pmu = true; +EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_pmu); +module_param (enable_pmu, bool,0444 ); + +/* Enable/disabled mediated PMU virtualization. */ +bool __read_mostly enable_mediated_pmu; +EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_mediated_pmu); +struct kvm_pmu_emulated_event_selectors {u64 INSTRUCTIONS_RETIRED;u64 BRANCH_INSTRUCTIONS_RETIRED;
arch/x86/kvm/pmu.h +3 -0
@@ -53,6 +53,9 @@ struct kvm_pmu_ops {const u32 MSR_STRIDE ; }; +extern bool enable_pmu; +extern bool enable_mediated_pmu; +void kvm_pmu_ops_update (const struct kvm_pmu_ops *pmu_ops);void kvm_handle_guest_mediated_pmi (void );
arch/x86/kvm/x86.c +0 -9
@@ -182,15 +182,6 @@ module_param(force_emulation_prefix, int, 0644);int __read_mostly pi_inject_timer =-1 ;module_param (pi_inject_timer, bint,0644 ); -/* Enable/disable PMU virtualization */ -bool __read_mostly enable_pmu = true; -EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_pmu); -module_param (enable_pmu, bool,0444 ); - -/* Enable/disabled mediated PMU virtualization. */ -bool __read_mostly enable_mediated_pmu; -EXPORT_SYMBOL_FOR_KVM_INTERNAL (enable_mediated_pmu); -bool __read_mostly eager_page_split = true;module_param (eager_page_split, bool,0644 );
arch/x86/kvm/x86.h +0 -3
@@ -480,9 +480,6 @@ fastpath_t handle_fastpath_invd(struct kvm_vcpu *vcpu);extern struct kvm_caps kvm_caps;extern struct kvm_host_values kvm_host; -extern bool enable_pmu; -extern bool enable_mediated_pmu; -void kvm_setup_xss_caps (void );/*
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v4 2/6 KVM: x86: Move guest_mode helpers to x86.h
In reply to Yosry AhmedMove enter_guest_mode(), leave_guest_mode(), and is_guest_mode() to x86.h. This is more semantically appropriate to keep kvm_cache_regs.h for register helpers, and more importantly it allows for expanding these helpers without including more headers into kvm_cache_regs.h, keeping it as much as possible a leaf header.
No functional change intended.
--- arch/x86/kvm/kvm_cache_regs.h | 23 ----------------------- arch/x86/kvm/x86.h | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 23 deletions(-)
arch/x86/kvm/kvm_cache_regs.h +0 -23
@@ -223,27 +223,4 @@ static inline u64 kvm_read_edx_eax(struct kvm_vcpu *vcpu) | ((u64)(kvm_rdx_read(vcpu) & -1u) << 32); } -static inline void enter_guest_mode (struct kvm_vcpu *vcpu) -{ - vcpu->arch.hflags |=HF_GUEST_MASK ; - vcpu->stat.guest_mode =1 ; -} - -static inline void leave_guest_mode (struct kvm_vcpu *vcpu) -{ - vcpu->arch.hflags &= ~HF_GUEST_MASK ; - -if (vcpu->arch.load_eoi_exitmap_pending) { - vcpu->arch.load_eoi_exitmap_pending = false; -kvm_make_request (KVM_REQ_LOAD_EOI_EXITMAP , vcpu); - } - - vcpu->stat.guest_mode =0 ; -} - -static inline bool is_guest_mode (struct kvm_vcpu *vcpu) -{ -return vcpu->arch.hflags &HF_GUEST_MASK ; -} -#endif
arch/x86/kvm/x86.h +23 -0
@@ -148,6 +148,29 @@ static inline unsigned int __shrink_ple_window(unsigned int val,void kvm_service_local_tlb_flush_requests (struct kvm_vcpu *vcpu);int kvm_check_nested_events (struct kvm_vcpu *vcpu); +static inline void enter_guest_mode (struct kvm_vcpu *vcpu) +{ + vcpu->arch.hflags |=HF_GUEST_MASK ; + vcpu->stat.guest_mode =1 ; +} + +static inline void leave_guest_mode (struct kvm_vcpu *vcpu) +{ + vcpu->arch.hflags &= ~HF_GUEST_MASK ; + +if (vcpu->arch.load_eoi_exitmap_pending) { + vcpu->arch.load_eoi_exitmap_pending = false; +kvm_make_request (KVM_REQ_LOAD_EOI_EXITMAP , vcpu); + } + + vcpu->stat.guest_mode =0 ; +} + +static inline bool is_guest_mode (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.hflags &HF_GUEST_MASK ; +} +/* Forcibly leave the nested mode in cases like a vCPU reset */ static inline void kvm_leave_nested (struct kvm_vcpu *vcpu) {
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
kernel test robot
build botRe: [PATCH v4 2/6] KVM: x86: Move guest_mode helpers to x86.h
kernel test robot noticed the following build errors:
Hi Yosry,
kernel test robot noticed the following build errors:
[auto build test ERROR on 3d6cdcc8883b5726513d245eef0e91cabfc397f7]
url: https://github.com/intel-lab-lkp/linux/commits/Yosry-Ahmed/KVM-x86-Move-enable_pmu-enable_mediated_pmu-to-pmu-h-and-pmu-c/20260326-191518 base: 3d6cdcc8883b5726513d245eef0e91cabfc397f7 patch link: https://lore.kernel.org/r/20260326031150.3774017-3-yosry%40kernel.org patch subject: [PATCH v4 2/6] KVM: x86: Move guest_mode helpers to x86.h config: x86_64-buildonly-randconfig-004-20260327 (https://download.01.org/0day-ci/archive/20260327/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260327/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from arch/x86/kvm/svm/svm_onhyperv.c:11:
>> arch/x86/kvm/svm/svm.h:520:6: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
520 | if (is_guest_mode(&svm->vcpu))
| ^
arch/x86/kvm/svm/svm.h:578:6: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
578 | if (is_guest_mode(&svm->vcpu) && !nested_vgif_enabled(svm))
| ^
arch/x86/kvm/svm/svm.h:639:6: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
639 | if (is_guest_mode(&svm->vcpu))
| ^
arch/x86/kvm/svm/svm.h:802:9: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
802 | return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK);
| ^
In file included from arch/x86/kvm/svm/svm_onhyperv.c:12:
In file included from arch/x86/kvm/svm/svm_ops.h:7:
>> arch/x86/kvm/x86.h:169:20: error: conflicting types for 'is_guest_mode'
169 | static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
| ^
arch/x86/kvm/svm/svm.h:520:6: note: previous implicit declaration is here
520 | if (is_guest_mode(&svm->vcpu))
| ^5 errors generated.
vim +/is_guest_mode +520 arch/x86/kvm/svm/svm.h
0b97f929831a70 Sean Christopherson 2026-02-18 509
0b97f929831a70 Sean Christopherson 2026-02-18 510 static inline void svm_mark_intercepts_dirty(struct vcpu_svm *svm)
0b97f929831a70 Sean Christopherson 2026-02-18 511 {
0b97f929831a70 Sean Christopherson 2026-02-18 512 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_INTERCEPTS);
0b97f929831a70 Sean Christopherson 2026-02-18 513
0b97f929831a70 Sean Christopherson 2026-02-18 514 /*
0b97f929831a70 Sean Christopherson 2026-02-18 515 * If L2 is active, recalculate the intercepts for vmcb02 to account
0b97f929831a70 Sean Christopherson 2026-02-18 516 * for the changes made to vmcb01. All intercept configuration is done
0b97f929831a70 Sean Christopherson 2026-02-18 517 * for vmcb01 and then propagated to vmcb02 to combine KVM's intercepts
0b97f929831a70 Sean Christopherson 2026-02-18 518 * with L1's intercepts (from the vmcb12 snapshot).
0b97f929831a70 Sean Christopherson 2026-02-18 519 */
0b97f929831a70 Sean Christopherson 2026-02-18 @520 if (is_guest_mode(&svm->vcpu))
0b97f929831a70 Sean Christopherson 2026-02-18 521 nested_vmcb02_recalc_intercepts(svm);
0b97f929831a70 Sean Christopherson 2026-02-18 522 }0b97f929831a70 Sean Christopherson 2026-02-18 523
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v4 2/6] KVM: x86: Move guest_mode helpers to x86.h
Quoted 37 lines · kernel test robot
Hi Yosry,
kernel test robot noticed the following build errors:
[auto build test ERROR on
3d6cdcc8883b5726513d245eef0e91cabfc397f7]url: https://github.com/intel-lab-lkp/linux/commits/Yosry-Ahmed/KVM-x86-Move-enable_pmu-enable_mediated_pmu-to-pmu-h-and-pmu-c/20260326-191518 base: 3d6cdcc8883b5726513d245eef0e91cabfc397f7 patch link: https://lore.kernel.org/r/20260326031150.3774017-3-yosry%40kernel.org patch subject: [PATCH v4 2/6] KVM: x86: Move guest_mode helpers to x86.h config: x86_64-buildonly-randconfig-004-20260327 (https://download.01.org/0day-ci/archive/20260327/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260327/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
In file included from
arch/x86/kvm/svm/svm_onhyperv.c:11:Quoted 1 line
Quoted 1 line
arch/x86/kvm/svm/svm.h:520:6: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]520 | if (is_guest_mode(&svm->vcpu)) | ^ arch/x86/kvm/svm/svm.h:578:6: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 578 | if (is_guest_mode(&svm->vcpu) && !nested_vgif_enabled(svm)) | ^ arch/x86/kvm/svm/svm.h:639:6: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 639 | if (is_guest_mode(&svm->vcpu)) | ^ arch/x86/kvm/svm/svm.h:802:9: error: call to undeclared function 'is_guest_mode'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 802 | return is_guest_mode(vcpu) && (svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK); | ^In file included from
arch/x86/kvm/svm/svm_onhyperv.c:12:
In file included fromarch/x86/kvm/svm/svm_ops.h:7:
Quoted 7 lines
Quoted 1 line
Quoted 1 line
arch/x86/kvm/x86.h:169:20: error: conflicting types for 'is_guest_mode'169 | static inline bool
is_guest_mode(struct kvm_vcpu *vcpu)
| ^
arch/x86/kvm/svm/svm.h:520:6: note: previous implicit declaration is here520 | if (is_guest_mode(&svm->vcpu)) | ^5 errors generated.
Sashiko caught this first :)
https://sashiko.dev/#/patchset/20260326031150.3774017-1-yosry%40kernel.org
I think we just need to fold in including x86.h in svm.h, this fixes it for me:
arch/x86/kvm/svm/svm.h +1 -0
@@ -24,6 +24,7 @@#include "cpuid.h" #include "kvm_cache_regs.h" +#include "x86.h" /* * Helpers to convert to/from physical addresses for pages whose address is
kernel test robot
build botRe: [PATCH v4 2/6] KVM: x86: Move guest_mode helpers to x86.h
In reply to Yosry Ahmedkernel test robot noticed the following build errors:
Hi Yosry,
kernel test robot noticed the following build errors:
[auto build test ERROR on 3d6cdcc8883b5726513d245eef0e91cabfc397f7]
url: https://github.com/intel-lab-lkp/linux/commits/Yosry-Ahmed/KVM-x86-Move-enable_pmu-enable_mediated_pmu-to-pmu-h-and-pmu-c/20260326-191518 base: 3d6cdcc8883b5726513d245eef0e91cabfc397f7 patch link: https://lore.kernel.org/r/20260326031150.3774017-3-yosry%40kernel.org patch subject: [PATCH v4 2/6] KVM: x86: Move guest_mode helpers to x86.h config: i386-allmodconfig (https://download.01.org/0day-ci/archive/20260327/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260327/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from arch/x86/kvm/svm/svm_onhyperv.c:11:
arch/x86/kvm/svm/svm.h: In function 'svm_mark_intercepts_dirty':
>> arch/x86/kvm/svm/svm.h:520:13: error: implicit declaration of function 'is_guest_mode' [-Wimplicit-function-declaration]
520 | if (is_guest_mode(&svm->vcpu))
| ^~~~~~~~~~~~~
In file included from arch/x86/kvm/svm/svm_ops.h:7,
from arch/x86/kvm/svm/svm_onhyperv.c:12:
arch/x86/kvm/x86.h: At top level:
>> arch/x86/kvm/x86.h:169:20: error: conflicting types for 'is_guest_mode'; have 'bool(struct kvm_vcpu *)' {aka '_Bool(struct kvm_vcpu *)'}
169 | static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~
arch/x86/kvm/svm/svm.h:520:13: note: previous implicit declaration of 'is_guest_mode' with type 'int()'
520 | if (is_guest_mode(&svm->vcpu))
| ^~~~~~~~~~~~~--
In file included from kvm/svm/svm_onhyperv.c:11:
kvm/svm/svm.h: In function 'svm_mark_intercepts_dirty':
kvm/svm/svm.h:520:13: error: implicit declaration of function 'is_guest_mode' [-Wimplicit-function-declaration]
520 | if (is_guest_mode(&svm->vcpu))
| ^~~~~~~~~~~~~
In file included from kvm/svm/svm_ops.h:7,
from kvm/svm/svm_onhyperv.c:12:
arch/x86/kvm/x86.h: At top level:
>> arch/x86/kvm/x86.h:169:20: error: conflicting types for 'is_guest_mode'; have 'bool(struct kvm_vcpu *)' {aka '_Bool(struct kvm_vcpu *)'}
169 | static inline bool is_guest_mode(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~
kvm/svm/svm.h:520:13: note: previous implicit declaration of 'is_guest_mode' with type 'int()'
520 | if (is_guest_mode(&svm->vcpu))
| ^~~~~~~~~~~~~vim +/is_guest_mode +520 arch/x86/kvm/svm/svm.h
0b97f929831a70 Sean Christopherson 2026-02-18 509
0b97f929831a70 Sean Christopherson 2026-02-18 510 static inline void svm_mark_intercepts_dirty(struct vcpu_svm *svm)
0b97f929831a70 Sean Christopherson 2026-02-18 511 {
0b97f929831a70 Sean Christopherson 2026-02-18 512 vmcb_mark_dirty(svm->vmcb01.ptr, VMCB_INTERCEPTS);
0b97f929831a70 Sean Christopherson 2026-02-18 513
0b97f929831a70 Sean Christopherson 2026-02-18 514 /*
0b97f929831a70 Sean Christopherson 2026-02-18 515 * If L2 is active, recalculate the intercepts for vmcb02 to account
0b97f929831a70 Sean Christopherson 2026-02-18 516 * for the changes made to vmcb01. All intercept configuration is done
0b97f929831a70 Sean Christopherson 2026-02-18 517 * for vmcb01 and then propagated to vmcb02 to combine KVM's intercepts
0b97f929831a70 Sean Christopherson 2026-02-18 518 * with L1's intercepts (from the vmcb12 snapshot).
0b97f929831a70 Sean Christopherson 2026-02-18 519 */
0b97f929831a70 Sean Christopherson 2026-02-18 @520 if (is_guest_mode(&svm->vcpu))
0b97f929831a70 Sean Christopherson 2026-02-18 521 nested_vmcb02_recalc_intercepts(svm);
0b97f929831a70 Sean Christopherson 2026-02-18 522 }0b97f929831a70 Sean Christopherson 2026-02-18 523
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 3/6 KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
In reply to Yosry AhmedIntroduce a per-vendor PMU callback for reprogramming counters with a mediated PMU, and register a callback on AMD to disable a counter based on the vCPU's setting of Host-Only or Guest-Only EVENT_SELECT bits (if EFER.SVME is set). In other words, disable the counter if Host-Only and guest_mode() or Guest-Only and !guest_mode().
kvm_mediated_pmu_refresh_event_filter() ensures that ARCH_PERFMON_EVENTSEL_ENABLE is set for any enabled counters before mediated_reprogram_counter() callback, and kvm_mediated_pmu_load() writes the updated value of eventsel_hw to the appropriate MSR after the counters are reprogrammed through KVM_REQ_PMU.
Note that the behavior is equivalent if both bits are cleared or if both bits are set, events are counted regardless of host/guest state (from L1's perspective), so KVM should always keep the counter enabled unless only one of the bits is set.
It's a bit unnatural to check if both bits are set or cleared, then check EFER.SVME, then go back to checking the set bit. This ordering will be needed by following changes that will track counters with only one Host-Only/Guest-Only bit set regardless of EFER.SVME.
Host-Only and Guest-Only bits are currently reserved, so this change is a noop, but the bits will be allowed with mediated PMU in a following change when fully supported.
--- arch/x86/include/asm/kvm-x86-pmu-ops.h | 1 + arch/x86/include/asm/perf_event.h | 2 ++ arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/pmu.h | 1 + arch/x86/kvm/svm/pmu.c | 29 ++++++++++++++++++++++++++ 5 files changed, 34 insertions(+)
arch/x86/include/asm/kvm-x86-pmu-ops.h +1 -0
@@ -27,6 +27,7 @@ KVM_X86_PMU_OP_OPTIONAL(cleanup) KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl) KVM_X86_PMU_OP(mediated_load) KVM_X86_PMU_OP(mediated_put) +KVM_X86_PMU_OP_OPTIONAL(mediated_reprogram_counter)#endif #undef KVM_X86_PMU_OP
arch/x86/include/asm/perf_event.h +2 -0
@@ -60,6 +60,8 @@#define AMD64_EVENTSEL_INT_CORE_ENABLE (1ULL << 36)#define AMD64_EVENTSEL_GUESTONLY (1ULL << 40)#define AMD64_EVENTSEL_HOSTONLY (1ULL << 41) +#define AMD64_EVENTSEL_HOST_GUEST_MASK \ + (AMD64_EVENTSEL_HOSTONLY | AMD64_EVENTSEL_GUESTONLY)#define AMD64_EVENTSEL_INT_CORE_SEL_SHIFT 37#define AMD64_EVENTSEL_INT_CORE_SEL_MASK \
arch/x86/kvm/pmu.c +1 -0
@@ -559,6 +559,7 @@ static int reprogram_counter(struct kvm_pmc *pmc)if (kvm_vcpu_has_mediated_pmu (pmu_to_vcpu (pmu))) {kvm_mediated_pmu_refresh_event_filter (pmc); +kvm_pmu_call (mediated_reprogram_counter)(pmc);return 0 ; }
arch/x86/kvm/pmu.h +1 -0
@@ -40,6 +40,7 @@ struct kvm_pmu_ops {bool (*is_mediated_pmu_supported)(struct x86_pmu_capability *host_pmu);void (*mediated_load)(struct kvm_vcpu *vcpu);void (*mediated_put)(struct kvm_vcpu *vcpu); +void (*mediated_reprogram_counter)(struct kvm_pmc *pmc);void (*write_global_ctrl)(u64 global_ctrl);const u64 EVENTSEL_EVENT ;
arch/x86/kvm/svm/pmu.c +29 -0
@@ -260,6 +260,34 @@ static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu)wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , pmu->global_status); } +static void amd_mediated_pmu_handle_host_guest_bits (struct kvm_pmc *pmc) +{ +struct kvm_vcpu *vcpu = pmc->vcpu; +u64 host_guest_bits; + +if (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE )) +return ; + +/* Count all events if both bits are cleared or both bits are set */ + host_guest_bits = pmc->eventsel &AMD64_EVENTSEL_HOST_GUEST_MASK ; +if (hweight64 (host_guest_bits) !=1 ) +return ; + +/* Host-Only and Guest-Only are ignored if EFER.SVME == 0 */ +if (!(vcpu->arch.efer &EFER_SVME )) +return ; + +if (!!(host_guest_bits &AMD64_EVENTSEL_GUESTONLY ) ==is_guest_mode (vcpu)) +return ; + + pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE ; +} + +static void amd_mediated_pmu_reprogram_counter (struct kvm_pmc *pmc) +{ +amd_mediated_pmu_handle_host_guest_bits (pmc); +} +struct kvm_pmu_ops amd_pmu_ops__initdata = { .rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc,
@@ -273,6 +301,7 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported, .mediated_load = amd_mediated_pmu_load, .mediated_put = amd_mediated_pmu_put, + .mediated_reprogram_counter = amd_mediated_pmu_reprogram_counter, .EVENTSEL_EVENT = AMD64_EVENTSEL_EVENT, .MAX_NR_GP_COUNTERS = KVM_MAX_NR_AMD_GP_COUNTERS,
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 18 lines · Yosry Ahmed
arch/x86/include/asm/perf_event.h +2
-0@@ -60,6 +60,8 @@#define AMD64_EVENTSEL_INT_CORE_ENABLE (1ULL << 36)#define AMD64_EVENTSEL_GUESTONLY (1ULL << 40)#define AMD64_EVENTSEL_HOSTONLY (1ULL << 41) +#define AMD64_EVENTSEL_HOST_GUEST_MASK \ + (AMD64_EVENTSEL_HOSTONLY | AMD64_EVENTSEL_GUESTONLY)#define AMD64_EVENTSEL_INT_CORE_SEL_SHIFT 37#define AMD64_EVENTSEL_INT_CORE_SEL_MASK \arch/x86/kvm/pmu.c +0
-0
arch/x86/kvm/pmu.c static int reprogram_counter(struct kvm_pmc *pmc) · line 562
Quoted 4 lines
arch/x86/kvm/pmu.c +1
-0@@ -559,6 +559,7 @@ static int reprogram_counter(struct kvm_pmc *pmc)if (kvm_vcpu_has_mediated_pmu (pmu_to_vcpu (pmu))) {kvm_mediated_pmu_refresh_event_filter (pmc); +kvm_pmu_call (mediated_reprogram_counter)(pmc);
I would rather make a single call from kvm_pmu_handle_event(), and let the vendor deal with mediated vs. legacy. I want to avoid mediated-specific ops as much as possible, and I think kvm_x86_ops.reprogram_counters() would be easier to understand overall.
arch/x86/kvm/pmu.c +2 -0
@@ -670,6 +670,8 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)set_bit (pmc->idx, pmu->reprogram_pmi); } +kvm_pmu_call (reprogram_counters)(vcpu, bitmap); +/* * Release unused perf_events if the corresponding guest MSRs weren't * accessed during the last vCPU time slice (need_cleanup is set when
Quoted 24 lines
arch/x86/kvm/svm/pmu.c +16
-0@@ -260,6 +260,34 @@ static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu)wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , pmu->global_status); } +static void amd_mediated_pmu_handle_host_guest_bits (struct kvm_pmc *pmc) +{ +struct kvm_vcpu *vcpu = pmc->vcpu; +u64 host_guest_bits; + +if (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE )) +return ; + +/* Count all events if both bits are cleared or both bits are set */ + host_guest_bits = pmc->eventsel &AMD64_EVENTSEL_HOST_GUEST_MASK ; +if (hweight64 (host_guest_bits) !=1 ) +return ; + +/* Host-Only and Guest-Only are ignored if EFER.SVME == 0 */ +if (!(vcpu->arch.efer &EFER_SVME )) +return ;
arch/x86/kvm/svm/pmu.c static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu) · line 288
Quoted 10 lines
arch/x86/kvm/svm/pmu.c +10
-0+ +if (!!(host_guest_bits &AMD64_EVENTSEL_GUESTONLY ) ==is_guest_mode (vcpu)) +return ; + + pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE ; +} + +static void amd_mediated_pmu_reprogram_counter (struct kvm_pmc *pmc) +{ +amd_mediated_pmu_handle_host_guest_bits (pmc);
And then this doesn't need to be such a wonky wrapper, and the "reprogram on nested transition" logic can also clear the entire bitmap instead of doing things piecemeal, e.g. it can be something like so in the end:
if (!kvm_vcpu_has_mediated_pmu(vcpu)) return; bitmap_zero(pmu->pmc_reprogram_on_nested_transition, X86_PMC_IDX_MAX); kvm_for_each_pmc(pmu, pmc, bit, bitmap) amd_mediated_pmu_handle_host_guest_bits(pmc);
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Yosry Ahmed
arch/x86/include/asm/perf_event.h +2
-0@@ -60,6 +60,8 @@#define AMD64_EVENTSEL_INT_CORE_ENABLE (1ULL << 36)#define AMD64_EVENTSEL_GUESTONLY (1ULL << 40)#define AMD64_EVENTSEL_HOSTONLY (1ULL << 41) +#define AMD64_EVENTSEL_HOST_GUEST_MASK \ + (AMD64_EVENTSEL_HOSTONLY | AMD64_EVENTSEL_GUESTONLY)#define AMD64_EVENTSEL_INT_CORE_SEL_SHIFT 37#define AMD64_EVENTSEL_INT_CORE_SEL_MASK \arch/x86/kvm/pmu.c +0
-0@@ -559,6 +559,7 @@ static int reprogram_counter(struct kvm_pmc *pmc)
Quoted 8 lines
arch/x86/kvm/pmu.c static int reprogram_counter(struct kvm_pmc *pmc) · line 562
Quoted 3 lines
arch/x86/kvm/pmu.c +1
-0if (kvm_vcpu_has_mediated_pmu (pmu_to_vcpu (pmu))) {kvm_mediated_pmu_refresh_event_filter (pmc); +kvm_pmu_call (mediated_reprogram_counter)(pmc);I would rather make a single call from
kvm_pmu_handle_event(), and let the vendor deal with mediated vs. legacy. I want to avoid mediated-specific ops as much as possible, and I thinkkvm_x86_ops.reprogram_counters()would be easier to understand overall.
I think this doesn't apply anymore now that most nested transitions won't be handled through kvm_pmu_handle_event(). Also because we need kvm_mediated_pmu_refresh_event_filter() to still be called before re-evaluating H/G bits and EFER.SVME.
I think leave this callback as-is and handle everything through reprogram_counter(). Export reprogram_counter() and rename it to kvm_pmu_reprogram_counter(), and end up with something like this:
void __kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu, bool defer)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
if (bitmap_empty(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX))
return;
bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX);
bitmap_zero(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX);
BUILD_BUG_ON(sizeof(pmu->reprogram_on_nested_transition) != sizeof(atomic64_t));
if (defer) {
atomic64_or(*(s64 *)pmu->reprogram_on_nested_transition,
&vcpu_to_pmu(vcpu)->__reprogram_pmi);
kvm_make_request(KVM_REQ_PMU, vcpu);
return;
}
kvm_for_each_pmc(pmu, pmc, bit, bitmap)
kvm_pmu_reprogram_counter(pmc);
}void kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu)
{
__kvm_pmu_handle_nested_transition(vcpu, false);
}Actually, if that's desired, we can move this logic into SVM code now. We won't be calling kvm_pmu_handle_nested_transition() from inside enter_guest_mode() and leave_guest_mode() anyway so that we can only defer for the svm_leave_nested() path.
So we can move:
- kvm_pmu_handle_nested_transition() tosvm_pmu_handle_nested_transition()
- pmu->reprogram_on_nested_transition tosvm->nested.reprogram_on_nested_transition
Not sure if we want to keep SVM-specific logic in SVM code, or if we want to keep code generic as much as possible. I can see good arguments for both stances.
Quoted 8 lines
arch/x86/kvm/pmu.c static int reprogram_counter(struct kvm_pmc *pmc)
Quoted 4 lines
arch/x86/kvm/pmu.c +4
-0+ +static void amd_mediated_pmu_reprogram_counter (struct kvm_pmc *pmc) +{ +amd_mediated_pmu_handle_host_guest_bits (pmc);And then this doesn't need to be such a wonky wrapper, and the "reprogram on nested transition" logic can also clear the entire bitmap instead of doing things piecemeal, e.g. it can be something like so in the end:
We can still clear the entire bitmap in one go with the above suggestion, but we'll keep the wonky wrapper :)
Quoted 8 lines
if (!kvm_vcpu_has_mediated_pmu(vcpu)) return; bitmap_zero(pmu->pmc_reprogram_on_nested_transition, X86_PMC_IDX_MAX); kvm_for_each_pmc(pmu, pmc, bit, bitmap) amd_mediated_pmu_handle_host_guest_bits(pmc);
Sean Christopherson
maintainerRe: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 54 lines · Yosry Ahmed
Quoted 4 lines · Sean Christopherson
I would rather make a single call from
kvm_pmu_handle_event(), and let the vendor deal with mediated vs. legacy. I want to avoid mediated-specific ops as much as possible, and I thinkkvm_x86_ops.reprogram_counters()would be easier to understand overall.I think this doesn't apply anymore now that most nested transitions won't be handled through
kvm_pmu_handle_event(). Also because we needkvm_mediated_pmu_refresh_event_filter()to still be called before re-evaluating H/G bits and EFER.SVME.I think leave this callback as-is and handle everything through
reprogram_counter(). Exportreprogram_counter()and rename it tokvm_pmu_reprogram_counter(), and end up with something like this:void
__kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu, bool defer){ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX); if (bitmap_empty(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX)) return; bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX); bitmap_zero(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX); BUILD_BUG_ON(sizeof(pmu->reprogram_on_nested_transition) != sizeof(atomic64_t)); if (defer) { atomic64_or(*(s64 *)pmu->reprogram_on_nested_transition, &vcpu_to_pmu(vcpu)->__reprogram_pmi); kvm_make_request(KVM_REQ_PMU, vcpu); return; } kvm_for_each_pmc(pmu, pmc, bit, bitmap) kvm_pmu_reprogram_counter(pmc); }void
kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu){ __kvm_pmu_handle_nested_transition(vcpu, false); }Actually, if that's desired, we can move this logic into SVM code now. We won't be calling
kvm_pmu_handle_nested_transition()from insideenter_guest_mode()andleave_guest_mode()anyway so that we can only defer for thesvm_leave_nested()path.So we can move:
-kvm_pmu_handle_nested_transition()tosvm_pmu_handle_nested_transition()
-pmu->reprogram_on_nested_transitiontosvm->nested.reprogram_on_nested_transition
Quoted 3 lines
Not sure if we want to keep SVM-specific logic in SVM code, or if we want to keep code generic as much as possible. I can see good arguments for both stances.
We can have our cake and eat it too. Add svm_pmu_handle_nested_transition(), but then also rename and rework reprogram_counters() to support both deferred and synchronous operation, e.g. something like so:
---
static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff,
bool defer)
{
struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu);
if (!diff)
return;
atomic64_or(diff, &pmu->__reprogram_pmi);
if (defer)
kvm_make_request(KVM_REQ_PMU, vcpu);
else
kvm_pmu_handle_event(pmu_to_vcpu(pmu));
}static inline void kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff)
{
__kvm_pmu_reprogram_counters(pmu, diff, true);
}---
and then have SVM code pass in the reprogram_on_nested_transition or whatever.
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 54 lines
Quoted 43 lines
I think leave this callback as-is and handle everything through
reprogram_counter(). Exportreprogram_counter()and rename it tokvm_pmu_reprogram_counter(), and end up with something like this:void
__kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu, bool defer){ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX); if (bitmap_empty(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX)) return; bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX); bitmap_zero(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX); BUILD_BUG_ON(sizeof(pmu->reprogram_on_nested_transition) != sizeof(atomic64_t)); if (defer) { atomic64_or(*(s64 *)pmu->reprogram_on_nested_transition, &vcpu_to_pmu(vcpu)->__reprogram_pmi); kvm_make_request(KVM_REQ_PMU, vcpu); return; } kvm_for_each_pmc(pmu, pmc, bit, bitmap) kvm_pmu_reprogram_counter(pmc); }void
kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu){ __kvm_pmu_handle_nested_transition(vcpu, false); }Actually, if that's desired, we can move this logic into SVM code now. We won't be calling
kvm_pmu_handle_nested_transition()from insideenter_guest_mode()andleave_guest_mode()anyway so that we can only defer for thesvm_leave_nested()path.So we can move:
-kvm_pmu_handle_nested_transition()tosvm_pmu_handle_nested_transition()
-pmu->reprogram_on_nested_transitiontosvm->nested.reprogram_on_nested_transitionQuoted 3 lines
Not sure if we want to keep SVM-specific logic in SVM code, or if we want to keep code generic as much as possible. I can see good arguments for both stances.
We can have our cake and eat it too. Add
svm_pmu_handle_nested_transition(), but then also rename and reworkreprogram_counters()to support both deferred and synchronous operation, e.g. something like so:---
static inline void __kvm_pmu_reprogram_counters(structkvm_pmu*pmu, u64 diff,
bool defer)
Quoted 15 lines
{
struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu);
if (!diff)
return;
atomic64_or(diff, &pmu->__reprogram_pmi);
if (defer)
kvm_make_request(KVM_REQ_PMU, vcpu);
else
kvm_pmu_handle_event(pmu_to_vcpu(pmu));
}I like that the KVM PMU code is now presenting a generic API to reprogram counters rather than handling nested transitions, even though reprogram_on_nested_transition fits better semantically in kvm_pmu (than svm_nested_state).
I do have a few questions:
1. Do we want to do all of the work in kvm_pmu_handle_event() on every nested transition (rather than just reprogram counters)? Genuinely asking as I am not sure if the rest of it is significant.
2. This approach will reprogram all counters that need it on nested transitions. In my proposed approach above, I only iterate over counters in reprogram_on_nested_transition and reprogram them. Do you think it matters? I guess if other counters need reprogramming we'll probably do it in kvm_pmu_handle_event() before running the vCPU anyway, but then we're repeating the work here?
3. In this world we still keep the mediated_reprogram_counter() callback, right?
Quoted 8 lines
static inline void
kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff){ __kvm_pmu_reprogram_counters(pmu, diff, true); }---
and then have SVM code pass in the
reprogram_on_nested_transitionor whatever.
Sean Christopherson
maintainerRe: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 23 lines · Yosry Ahmed
Quoted 17 lines
static inline void __kvm_pmu_reprogram_counters(struct
kvm_pmu*pmu, u64 diff,
bool defer){ struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu); if (!diff) return; atomic64_or(diff, &pmu->__reprogram_pmi); if (defer) kvm_make_request(KVM_REQ_PMU, vcpu); else kvm_pmu_handle_event(pmu_to_vcpu(pmu)); }I like that the KVM PMU code is now presenting a generic API to reprogram counters rather than handling nested transitions, even though
reprogram_on_nested_transitionfits better semantically inkvm_pmu(thansvm_nested_state).
Quoted 5 lines
I do have a few questions:
1. Do we want to do all of the work in
kvm_pmu_handle_event()on every nested transition (rather than just reprogram counters)? Genuinely asking as I am not sure if the rest of it is significant.
Yes, we have to for correctness. And somewhat sneakily, it's not that as much work as it might seem at first glance because the Host/Guest stuff is limited to the mediated PMU. Specifically, pmu->need_cleanup will never be true and so the heavy-ish kvm_pmu_cleanup() will never be invoked.
As for correctness, we either need to run through this code:
kvm_for_each_pmc(pmu, pmc, bit, bitmap) kvm_pmu_recalc_pmc_emulation(pmu, pmc);
or pend a KVM_REQ_PMU so that it's done before re-entering the guest, so that KVM does the right thing when skipping/emulating guest instructions. That flow is relatively cheap, so I don't see any reason to defer it.
Quoted 6 lines
2. This approach will reprogram all counters that need it on nested transitions. In my proposed approach above, I only iterate over counters in
reprogram_on_nested_transitionand reprogram them. Do you think it matters? I guess if other counters need reprogramming we'll probably do it inkvm_pmu_handle_event()before running the vCPU anyway,
Correct. KVM has to do the work before the next VMRUN, all we're doing is completing the work earlier than is strictly necessary.
Quoted 1 line
but then we're repeating the work here?
No, it's not repeated. That's why I want to callkvm_pmu_handle_event(): it updates pmu->reprogram_pmi to clear bits for PMCs that are successfully reprogrammed.
Quoted 1 line
3. In this world we still keep the
mediated_reprogram_counter()callback, right?
Weren't we planning on a callback that would take the diff of counters? I.e. one callback per kvm_pmu_handle_event(), not one callback per PMC?
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 36 lines · Sean Christopherson
Quoted 23 lines · Yosry Ahmed
Quoted 17 lines
static inline void __kvm_pmu_reprogram_counters(struct
kvm_pmu*pmu, u64 diff,
bool defer){ struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu); if (!diff) return; atomic64_or(diff, &pmu->__reprogram_pmi); if (defer) kvm_make_request(KVM_REQ_PMU, vcpu); else kvm_pmu_handle_event(pmu_to_vcpu(pmu)); }I like that the KVM PMU code is now presenting a generic API to reprogram counters rather than handling nested transitions, even though
reprogram_on_nested_transitionfits better semantically inkvm_pmu(thansvm_nested_state).Quoted 5 lines
I do have a few questions:
1. Do we want to do all of the work in
kvm_pmu_handle_event()on every nested transition (rather than just reprogram counters)? Genuinely asking as I am not sure if the rest of it is significant.Yes, we have to for correctness. And somewhat sneakily, it's not that as much work as it might seem at first glance because the Host/Guest stuff is limited to the mediated PMU. Specifically,
pmu->need_cleanupwill never be true and so the heavy-ishkvm_pmu_cleanup()will never be invoked.
Quoted 8 lines
As for correctness, we either need to run through this code:
kvm_for_each_pmc(pmu, pmc, bit, bitmap)
kvm_pmu_recalc_pmc_emulation(pmu, pmc);or pend a
KVM_REQ_PMUso that it's done before re-entering the guest, so that KVM does the right thing when skipping/emulating guest instructions. That flow is relatively cheap, so I don't see any reason to defer it.
As a micro-optimization, should kvm_pmu_handle_event() clear KVM_REQ_PMU?
Quoted 9 lines
Quoted 8 lines
2. This approach will reprogram all counters that need it on nested transitions. In my proposed approach above, I only iterate over counters in
reprogram_on_nested_transitionand reprogram them. Do you think it matters? I guess if other counters need reprogramming we'll probably do it inkvm_pmu_handle_event()before running the vCPU anyway,
Quoted 7 lines
Correct. KVM has to do the work before the next VMRUN, all we're doing is completing the work earlier than is strictly necessary.
Quoted 1 line
but then we're repeating the work here?
No, it's not repeated. That's why I want to
callkvm_pmu_handle_event(): it updatespmu->reprogram_pmito clear bits for PMCs that are successfully reprogrammed.
Yeah kvm_pmu_cleanup() is the only thing that could be done, I didn't know that doesn't apply to the mediated PMU.
Quoted 5 lines
Quoted 2 lines
3. In this world we still keep the
mediated_reprogram_counter()callback, right?Weren't we planning on a callback that would take the diff of counters? I.e. one callback per
kvm_pmu_handle_event(), not one callback per PMC?
Yeah I initially dismissed this because it cannot be done with the implementation I proposed above, as we don't always go through kvm_pmu_handle_event(). But with your proposed implementation we can have a single callback in kvm_pmu_handle_event().
Thank you!
Sean Christopherson
maintainerRe: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 39 lines · Yosry Ahmed
Quoted 38 lines · Sean Christopherson
Quoted 23 lines · Yosry Ahmed
Quoted 17 lines
static inline void __kvm_pmu_reprogram_counters(struct
kvm_pmu*pmu, u64 diff,
bool defer){ struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu); if (!diff) return; atomic64_or(diff, &pmu->__reprogram_pmi); if (defer) kvm_make_request(KVM_REQ_PMU, vcpu); else kvm_pmu_handle_event(pmu_to_vcpu(pmu)); }I like that the KVM PMU code is now presenting a generic API to reprogram counters rather than handling nested transitions, even though
reprogram_on_nested_transitionfits better semantically inkvm_pmu(thansvm_nested_state).Quoted 5 lines
I do have a few questions:
1. Do we want to do all of the work in
kvm_pmu_handle_event()on every nested transition (rather than just reprogram counters)? Genuinely asking as I am not sure if the rest of it is significant.Yes, we have to for correctness. And somewhat sneakily, it's not that as much work as it might seem at first glance because the Host/Guest stuff is limited to the mediated PMU. Specifically,
pmu->need_cleanupwill never be true and so the heavy-ishkvm_pmu_cleanup()will never be invoked.As for correctness, we either need to run through this code:
Quoted 8 lines
Quoted 6 lines
kvm_for_each_pmc(pmu, pmc, bit, bitmap)
kvm_pmu_recalc_pmc_emulation(pmu, pmc);or pend a
KVM_REQ_PMUso that it's done before re-entering the guest, so that KVM does the right thing when skipping/emulating guest instructions. That flow is relatively cheap, so I don't see any reason to defer it.As a micro-optimization, should
kvm_pmu_handle_event()clearKVM_REQ_PMU?
I vote no. The odds of introducing a race, now or in the future, far outweigh the benefits.
Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
2. This approach will reprogram all counters that need it on nested transitions. In my proposed approach above, I only iterate over counters in
reprogram_on_nested_transitionand reprogram them. Do you think it matters? I guess if other counters need reprogramming we'll probably do it inkvm_pmu_handle_event()before running the vCPU anyway,Correct. KVM has to do the work before the next VMRUN, all we're doing is completing the work earlier than is strictly necessary.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
but then we're repeating the work here?
No, it's not repeated. That's why I want to
callkvm_pmu_handle_event(): it updatespmu->reprogram_pmito clear bits for PMCs that are successfully reprogrammed.Yeah
kvm_pmu_cleanup()is the only thing that could be done, I didn't know that doesn't apply to the mediated PMU.
It's a less-than-awesome name. It's a flag that says "go ahead and release perf_events that haven't been used for an entire time slice". I.e. it's garbage collection for the legacy PMU.
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
In reply to Sean ChristophersonQuoted 6 lines
We can have our cake and eat it too. Add
svm_pmu_handle_nested_transition(), but then also rename and reworkreprogram_counters()to support both deferred and synchronous operation, e.g. something like so:---
static inline void __kvm_pmu_reprogram_counters(structkvm_pmu*pmu, u64 diff,
I don't like 'diff', I think just 'unsigned long *bitmap' and pass a bitmap in here like most PMU code?
Quoted 5 lines
bool defer)
{ struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu);
Hmm why do we need this? Why not just pass in a vcpu? All callers have the vcpu and it should always be the running vcpu whether we are deferring or not.
Quoted 2 lines
if (!diff)Then this becomes bitmap_empty(bitmap)
Quoted 3 lines
return;
atomic64_or(diff, &pmu->__reprogram_pmi);and this unfortunately becomes aatomic64_or(*(s64 *)bitmap, &pmu->__reprogram_pmi);
Quoted 14 lines
if (defer) kvm_make_request(KVM_REQ_PMU, vcpu); else kvm_pmu_handle_event(pmu_to_vcpu(pmu)); }static inline void
kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff){ __kvm_pmu_reprogram_counters(pmu, diff, true); }---
and then have SVM code pass in the
reprogram_on_nested_transitionor whatever.
Sean Christopherson
maintainerRe: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 8 lines · Yosry Ahmed
Quoted 6 lines
We can have our cake and eat it too. Add
svm_pmu_handle_nested_transition(), but then also rename and reworkreprogram_counters()to support both deferred and synchronous operation, e.g. something like so:---
static inline void __kvm_pmu_reprogram_counters(structkvm_pmu*pmu, u64 diff,I don't like 'diff', I think just 'unsigned long *bitmap' and pass a
Hard no. I agree @diff is a weird name (I was literally just copy+pasting the existing code), but I _really_ don't like passing a pointer, especially not to an unsigned long. The bitmap usage throughout the PMU code is mostly internal implementation details. But for what is reprogram_counters(), and what will be __kvm_pmu_reprogram_counters(), the "counters to reprogram" is very tightly coupled to the architectural layout of PERF_GLOBAL_CTRL and PEBS_ENABLED. And more broadly in the PMU, for the layout of GLOBAL_STATUS_BUFFER_OVF_BIT, MSR_CORE_PERF_GLOBAL_STATUS, and probably at least one other MSRs. That all should be captured in the APIs.
The other reason I don't want to pass a pointer is so that even when the source _is_ a PMU-internal bitmap, it's super duper obvious that the source bitmap isn't modified, and that it's operating on a snapshot in time.
Quoted 1 line
bitmap in here like most PMU code?
FWIW, AFAICT, passing a bitmap as a function argument isn't common at all. I only see kvm_for_each_pmc() and kvm_pmu_trigger_event() taking a bitmap. There is a lot of bitmap _usage_, but rarely does KVM pass around a bitmap as a function argument.
Quoted 9 lines
Quoted 5 lines
bool defer)
{ struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu); lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu);Hmm why do we need this? Why not just pass in a vcpu? All callers have the vcpu and it should always be the running vcpu whether we are deferring or not.
No preference on my end (I was again largely just copy+pasting).
Quoted 26 lines
Quoted 2 lines
if (!diff)Then this becomes
bitmap_empty(bitmap)Quoted 3 lines
return; atomic64_or(diff, &pmu->__reprogram_pmi);and this unfortunately becomes
aatomic64_or(*(s64 *)bitmap, &pmu->__reprogram_pmi);Quoted 14 lines
if (defer) kvm_make_request(KVM_REQ_PMU, vcpu); else kvm_pmu_handle_event(pmu_to_vcpu(pmu)); }static inline void
kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff){ __kvm_pmu_reprogram_counters(pmu, diff, true); }---
and then have SVM code pass in the
reprogram_on_nested_transitionor whatever.
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 21 lines · Sean Christopherson
Quoted 8 lines · Yosry Ahmed
Quoted 6 lines
We can have our cake and eat it too. Add
svm_pmu_handle_nested_transition(), but then also rename and reworkreprogram_counters()to support both deferred and synchronous operation, e.g. something like so:---
static inline void __kvm_pmu_reprogram_counters(structkvm_pmu*pmu, u64 diff,I don't like 'diff', I think just 'unsigned long *bitmap' and pass a
Hard no. I agree @diff is a weird name (I was literally just copy+pasting the existing code), but I _really_ don't like passing a pointer, especially not to an unsigned long. The bitmap usage throughout the PMU code is mostly internal implementation details. But for what is
reprogram_counters(), and what will be__kvm_pmu_reprogram_counters(), the "counters to reprogram" is very tightly coupled to the architectural layout ofPERF_GLOBAL_CTRLandPEBS_ENABLED. And more broadly in the PMU, for the layout ofGLOBAL_STATUS_BUFFER_OVF_BIT,MSR_CORE_PERF_GLOBAL_STATUS, and probably at least one other MSRs. That all should be captured in the APIs.
Quoted 3 lines
The other reason I don't want to pass a pointer is so that even when the source _is_ a PMU-internal bitmap, it's super duper obvious that the source bitmap isn't modified, and that it's operating on a snapshot in time.
Sounds good, I will keep u64 (and maybe name it 'counters'), or maybe s64 as that's what kvm_pmu_handle_event() uses (and what the atomic helpers take).
Quoted 7 lines
Quoted 2 lines
bitmap in here like most PMU code?
FWIW, AFAICT, passing a bitmap as a function argument isn't common at all. I only see
kvm_for_each_pmc()andkvm_pmu_trigger_event()taking a bitmap. There is a lot of bitmap _usage_, but rarely does KVM pass around a bitmap as a function argument.
We still need to pass a bitmap as an argument to the reprogram_counters() callback that we'll add in kvm_pmu_handle_event(), or would you rather we cast the bitmap to s64 here as well?
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 39 lines · Yosry Ahmed
Quoted 21 lines · Sean Christopherson
Quoted 8 lines · Yosry Ahmed
Quoted 6 lines
We can have our cake and eat it too. Add
svm_pmu_handle_nested_transition(), but then also rename and reworkreprogram_counters()to support both deferred and synchronous operation, e.g. something like so:---
static inline void __kvm_pmu_reprogram_counters(structkvm_pmu*pmu, u64 diff,I don't like 'diff', I think just 'unsigned long *bitmap' and pass a
Hard no. I agree @diff is a weird name (I was literally just copy+pasting the existing code), but I _really_ don't like passing a pointer, especially not to an unsigned long. The bitmap usage throughout the PMU code is mostly internal implementation details. But for what is
reprogram_counters(), and what will be__kvm_pmu_reprogram_counters(), the "counters to reprogram" is very tightly coupled to the architectural layout ofPERF_GLOBAL_CTRLandPEBS_ENABLED. And more broadly in the PMU, for the layout ofGLOBAL_STATUS_BUFFER_OVF_BIT,MSR_CORE_PERF_GLOBAL_STATUS, and probably at least one other MSRs. That all should be captured in the APIs.Quoted 3 lines
The other reason I don't want to pass a pointer is so that even when the source _is_ a PMU-internal bitmap, it's super duper obvious that the source bitmap isn't modified, and that it's operating on a snapshot in time.
Sounds good, I will keep u64 (and maybe name it 'counters'), or maybe s64 as that's what
kvm_pmu_handle_event()uses (and what the atomic helpers take).Quoted 7 lines
Quoted 2 lines
bitmap in here like most PMU code?
FWIW, AFAICT, passing a bitmap as a function argument isn't common at all. I only see
kvm_for_each_pmc()andkvm_pmu_trigger_event()taking a bitmap. There is a lot of bitmap _usage_, but rarely does KVM pass around a bitmap as a function argument.
Quoted 4 lines
We still need to pass a bitmap as an argument to the
reprogram_counters()callback that we'll add inkvm_pmu_handle_event(), or would you rather we cast the bitmap to s64 here as well?
Hmm I think we should pass the bitmap as-is, otherwise we'll cast it back when calling kvm_for_each_pmc().
Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 41 lines · Yosry Ahmed
Quoted 39 lines · Yosry Ahmed
Quoted 21 lines · Sean Christopherson
Quoted 8 lines · Yosry Ahmed
Quoted 6 lines
We can have our cake and eat it too. Add
svm_pmu_handle_nested_transition(), but then also rename and reworkreprogram_counters()to support both deferred and synchronous operation, e.g. something like so:---
static inline void __kvm_pmu_reprogram_counters(structkvm_pmu*pmu, u64 diff,I don't like 'diff', I think just 'unsigned long *bitmap' and pass a
Hard no. I agree @diff is a weird name (I was literally just copy+pasting the existing code), but I _really_ don't like passing a pointer, especially not to an unsigned long. The bitmap usage throughout the PMU code is mostly internal implementation details. But for what is
reprogram_counters(), and what will be__kvm_pmu_reprogram_counters(), the "counters to reprogram" is very tightly coupled to the architectural layout ofPERF_GLOBAL_CTRLandPEBS_ENABLED. And more broadly in the PMU, for the layout ofGLOBAL_STATUS_BUFFER_OVF_BIT,MSR_CORE_PERF_GLOBAL_STATUS, and probably at least one other MSRs. That all should be captured in the APIs.Quoted 3 lines
The other reason I don't want to pass a pointer is so that even when the source _is_ a PMU-internal bitmap, it's super duper obvious that the source bitmap isn't modified, and that it's operating on a snapshot in time.
Sounds good, I will keep u64 (and maybe name it 'counters'), or maybe s64 as that's what
kvm_pmu_handle_event()uses (and what the atomic helpers take).Quoted 7 lines
Quoted 2 lines
bitmap in here like most PMU code?
FWIW, AFAICT, passing a bitmap as a function argument isn't common at all. I only see
kvm_for_each_pmc()andkvm_pmu_trigger_event()taking a bitmap. There is a lot of bitmap _usage_, but rarely does KVM pass around a bitmap as a function argument.
Quoted 7 lines
Quoted 4 lines
We still need to pass a bitmap as an argument to the
reprogram_counters()callback that we'll add inkvm_pmu_handle_event(), or would you rather we cast the bitmap to s64 here as well?Hmm I think we should pass the bitmap as-is, otherwise we'll cast it back when calling
kvm_for_each_pmc().
intel_pmu_cross_mapped_check() actually does this. I will pass in a s64 and cast it in this case.
PATCH v4 4/6 KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
In reply to Yosry AhmedReprogram all counters on nested transitions for the mediated PMU, to re-evaluate Host-Only and Guest-Only bits and enable/disable the PMU counters accordingly. For example, if Host-Only is set and Guest-Only is cleared, a counter should be disabled when entering guest mode and enabled when exiting guest mode.
Having one of Host-Only and Guest-Only set is only effective when EFER.SVME is set, so also trigger counter reprogramming when EFER.SVME is toggled.
Track counters with one of Host-Only and Guest-Only set as counters requiring reprogramming on nested transitions in a bitmap. Use the bitmap to only request KVM_PMU_REQ if some counters need reprogramming, and only reprogram the counters that actually need it.
Track such counters even if EFER.SVME is cleared, such that if/when EFER.SVME is set, KVM can reprogram those counters and enable/disable them appropriately. Otherwise, toggling EFER.SVME would need to reprogram all counters and use a different code path than kvm_pmu_handle_nested_transition().
--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/pmu.h | 13 +++++++++++++ arch/x86/kvm/svm/pmu.c | 13 ++++++++++++- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/x86.h | 5 +++++ 6 files changed, 38 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -594,6 +594,12 @@ struct kvm_pmu {DECLARE_BITMAP (pmc_counting_instructions,X86_PMC_IDX_MAX );DECLARE_BITMAP (pmc_counting_branches,X86_PMC_IDX_MAX ); +/* +* Whether or not PMU counters need to be reprogrammed on transitions +* between L1 and L2 (or when nesting enablement is toggled). +*/ +DECLARE_BITMAP (pmc_needs_nested_reprogram,X86_PMC_IDX_MAX ); +u64 ds_area;u64 pebs_enable;u64 pebs_enable_rsvd;
arch/x86/kvm/pmu.c +1 -0
@@ -932,6 +932,7 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu) pmu->need_cleanup = false;bitmap_zero (pmu->reprogram_pmi,X86_PMC_IDX_MAX ); +bitmap_zero (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX );kvm_for_each_pmc (pmu, pmc, i, pmu->all_valid_pmc_idx) {pmc_stop_counter (pmc);
arch/x86/kvm/pmu.h +13 -0
@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi); +kvm_make_request (KVM_REQ_PMU , vcpu); +} +void kvm_pmu_deliver_pmi (struct kvm_vcpu *vcpu);void kvm_pmu_handle_event (struct kvm_vcpu *vcpu);int kvm_pmu_rdpmc (struct kvm_vcpu *vcpu,unsigned pmc,u64 *data);
arch/x86/kvm/svm/pmu.c +12 -1
@@ -262,17 +262,28 @@ static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu)static void amd_mediated_pmu_handle_host_guest_bits (struct kvm_pmc *pmc) { +struct kvm_pmu *pmu =pmc_to_pmu (pmc);struct kvm_vcpu *vcpu = pmc->vcpu;u64 host_guest_bits; +__clear_bit (pmc->idx, pmu->pmc_needs_nested_reprogram); +if (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ))return ; -/* Count all events if both bits are cleared or both bits are set */ +/* +* If both bits are cleared or both bits are set, count all events. +* Otherwise, the counter enablement should be re-evaluated on every +* nested transition. Track which counters need to be re-evaluated even +* if EFER.SVME == 0, such that the counters are correctly reprogrammed +* on nested transitions after EFER.SVME is set. +*/ host_guest_bits = pmc->eventsel &AMD64_EVENTSEL_HOST_GUEST_MASK ;if (hweight64 (host_guest_bits) !=1 )return ; +__set_bit (pmc->idx, pmu->pmc_needs_nested_reprogram); +/* Host-Only and Guest-Only are ignored if EFER.SVME == 0 */ if (!(vcpu->arch.efer &EFER_SVME ))return ;
arch/x86/kvm/svm/svm.c +1 -0
@@ -261,6 +261,7 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)set_exception_intercept (svm,GP_VECTOR ); } +kvm_pmu_handle_nested_transition (vcpu);kvm_make_request (KVM_REQ_RECALC_INTERCEPTS , vcpu); }
arch/x86/kvm/x86.h +5 -0
@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32
@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }static inline void leave_guest_mode (struct kvm_vcpu *vcpu)
@@ -164,6 +167,8 @@ static inline void leave_guest_mode(struct kvm_vcpu *vcpu) } vcpu->stat.guest_mode =0 ; + +kvm_pmu_handle_nested_transition (vcpu); }static inline bool is_guest_mode (struct kvm_vcpu *vcpu)
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 36 lines · Yosry Ahmed
Reprogram all counters on nested transitions for the mediated PMU, to re-evaluate Host-Only and Guest-Only bits and enable/disable the PMU counters accordingly. For example, if Host-Only is set and Guest-Only is cleared, a counter should be disabled when entering guest mode and enabled when exiting guest mode.
Having one of Host-Only and Guest-Only set is only effective when EFER.SVME is set, so also trigger counter reprogramming when EFER.SVME is toggled.
Track counters with one of Host-Only and Guest-Only set as counters requiring reprogramming on nested transitions in a bitmap. Use the bitmap to only request
KVM_PMU_REQif some counters need reprogramming, and only reprogram the counters that actually need it.Track such counters even if EFER.SVME is cleared, such that if/when EFER.SVME is set, KVM can reprogram those counters and enable/disable them appropriately. Otherwise, toggling EFER.SVME would need to reprogram all counters and use a different code path than
kvm_pmu_handle_nested_transition().Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/pmu.h | 13 +++++++++++++ arch/x86/kvm/svm/pmu.c | 13 ++++++++++++- arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/x86.h | 5 +++++ 6 files changed, 38 insertions(+), 1 deletion(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 601
Quoted 8 lines
arch/x86/include/asm/kvm_host.h +5
-0@@ -594,6 +594,12 @@ struct kvm_pmu {DECLARE_BITMAP (pmc_counting_instructions,X86_PMC_IDX_MAX );DECLARE_BITMAP (pmc_counting_branches,X86_PMC_IDX_MAX ); +/* +* Whether or not PMU counters need to be reprogrammed on transitions +* between L1 and L2 (or when nesting enablement is toggled). +*/ +DECLARE_BITMAP (pmc_needs_nested_reprogram,X86_PMC_IDX_MAX );
Hmm, I think this should be reprogram_pmc_on_nested_transition, or something like that. I don't like using "needs" because KVM tends to use "needs" for one-shot things, e.g. "xyz needs to be sync'd on the next whatever". And while the code kinda-sorta takes that approach, in practice it doesn't; clearing the bits and then setting them again is really just an implementation quirk to keep things simple.
Conceptually, these flags are much "stickier" in that they are kept set across all nested transitions, and only cleared when the event selector itself is changed in some way.
Quoted 8 lines
arch/x86/kvm/pmu.h +0
-0@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); }
arch/x86/kvm/pmu.h static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) · line 260
Quoted 10 lines
arch/x86/kvm/pmu.h +10
-0+static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi);
And here especially I think reprogram_pmc_on_nested_transition works better, e.g. it's a bit more obvious that leaving reprogram_pmc_on_nested_transition as-is is intentional.
Quoted 1 line
arch/x86/kvm/pmu.h +1
-0+kvm_make_request (KVM_REQ_PMU , vcpu);
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
In reply to Yosry AhmedQuoted 35 lines · Yosry Ahmed
Reprogram all counters on nested transitions for the mediated PMU, to re-evaluate Host-Only and Guest-Only bits and enable/disable the PMU counters accordingly. For example, if Host-Only is set and Guest-Only is cleared, a counter should be disabled when entering guest mode and enabled when exiting guest mode.
Having one of Host-Only and Guest-Only set is only effective when EFER.SVME is set, so also trigger counter reprogramming when EFER.SVME is toggled.
Track counters with one of Host-Only and Guest-Only set as counters requiring reprogramming on nested transitions in a bitmap. Use the bitmap to only request
KVM_PMU_REQif some counters need reprogramming, and only reprogram the counters that actually need it.Track such counters even if EFER.SVME is cleared, such that if/when EFER.SVME is set, KVM can reprogram those counters and enable/disable them appropriately. Otherwise, toggling EFER.SVME would need to reprogram all counters and use a different code path than
kvm_pmu_handle_nested_transition().Signed-off-by Yosry Ahmed <[email protected]>---
...arch/x86/kvm/pmu.h +2
-0@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{
arch/x86/kvm/pmu.h static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) · line 262
Quoted 10 lines
arch/x86/kvm/pmu.h +10
-0+struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi); +kvm_make_request (KVM_REQ_PMU , vcpu); +}
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call kvm_pmu_instruction_retired() for an emulated instruction.
Quoted 14 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 6 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }
This happens too late for VMRUN, since we have already called kvm_pmu_instruction_retired() via kvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 39 lines · Jim Mattson
Quoted 10 lines · Yosry Ahmed
arch/x86/kvm/pmu.h +2
-0@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{arch/x86/kvm/pmu.h static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) · line 262
Quoted 10 lines
arch/x86/kvm/pmu.h +10
-0+struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi); +kvm_make_request (KVM_REQ_PMU , vcpu); +}In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32
Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 53 lines · Sean Christopherson
Quoted 39 lines · Jim Mattson
Quoted 10 lines · Yosry Ahmed
arch/x86/kvm/pmu.h +2
-0@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{arch/x86/kvm/pmu.h static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) · line 262
Quoted 10 lines
arch/x86/kvm/pmu.h +10
-0+struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi); +kvm_make_request (KVM_REQ_PMU , vcpu); +}In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.
Quoted 4 lines
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 54 lines · Jim Mattson
Quoted 53 lines · Sean Christopherson
Quoted 50 lines · Jim Mattson
Quoted 10 lines · Yosry Ahmed
arch/x86/kvm/pmu.h +2
-0@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{arch/x86/kvm/pmu.h static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) · line 262
Quoted 10 lines
arch/x86/kvm/pmu.h +10
-0+struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi); +kvm_make_request (KVM_REQ_PMU , vcpu); +}In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 14 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 6 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.
Quoted 6 lines
Quoted 4 lines
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 61 lines · Sean Christopherson
Quoted 59 lines · Jim Mattson
Quoted 57 lines · Sean Christopherson
Quoted 39 lines · Jim Mattson
Quoted 10 lines · Yosry Ahmed
arch/x86/kvm/pmu.h +2
-0@@ -248,6 +248,19 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline void kvm_pmu_handle_nested_transition (struct kvm_vcpu *vcpu) +{arch/x86/kvm/pmu.h static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) · line 262
Quoted 10 lines
arch/x86/kvm/pmu.h +10
-0+struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); + +if (bitmap_empty (pmu->pmc_needs_nested_reprogram,X86_PMC_IDX_MAX )) +return ; + +BUILD_BUG_ON (sizeof (pmu->pmc_needs_nested_reprogram) !=sizeof (atomic64_t)); +atomic64_or (*(s64 *)pmu->pmc_needs_nested_reprogram, + &vcpu_to_pmu (vcpu)->__reprogram_pmi); +kvm_make_request (KVM_REQ_PMU , vcpu); +}In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Quoted 5 lines
Quoted 1 line
Here, it's just VMRUN. Above, it's WRMSR(EFER).
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 36 lines · Jim Mattson
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Jim Mattson
Quoted 33 lines
Quoted 17 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Here, it's just VMRUN. Above, it's WRMSR(EFER).
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 39 lines · Sean Christopherson
Quoted 38 lines · Jim Mattson
Quoted 37 lines · Sean Christopherson
Quoted 35 lines · Jim Mattson
Quoted 29 lines
Quoted 17 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.Quoted 4 lines
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
What do you think about having two flavors of kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.
Poking into PMU state in arbitrary contexts makes me nervous. E.g. when calling svm_leave_nested(), odds are good EFER isn't even correct, and the update *needs* to be deferred.
I definitely don't love having two separate update mechanisms, but it seems like the safest option in this case.
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 45 lines · Sean Christopherson
Quoted 43 lines · Sean Christopherson
Quoted 42 lines · Jim Mattson
Quoted 40 lines · Sean Christopherson
Quoted 29 lines · Jim Mattson
Quoted 29 lines
Quoted 28 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 14 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 6 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.Quoted 6 lines
Quoted 4 lines
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
What do you think about having two flavors of
kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.
When would we use the deferred version? As far as the Intel PMU is concerned. there's nothing special about a nested transition.
Quoted 6 lines
Poking into PMU state in arbitrary contexts makes me nervous. E.g. when calling
svm_leave_nested(), odds are good EFER isn't even correct, and the update *needs* to be deferred.I definitely don't love having two separate update mechanisms, but it seems like the safest option in this case.
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 51 lines · Jim Mattson
Quoted 50 lines · Sean Christopherson
Quoted 47 lines · Sean Christopherson
Quoted 36 lines · Jim Mattson
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Jim Mattson
Quoted 33 lines
Quoted 17 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Here, it's just VMRUN. Above, it's WRMSR(EFER).
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
Quoted 6 lines
Quoted 3 lines
What do you think about having two flavors of
kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.When would we use the deferred version? As far as the Intel PMU is concerned. there's nothing special about a nested transition.
Actually, outside of VMRUN=>#VMEXIT, isn't the deferred version correct? E.g. when counting instructions, shouldn't WRMSR(EFER) be accounted based on the *old* EFER value, not the new EFER value?
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 57 lines · Sean Christopherson
Quoted 55 lines · Jim Mattson
Quoted 53 lines · Sean Christopherson
Quoted 39 lines · Sean Christopherson
Quoted 38 lines · Jim Mattson
Quoted 37 lines · Sean Christopherson
Quoted 35 lines · Jim Mattson
Quoted 29 lines
Quoted 17 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.Quoted 4 lines
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
What do you think about having two flavors of
kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.
Quoted 6 lines
Quoted 2 lines
When would we use the deferred version? As far as the Intel PMU is concerned. there's nothing special about a nested transition.
Actually, outside of VMRUN=>#VMEXIT, isn't the deferred version correct? E.g. when counting instructions, shouldn't WRMSR(EFER) be accounted based on the *old* EFER value, not the new EFER value?
Unless you can point to documentation stating otherwise, I think this needs testing. By analogy with VMRUN and #VMEXIT, accounting should be based on the CPU state at instruction retirement.
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 5 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Jim Mattson
When would we use the deferred version? As far as the Intel PMU is concerned. there's nothing special about a nested transition.
Quoted 7 lines
Quoted 3 lines
Actually, outside of VMRUN=>#VMEXIT, isn't the deferred version correct? E.g. when counting instructions, shouldn't WRMSR(EFER) be accounted based on the *old* EFER value, not the new EFER value?
Unless you can point to documentation stating otherwise, I think this needs testing. By analogy with VMRUN and #VMEXIT, accounting should be based on the CPU state at instruction retirement.
IMO, VMRUN and #VMEXIT are broken :-)
But yeah, +1 to following what hardware does. I was thinking we could look at our KUT and selftests code, but AFAICT all of the precise measurements are bookended by WRMSRs, so an off-by-one on the frontend would be corrected by the off-by-one on the backend.
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 15 lines · Sean Christopherson
Quoted 5 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
Quoted 2 lines · Jim Mattson
When would we use the deferred version? As far as the Intel PMU is concerned. there's nothing special about a nested transition.
Quoted 7 lines
Quoted 3 lines
Actually, outside of VMRUN=>#VMEXIT, isn't the deferred version correct? E.g. when counting instructions, shouldn't WRMSR(EFER) be accounted based on the *old* EFER value, not the new EFER value?
Unless you can point to documentation stating otherwise, I think this needs testing. By analogy with VMRUN and #VMEXIT, accounting should be based on the CPU state at instruction retirement.
Quoted 6 lines
IMO, VMRUN and #VMEXIT are broken :-)
But yeah, +1 to following what hardware does. I was thinking we could look at our KUT and selftests code, but AFAICT all of the precise measurements are bookended by WRMSRs, so an off-by-one on the frontend would be corrected by the off-by-one on the backend.
LOL! When EFER.SVME is clear, setting one or both of the G/H bits disables the counter.
In the following code sequence, 10 instructions are retired, and 5 host-only instructions are retired:
mov $0xc0000301,%ecx mov $0x3,%eax xor %edx,%edx wrmsr nopl 0x0(%rax,%rax,1) mov $0xc0000080,%ecx mov %ebx,%eax mov %r14d,%edx wrmsr nopl 0x0(%rax,%rax,1) mov $0xc0000301,%ecx xor %eax,%eax xor %edx,%edx
wrmsr
I think the first wrmsr counts (because the PGC bits are set when it retires) and the last one does not (because the PGC bits are clear when it retires). If so, then the second wrmsr (setting EFER.SVME) counts in host mode.
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
In reply to Sean ChristophersonQuoted 49 lines · Sean Christopherson
Quoted 39 lines · Sean Christopherson
Quoted 38 lines · Jim Mattson
Quoted 37 lines · Sean Christopherson
Quoted 35 lines · Jim Mattson
Quoted 29 lines
Quoted 17 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.Quoted 13 lines
...
arch/x86/kvm/x86.h +1
-0@@ -9,6 +9,7 @@#include "kvm_cache_regs.h" #include "kvm_emulate.h" #include "cpuid.h" +#include "pmu.h" #define KVM_MAX_MCE_BANKS 32Quoted 11 lines
arch/x86/kvm/x86.h static inline void enter_guest_mode(struct kvm_vcpu *vcpu) · line 158
Quoted 7 lines
arch/x86/kvm/x86.h +2
-0@@ -152,6 +153,8 @@ static inline void enter_guest_mode(struct kvm_vcpu *vcpu) { vcpu->arch.hflags |=HF_GUEST_MASK ; vcpu->stat.guest_mode =1 ; + +kvm_pmu_handle_nested_transition (vcpu); }This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.Quoted 4 lines
It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
Quoted 7 lines
What do you think about having two flavors of
kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.Poking into PMU state in arbitrary contexts makes me nervous. E.g. when calling
svm_leave_nested(), odds are good EFER isn't even correct, and the update *needs* to be deferred.
Hmm is it really that bad?
- In the emulated VMRUN and #VMEXIT paths, EFER.SVME should be set in both L1 and L2, so it should be fine.
- In the restore path entering guest mode, EFER.SVME should also be set in both L1 and L2.
So I think svm_leave_nested() is the only interesting case:
- In the restore path, svm_leave_nested() should only be called if the CPU is in guest mode and EFER.SVME is set in both L1 and L2.
- In the EFER update path, L1 will get a shutdown if we forcefully leave nested anyway, unless userspace is setting state. Either way, the value of EFER should be correct and valid to use to update the PMU here.
- In the vCPU free path, it shouldn't really matter, but the value of EFER should still be correct.
So I *think* generally the value of EFER should be fine to use. The other inputs are is_guest_mode() and eventsel. In both cases we should just make sure to update the PMU *after* updating the state.
So I think we'd end up with something similar to Jim's v2:
https://lore.kernel.org/kvm/[email protected]/
We will directly re-evaluate eventsel_hw on nested transitions, EFER updates, and PMU MSR updates -- without deferring anything.
We'd still need to make other changes:
- Always disable the PMC if EFER.SVME is clear and either H/G bit (or both) is set.
- Handle VMRUN correctly. I was going to suggest just moving the call to kvm_skip_emulated_instruction() to the end of the function, but that doesn't handle the case where we inject #VMEXIT(INVALID) due to a VMRUN failure (e.g. consistency checks, loading CR3, etc).
I am actually not sure if the instruction should count in host or guest mode in this case. Logically, we never entered the guest, so perhaps counting it in host mode is the right thing to do? I think we'll also need to test what HW does.
Honestly, it would be a lot easier of someone from AMD could just tell us these things :)
Basically:
- Does the PMU generally count based on processor state (e.g. guest mode, EFER.SVME) before or after instruction retirement?
- A successful VMRUN will be counted in guest mode, what about a failed VMRUN that produces #VMEXIT(INVALID)?
Quoted 2 lines
I definitely don't love having two separate update mechanisms, but it seems like the safest option in this case.
Same here, and I like the deferred handling, but to Jim's point I think we can use it anywhere :/
Sean Christopherson
maintainerRe: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 8 lines · Yosry Ahmed
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson
Quoted 3 lines · Jim Mattson
Quoted 3 lines
Quoted 3 lines
In general, this deferral is misguided. The G/H bits should be re-evaluated before we call
kvm_pmu_instruction_retired()for an emulated instruction.
...
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 8 lines
Quoted 3 lines
This happens too late for VMRUN, since we have already called
kvm_pmu_instruction_retired()viakvm_skip_emulated_instruction(), and VMRUN counts as a *guest* instruction.It's just VMRUN that's problematic though, correct? I.e. the scheme as a whole is fine, we just need to special case VMRUN due to SVM's erratum^Warchitecture. Alternatively, maybe we could get AMD to document the silly VMRUN behavior as an erratum, then we could claim KVM is architecturally superior. :-D
Here, it's just VMRUN. Above, it's WRMSR(EFER).
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
But clearing EFER.SVME while in the guest generates architecturally undefined behavior. I don't see any reason to complicate PMU virtualization for that scenario, especially now that KVM synthesizes triple fault for L1.
L1 can clear the virtual EFER.SVME. That is well-defined.
Gah, I forgot that the H/G bits are ignored when EFER.SVME=0. That's really annoying.
What do you think about having two flavors of
kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.
Quoted 5 lines
Quoted 3 lines
Poking into PMU state in arbitrary contexts makes me nervous. E.g. when calling
svm_leave_nested(), odds are good EFER isn't even correct, and the update *needs* to be deferred.Hmm is it really that bad?
It's not horrible, but it's a lot of "I think" and "should" and whatnot. I generally agree that it's unlikely to be a problem, but I can point at far too many bugs where KVM unexpectedly invokes a helper and consumes stale state.
I'm not completely opposed to non-deferred updates, but I really don't want to use them for svm_leave_nested().
Quoted 52 lines
- In the emulated VMRUN and #VMEXIT paths, EFER.SVME should be set in both L1 and L2, so it should be fine.
- In the restore path entering guest mode, EFER.SVME should also be set in both L1 and L2.
So I think
svm_leave_nested()is the only interesting case:- In the restore path,
svm_leave_nested()should only be called if the CPU is in guest mode and EFER.SVME is set in both L1 and L2.- In the EFER update path, L1 will get a shutdown if we forcefully leave nested anyway, unless userspace is setting state. Either way, the value of EFER should be correct and valid to use to update the PMU here.
- In the vCPU free path, it shouldn't really matter, but the value of EFER should still be correct.
So I *think* generally the value of EFER should be fine to use. The other inputs are
is_guest_mode()and eventsel. In both cases we should just make sure to update the PMU *after* updating the state.So I think we'd end up with something similar to Jim's v2:
https://lore.kernel.org/kvm/[email protected]/We will directly re-evaluate
eventsel_hwon nested transitions, EFER updates, and PMU MSR updates -- without deferring anything.We'd still need to make other changes:
- Always disable the PMC if EFER.SVME is clear and either H/G bit (or both) is set.- Handle VMRUN correctly. I was going to suggest just moving the call to
kvm_skip_emulated_instruction()to the end of the function, but that doesn't handle the case where we inject #VMEXIT(INVALID) due to a VMRUN failure (e.g. consistency checks, loading CR3, etc).I am actually not sure if the instruction should count in host or guest mode in this case. Logically, we never entered the guest, so perhaps counting it in host mode is the right thing to do? I think we'll also need to test what HW does.
Honestly, it would be a lot easier of someone from AMD could just tell us these things :)
Basically:
- Does the PMU generally count based on processor state (e.g. guest mode, EFER.SVME) before or after instruction retirement?
- A successful VMRUN will be counted in guest mode, what about a failed VMRUN that produces #VMEXIT(INVALID)?
Quoted 5 lines
Quoted 2 lines
I definitely don't love having two separate update mechanisms, but it seems like the safest option in this case.
Same here, and I like the deferred handling, but to Jim's point I think we can use it anywhere :/
Why can't we defer the svm_leave_nested() case? The only flows the invoke svm_leave_nested() are non-architectural, being precise there doesn't matter at all (and I'm not convinced it matters in general given none of us can figure out what hardware is _supposed_ to do).
Having a synchronous path for architectural flows, and a deferred mechanism for everything else seems reasonable, and would all but eliminate my concerns about consuming stale state and/or doing things like attempting to write MSRs while freeing a vCPU.
Re: [PATCH v4 4/6] KVM: x86/pmu: Re-evaluate Host-Only/Guest-Only on nested SVM transitions
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
What do you think about having two flavors of
kvm_pmu_handle_nested_transition()? One that defers via a request, and a "special" (SVM-only?) version that does direct updates.Poking into PMU state in arbitrary contexts makes me nervous. E.g. when calling
svm_leave_nested(), odds are good EFER isn't even correct, and the update *needs* to be deferred.
Quoted 8 lines
Quoted 1 line
Hmm is it really that bad?
It's not horrible, but it's a lot of "I think" and "should" and whatnot. I generally agree that it's unlikely to be a problem, but I can point at far too many bugs where KVM unexpectedly invokes a helper and consumes stale state.
I'm not completely opposed to non-deferred updates, but I really don't want to use them for
svm_leave_nested().
That makes sense, I had similar thoughts at some point.
Quoted 64 lines
Quoted 53 lines
- In the emulated VMRUN and #VMEXIT paths, EFER.SVME should be set in both L1 and L2, so it should be fine.
- In the restore path entering guest mode, EFER.SVME should also be set in both L1 and L2.
So I think
svm_leave_nested()is the only interesting case:- In the restore path,
svm_leave_nested()should only be called if the CPU is in guest mode and EFER.SVME is set in both L1 and L2.- In the EFER update path, L1 will get a shutdown if we forcefully leave nested anyway, unless userspace is setting state. Either way, the value of EFER should be correct and valid to use to update the PMU here.
- In the vCPU free path, it shouldn't really matter, but the value of EFER should still be correct.
So I *think* generally the value of EFER should be fine to use. The other inputs are
is_guest_mode()and eventsel. In both cases we should just make sure to update the PMU *after* updating the state.So I think we'd end up with something similar to Jim's v2:
https://lore.kernel.org/kvm/[email protected]/We will directly re-evaluate
eventsel_hwon nested transitions, EFER updates, and PMU MSR updates -- without deferring anything.We'd still need to make other changes:
- Always disable the PMC if EFER.SVME is clear and either H/G bit (or both) is set.- Handle VMRUN correctly. I was going to suggest just moving the call to
kvm_skip_emulated_instruction()to the end of the function, but that doesn't handle the case where we inject #VMEXIT(INVALID) due to a VMRUN failure (e.g. consistency checks, loading CR3, etc).I am actually not sure if the instruction should count in host or guest mode in this case. Logically, we never entered the guest, so perhaps counting it in host mode is the right thing to do? I think we'll also need to test what HW does.
Honestly, it would be a lot easier of someone from AMD could just tell us these things :)
Basically:
- Does the PMU generally count based on processor state (e.g. guest mode, EFER.SVME) before or after instruction retirement?
- A successful VMRUN will be counted in guest mode, what about a failed VMRUN that produces #VMEXIT(INVALID)?Quoted 5 lines
Quoted 2 lines
I definitely don't love having two separate update mechanisms, but it seems like the safest option in this case.
Same here, and I like the deferred handling, but to Jim's point I think we can use it anywhere :/
Why can't we defer the
svm_leave_nested()case? The only flows the invokesvm_leave_nested()are non-architectural, being precise there doesn't matter at all (and I'm not convinced it matters in general given none of us can figure out what hardware is _supposed_ to do).
Quoted 4 lines
Having a synchronous path for architectural flows, and a deferred mechanism for everything else seems reasonable, and would all but eliminate my concerns about consuming stale state and/or doing things like attempting to write MSRs while freeing a vCPU.
Sounds good to me. See my other reply about specifics.
PATCH v4 5/6 KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated PMU
In reply to Yosry AhmedFrom: Jim Mattson <[email protected]>
Now that KVM correctly handles Host-Only and Guest-Only bits in the event selector MSRs, allow the guest to set them if the vCPU advertises SVM and uses the mediated PMU.
--- arch/x86/kvm/svm/pmu.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/svm/pmu.c +4 -0
@@ -207,7 +207,11 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu) } pmu->counter_bitmask[KVM_PMC_GP ] =BIT_ULL (48 ) -1 ; + pmu->reserved_bits =0xfffffff000280000ull ; +if (guest_cpu_cap_has (vcpu,X86_FEATURE_SVM ) &&kvm_vcpu_has_mediated_pmu (vcpu)) + pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK ; + pmu->raw_event_mask =AMD64_RAW_EVENT_MASK ;/* not applicable to AMD; but clean them to prevent any fall out */ pmu->counter_bitmask[KVM_PMC_FIXED ] =0 ;
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
PATCH v4 6/6 KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
In reply to Yosry AhmedFrom: Jim Mattson <[email protected]>
Add a selftest to verify KVM correctly virtualizes the AMD PMU Host-Only (bit 41) and Guest-Only (bit 40) event selector bits across all relevant SVM state transitions.
The test programs 4 PMCs simultaneously with all combinations of the Host-Only and Guest-Only bits, then verifies correct counting behavior:
1. SVME=0: all counters count (Host-Only/Guest-Only bits ignored)
2. Set SVME=1: Host-Only and neither/both count; Guest-Only stops
3. VMRUN to L2: Guest-Only and neither/both count; Host-Only stops
4. VMEXIT to L1: Host-Only and neither/both count; Guest-Only stops
5. Clear SVME=0: all counters count (bits ignored again)
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../selftests/kvm/include/x86/processor.h | 2 + .../kvm/x86/svm_pmu_host_guest_test.c | 199 ++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_invalid_vmcb12_gpa TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state +TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync TEST_GEN_PROGS_x86 += x86/sync_regs_test TEST_GEN_PROGS_x86 += x86/ucna_injection_test
tools/testing/selftests/kvm/include/x86/pmu.h +6 -0
@@ -38,6 +38,12 @@#define ARCH_PERFMON_EVENTSEL_INV BIT_ULL(23)#define ARCH_PERFMON_EVENTSEL_CMASK GENMASK_ULL(31, 24) +/* +* These are AMD-specific bits. +*/ +#define AMD64_EVENTSEL_GUESTONLY BIT_ULL(40) +#define AMD64_EVENTSEL_HOSTONLY BIT_ULL(41) +/* RDPMC control flags, Intel only. */ #define INTEL_RDPMC_METRICS BIT_ULL(29)#define INTEL_RDPMC_FIXED BIT_ULL(30)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
@@ -19,6 +19,8 @@#include "kvm_util.h" #include "ucall_common.h" +#define __stack_aligned__ __aligned(16) +extern bool host_cpu_is_intel;extern bool host_cpu_is_amd;extern bool host_cpu_is_hygon;
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Sean Christopherson
maintainerRe: [PATCH v4 6/6] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
Quoted 58 lines · Yosry Ahmed
From: Jim Mattson <[email protected]>
Add a selftest to verify KVM correctly virtualizes the AMD PMU Host-Only (bit 41) and Guest-Only (bit 40) event selector bits across all relevant SVM state transitions.
The test programs 4 PMCs simultaneously with all combinations of the Host-Only and Guest-Only bits, then verifies correct counting behavior:
1. SVME=0: all counters count (Host-Only/Guest-Only bits ignored)
2. Set SVME=1: Host-Only and neither/both count; Guest-Only stops
3. VMRUN to L2: Guest-Only and neither/both count; Host-Only stops
4. VMEXIT to L1: Host-Only and neither/both count; Guest-Only stops
5. Clear SVME=0: all counters count (bits ignored again)Signed-off-by Jim Mattson <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../selftests/kvm/include/x86/processor.h | 2 + .../kvm/x86/svm_pmu_host_guest_test.c | 199 ++++++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_invalid_vmcb12_gpa TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state +TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync TEST_GEN_PROGS_x86 += x86/sync_regs_test TEST_GEN_PROGS_x86 += x86/ucna_injection_testtools/testing/selftests/kvm/include/x86/pmu.h +6
-0@@ -38,6 +38,12 @@#define ARCH_PERFMON_EVENTSEL_INV BIT_ULL(23)#define ARCH_PERFMON_EVENTSEL_CMASK GENMASK_ULL(31, 24) +/* +* These are AMD-specific bits. +*/ +#define AMD64_EVENTSEL_GUESTONLY BIT_ULL(40) +#define AMD64_EVENTSEL_HOSTONLY BIT_ULL(41) +/* RDPMC control flags, Intel only. */ #define INTEL_RDPMC_METRICS BIT_ULL(29)#define INTEL_RDPMC_FIXED BIT_ULL(30)tools/testing/selftests/kvm/include/x86/processor.h +0
-0
tools/testing/selftests/kvm/include/x86/processor.h line 22
Quoted 4 lines
tools/testing/selftests/kvm/include/x86/processor.h +1
-0@@ -19,6 +19,8 @@#include "kvm_util.h" #include "ucall_common.h" +#define __stack_aligned__ __aligned(16)
I would much prefer to provide a macro helper to declare the stack in a prep patch, and update the bajillion instances of "unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]" through KVM selftests.
A blurb in the changelog explaining why _this_ test needs to honor alignment while we've managed to squeak by without problems in other tests would also be helpful
Quoted 4 lines
tools/testing/selftests/kvm/include/x86/processor.h +1
-0+extern bool host_cpu_is_intel;extern bool host_cpu_is_amd;extern bool host_cpu_is_hygon;
Re: [PATCH v4 6/6] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
Quoted 3 lines · Sean Christopherson
A blurb in the changelog explaining why _this_ test needs to honor alignment while we've managed to squeak by without problems in other tests would also be helpful
read_counters() induces the compiler to generate a movdqa instruction referencing L2's stack, so the stack pointer at entry to l2_guest_code() must not be 16-byte aligned. (Odd 8-byte alignment?)
Presumably, we've squeaked by without problems in other tests, because no L2 instructions have required 16-byte (or greater) stack alignment.