KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits
Subsystems (5)
v7 cover KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits
v7 of Jim and myself's series adding support for AMD's Host-Only and Guest-Only performance counter eventsel bits in KVM's mediated PMU passthrough implementation. The series keeps growing in size over versions. This is Sean's fault (as usual), as he keeps finding subtle problems and making excellent suggestions.
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 nested transitions and EFER.SVME changes such that counters only count in the appropriate mode.
The series grew significantly after v4, as it now includes semi-related nSVM fixups and selftests cleanups needed for the series. I think parts of this series can land independently (patches 1-8 and patches 14-16), but then the remaining series would depend on both.
v6 -> v7:
- Instead of creating __kvm_skip_emulated_instruction(), call svm_skip_emulated_instruction() directly when emulating VMRUN [Sean].
- Split bailing early on failed VMRUN into two patches, one for the actual fix (patch 2) and one for the code cleanup (patch 3).
- Add a Fixes tag for patch 3.
- Indentation fixes and changelog updates.
v5 -> v6:
- Use a per-counter PMU callback that is called from pmc_is_locally_enabled(), rather than a single callback for all counters from kvm_pmu_handle_event(). This is mainly to address the problem Sashiko found where kvm_pmu_recalc_pmc_emulation() does not capture counter disablement due to Host-Only/Guest-Only bits [Sean].
- Move the bitmap tracking counters using Host-Only/Guest-Only bits from SVM code to the generic PMU code (and in its own patch), as it's now a generic optimization for pmc_is_locally_enabled() that is reused by SVM code on nested transitions [Sean].
v4 -> v5:
- Dropped moving leave_guest_mode() and enter_guest_mode() definitions, since the calls to update the vPMU no longer happen within these functions.
- Add PMU helpers refactoring to facilitate SVM usage.
- Added nested SVM fixups to count VMRUN correctly in guest mode when Host-Only/Guest-Only support is enabled [Jim/Sean].
- Update the vPMU synchronously on nested VM-Enter/VM-Exit and EFER.SVME changes, such that counter enablement is reevaluated before the instructions are counted, as the vPMU counts based on the vCPU state at instruction retirement (e.g. using new EFER value when EFER.SVME changes) [Jim/Sean].
- Keep deferring vPMU updates using KVM_REQ_PMU in the svm_leave_nested() path to avoid KVM potentially consuming stale state [Sean].
- Use a single PMU callback for reprogramming counters instead of a per-counter callback [Sean].
- Move the bitmap tracking counters into SVM code. The generic vPMU code now only exposes an API to reprogram counters, and an SVM wrapper uses it on nested transitions [Sean].
- Drop the manual stack-alignment fixes in the vPMU selftest, instead rework L2 stack setup in all nested selftests to reuse the allocation and alignment logic used by L1, and completely drop L1-provided stacks for L2 [Sean].
v6: https://lore.kernel.org/kvm/[email protected]/
v5: https://lore.kernel.org/kvm/[email protected]/
v4: 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 (15):
KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails
KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN
KVM: nSVM: Move VMRUN instruction retirement after entering guest mode
KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c
KVM: x86/pmu: Rename reprogram_counters() to clarify usage
KVM: x86/pmu: Do a single atomic OR when reprogramming counters
KVM: x86/pmu: Check mediated PMU counter enablement before event
filters
KVM: x86/pmu: Add support for KVM_X86_PMU_OP_OPTIONAL_RET0
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in
SVM
KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions
KVM: selftests: Refactor allocating guest stack into a helper
KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
KVM: selftests: Drop L1-provided stacks for L2 guests on x86
arch/x86/include/asm/kvm-x86-pmu-ops.h | 5 +- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/include/asm/perf_event.h | 2 + arch/x86/kvm/pmu.c | 21 +- arch/x86/kvm/pmu.h | 44 +++- arch/x86/kvm/svm/nested.c | 43 +++- arch/x86/kvm/svm/pmu.c | 42 ++++ arch/x86/kvm/svm/svm.c | 3 +- arch/x86/kvm/svm/svm.h | 24 ++ arch/x86/kvm/vmx/pmu_intel.c | 2 +- arch/x86/kvm/x86.c | 9 - arch/x86/kvm/x86.h | 3 - tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../selftests/kvm/include/x86/processor.h | 2 + .../selftests/kvm/include/x86/svm_util.h | 5 +- tools/testing/selftests/kvm/include/x86/vmx.h | 4 +- .../testing/selftests/kvm/lib/x86/memstress.c | 19 +- .../testing/selftests/kvm/lib/x86/processor.c | 45 ++-- tools/testing/selftests/kvm/lib/x86/svm.c | 6 +- tools/testing/selftests/kvm/lib/x86/vmx.c | 6 +- .../selftests/kvm/x86/aperfmperf_test.c | 9 +- .../kvm/x86/evmcs_smm_controls_test.c | 5 +- .../testing/selftests/kvm/x86/hyperv_evmcs.c | 6 +- .../selftests/kvm/x86/hyperv_svm_test.c | 6 +- .../selftests/kvm/x86/kvm_buslock_test.c | 9 +- .../selftests/kvm/x86/nested_close_kvm_test.c | 12 +- .../selftests/kvm/x86/nested_dirty_log_test.c | 8 +- .../selftests/kvm/x86/nested_emulation_test.c | 4 +- .../kvm/x86/nested_exceptions_test.c | 9 +- .../kvm/x86/nested_invalid_cr3_test.c | 10 +- .../selftests/kvm/x86/nested_tdp_fault_test.c | 9 +- .../kvm/x86/nested_tsc_adjust_test.c | 10 +- .../kvm/x86/nested_tsc_scaling_test.c | 10 +- .../kvm/x86/nested_vmsave_vmload_test.c | 6 +- tools/testing/selftests/kvm/x86/smm_test.c | 8 +- tools/testing/selftests/kvm/x86/state_test.c | 11 +- .../selftests/kvm/x86/svm_int_ctl_test.c | 5 +- .../selftests/kvm/x86/svm_lbr_nested_state.c | 6 +- .../kvm/x86/svm_nested_clear_efer_svme.c | 7 +- .../kvm/x86/svm_nested_shutdown_test.c | 5 +- .../kvm/x86/svm_nested_soft_inject_test.c | 6 +- .../selftests/kvm/x86/svm_nested_vmcb12_gpa.c | 13 +- .../kvm/x86/svm_pmu_host_guest_test.c | 216 ++++++++++++++++++ .../selftests/kvm/x86/svm_vmcall_test.c | 5 +- .../kvm/x86/triple_fault_event_test.c | 9 +- .../selftests/kvm/x86/vmx_apic_access_test.c | 5 +- .../kvm/x86/vmx_apicv_updates_test.c | 4 +- .../kvm/x86/vmx_invalid_nested_guest_state.c | 6 +- .../kvm/x86/vmx_nested_la57_state_test.c | 5 +- .../kvm/x86/vmx_preemption_timer_test.c | 5 +- 51 files changed, 479 insertions(+), 245 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
base-commit: 9f2a49c511cb05b85745e1578e4fd425bff87f58
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 1/17 KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
According to the APM, TF on VMRUN causes a #DB after VMRUN completes on the _host_ side. However, KVM injects a #DB in L2 context instead (or exits to userspace if KVM_GUESTDBG_SINGLESTEP is set) in kvm_skip_emulated_instruction().
Avoid single-step handling on VMRUN by open-coding the rest of kvm_skip_emulated_instruction() in nested_svm_vmrun(). This doesn't look pretty, but following changes will need to open-code kvm_pmu_instruction_retired() anyway, and will cleanup the code. This ignores TF on VMRUN instead of injecting a spurious exception into L2. Document this virtualization hole with a FIXME.
Note that a failed VMRUN would have been correctly single-stepped, but now TF is always ignored for consistency and simplicity purposes. VMX does not support TF on a successful VMLAUNCH/VMRESUME, so it's unlikely that single-stepping VMRUN properly is important, especially if it's only for failed VMRUNs.
--- arch/x86/kvm/svm/nested.c | 18 +++++++++++++++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 18 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +15 -3
@@ -30,6 +30,7 @@#include "lapic.h" #include "svm.h" #include "hyperv.h" +#include "pmu.h" #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK
@@ -1145,11 +1146,22 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)return kvm_handle_memory_failure (vcpu,X86EMUL_IO_NEEDED ,NULL );/* Advance RIP past VMRUN as part of the nested #VMEXIT. */ -return kvm_skip_emulated_instruction (vcpu); +if (!svm_skip_emulated_instruction (vcpu)) +return 0 ; + +kvm_pmu_instruction_retired (vcpu); +return 1 ; } -/* At this point, VMRUN is guaranteed to not fault; advance RIP. */ - ret =kvm_skip_emulated_instruction (vcpu); +/* +* At this point, VMRUN is guaranteed to not fault; advance RIP. +* +* FIXME: If TF is set on VMRUN should inject a #DB (or handle guest +* debugging) right after #VMEXIT, right now it's just ignored. +*/ + ret =svm_skip_emulated_instruction (vcpu); +if (ret) +kvm_pmu_instruction_retired (vcpu);/* * Since vmcb01 is not in use, we can use it to store some of the L1
arch/x86/kvm/svm/svm.c +1 -1
@@ -333,7 +333,7 @@ static int __svm_skip_emulated_instruction(struct kvm_vcpu *vcpu,return 1 ; } -static int svm_skip_emulated_instruction (struct kvm_vcpu *vcpu) +int svm_skip_emulated_instruction (struct kvm_vcpu *vcpu) {return __svm_skip_emulated_instruction (vcpu,EMULTYPE_SKIP , true); }
arch/x86/kvm/svm/svm.h +2 -0
@@ -832,6 +832,8 @@ static inline void svm_enable_intercept_for_msr(struct kvm_vcpu *vcpu,svm_set_intercept_for_msr (vcpu, msr, type, true); } +int svm_skip_emulated_instruction (struct kvm_vcpu *vcpu); +/* nested.c */ #define NESTED_EXIT_HOST 0/* Exit handled on host level */
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 2/17 KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails
In reply to Yosry AhmedIf svm_skip_emulation_instruction() fails, then RIP could not be advanced correctly (e.g. decode failure when NextRIP is not available). KVM will exit to userspace to handle the emulation failure, but only after stuffing the wrong RIP into vmcb01 and entering guest mode.
Bail early and exit to userspace before committing any side-effects of emulating the VMRUN (e.g. entering guest mode).
--- arch/x86/kvm/svm/nested.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/nested.c +5 -4
@@ -1159,9 +1159,10 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)* FIXME: If TF is set on VMRUN should inject a #DB (or handle guest * debugging) right after #VMEXIT, right now it's just ignored. */ - ret =svm_skip_emulated_instruction (vcpu); -if (ret) -kvm_pmu_instruction_retired (vcpu); +if (!svm_skip_emulated_instruction (vcpu)) +return 0 ; + +kvm_pmu_instruction_retired (vcpu);/* * Since vmcb01 is not in use, we can use it to store some of the L1
@@ -1191,7 +1192,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)nested_svm_vmexit (svm); } -return ret; +return 1 ; }/* Copy state save area fields which are handled by VMRUN */
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 3/17 KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN
In reply to Yosry AhmedThe code paths for advancing RIP and retiring the instruction for RIP are very similar whether or not caching vmcb12 succeeds. The only difference is handling mapping failures (i.e. EFAULT).
Pull the mapping failure handling out and unify the calls to svm_skip_emulated_instruction() and kvm_pmu_instruction_retired(), but return immediately after if copying and caching vmcb12 failed. A nice side effect of this is that the FIXME comment is now above the only code path calling svm_skip_emulated_instruction().
--- arch/x86/kvm/svm/nested.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
arch/x86/kvm/svm/nested.c +8 -12
@@ -1141,20 +1141,13 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu) } ret =nested_svm_copy_vmcb12_to_cache (vcpu, vmcb12_gpa); -if (ret) { -if (ret == -EFAULT ) -return kvm_handle_memory_failure (vcpu,X86EMUL_IO_NEEDED ,NULL ); - -/* Advance RIP past VMRUN as part of the nested #VMEXIT. */ -if (!svm_skip_emulated_instruction (vcpu)) -return 0 ; - -kvm_pmu_instruction_retired (vcpu); -return 1 ; - } +if (ret == -EFAULT ) +return kvm_handle_memory_failure (vcpu,X86EMUL_IO_NEEDED ,NULL );/* -* At this point, VMRUN is guaranteed to not fault; advance RIP. +* At this point, VMRUN is guaranteed to not fault; advance RIP. If +* caching vmcb12 failed for other reasons, return immediately afterward +* as a nested #VMEXIT was already set up. * * FIXME: If TF is set on VMRUN should inject a #DB (or handle guest * debugging) right after #VMEXIT, right now it's just ignored.
@@ -1164,6 +1157,9 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)kvm_pmu_instruction_retired (vcpu); +if (ret) +return 1 ; +/* * Since vmcb01 is not in use, we can use it to store some of the L1 * state.
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 4/17 KVM: nSVM: Move VMRUN instruction retirement after entering guest mode
In reply to Yosry AhmedA successful VMRUN retires in guest mode and should be counted by the PMU as a guest instruction. Move the call to kvm_pmu_instruction_retired() after potentially entering guest mode, such that VMRUN is counted correctly.
The PMU event will be matched against L2's CPL, but otherwise this does not change the behavior in terms of guest vs. host, because KVM does not virtualize Host-Only/Guest-Only PMC controls yet, so all instructions are counted regardless of the vCPU's host/guest state. But this change is needed for the incoming support for Host-Only/Guest-Only controls to count VMRUN correctly.
--- arch/x86/kvm/svm/nested.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/nested.c +7 -3
@@ -1155,10 +1155,8 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)if (!svm_skip_emulated_instruction (vcpu))return 0 ; -kvm_pmu_instruction_retired (vcpu); -if (ret) -return 1 ; + goto insn_retired;/* * Since vmcb01 is not in use, we can use it to store some of the L1
@@ -1188,6 +1186,12 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)nested_svm_vmexit (svm); } +insn_retired: +/* +* A successful VMRUN is counted by the PMU in guest mode, so only +* retire the instruction after potentially entering guest mode. +*/ +kvm_pmu_instruction_retired (vcpu);return 1 ; }
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 5/17 KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c
In reply to Yosry AhmedThe 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
@@ -181,15 +181,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
@@ -490,9 +490,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.54.0.794.g4f17f83d09-goog
PATCH v7 6/17 KVM: x86/pmu: Rename reprogram_counters() to clarify usage
In reply to Yosry AhmedRename reprogram_counters() to kvm_pmu_request_counters_reprogram() clarifying that it is more similar to kvm_pmu_request_counter_reprogram(), and less similar to reprogram_counter(). The kvm_pmu_* prefix is also appropriate as the function is exposed in the header.
Opportunistically rename the argument from 'diff' to 'counters'.
No functional change intended.
--- arch/x86/kvm/pmu.c | 2 +- arch/x86/kvm/pmu.h | 7 ++++--- arch/x86/kvm/vmx/pmu_intel.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
arch/x86/kvm/pmu.c +1 -1
@@ -889,7 +889,7 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (pmu->global_ctrl != data) { diff = pmu->global_ctrl ^ data; pmu->global_ctrl = data; -reprogram_counters (pmu, diff); +kvm_pmu_request_counters_reprogram (pmu, diff); }/* * Unconditionally forward writes to vendor code, i.e. to the
arch/x86/kvm/pmu.h +4 -3
@@ -210,14 +210,15 @@ static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)kvm_make_request (KVM_REQ_PMU , pmc->vcpu); } -static inline void reprogram_counters (struct kvm_pmu *pmu,u64 diff) +static inline void kvm_pmu_request_counters_reprogram (struct kvm_pmu *pmu, +u64 counters) {int bit; -if (!diff) +if (!counters)return ; -for_each_set_bit (bit, (unsigned long *)&diff,X86_PMC_IDX_MAX ) +for_each_set_bit (bit, (unsigned long *)&counters,X86_PMC_IDX_MAX )set_bit (bit, pmu->reprogram_pmi);kvm_make_request (KVM_REQ_PMU ,pmu_to_vcpu (pmu)); }
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -391,7 +391,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (pmu->pebs_enable != data) { diff = pmu->pebs_enable ^ data; pmu->pebs_enable = data; -reprogram_counters (pmu, diff); +kvm_pmu_request_counters_reprogram (pmu, diff); }break ;case MSR_IA32_DS_AREA :
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 7/17 KVM: x86/pmu: Do a single atomic OR when reprogramming counters
In reply to Yosry AhmedDo a single atomic OR using the atomic overlay of reprogram_pmi bitmask, instead of one atomic set_bit() call per counter.
--- arch/x86/kvm/pmu.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
arch/x86/kvm/pmu.h +1 -4
@@ -213,13 +213,10 @@ static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)static inline void kvm_pmu_request_counters_reprogram (struct kvm_pmu *pmu,u64 counters) { -int bit; -if (!counters)return ; -for_each_set_bit (bit, (unsigned long *)&counters,X86_PMC_IDX_MAX ) -set_bit (bit, pmu->reprogram_pmi); +atomic64_or (counters, &pmu->__reprogram_pmi);kvm_make_request (KVM_REQ_PMU ,pmu_to_vcpu (pmu)); }
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 8/17 KVM: x86/pmu: Check mediated PMU counter enablement before event filters
In reply to Yosry AhmedIf the guest disables the counter (by clearing ARCH_PERFMON_EVENTSEL_ENABLE), KVM still performs the PMU filter lookup, even though it doesn't end up changing eventsel_hw. Check if the counter is enabled by the guest before doing the potentially expensive PMU filter lookup.
--- arch/x86/kvm/pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/pmu.c +1 -1
@@ -532,7 +532,7 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) { -bool allowed =pmc_is_event_allowed (pmc); +bool allowed =pmc_is_locally_enabled (pmc) &&pmc_is_event_allowed (pmc);struct kvm_pmu *pmu =pmc_to_pmu (pmc);if (pmc_is_gp (pmc)) {
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
[email protected]
AI reviewRe: [PATCH v7 08/17] KVM: x86/pmu: Check mediated PMU counter enablement before event filters
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Guest can bypass PMU event filters for fixed counters by modifying MSR_CORE_PERF_FIXED_CTR_CTRL. --
commit 270fc772a7eb457f31ead154a0c426817961ae8a
Author: Yosry Ahmed <[email protected]>
KVM: x86/pmu: Check mediated PMU counter enablement before event filters
This commit checks if a PMU counter is enabled by the guest before performing the potentially expensive PMU event filter lookup. It modifies kvm_mediated_pmu_refresh_event_filter to use pmc_is_locally_enabled.
arch/x86/kvm/pmu.c static bool pmc_is_event_allowed(struct kvm_pmc *pmc) · line 535
Quoted 10 lines
arch/x86/kvm/pmu.c +1
-1@@ -532,7 +532,7 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) { -bool allowed =pmc_is_event_allowed (pmc); +bool allowed =pmc_is_locally_enabled (pmc) &&pmc_is_event_allowed (pmc);
[Severity: High]
This is a pre-existing issue, but was it intended for guests to be able to bypass PMU event filters for fixed counters by writing to the MSR twice?
Looking at reprogram_fixed_counters() in arch/x86/kvm/vmx/pmu_intel.c, pmu->fixed_ctr_ctrl_hw is unconditionally overwritten with raw guest data before checking which counters changed:
arch/x86/kvm/vmx/pmu_intel.c:reprogram_fixed_counters() {
...
pmu->fixed_ctr_ctrl = data;
pmu->fixed_ctr_ctrl_hw = data;
for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
u8 new_ctrl = fixed_ctrl_field(data, i);
u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
if (old_ctrl == new_ctrl)
continue;
...
kvm_pmu_request_counter_reprogram(pmc);
}
}Because kvm_pmu_request_counter_reprogram() is skipped when the counter's control bits haven't changed, kvm_mediated_pmu_refresh_event_filter() is never called to apply the filter mask.
Could a guest enable a rejected counter (which gets correctly filtered and cleared in fixed_ctr_ctrl_hw by the first write), and then write the MSR a second time to enable a different counter while leaving the rejected counter's bits set?
The second write would inject the unfiltered enable bits into fixed_ctr_ctrl_hw, and since the software state (old_ctrl == new_ctrl) matches, KVM skips reprogramming it. Does this allow the rejected counter to remain enabled in hardware upon the next VM-enter, completely bypassing the event filters?
Quoted 3 lines
arch/x86/kvm/pmu.c +0
-0struct kvm_pmu *pmu =pmc_to_pmu (pmc);if (pmc_is_gp (pmc)) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Sean Christopherson
maintainerRe: [PATCH v7 08/17] KVM: x86/pmu: Check mediated PMU counter enablement before event filters
Quoted 58 lines · On Thu, May 28, 2026, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Guest can bypass PMU event filters for fixed counters by modifyingMSR_CORE_PERF_FIXED_CTR_CTRL. --commit
270fc772a7eb457f31ead154a0c426817961ae8a
Author: Yosry Ahmed <[email protected]>KVM: x86/pmu: Check mediated PMU counter enablement before event filters
This commit checks if a PMU counter is enabled by the guest before performing the potentially expensive PMU event filter lookup. It modifies
kvm_mediated_pmu_refresh_event_filterto usepmc_is_locally_enabled.arch/x86/kvm/pmu.c static bool pmc_is_event_allowed(struct kvm_pmc *pmc) · line 535
Quoted 10 lines
arch/x86/kvm/pmu.c +1
-1@@ -532,7 +532,7 @@ static bool pmc_is_event_allowed(struct kvm_pmc *pmc)static void kvm_mediated_pmu_refresh_event_filter (struct kvm_pmc *pmc) { -bool allowed =pmc_is_event_allowed (pmc); +bool allowed =pmc_is_locally_enabled (pmc) &&pmc_is_event_allowed (pmc);[Severity: High]
This is a pre-existing issue, but was it intended for guests to be able to bypass PMU event filters for fixed counters by writing to the MSR twice?Looking at
reprogram_fixed_counters()inarch/x86/kvm/vmx/pmu_intel.c,pmu->fixed_ctr_ctrl_hwis unconditionally overwritten with raw guest data before checking which counters changed:arch/x86/kvm/vmx/pmu_intel.c:reprogram_fixed_counters() { ... pmu->fixed_ctr_ctrl = data; pmu->fixed_ctr_ctrl_hw = data; for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { u8 new_ctrl = fixed_ctrl_field(data, i); u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i); if (old_ctrl == new_ctrl) continue; ... kvm_pmu_request_counter_reprogram(pmc); } }Because
kvm_pmu_request_counter_reprogram()is skipped when the counter's control bits haven't changed,kvm_mediated_pmu_refresh_event_filter()is never called to apply the filter mask.Could a guest enable a rejected counter (which gets correctly filtered and cleared in
fixed_ctr_ctrl_hwby the first write), and then write the MSR a second time to enable a different counter while leaving the rejected counter's bits set?
Quoted 5 lines
The second write would inject the unfiltered enable bits into
fixed_ctr_ctrl_hw, and since the software state (old_ctrl==new_ctrl) matches, KVM skips reprogramming it. Does this allow the rejected counter to remain enabled in hardware upon the next VM-enter, completely bypassing the event filters?
Hrm, does look like that's the case. If the guest toggles a different PMC, KVM will update pmu->fixed_ctr_ctrl_hw and reprogram the PMC that is changing, but not the others that are enabled in pmu->fixed_ctr_ctrl_hw.
Aha! I think this is quite easy to fix: use fixed_ctr_ctrl_hw as the basis for the diff. For the perf-based PMU, fixed_ctr_ctrl_hw == fixed_ctr_ctrl at all times. For the mediated PMU, "reprogramming" means changes the values that are loaded into hardware, and so unless I'm missing something, this? I'll test and send a patch next week.
idiff --git arch/x86/kvm/vmx/pmu_intel.c arch/x86/kvm/vmx/pmu_intel.c index 453cb3d3ec9b..a73a9515d96c 100644
x86/kvm/vmx/pmu_intel.c +9 -1
@@ -56,8 +56,16 @@ static struct x86_pmu_lbr *vcpu_to_lbr_records(struct kvm_vcpu *vcpu)static void reprogram_fixed_counters (struct kvm_pmu *pmu,u64 data) { +/* +* Compare against the value the mediated PMU shoves into hardware, not +* the guest's desired value. For the emulated PMU (proxied via perf), +* they are one and the same (fixed_ctr_ctrl_hw isn't used other than +* here). For the mediated PMU, KVM needs to reprogram the actual MSR, +* and so needs to react to potential changes in the value shoved into +* hardware, e.g. to ensure the event filter is enforced. +*/ +u64 old_fixed_ctr_ctrl = pmu->fixed_ctr_ctrl_hw;struct kvm_pmc *pmc; -u64 old_fixed_ctr_ctrl = pmu->fixed_ctr_ctrl;int i; pmu->fixed_ctr_ctrl = data;
Note, general purpose counters (thankfully) don't have the same bug.
if (data != pmc->eventsel) {
pmc->eventsel = data;
pmc->eventsel_hw = data;
kvm_pmu_request_counter_reprogram(pmc);
}Oh, and for posterity, using fixed_ctr_ctrl_hw won't mess up pmc_in_use, because it's unused by the mediated PMU. It's purpose is solely for releasing perf events that are no longer being actively used. It's a bit hard to see, but need_cleanup will never be set for the mediated PMU, because pmu->event_count will always be zero.
if (vcpu->scheduled_out && pmu->version && pmu->event_count) {
pmu->need_cleanup = true;
kvm_make_request(KVM_REQ_PMU, vcpu);
}To make that more obvious, I think it makes sense to add
static inline void __kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u8 idx)
{
if (kvm_vcpu_has_mediated_pmu(vcpu))
return;
__set_bit(idx, vcpu_to_pmu(vcpu)->pmc_in_use);}
and then use that everywhere, and WARN if kvm_pmu_cleanup() is ever called with a mediated PMU.
PATCH v7 9/17 KVM: x86/pmu: Add support for KVM_X86_PMU_OP_OPTIONAL_RET0
In reply to Yosry AhmedAdd definitions for KVM_X86_PMU_OP_OPTIONAL_RET0() to resolve to __static_call_return0, similar to KVM_X86_OP_OPTIONAL_RET0(). Move the definition of kvm_pmu_call() to pmu.h, and add declarations for the static PMU calls in the header to allow making callbacks from the header in following changes.
--- arch/x86/include/asm/kvm-x86-pmu-ops.h | 4 +++- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/pmu.c | 4 ++++ arch/x86/kvm/pmu.h | 8 ++++++++ 4 files changed, 15 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +3 -1
@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 */ #if !defined(KVM_X86_PMU_OP ) || \ - !defined(KVM_X86_PMU_OP_OPTIONAL ) + !defined(KVM_X86_PMU_OP_OPTIONAL ) || \ + !defined(KVM_X86_PMU_OP_OPTIONAL_RET0 )#error Missing one or more KVM_X86_PMU_OP #defines#else
@@ -31,3 +32,4 @@ KVM_X86_PMU_OP(mediated_put)#undef KVM_X86_PMU_OP#undef KVM_X86_PMU_OP_OPTIONAL +#undef KVM_X86_PMU_OP_OPTIONAL_RET0
arch/x86/include/asm/kvm_host.h +0 -1
@@ -2068,7 +2068,6 @@ extern bool __read_mostly enable_device_posted_irqs;extern struct kvm_x86_ops kvm_x86_ops;#define kvm_x86_call (func) static_call(kvm_x86_##func) -#define kvm_pmu_call (func) static_call(kvm_x86_pmu_##func)#define KVM_X86_OP (func) \ DECLARE_STATIC_CALL(kvm_x86_##func, *(((struct kvm_x86_ops *)0)->func));
arch/x86/kvm/pmu.c +4 -0
@@ -98,6 +98,7 @@ static struct kvm_pmu_ops kvm_pmu_ops __read_mostly; DEFINE_STATIC_CALL_NULL(kvm_x86_pmu_##func, \ *(((struct kvm_pmu_ops *)0)->func));#define KVM_X86_PMU_OP_OPTIONAL KVM_X86_PMU_OP +#define KVM_X86_PMU_OP_OPTIONAL_RET0 KVM_X86_PMU_OP#include <asm/kvm-x86-pmu-ops.h> void kvm_pmu_ops_update (const struct kvm_pmu_ops *pmu_ops)
@@ -109,6 +110,9 @@ void kvm_pmu_ops_update(const struct kvm_pmu_ops *pmu_ops)#define KVM_X86_PMU_OP (func) \ WARN_ON(!kvm_pmu_ops.func); __KVM_X86_PMU_OP(func)#define KVM_X86_PMU_OP_OPTIONAL __KVM_X86_PMU_OP +#define KVM_X86_PMU_OP_OPTIONAL_RET0 (func) \ + static_call_update(kvm_x86_pmu_##func, (void *)kvm_pmu_ops.func ? : \ + (void *)__static_call_return0);#include <asm/kvm-x86-pmu-ops.h> #undef __KVM_X86_PMU_OP }
arch/x86/kvm/pmu.h +8 -0
@@ -53,6 +53,14 @@ struct kvm_pmu_ops {const u32 MSR_STRIDE ; }; +#define kvm_pmu_call (func) static_call(kvm_x86_pmu_##func) + +#define KVM_X86_PMU_OP (func) \ + DECLARE_STATIC_CALL(kvm_x86_pmu_##func, *(((struct kvm_pmu_ops *)0)->func)); +#define KVM_X86_PMU_OP_OPTIONAL KVM_X86_PMU_OP +#define KVM_X86_PMU_OP_OPTIONAL_RET0 KVM_X86_PMU_OP +#include <asm/kvm-x86-pmu-ops.h> +extern bool enable_pmu;extern bool enable_mediated_pmu;
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 10/17 KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
In reply to Yosry AhmedIntroduce an optional per-vendor PMU callback for checking if a counter is disabled in the current mode, 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 with the mediated PMU.
If EFER.SVME is set, all events are counted if both bits are set or cleared. If only one bit is set, the counter is disabled if the vCPU context does not match the set bit.
If EFER.SVME is cleared, the counter is disabled if any of the bits is set, otherwise all events are counted. Note that a Linux guest correctly handles this and clears Host-Only when EFER.SVME is cleared, see commit 1018faa6cf23 ("perf/x86/kvm: Fix Host-Only/Guest-Only counting with SVM disabled").
The callback is made from pmc_is_locally_enabled(), which is used for the mediated PMU when updating eventsel_hw in kvm_mediated_pmu_refresh_eventsel_hw(), as well as when checking what PMCs count instructions/branches for emulation in kvm_pmu_recalc_pmc_emulation().
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 | 4 +++- arch/x86/kvm/svm/pmu.c | 32 ++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +1 -0
@@ -24,6 +24,7 @@ KVM_X86_PMU_OP(init) KVM_X86_PMU_OP_OPTIONAL(reset) KVM_X86_PMU_OP_OPTIONAL(deliver_pmi) KVM_X86_PMU_OP_OPTIONAL(cleanup) +KVM_X86_PMU_OP_OPTIONAL_RET0(pmc_is_disabled_in_current_mode) KVM_X86_PMU_OP_OPTIONAL(write_global_ctrl) KVM_X86_PMU_OP(mediated_load)
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
@@ -100,6 +100,7 @@ static struct kvm_pmu_ops kvm_pmu_ops __read_mostly;#define KVM_X86_PMU_OP_OPTIONAL KVM_X86_PMU_OP#define KVM_X86_PMU_OP_OPTIONAL_RET0 KVM_X86_PMU_OP#include <asm/kvm-x86-pmu-ops.h> +EXPORT_STATIC_CALL_GPL (kvm_x86_pmu_pmc_is_disabled_in_current_mode);void kvm_pmu_ops_update (const struct kvm_pmu_ops *pmu_ops) {
arch/x86/kvm/pmu.h +3 -1
@@ -36,6 +36,7 @@ struct kvm_pmu_ops {void (*reset)(struct kvm_vcpu *vcpu);void (*deliver_pmi)(struct kvm_vcpu *vcpu);void (*cleanup)(struct kvm_vcpu *vcpu); +bool (*pmc_is_disabled_in_current_mode)(struct kvm_pmc *pmc);bool (*is_mediated_pmu_supported)(struct x86_pmu_capability *host_pmu);void (*mediated_load)(struct kvm_vcpu *vcpu);
@@ -201,7 +202,8 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc) pmc->idx - KVM_FIXED_PMC_BASE_IDX) & (INTEL_FIXED_0_KERNEL |INTEL_FIXED_0_USER ); -return pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ; +return (pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ) && + !kvm_pmu_call (pmc_is_disabled_in_current_mode)(pmc); }extern struct x86_pmu_capability kvm_pmu_cap;
arch/x86/kvm/svm/pmu.c +32 -0
@@ -260,6 +260,37 @@ static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu)wrmsrq (MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , pmu->global_status); } +static bool amd_pmc_is_disabled_in_current_mode (struct kvm_pmc *pmc) +{ +struct kvm_vcpu *vcpu = pmc->vcpu; +u64 host_guest_bits; + +if (!kvm_vcpu_has_mediated_pmu (vcpu)) +return false; + +/* Common code is supposed to check the common enable bit */ +if (WARN_ON_ONCE (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ))) +return false; + +/* If both bits are cleared, the counter is always enabled */ + host_guest_bits = pmc->eventsel &AMD64_EVENTSEL_HOST_GUEST_MASK ; +if (!host_guest_bits) +return false; + +/* If EFER.SVME=0 and either bit is set, the counter is disabled */ +if (!(vcpu->arch.efer &EFER_SVME )) +return true; + +/* +* If EFER.SVME=1, the counter is disabled iff only one of the bits is +* set AND the set bit doesn't match the vCPU mode. +*/ +if (host_guest_bits ==AMD64_EVENTSEL_HOST_GUEST_MASK ) +return false; + +return !!(host_guest_bits &AMD64_EVENTSEL_GUESTONLY ) !=is_guest_mode (vcpu); +} +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,
@@ -269,6 +300,7 @@ struct kvm_pmu_ops amd_pmu_ops __initdata = { .set_msr = amd_pmu_set_msr, .refresh = amd_pmu_refresh, .init = amd_pmu_init, + .pmc_is_disabled_in_current_mode = amd_pmc_is_disabled_in_current_mode, .is_mediated_pmu_supported = amd_pmu_is_mediated_pmu_supported, .mediated_load = amd_mediated_pmu_load,
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
[email protected]
AI reviewRe: [PATCH v7 10/17] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] `amd_pmc_is_disabled_in_current_mode` incorrectly evaluates the L2 guest's `EFER.SVME` bit instead…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] amd_pmc_is_disabled_in_current_mode incorrectly evaluates the L2 guest's EFER.SVME bit instead of the L1 guest's EFER.SVME during nested execution, breaking L1's ability to profile L2. --
commit 59cc076298715b9973873c88e8dfa9b46008c836
Author: Yosry Ahmed <[email protected]>
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
This commit introduces an optional per-vendor PMU callback to check if a counter is disabled in the current mode. For AMD, it registers a callback to disable a counter based on the vCPU's setting of Host-Only or Guest-Only EVENT_SELECT bits.
Quoted 4 lines
arch/x86/kvm/svm/pmu.c +0
-0
[ ... ]
Quoted 9 lines
arch/x86/kvm/svm/pmu.c +9
-0+static bool amd_pmc_is_disabled_in_current_mode (struct kvm_pmc *pmc) +{ +struct kvm_vcpu *vcpu = pmc->vcpu; +u64 host_guest_bits; + +if (!kvm_vcpu_has_mediated_pmu (vcpu)) +return false; + +/* Common code is supposed to check the common enable bit */
arch/x86/kvm/svm/pmu.c static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu) · line 281
Quoted 10 lines
arch/x86/kvm/svm/pmu.c +10
-0+if (WARN_ON_ONCE (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ))) +return false; + +/* If both bits are cleared, the counter is always enabled */ + host_guest_bits = pmc->eventsel &AMD64_EVENTSEL_HOST_GUEST_MASK ; +if (!host_guest_bits) +return false; + +/* If EFER.SVME=0 and either bit is set, the counter is disabled */ +if (!(vcpu->arch.efer &EFER_SVME ))
[Severity: High]
Does this correctly evaluate the L1 guest's EFER.SVME bit, or does it evaluate the L2 guest's EFER.SVME bit during nested execution?
When L1 programs a Guest-Only PMU counter to profile the L2 guest, KVM evaluates L1's PMU configuration. However, during nested execution, vcpu->arch.efer reflects the L2 guest's EFER.
Since most L2 guests do not enable nested SVM (meaning L2's EFER.SVME is 0), could this cause the counter to be incorrectly disabled, preventing L1 from profiling L2?
Quoted 1 line
arch/x86/kvm/svm/pmu.c +1
-0+return true;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Sean Christopherson
maintainerRe: [PATCH v7 10/17] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Quoted 29 lines · On Thu, May 28, 2026, [email protected] wrote:
Quoted 4 lines
arch/x86/kvm/svm/pmu.c +0
-0[ ... ]
Quoted 9 lines
arch/x86/kvm/svm/pmu.c +9
-0+static bool amd_pmc_is_disabled_in_current_mode (struct kvm_pmc *pmc) +{ +struct kvm_vcpu *vcpu = pmc->vcpu; +u64 host_guest_bits; + +if (!kvm_vcpu_has_mediated_pmu (vcpu)) +return false; + +/* Common code is supposed to check the common enable bit */ arch/x86/kvm/svm/pmu.c static void amd_mediated_pmu_put(struct kvm_vcpu *vcpu) · line 281
Quoted 10 lines
arch/x86/kvm/svm/pmu.c +10
-0+if (WARN_ON_ONCE (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ))) +return false; + +/* If both bits are cleared, the counter is always enabled */ + host_guest_bits = pmc->eventsel &AMD64_EVENTSEL_HOST_GUEST_MASK ; +if (!host_guest_bits) +return false; + +/* If EFER.SVME=0 and either bit is set, the counter is disabled */ +if (!(vcpu->arch.efer &EFER_SVME ))[Severity: High]
Does this correctly evaluate the L1 guest's EFER.SVME bit, or does it evaluate the L2 guest's EFER.SVME bit during nested execution?
Quoted 7 lines
When L1 programs a Guest-Only PMU counter to profile the L2 guest, KVM evaluates L1's PMU configuration. However, during nested execution,
vcpu->arch.eferreflects the L2 guest's EFER.Since most L2 guests do not enable nested SVM (meaning L2's EFER.SVME is 0), could this cause the counter to be incorrectly disabled, preventing L1 from profiling L2?
VMRUN fails if EFER.SVME isn't set in the VMCB save area, i.e. in vmcb12, and clearing EFER.SVME while in guest mode is architecturally undefined (KVM triple faults the entire VM). I.e. this can't happen.
PATCH v7 11/17 KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
In reply to Yosry AhmedInstead of always checking of a counter needs to be disabled for mode-specific reasons (e.g. Host-Only/Guest-Only bits in SVM), add a bitmap to track such counters. Set the bit for counters using either Host-Only or Guest-Only bits in EVENTSEL on SVM.
This bitmap will also be reused in following changes to selectively apply changes to such counters.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/pmu.h | 9 +++++++-- arch/x86/kvm/svm/pmu.c | 6 ++++++ 4 files changed, 16 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -615,6 +615,8 @@ struct kvm_pmu {DECLARE_BITMAP (pmc_counting_instructions,X86_PMC_IDX_MAX );DECLARE_BITMAP (pmc_counting_branches,X86_PMC_IDX_MAX ); +DECLARE_BITMAP (pmc_has_mode_specific_enables,X86_PMC_IDX_MAX ); +u64 ds_area;u64 pebs_enable;u64 pebs_enable_rsvd;
arch/x86/kvm/pmu.c +1 -0
@@ -936,6 +936,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_has_mode_specific_enables,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 +7 -2
@@ -202,8 +202,13 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc) pmc->idx - KVM_FIXED_PMC_BASE_IDX) & (INTEL_FIXED_0_KERNEL |INTEL_FIXED_0_USER ); -return (pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ) && - !kvm_pmu_call (pmc_is_disabled_in_current_mode)(pmc); +if (!(pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE )) +return false; + +if (!test_bit (pmc->idx, pmu->pmc_has_mode_specific_enables)) +return true; + +return !kvm_pmu_call (pmc_is_disabled_in_current_mode)(pmc); }extern struct x86_pmu_capability kvm_pmu_cap;
arch/x86/kvm/svm/pmu.c +6 -0
@@ -168,6 +168,12 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) pmc->eventsel = data; pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY ) |AMD64_EVENTSEL_GUESTONLY ; + +if (data &AMD64_EVENTSEL_HOST_GUEST_MASK ) +__set_bit (pmc->idx, pmu->pmc_has_mode_specific_enables); +else +__clear_bit (pmc->idx, pmu->pmc_has_mode_specific_enables); +kvm_pmu_request_counter_reprogram (pmc); }return 0 ;
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
[email protected]
AI reviewRe: [PATCH v7 11/17] KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Non-atomic modification of pmu->global_status in NMI context can cause lost updates. --
commit 0456def2f622ab0d0efee8781cb983557b090999
Author: Yosry Ahmed <[email protected]>
KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
This commit tracks mediated PMU counters with mode-specific enables by adding a new bitmap to the PMU state. This avoids having to constantly check if a counter needs to be disabled for mode-specific reasons, such as Host-Only or Guest-Only bits in EVENTSEL on SVM.
arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 618
Quoted 9 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -615,6 +615,8 @@ struct kvm_pmu {DECLARE_BITMAP (pmc_counting_instructions,X86_PMC_IDX_MAX );DECLARE_BITMAP (pmc_counting_branches,X86_PMC_IDX_MAX ); +DECLARE_BITMAP (pmc_has_mode_specific_enables,X86_PMC_IDX_MAX );
[Severity: High]
This is a pre-existing issue, but can the non-atomic modification of pmu->global_status in NMI context cause lost updates?
Looking at kvm_perf_overflow(), it runs in NMI context on the host when a performance counter overflows. It calls __kvm_perf_overflow(), which uses the non-atomic __set_bit() and __test_and_set_bit() to update pmu->global_status.
Concurrently, the vCPU thread modifies pmu->global_status during MSR emulation (e.g., handling MSR_CORE_PERF_GLOBAL_OVF_CTRL or MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR) using non-atomic read-modify-write operations like pmu->global_status &= ~data.
If an NMI fires between the vCPU thread's load and store of global_status:
vCPU thread:
u64 status = pmu->global_status;
NMI fires here:
__kvm_perf_overflow()
__set_bit(..., (unsigned long *)&pmu->global_status);vCPU thread resumes:
pmu->global_status = status & ~data;
Will the vCPU thread completely overwrite the NMI's update? Does this cause KVM to silently drop performance counter overflows, leading to lost PMIs in the guest?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH v7 12/17 KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions
In reply to Yosry AhmedReprogram PMU 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.
According to the APM, when EFER.SVME is cleared, setting Host-Only or Guest-Only disables the counter, so also trigger counter reprogramming when EFER.SVME is toggled.
Counters setting any of Host-Only and Guest-Only bits are already being tracked in pmc_has_mode_specific_enables, use the bitmap to reprogram these counters.
Reprogram the counters synchronously on nested VMRUN/#VMEXIT and EFER.SVME toggling. This is necessary as these instructions are counted based on the new CPU state (after the instruction is retired in hardware). Hence, the PMU needs to be updated before instruction emulation is completed and kvm_pmu_instruction_retired() is called.
Defer reprogramming the counters when force leaving guest mode through svm_leave_nested() to avoid potentially reading stale state (e.g. incorrect EFER). All flows force leaving nested are not architectural, so precision is not a priority.
Refactor a helper out of kvm_pmu_request_reprogram_counters() that accepts a boolean allowing synchronous vs deferred reprogramming, and use that from SVM code to support both scenarios.
--- arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/pmu.h | 18 ++++++++++++++---- arch/x86/kvm/svm/nested.c | 12 ++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 22 ++++++++++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-)
arch/x86/kvm/pmu.c +1 -0
@@ -685,6 +685,7 @@ void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)kvm_for_each_pmc (pmu, pmc, bit, bitmap)kvm_pmu_recalc_pmc_emulation (pmu, pmc); } +EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_pmu_handle_event);int kvm_pmu_check_rdpmc_early (struct kvm_vcpu *vcpu,unsigned int idx) {
arch/x86/kvm/pmu.h +14 -4
@@ -216,6 +216,7 @@ extern struct x86_pmu_capability kvm_pmu_cap;void kvm_init_pmu_capability (struct kvm_pmu_ops *pmu_ops);void kvm_pmu_recalc_pmc_emulation (struct kvm_pmu *pmu,struct kvm_pmc *pmc); +void kvm_pmu_handle_event (struct kvm_vcpu *vcpu);static inline void kvm_pmu_request_counter_reprogram (struct kvm_pmc *pmc) {
@@ -225,14 +226,24 @@ static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)kvm_make_request (KVM_REQ_PMU , pmc->vcpu); } -static inline void kvm_pmu_request_counters_reprogram (struct kvm_pmu *pmu, -u64 counters) +static inline void __kvm_pmu_reprogram_counters (struct kvm_pmu *pmu, +u64 counters, +bool defer) {if (!counters)return ;atomic64_or (counters, &pmu->__reprogram_pmi); -kvm_make_request (KVM_REQ_PMU ,pmu_to_vcpu (pmu)); +if (defer) +kvm_make_request (KVM_REQ_PMU ,pmu_to_vcpu (pmu)); +else +kvm_pmu_handle_event (pmu_to_vcpu (pmu)); +} + +static inline void kvm_pmu_request_counters_reprogram (struct kvm_pmu *pmu, +u64 counters) +{ +__kvm_pmu_reprogram_counters (pmu, counters, true); }/*
@@ -261,7 +272,6 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *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);int kvm_pmu_check_rdpmc_early (struct kvm_vcpu *vcpu,unsigned int idx);bool kvm_pmu_is_valid_msr (struct kvm_vcpu *vcpu,u32 msr);
arch/x86/kvm/svm/nested.c +12 -0
@@ -860,6 +860,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)/* Enter Guest-Mode */ enter_guest_mode (vcpu); +svm_pmu_handle_nested_transition (svm);/* * Filled at exit: exit_code, exit_info_1, exit_info_2, exit_int_info,
@@ -1325,6 +1326,8 @@ void nested_svm_vmexit(struct vcpu_svm *svm)/* Exit Guest-Mode */ leave_guest_mode (vcpu); +svm_pmu_handle_nested_transition (svm); + svm->nested.vmcb12_gpa =0 ;kvm_warn_on_nested_run_pending (vcpu);
@@ -1536,6 +1539,15 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)leave_guest_mode (vcpu); +/* +* Force leaving nested is a non-architectural flow so precision +* is not a priority. Defer updating the PMU until the next vCPU +* run, potentially tolerating some imprecision to avoid poking +* into PMU state from arbitrary contexts (e.g. KVM may end up +* using stale state). +*/ +__svm_pmu_handle_nested_transition (svm, true); +svm_switch_vmcb (svm, &svm->vmcb01);nested_svm_uninit_mmu_context (vcpu);
arch/x86/kvm/svm/svm.c +1 -0
@@ -265,6 +265,7 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)set_exception_intercept (svm,GP_VECTOR ); } +svm_pmu_handle_nested_transition (svm);kvm_make_request (KVM_REQ_RECALC_INTERCEPTS , vcpu); }
arch/x86/kvm/svm/svm.h +22 -0
@@ -25,6 +25,7 @@#include "cpuid.h" #include "kvm_cache_regs.h" #include "x86.h" +#include "pmu.h" /* * Helpers to convert to/from physical addresses for pages whose address is
@@ -897,6 +898,27 @@ void nested_copy_vmcb_save_to_cache(struct vcpu_svm *svm,void nested_sync_control_from_vmcb02 (struct vcpu_svm *svm);void svm_switch_vmcb (struct vcpu_svm *svm,struct kvm_vmcb_info *target_vmcb); + +static inline void __svm_pmu_handle_nested_transition (struct vcpu_svm *svm,bool defer) +{ +struct kvm_pmu *pmu =vcpu_to_pmu (&svm->vcpu); +u64 counters = *(u64 *)pmu->pmc_has_mode_specific_enables; + +__kvm_pmu_reprogram_counters (pmu, counters, defer); +} + +static inline void svm_pmu_handle_nested_transition (struct vcpu_svm *svm) +{ +/* +* Do NOT defer reprogramming the counters by default. Instructions +* causing a state change are counted based on the _new_ CPU state +* (e.g. a successful VMRUN is counted in guest mode). Hence, the +* counters should be reprogrammed with the new state _before_ the +* instruction is potentially counted upon emulation completion. +*/ +__svm_pmu_handle_nested_transition (svm, false); +} +extern struct kvm_x86_nested_ops svm_nested_ops;/* avic.c */
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 13/17 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
@@ -213,7 +213,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.54.0.794.g4f17f83d09-goog
PATCH v7 14/17 KVM: selftests: Refactor allocating guest stack into a helper
In reply to Yosry AhmedIn preparation for reusing the logic to allocate stacks for nested guests, refactoring allocating a guest stack and aligning RSP into a helper.
No functional change intended.
--- .../testing/selftests/kvm/lib/x86/processor.c | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/lib/x86/processor.c +25 -20
@@ -778,6 +778,30 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)REPORT_GUEST_ASSERT (uc); } +static gva_t vm_alloc_stack (struct kvm_vm *vm,int nr_pages) +{ +int size = nr_pages *getpagesize (); +gva_t stack_gva; + + stack_gva =__vm_alloc (vm, size,DEFAULT_GUEST_STACK_VADDR_MIN ,MEM_REGION_DATA ); + stack_gva += size; + +/* +* Align stack to match calling sequence requirements in section "The +* Stack Frame" of the System V ABI AMD64 Architecture Processor +* Supplement, which requires the value (%rsp + 8) to be a multiple of +* 16 when control is transferred to the function entry point. +* +* If this code is ever used to launch a vCPU with 32-bit entry point it +* may need to subtract 4 bytes instead of 8 bytes. +*/ +TEST_ASSERT (IS_ALIGNED (stack_gva,PAGE_SIZE ), +"__vm_alloc() did not provide a page-aligned address" ); + stack_gva -=8 ; + +return stack_gva; +} +void kvm_arch_vm_post_create (struct kvm_vm *vm,unsigned int nr_vcpus) {int r;
@@ -820,27 +844,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, u32 vcpu_id) {struct kvm_mp_state mp_state;struct kvm_regs regs; -gva_t stack_gva;struct kvm_vcpu *vcpu; - stack_gva =__vm_alloc (vm,DEFAULT_STACK_PGS *getpagesize (), -DEFAULT_GUEST_STACK_VADDR_MIN ,MEM_REGION_DATA ); - - stack_gva +=DEFAULT_STACK_PGS *getpagesize (); - -/* -* Align stack to match calling sequence requirements in section "The -* Stack Frame" of the System V ABI AMD64 Architecture Processor -* Supplement, which requires the value (%rsp + 8) to be a multiple of -* 16 when control is transferred to the function entry point. -* -* If this code is ever used to launch a vCPU with 32-bit entry point it -* may need to subtract 4 bytes instead of 8 bytes. -*/ -TEST_ASSERT (IS_ALIGNED (stack_gva,PAGE_SIZE ), -"__vm_alloc() did not provide a page-aligned address" ); - stack_gva -=8 ; - vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);
@@ -849,7 +854,7 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, u32 vcpu_id)/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |X86_EFLAGS_FIXED ; - regs.rsp = stack_gva; + regs.rsp =vm_alloc_stack (vm,DEFAULT_STACK_PGS );vcpu_regs_set (vcpu, ®s);/* Setup the MP state */
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 15/17 KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
In reply to Yosry AhmedInstead of relying on the L1-provided stack for L2, which is usually an array on L1's own stack, allocate a dedicated page of VM memory for the L2 stack in vcpu_alloc_{vmx/svm}() and use that as L2's RSP in the VMCS/VMCB instead of the L1-provided value.
Most L1 guest code does not do anything with the L2 stack other than stuff it in RSP, so this change is transparent and the L1-provided stack is silently ignored. The only exception is memstress nested L1 code which puts the vCPU index on L2's stack, so update this code to use the newly allocated stack.
L1-provided stacks will be dropped and cleaned up separately.
--- tools/testing/selftests/kvm/include/x86/processor.h | 2 ++ tools/testing/selftests/kvm/include/x86/svm_util.h | 3 +++ tools/testing/selftests/kvm/include/x86/vmx.h | 2 ++ tools/testing/selftests/kvm/lib/x86/memstress.c | 5 ++--- tools/testing/selftests/kvm/lib/x86/processor.c | 2 +- tools/testing/selftests/kvm/lib/x86/svm.c | 4 +++- tools/testing/selftests/kvm/lib/x86/vmx.c | 4 +++- 7 files changed, 16 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
@@ -1225,6 +1225,8 @@ struct idt_entry {void vm_install_exception_handler (struct kvm_vm *vm,int vector,void (*handler)(struct ex_regs *)); +gva_t vm_alloc_stack (struct kvm_vm *vm,int nr_pages); +/* * Exception fixup morphs #DE to an arbitrary magic vector so that '0' can be * used to signal "no expcetion".
tools/testing/selftests/kvm/include/x86/svm_util.h +3 -0
@@ -28,6 +28,9 @@ struct svm_test_data {void *msr_hva;u64 msr_gpa; +/* Stack */ +void *stack;/* gva */ +/* NPT */ u64 ncr3_gpa; };
tools/testing/selftests/kvm/include/x86/vmx.h +2 -0
@@ -524,6 +524,8 @@ struct vmx_pages {u64 apic_access_gpa;void *apic_access; +void *stack; +u64 eptp_gpa; };
tools/testing/selftests/kvm/lib/x86/memstress.c +2 -3
@@ -43,7 +43,7 @@ static void l1_vmx_code(struct vmx_pages *vmx, u64 vcpu_id)GUEST_ASSERT (ept_1g_pages_supported ()); rsp = &l2_guest_stack[L2_GUEST_STACK_SIZE -1 ]; - *rsp = vcpu_id; + *(u64 *)vmx->stack = vcpu_id;prepare_vmcs (vmx, memstress_l2_guest_entry, rsp);GUEST_ASSERT (!vmlaunch ());
@@ -56,9 +56,8 @@ static void l1_svm_code(struct svm_test_data *svm, u64 vcpu_id)unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];unsigned long *rsp; - rsp = &l2_guest_stack[L2_GUEST_STACK_SIZE -1 ]; - *rsp = vcpu_id; + *(u64 *)svm->stack = vcpu_id;generic_svm_setup (svm, memstress_l2_guest_entry, rsp);run_guest (svm->vmcb, svm->vmcb_gpa);
tools/testing/selftests/kvm/lib/x86/processor.c +1 -1
@@ -778,7 +778,7 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)REPORT_GUEST_ASSERT (uc); } -static gva_t vm_alloc_stack (struct kvm_vm *vm,int nr_pages) +gva_t vm_alloc_stack (struct kvm_vm *vm,int nr_pages) {int size = nr_pages *getpagesize ();gva_t stack_gva;
tools/testing/selftests/kvm/lib/x86/svm.c +3 -1
@@ -46,6 +46,8 @@ vcpu_alloc_svm(struct kvm_vm *vm, gva_t *p_svm_gva) svm->msr_gpa =addr_gva2gpa (vm, (uintptr_t )svm->msr);memset (svm->msr_hva,0 ,getpagesize ()); + svm->stack = (void *)vm_alloc_stack (vm,1 ); +if (vm->stage2_mmu.pgd_created) svm->ncr3_gpa = vm->stage2_mmu.pgd;
@@ -122,7 +124,7 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r ctrl->msrpm_base_pa = svm->msr_gpa; vmcb->save.rip = (u64 )guest_rip; - vmcb->save.rsp = (u64 )guest_rsp; + vmcb->save.rsp = (u64 )svm->stack; guest_regs.rdi = (u64 )svm;if (svm->ncr3_gpa) {
tools/testing/selftests/kvm/lib/x86/vmx.c +3 -1
@@ -116,6 +116,8 @@ vcpu_alloc_vmx(struct kvm_vm *vm, gva_t *p_vmx_gva) vmx->vmwrite_gpa =addr_gva2gpa (vm, (uintptr_t )vmx->vmwrite);memset (vmx->vmwrite_hva,0 ,getpagesize ()); + vmx->stack = (void *)vm_alloc_stack (vm,1 ); +if (vm->stage2_mmu.pgd_created) vmx->eptp_gpa = vm->stage2_mmu.pgd;
@@ -370,7 +372,7 @@ void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp) {init_vmcs_control_fields (vmx);init_vmcs_host_state (); -init_vmcs_guest_state (guest_rip, guest_rsp); +init_vmcs_guest_state (guest_rip, vmx->stack); }bool kvm_cpu_has_ept (void )
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Sean Christopherson
maintainerRe: [PATCH v7 15/17] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
Quoted 11 lines · Yosry Ahmed
Instead of relying on the L1-provided stack for L2, which is usually an array on L1's own stack, allocate a dedicated page of VM memory for the L2 stack in vcpu_alloc_{vmx/svm}() and use that as L2's RSP in the VMCS/VMCB instead of the L1-provided value.
Most L1 guest code does not do anything with the L2 stack other than stuff it in RSP, so this change is transparent and the L1-provided stack is silently ignored. The only exception is memstress nested L1 code which puts the vCPU index on L2's stack, so update this code to use the newly allocated stack.
Quoted 5 lines
L1-provided stacks will be dropped and cleaned up separately.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>---
Blech. This exposed a nasty edge in selftests. For tests that enable TDP, the slots need to be identity mapped *after* allocating SVM/VMX. Found out the hard
way: the gPAT test starting failing.
Not worth worrying about right now, just one more wart in selftests that needs to be cleaned up.
testing/selftests/kvm/x86/svm_nested_pat_test.c +5 -3
@@ -115,12 +115,14 @@ static void run_test(void *guest_code, bool do_save_restore, int nr_iters)vm_enable_cap (vm,KVM_CAP_DISABLE_QUIRKS2 ,KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT ); -if (npt_enabled) { +if (npt_enabled)vm_enable_npt (vm); -tdp_identity_map_default_memslots (vm); - }vcpu_alloc_svm (vm, &svm_gva); + +if (npt_enabled) +tdp_identity_map_default_memslots (vm); +vcpu_args_set (vcpu,1 , svm_gva); nr_iterations = nr_iters;
Re: [PATCH v7 15/17] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
Quoted 19 lines · Sean Christopherson
Quoted 11 lines · Yosry Ahmed
Instead of relying on the L1-provided stack for L2, which is usually an array on L1's own stack, allocate a dedicated page of VM memory for the L2 stack in vcpu_alloc_{vmx/svm}() and use that as L2's RSP in the VMCS/VMCB instead of the L1-provided value.
Most L1 guest code does not do anything with the L2 stack other than stuff it in RSP, so this change is transparent and the L1-provided stack is silently ignored. The only exception is memstress nested L1 code which puts the vCPU index on L2's stack, so update this code to use the newly allocated stack.
Quoted 5 lines
L1-provided stacks will be dropped and cleaned up separately.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>---
Quoted 6 lines
Blech. This exposed a nasty edge in selftests. For tests that enable TDP, the slots need to be identity mapped *after* allocating SVM/VMX. Found out the hard
way: the gPAT test starting failing.Not worth worrying about right now, just one more wart in selftests that needs to be cleaned up.
I would squash that diff below to the gPAT test rather than this patch, but up to you.
Generally speaking, the tdp_identity_map_default_memslots() API sucks. It has to be called after all mappings/allocations are done, which is soooo user unfriendly and when it breaks it takes time to figure out what's going on.
I think it should go away. Ideally either:
(a) When TDP is enabled in the VM, tdp_identity_map_default_memslots() is called, and then every time we create a new mapping in the VM we mirror it in TDP page tables.
(b) Every time we update guest mappings, we mark the TDP page tables as "out of sync", and we "resync" on vCPU run as needed.
Either way, it is not as straightforward as just calling tdp_identity_map_default_memslots(), because the current code reuses the same logic as stage-1 mappings, and it fails if there's already a PTE. So consecutive calls will fail if they try to map the whole thing -- unless we free all TDP page tables first.
I don't think I have time to spend on this right now, mainly documenting my thoughts, and putting this out here in case you (or anyone else) has enough hatred for the API to go do this before I get the time to.
Sean Christopherson
maintainerRe: [PATCH v7 15/17] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
Quoted 24 lines · Yosry Ahmed
Quoted 23 lines · Sean Christopherson
Quoted 11 lines · Yosry Ahmed
Instead of relying on the L1-provided stack for L2, which is usually an array on L1's own stack, allocate a dedicated page of VM memory for the L2 stack in vcpu_alloc_{vmx/svm}() and use that as L2's RSP in the VMCS/VMCB instead of the L1-provided value.
Most L1 guest code does not do anything with the L2 stack other than stuff it in RSP, so this change is transparent and the L1-provided stack is silently ignored. The only exception is memstress nested L1 code which puts the vCPU index on L2's stack, so update this code to use the newly allocated stack.
Quoted 5 lines
L1-provided stacks will be dropped and cleaned up separately.
Suggested-by Sean Christopherson <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>---
Blech. This exposed a nasty edge in selftests. For tests that enable TDP, the slots need to be identity mapped *after* allocating SVM/VMX. Found out the hard
way: the gPAT test starting failing.
Quoted 5 lines
Quoted 2 lines
Not worth worrying about right now, just one more wart in selftests that needs to be cleaned up.
I would squash that diff below to the gPAT test rather than this patch, but up to you.
Ya, that's the plan.
Quoted 19 lines
Generally speaking, the
tdp_identity_map_default_memslots()API sucks. It has to be called after all mappings/allocations are done, which is soooo user unfriendly and when it breaks it takes time to figure out what's going on.I think it should go away. Ideally either:
(a) When TDP is enabled in the VM,tdp_identity_map_default_memslots()is called, and then every time we create a new mapping in the VM we mirror it in TDP page tables.(b) Every time we update guest mappings, we mark the TDP page tables as "out of sync", and we "resync" on vCPU run as needed.
Either way, it is not as straightforward as just calling
tdp_identity_map_default_memslots(), because the current code reuses the same logic as stage-1 mappings, and it fails if there's already a PTE. So consecutive calls will fail if they try to map the whole thing -- unless we free all TDP page tables first.
Quoted 4 lines
I don't think I have time to spend on this right now, mainly documenting my thoughts, and putting this out here in case you (or anyone else) has enough hatred for the API to go do this before I get the time to.
We're on the same page, this probably doesn't even crack my Top 10 list of things I want to fix in selftests. Ok, it's probably in the Top 10, but not the Top 5.
I responded on-list mostly to point out the problem in case someone else happens to have in-flight code that will be affected.
Re: [PATCH v7 15/17] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
Quoted 19 lines
Quoted 19 lines
Generally speaking, the
tdp_identity_map_default_memslots()API sucks. It has to be called after all mappings/allocations are done, which is soooo user unfriendly and when it breaks it takes time to figure out what's going on.I think it should go away. Ideally either:
(a) When TDP is enabled in the VM,tdp_identity_map_default_memslots()is called, and then every time we create a new mapping in the VM we mirror it in TDP page tables.(b) Every time we update guest mappings, we mark the TDP page tables as "out of sync", and we "resync" on vCPU run as needed.
Either way, it is not as straightforward as just calling
tdp_identity_map_default_memslots(), because the current code reuses the same logic as stage-1 mappings, and it fails if there's already a PTE. So consecutive calls will fail if they try to map the whole thing -- unless we free all TDP page tables first.
Quoted 7 lines
Quoted 4 lines
I don't think I have time to spend on this right now, mainly documenting my thoughts, and putting this out here in case you (or anyone else) has enough hatred for the API to go do this before I get the time to.
We're on the same page, this probably doesn't even crack my Top 10 list of things I want to fix in selftests. Ok, it's probably in the Top 10, but not the Top 5.
I suspect at some point I won't be able to help myself, just because I am curious how this turns out :)
Quoted 4 lines
I responded on-list mostly to point out the problem in case someone else happens to have in-flight code that will be affected.
PATCH v7 16/17 KVM: selftests: Drop L1-provided stacks for L2 guests on x86
In reply to Yosry AhmedNow that a dedicated page is allocated for L2's stack and stuffed in RSP, the L1-provided stack is unused. Drop the stacks allocated by L1 guest code for L2 in all x86 tests.
--- tools/testing/selftests/kvm/include/x86/svm_util.h | 2 +- tools/testing/selftests/kvm/include/x86/vmx.h | 2 +- tools/testing/selftests/kvm/lib/x86/memstress.c | 14 ++------------ tools/testing/selftests/kvm/lib/x86/svm.c | 2 +- tools/testing/selftests/kvm/lib/x86/vmx.c | 2 +- tools/testing/selftests/kvm/x86/aperfmperf_test.c | 9 ++------- .../selftests/kvm/x86/evmcs_smm_controls_test.c | 5 +---- tools/testing/selftests/kvm/x86/hyperv_evmcs.c | 6 +----- tools/testing/selftests/kvm/x86/hyperv_svm_test.c | 6 +----- tools/testing/selftests/kvm/x86/kvm_buslock_test.c | 9 ++------- .../selftests/kvm/x86/nested_close_kvm_test.c | 12 ++---------- .../selftests/kvm/x86/nested_dirty_log_test.c | 8 ++------ .../selftests/kvm/x86/nested_emulation_test.c | 4 ++-- .../selftests/kvm/x86/nested_exceptions_test.c | 9 ++------- .../selftests/kvm/x86/nested_invalid_cr3_test.c | 10 ++-------- .../selftests/kvm/x86/nested_tdp_fault_test.c | 9 ++------- .../selftests/kvm/x86/nested_tsc_adjust_test.c | 10 ++-------- .../selftests/kvm/x86/nested_tsc_scaling_test.c | 10 ++-------- .../selftests/kvm/x86/nested_vmsave_vmload_test.c | 6 +----- tools/testing/selftests/kvm/x86/smm_test.c | 8 ++------ tools/testing/selftests/kvm/x86/state_test.c | 11 ++--------- tools/testing/selftests/kvm/x86/svm_int_ctl_test.c | 5 +---- .../selftests/kvm/x86/svm_lbr_nested_state.c | 6 +----- .../selftests/kvm/x86/svm_nested_clear_efer_svme.c | 7 +------ .../selftests/kvm/x86/svm_nested_shutdown_test.c | 5 +---- .../kvm/x86/svm_nested_soft_inject_test.c | 6 +----- .../selftests/kvm/x86/svm_nested_vmcb12_gpa.c | 13 ++++--------- tools/testing/selftests/kvm/x86/svm_vmcall_test.c | 5 +---- .../selftests/kvm/x86/triple_fault_event_test.c | 9 ++------- .../selftests/kvm/x86/vmx_apic_access_test.c | 5 +---- .../selftests/kvm/x86/vmx_apicv_updates_test.c | 4 +--- .../kvm/x86/vmx_invalid_nested_guest_state.c | 6 +----- .../selftests/kvm/x86/vmx_nested_la57_state_test.c | 5 +---- .../selftests/kvm/x86/vmx_preemption_timer_test.c | 5 +---- 34 files changed, 51 insertions(+), 184 deletions(-)
tools/testing/selftests/kvm/include/x86/svm_util.h +1 -1
@@ -60,7 +60,7 @@ static inline void vmmcall(void) )struct svm_test_data *vcpu_alloc_svm (struct kvm_vm *vm,gva_t *p_svm_gva); -void generic_svm_setup (struct svm_test_data *svm,void *guest_rip,void *guest_rsp); +void generic_svm_setup (struct svm_test_data *svm,void *guest_rip);void run_guest (struct vmcb *vmcb,u64 vmcb_gpa);static inline bool kvm_cpu_has_npt (void )
tools/testing/selftests/kvm/include/x86/vmx.h +1 -1
@@ -554,7 +554,7 @@ union vmx_ctrl_msr {struct vmx_pages *vcpu_alloc_vmx (struct kvm_vm *vm,gva_t *p_vmx_gva);bool prepare_for_vmx_operation (struct vmx_pages *vmx); -void prepare_vmcs (struct vmx_pages *vmx,void *guest_rip,void *guest_rsp); +void prepare_vmcs (struct vmx_pages *vmx,void *guest_rip);bool load_vmcs (struct vmx_pages *vmx);bool ept_1g_pages_supported (void );
tools/testing/selftests/kvm/lib/x86/memstress.c +2 -12
@@ -30,21 +30,15 @@ __asm__(" ud2;" ); -#define L2_GUEST_STACK_SIZE 64 -static void l1_vmx_code (struct vmx_pages *vmx,u64 vcpu_id) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -unsigned long *rsp; -GUEST_ASSERT (vmx->vmcs_gpa);GUEST_ASSERT (prepare_for_vmx_operation (vmx));GUEST_ASSERT (load_vmcs (vmx));GUEST_ASSERT (ept_1g_pages_supported ()); - rsp = &l2_guest_stack[L2_GUEST_STACK_SIZE -1 ]; *(u64 *)vmx->stack = vcpu_id; -prepare_vmcs (vmx, memstress_l2_guest_entry, rsp); +prepare_vmcs (vmx, memstress_l2_guest_entry);GUEST_ASSERT (!vmlaunch ());GUEST_ASSERT_EQ (vmreadz (VM_EXIT_REASON ),EXIT_REASON_VMCALL );
@@ -53,12 +47,8 @@ static void l1_vmx_code(struct vmx_pages *vmx, u64 vcpu_id)static void l1_svm_code (struct svm_test_data *svm,u64 vcpu_id) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -unsigned long *rsp; - - rsp = &l2_guest_stack[L2_GUEST_STACK_SIZE -1 ]; *(u64 *)svm->stack = vcpu_id; -generic_svm_setup (svm, memstress_l2_guest_entry, rsp); +generic_svm_setup (svm, memstress_l2_guest_entry);run_guest (svm->vmcb, svm->vmcb_gpa);GUEST_ASSERT_EQ (svm->vmcb->control.exit_code,SVM_EXIT_VMMCALL );
tools/testing/selftests/kvm/lib/x86/svm.c +1 -1
@@ -83,7 +83,7 @@ void vm_enable_npt(struct kvm_vm *vm)tdp_mmu_init (vm, vm->mmu.pgtable_levels, &pte_masks); } -void generic_svm_setup (struct svm_test_data *svm,void *guest_rip,void *guest_rsp) +void generic_svm_setup (struct svm_test_data *svm,void *guest_rip) {struct vmcb *vmcb = svm->vmcb;u64 vmcb_gpa = svm->vmcb_gpa;
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
@@ -368,7 +368,7 @@ static inline void init_vmcs_guest_state(void *rip, void *rsp)vmwrite (GUEST_SYSENTER_EIP ,vmreadz (HOST_IA32_SYSENTER_EIP )); } -void prepare_vmcs (struct vmx_pages *vmx,void *guest_rip,void *guest_rsp) +void prepare_vmcs (struct vmx_pages *vmx,void *guest_rip) {init_vmcs_control_fields (vmx);init_vmcs_host_state ();
tools/testing/selftests/kvm/x86/aperfmperf_test.c +2 -7
@@ -54,8 +54,6 @@ static void guest_read_aperf_mperf(void)GUEST_SYNC2 (rdmsr (MSR_IA32_APERF ),rdmsr (MSR_IA32_MPERF )); } -#define L2_GUEST_STACK_SIZE 64 -static void l2_guest_code (void ) {guest_read_aperf_mperf ();
@@ -64,21 +62,18 @@ static void l2_guest_code(void)static void l1_svm_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb; -generic_svm_setup (svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);run_guest (vmcb, svm->vmcb_gpa); }static void l1_vmx_code (struct vmx_pages *vmx) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -GUEST_ASSERT_EQ (prepare_for_vmx_operation (vmx), true);GUEST_ASSERT_EQ (load_vmcs (vmx), true); -prepare_vmcs (vmx,NULL , &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx,NULL );/* * Enable MSR bitmaps (the bitmap itself is allocated, zeroed, and set
tools/testing/selftests/kvm/x86/evmcs_smm_controls_test.c +1 -4
@@ -52,8 +52,6 @@ static void l2_guest_code(void)static void guest_code (struct vmx_pages *vmx_pages,struct hyperv_test_pages *hv_pages) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];/* Set up Hyper-V enlightenments and eVMCS */ wrmsr (HV_X64_MSR_GUEST_OS_ID ,HYPERV_LINUX_OS_ID );
@@ -62,8 +60,7 @@ static void guest_code(struct vmx_pages *vmx_pages,GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_evmcs (hv_pages)); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);GUEST_ASSERT (!vmlaunch ());
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +1 -5
@@ -78,9 +78,6 @@ void l2_guest_code(void)void guest_code (struct vmx_pages *vmx_pages,struct hyperv_test_pages *hv_pages,gpa_t hv_hcall_page_gpa) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -wrmsr (HV_X64_MSR_GUEST_OS_ID ,HYPERV_LINUX_OS_ID );wrmsr (HV_X64_MSR_HYPERCALL , hv_hcall_page_gpa);
@@ -100,8 +97,7 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,GUEST_SYNC (4 );GUEST_ASSERT (vmptrstz () == hv_pages->enlightened_vmcs_gpa); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);GUEST_SYNC (5 );GUEST_ASSERT (vmptrstz () == hv_pages->enlightened_vmcs_gpa);
tools/testing/selftests/kvm/x86/hyperv_svm_test.c +1 -5
@@ -18,8 +18,6 @@#include "svm_util.h" #include "hyperv.h" -#define L2_GUEST_STACK_SIZE 256 -/* Exit to L1 from L2 with RDMSR instruction */ static inline void rdmsr_from_l2 (u32 msr) {
@@ -69,7 +67,6 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm, struct hyperv_test_pages *hv_pages, gpa_t pgs_gpa) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;struct hv_vmcb_enlightenments *hve = &vmcb->control.hv_enlightenments;
@@ -81,8 +78,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm,GUEST_ASSERT (svm->vmcb_gpa);/* Prepare for L2 execution. */ -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);/* L2 TLB flush setup */ hve->partition_assist_page = hv_pages->partition_assist_gpa;
tools/testing/selftests/kvm/x86/kvm_buslock_test.c +2 -7
@@ -26,8 +26,6 @@ static void guest_generate_buslocks(void)atomic_inc (val); } -#define L2_GUEST_STACK_SIZE 64 -static void l2_guest_code (void ) {guest_generate_buslocks ();
@@ -36,21 +34,18 @@ static void l2_guest_code(void)static void l1_svm_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb; -generic_svm_setup (svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);run_guest (vmcb, svm->vmcb_gpa); }static void l1_vmx_code (struct vmx_pages *vmx) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -GUEST_ASSERT_EQ (prepare_for_vmx_operation (vmx), true);GUEST_ASSERT_EQ (load_vmcs (vmx), true); -prepare_vmcs (vmx,NULL , &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx,NULL );GUEST_ASSERT (!vmwrite (GUEST_RIP , (u64 )l2_guest_code));GUEST_ASSERT (!vmlaunch ());
tools/testing/selftests/kvm/x86/nested_close_kvm_test.c +2 -10
@@ -21,8 +21,6 @@ enum { PORT_L0_EXIT = 0x2000, }; -#define L2_GUEST_STACK_SIZE 64 -static void l2_guest_code (void ) {/* Exit to L0 */
@@ -32,14 +30,11 @@ static void l2_guest_code(void)static void l1_vmx_code (struct vmx_pages *vmx_pages) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages));/* Prepare the VMCS for L2 execution. */ -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);GUEST_ASSERT (!vmlaunch ());GUEST_ASSERT (0 );
@@ -47,11 +42,8 @@ static void l1_vmx_code(struct vmx_pages *vmx_pages)static void l1_svm_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -/* Prepare the VMCB for L2 execution. */ -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);run_guest (svm->vmcb, svm->vmcb_gpa);GUEST_ASSERT (0 );
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +2 -6
@@ -40,8 +40,6 @@#define TEST_HVA (vm, idx) addr_gpa2hva(vm, TEST_GPA(idx)) -#define L2_GUEST_STACK_SIZE 64 -/* Use the page offset bits to communicate the access+fault type. */ #define TEST_SYNC_READ_FAULT BIT(0)#define TEST_SYNC_WRITE_FAULT BIT(1)
@@ -92,7 +90,6 @@ static void l2_guest_code_tdp_disabled(void)void l1_vmx_code (struct vmx_pages *vmx) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];void *l2_rip;GUEST_ASSERT (vmx->vmcs_gpa);
@@ -104,7 +101,7 @@ void l1_vmx_code(struct vmx_pages *vmx)else l2_rip = l2_guest_code_tdp_disabled; -prepare_vmcs (vmx, l2_rip, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx, l2_rip);GUEST_SYNC (TEST_SYNC_NO_FAULT );GUEST_ASSERT (!vmlaunch ());
@@ -115,7 +112,6 @@ void l1_vmx_code(struct vmx_pages *vmx)static void l1_svm_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];void *l2_rip;if (svm->ncr3_gpa)
@@ -123,7 +119,7 @@ static void l1_svm_code(struct svm_test_data *svm)else l2_rip = l2_guest_code_tdp_disabled; -generic_svm_setup (svm, l2_rip, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_rip);GUEST_SYNC (TEST_SYNC_NO_FAULT );run_guest (svm->vmcb, svm->vmcb_gpa);
tools/testing/selftests/kvm/x86/nested_emulation_test.c +2 -2
@@ -57,7 +57,7 @@ static void guest_code(void *test_data)struct svm_test_data *svm = test_data;struct vmcb *vmcb = svm->vmcb; -generic_svm_setup (svm,NULL ,NULL ); +generic_svm_setup (svm,NULL ); vmcb->save.idtr.limit =0 ; vmcb->save.rip = (u64 )l2_guest_code;
@@ -69,7 +69,7 @@ static void guest_code(void *test_data)GUEST_ASSERT (prepare_for_vmx_operation (test_data));GUEST_ASSERT (load_vmcs (test_data)); -prepare_vmcs (test_data,NULL ,NULL ); +prepare_vmcs (test_data,NULL );GUEST_ASSERT (!vmwrite (GUEST_IDTR_LIMIT ,0 ));GUEST_ASSERT (!vmwrite (GUEST_RIP , (u64 )l2_guest_code));GUEST_ASSERT (!vmwrite (EXCEPTION_BITMAP ,0 ));
tools/testing/selftests/kvm/x86/nested_exceptions_test.c +2 -7
@@ -5,8 +5,6 @@#include "vmx.h" #include "svm_util.h" -#define L2_GUEST_STACK_SIZE 256 -/* * Arbitrary, never shoved into KVM/hardware, just need to avoid conflict with * the "real" exceptions used, #SS/#GP/#DF (12/13/8).
@@ -91,9 +89,8 @@ static void svm_run_l2(struct svm_test_data *svm, void *l2_code, int vector,static void l1_svm_code (struct svm_test_data *svm) {struct vmcb_control_area *ctrl = &svm->vmcb->control; -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -generic_svm_setup (svm,NULL , &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm,NULL ); svm->vmcb->save.idtr.limit =0 ; ctrl->intercept |=BIT_ULL (INTERCEPT_SHUTDOWN );
@@ -128,13 +125,11 @@ static void vmx_run_l2(void *l2_code, int vector, u32 error_code)static void l1_vmx_code (struct vmx_pages *vmx) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -GUEST_ASSERT_EQ (prepare_for_vmx_operation (vmx), true);GUEST_ASSERT_EQ (load_vmcs (vmx), true); -prepare_vmcs (vmx,NULL , &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx,NULL );GUEST_ASSERT_EQ (vmwrite (GUEST_IDTR_LIMIT ,0 ),0 );/*
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c +2 -8
@@ -11,8 +11,6 @@#include "kselftest.h" -#define L2_GUEST_STACK_SIZE 64 -static void l2_guest_code (void ) {vmcall ();
@@ -20,11 +18,9 @@ static void l2_guest_code(void)static void l1_svm_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];uintptr_t save_cr3; -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);/* Try to run L2 with invalid CR3 and make sure it fails */ save_cr3 = svm->vmcb->save.cr3;
@@ -42,14 +38,12 @@ static void l1_svm_code(struct svm_test_data *svm)static void l1_vmx_code (struct vmx_pages *vmx_pages) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];uintptr_t save_cr3;GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages)); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);/* Try to run L2 with invalid CR3 and make sure it fails */ save_cr3 =vmreadz (GUEST_CR3 );
tools/testing/selftests/kvm/x86/nested_tdp_fault_test.c +2 -7
@@ -9,8 +9,6 @@#include "svm_util.h" #include "vmx.h" -#define L2_GUEST_STACK_SIZE 64 -enum test_type {TEST_FINAL_PAGE_UNMAPPED ,/* Final data page not present */ TEST_PT_PAGE_UNMAPPED ,/* Page table page not present */
@@ -54,14 +52,13 @@ static void l2_guest_code_ins(void)static void l1_vmx_code (struct vmx_pages *vmx,u64 expected_fault_gpa,u64 test_type) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u64 exit_qual;GUEST_ASSERT (vmx->vmcs_gpa);GUEST_ASSERT (prepare_for_vmx_operation (vmx));GUEST_ASSERT (load_vmcs (vmx)); -prepare_vmcs (vmx, l2_entry, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx, l2_entry);GUEST_ASSERT (!vmlaunch ());
@@ -120,12 +117,10 @@ static void l1_vmx_code(struct vmx_pages *vmx, u64 expected_fault_gpa,static void l1_svm_code (struct svm_test_data *svm,u64 expected_fault_gpa,u64 test_type) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;u64 exit_info_1; -generic_svm_setup (svm, l2_entry, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_entry);run_guest (vmcb, svm->vmcb_gpa);
tools/testing/selftests/kvm/x86/nested_tsc_adjust_test.c +2 -8
@@ -34,8 +34,6 @@#define TSC_ADJUST_VALUE (1ll << 32)#define TSC_OFFSET_VALUE -(1ll << 48) -#define L2_GUEST_STACK_SIZE 64 -enum {PORT_ABORT =0x1000 ,PORT_REPORT ,
@@ -75,8 +73,6 @@ static void l2_guest_code(void)static void l1_guest_code (void *data) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -/* Set TSC from L1 and make sure TSC_ADJUST is updated correctly */ GUEST_ASSERT (rdtsc () <TSC_ADJUST_VALUE );wrmsr (MSR_IA32_TSC ,rdtsc () -TSC_ADJUST_VALUE );
@@ -93,8 +89,7 @@ static void l1_guest_code(void *data)GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages)); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code); control =vmreadz (CPU_BASED_VM_EXEC_CONTROL ); control |=CPU_BASED_USE_MSR_BITMAPS |CPU_BASED_USE_TSC_OFFSETTING ;vmwrite (CPU_BASED_VM_EXEC_CONTROL , control);
@@ -105,8 +100,7 @@ static void l1_guest_code(void *data) }else {struct svm_test_data *svm = data; -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code); svm->vmcb->control.tsc_offset =TSC_OFFSET_VALUE ;run_guest (svm->vmcb, svm->vmcb_gpa);
tools/testing/selftests/kvm/x86/nested_tsc_scaling_test.c +2 -8
@@ -22,8 +22,6 @@#define TSC_OFFSET_L2 ((u64)-33125236320908)#define TSC_MULTIPLIER_L2 (L2_SCALE_FACTOR << 48) -#define L2_GUEST_STACK_SIZE 64 -enum {USLEEP ,UCHECK_L1 ,UCHECK_L2 };#define GUEST_SLEEP (sec) ucall(UCALL_SYNC, 2, USLEEP, sec)#define GUEST_CHECK (level, freq) ucall(UCALL_SYNC, 2, level, freq)
@@ -82,13 +80,10 @@ static void l2_guest_code(void)static void l1_svm_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -/* check that L1's frequency looks alright before launching L2 */ check_tsc_freq (UCHECK_L1 ); -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);/* enable TSC scaling for L2 */ wrmsr (MSR_AMD64_TSC_RATIO ,L2_SCALE_FACTOR <<32 );
@@ -105,7 +100,6 @@ static void l1_svm_code(struct svm_test_data *svm)static void l1_vmx_code (struct vmx_pages *vmx_pages) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u32 control;/* check that L1's frequency looks alright before launching L2 */
@@ -115,7 +109,7 @@ static void l1_vmx_code(struct vmx_pages *vmx_pages)GUEST_ASSERT (load_vmcs (vmx_pages));/* prepare the VMCS for L2 execution */ -prepare_vmcs (vmx_pages, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);/* enable TSC offsetting and TSC scaling for L2 */ control =vmreadz (CPU_BASED_VM_EXEC_CONTROL );
tools/testing/selftests/kvm/x86/nested_vmsave_vmload_test.c +1 -5
@@ -28,8 +28,6 @@#define TEST_VMCB_L2_GPA TEST_VMCB_L1_GPA(0) -#define L2_GUEST_STACK_SIZE 64 -static void l2_guest_code_vmsave (void ) { asmvolatile ("vmsave %0" : :"a" (TEST_VMCB_L2_GPA ) :"memory" );
@@ -70,10 +68,8 @@ static void l2_guest_code_vmcb1(void)static void l1_guest_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -/* Each test case initializes the guest RIP below */ -generic_svm_setup (svm,NULL , &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm,NULL );/* Set VMSAVE/VMLOAD intercepts and make sure they work with.. */ svm->vmcb->control.intercept |= (BIT_ULL(INTERCEPT_VMSAVE) |
tools/testing/selftests/kvm/x86/smm_test.c +2 -6
@@ -63,8 +63,6 @@ static void l2_guest_code(void)static void guest_code (void *arg) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u64 apicbase =rdmsr (MSR_IA32_APICBASE );struct svm_test_data *svm = arg;struct vmx_pages *vmx_pages = arg;
@@ -81,13 +79,11 @@ static void guest_code(void *arg)if (arg) {if (this_cpu_has (X86_FEATURE_SVM )) { -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code); }else {GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages)); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code); }sync_with_host (5 );
tools/testing/selftests/kvm/x86/state_test.c +2 -9
@@ -19,8 +19,6 @@#include "vmx.h" #include "svm_util.h" -#define L2_GUEST_STACK_SIZE 256 -void svm_l2_guest_code (void ) {GUEST_SYNC (4 );
@@ -35,13 +33,11 @@ void svm_l2_guest_code(void)static void svm_l1_guest_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;GUEST_ASSERT (svm->vmcb_gpa);/* Prepare for L2 execution. */ -generic_svm_setup (svm, svm_l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, svm_l2_guest_code); vmcb->control.int_ctl |= (V_GIF_ENABLE_MASK |V_GIF_MASK );
@@ -78,8 +74,6 @@ void vmx_l2_guest_code(void)static void vmx_l1_guest_code (struct vmx_pages *vmx_pages) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -GUEST_ASSERT (vmx_pages->vmcs_gpa);GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_SYNC (3 );
@@ -89,8 +83,7 @@ static void vmx_l1_guest_code(struct vmx_pages *vmx_pages)GUEST_SYNC (4 );GUEST_ASSERT (vmptrstz () == vmx_pages->vmcs_gpa); -prepare_vmcs (vmx_pages, vmx_l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, vmx_l2_guest_code);GUEST_SYNC (5 );GUEST_ASSERT (vmptrstz () == vmx_pages->vmcs_gpa);
tools/testing/selftests/kvm/x86/svm_int_ctl_test.c +1 -4
@@ -54,15 +54,12 @@ static void l2_guest_code(struct svm_test_data *svm)static void l1_guest_code (struct svm_test_data *svm) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;x2apic_enable ();/* Prepare for L2 execution. */ -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);/* No virtual interrupt masking */ vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK ;
tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c +1 -5
@@ -9,8 +9,6 @@#include "svm_util.h" -#define L2_GUEST_STACK_SIZE 64 -#define DO_BRANCH () do { asm volatile("jmp 1f\n 1: nop"); } while (0)struct lbr_branch {
@@ -55,7 +53,6 @@ static void l2_guest_code(struct svm_test_data *svm)static void l1_guest_code (struct svm_test_data *svm,bool nested_lbrv) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;struct lbr_branch l1_branch;
@@ -65,8 +62,7 @@ static void l1_guest_code(struct svm_test_data *svm, bool nested_lbrv)CHECK_BRANCH_MSRS (&l1_branch);/* Run L2, which will also do the same */ -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);if (nested_lbrv) vmcb->control.misc_ctl2 =SVM_MISC2_ENABLE_V_LBR ;
tools/testing/selftests/kvm/x86/svm_nested_clear_efer_svme.c +1 -6
@@ -8,8 +8,6 @@#include "kselftest.h" -#define L2_GUEST_STACK_SIZE 64 -static void l2_guest_code (void ) {unsigned long efer =rdmsr (MSR_EFER );
@@ -24,10 +22,7 @@ static void l2_guest_code(void)static void l1_guest_code (struct svm_test_data *svm) { -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; - -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);run_guest (svm->vmcb, svm->vmcb_gpa);/* Unreachable, L1 should be shutdown */
tools/testing/selftests/kvm/x86/svm_nested_shutdown_test.c +1 -4
@@ -19,12 +19,9 @@ static void l2_guest_code(struct svm_test_data *svm)static void l1_guest_code (struct svm_test_data *svm,struct idt_entry *idt) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb; -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code); vmcb->control.intercept &= ~(BIT (INTERCEPT_SHUTDOWN ));
tools/testing/selftests/kvm/x86/svm_nested_soft_inject_test.c +1 -5
@@ -78,17 +78,13 @@ static void l2_guest_code_nmi(void)static void l1_guest_code (struct svm_test_data *svm,u64 is_nmi,u64 idt_alt) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;if (is_nmi)x2apic_enable ();/* Prepare for L2 execution. */ -generic_svm_setup (svm, - is_nmi ? l2_guest_code_nmi : l2_guest_code_int, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, is_nmi ? l2_guest_code_nmi : l2_guest_code_int); vmcb->control.intercept_exceptions |=BIT (PF_VECTOR ) |BIT (UD_VECTOR ); vmcb->control.intercept |=BIT (INTERCEPT_NMI ) |BIT (INTERCEPT_HLT );
tools/testing/selftests/kvm/x86/svm_nested_vmcb12_gpa.c +4 -9
@@ -9,14 +9,9 @@#include "kvm_test_harness.h" #include "test_util.h" - -#define L2_GUEST_STACK_SIZE 64 -#define SYNC_GP 101#define SYNC_L2_STARTED 102 -static unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -static void guest_gp_handler (struct ex_regs *regs) {GUEST_SYNC (SYNC_GP );
@@ -30,28 +25,28 @@ static void l2_code(void)static void l1_vmrun (struct svm_test_data *svm,gpa_t gpa) { -generic_svm_setup (svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_code); asmvolatile ("vmrun %[gpa]" : : [gpa]"a" (gpa) :"memory" ); }static void l1_vmload (struct svm_test_data *svm,gpa_t gpa) { -generic_svm_setup (svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_code); asmvolatile ("vmload %[gpa]" : : [gpa]"a" (gpa) :"memory" ); }static void l1_vmsave (struct svm_test_data *svm,gpa_t gpa) { -generic_svm_setup (svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_code); asmvolatile ("vmsave %[gpa]" : : [gpa]"a" (gpa) :"memory" ); }static void l1_vmexit (struct svm_test_data *svm,gpa_t gpa) { -generic_svm_setup (svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_code);run_guest (svm->vmcb, svm->vmcb_gpa);GUEST_ASSERT (svm->vmcb->control.exit_code ==SVM_EXIT_VMMCALL );
tools/testing/selftests/kvm/x86/svm_vmcall_test.c +1 -4
@@ -19,13 +19,10 @@ static void l2_guest_code(struct svm_test_data *svm)static void l1_guest_code (struct svm_test_data *svm) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];struct vmcb *vmcb = svm->vmcb;/* Prepare for L2 execution. */ -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);run_guest (vmcb, svm->vmcb_gpa);
tools/testing/selftests/kvm/x86/triple_fault_event_test.c +2 -7
@@ -21,9 +21,6 @@ static void l2_guest_code(void) : : [port] "d" (ARBITRARY_IO_PORT) : "rax"); } -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -void l1_guest_code_vmx (struct vmx_pages *vmx) {
@@ -31,8 +28,7 @@ void l1_guest_code_vmx(struct vmx_pages *vmx)GUEST_ASSERT (prepare_for_vmx_operation (vmx));GUEST_ASSERT (load_vmcs (vmx)); -prepare_vmcs (vmx, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx, l2_guest_code);GUEST_ASSERT (!vmlaunch ());/* L2 should triple fault after a triple fault event injected. */
@@ -44,8 +40,7 @@ void l1_guest_code_svm(struct svm_test_data *svm) {struct vmcb *vmcb = svm->vmcb; -generic_svm_setup (svm, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +generic_svm_setup (svm, l2_guest_code);/* don't intercept shutdown to test the case of SVM allowing to do so */ vmcb->control.intercept &= ~(BIT (INTERCEPT_SHUTDOWN ));
tools/testing/selftests/kvm/x86/vmx_apic_access_test.c +1 -4
@@ -36,16 +36,13 @@ static void l2_guest_code(void)static void l1_guest_code (struct vmx_pages *vmx_pages,unsigned long high_gpa) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u32 control;GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages));/* Prepare the VMCS for L2 execution. */ -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code); control =vmreadz (CPU_BASED_VM_EXEC_CONTROL ); control |=CPU_BASED_ACTIVATE_SECONDARY_CONTROLS ;vmwrite (CPU_BASED_VM_EXEC_CONTROL , control);
tools/testing/selftests/kvm/x86/vmx_apicv_updates_test.c +1 -3
@@ -31,15 +31,13 @@ static void l2_guest_code(void)static void l1_guest_code (struct vmx_pages *vmx_pages) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u32 control;GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages));/* Prepare the VMCS for L2 execution. */ -prepare_vmcs (vmx_pages, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code); control =vmreadz (CPU_BASED_VM_EXEC_CONTROL ); control |=CPU_BASED_USE_MSR_BITMAPS ;vmwrite (CPU_BASED_VM_EXEC_CONTROL , control);
tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +1 -5
@@ -25,15 +25,11 @@ static void l2_guest_code(void)static void l1_guest_code (struct vmx_pages *vmx_pages) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ]; -GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages));/* Prepare the VMCS for L2 execution. */ -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);/* * L2 must be run without unrestricted guest, verify that the selftests
tools/testing/selftests/kvm/x86/vmx_nested_la57_state_test.c +1 -4
@@ -27,8 +27,6 @@ static void l2_guest_code(void)static void l1_guest_code (struct vmx_pages *vmx_pages) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u64 guest_cr4;gpa_t pml5_pa, pml4_pa;u64 *pml5;
@@ -42,8 +40,7 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_ASSERT (load_vmcs (vmx_pages)); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);/* * Set up L2 with a 4-level page table by pointing its CR3 to
tools/testing/selftests/kvm/x86/vmx_preemption_timer_test.c +1 -4
@@ -66,8 +66,6 @@ void l2_guest_code(void)void l1_guest_code (struct vmx_pages *vmx_pages) { -#define L2_GUEST_STACK_SIZE 64 -unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE ];u64 l1_vmx_pt_start;u64 l1_vmx_pt_finish;u64 l1_tsc_deadline, l2_tsc_deadline;
@@ -77,8 +75,7 @@ void l1_guest_code(struct vmx_pages *vmx_pages)GUEST_ASSERT (load_vmcs (vmx_pages));GUEST_ASSERT (vmptrstz () == vmx_pages->vmcs_gpa); -prepare_vmcs (vmx_pages, l2_guest_code, - &l2_guest_stack[L2_GUEST_STACK_SIZE ]); +prepare_vmcs (vmx_pages, l2_guest_code);/* * Check for Preemption timer support
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
PATCH v7 17/17 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 with EFER.SVME clear and set, as well as in host mode and guest mode. The test also verifies that updating Host-Only / Guest-Only bits for a PMC works as intended, and that event filtering is still respected.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../kvm/x86/svm_pmu_host_guest_test.c | 216 ++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -119,6 +119,7 @@ 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_nested_vmcb12_gpa 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/x86/svm_pmu_host_guest_test.c new file +216 -0
216 lines not on this page · raw message
Signature · 1 line
2.54.0.794.g4f17f83d09-goog
Sean Christopherson
maintainerRe: [PATCH v7 17/17] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c line 184
Quoted 11 lines · Yosry Ahmed
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +11
-0+int main (int argc,char *argv[]) +{ +struct kvm_pmu_event_filter *filter; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +gva_t svm_gva; + +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); +TEST_REQUIRE (kvm_is_pmu_enabled ()); +TEST_REQUIRE (get_kvm_amd_param_bool ("enable_mediated_pmu" ));
This can and should be handled in a helper, using either host_cpu_is_intel or host_cpu_is_amd_compatible to choose while vendor to query.
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c line 185
Quoted 1 line
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +1
-0+TEST_REQUIRE (host_cpu_is_amd &&kvm_cpu_family () >=0x17 );
Why the family check? Actually, why check host_cpu_is_amd at all?
Re: [PATCH v7 17/17] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
Quoted 17 lines · Sean Christopherson
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c line 184
Quoted 11 lines · Yosry Ahmed
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +11
-0+int main (int argc,char *argv[]) +{ +struct kvm_pmu_event_filter *filter; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +gva_t svm_gva; + +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); +TEST_REQUIRE (kvm_is_pmu_enabled ()); +TEST_REQUIRE (get_kvm_amd_param_bool ("enable_mediated_pmu" ));This can and should be handled in a helper, using either
host_cpu_is_intelorhost_cpu_is_amd_compatibleto choose while vendor to query.
Quoted 3 lines
Quoted 1 line
+
TEST_REQUIRE(host_cpu_is_amd && kvm_cpu_family() >= 0x17);Why the family check? Actually, why check
host_cpu_is_amdat all?
I assume you mean that we don't need host_cpu_is_amd because we require X86_FEATURE_SVM above? Unless H/G emulation doesn't really work for cross-vendor emulation? I honestly don't know.
I will defer to Jim for the family check, I took this as-is. My guess is that maybe older families did not support H/G bits or some of the other PMU features used by the test.
Sean Christopherson
maintainerRe: [PATCH v7 17/17] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
Quoted 18 lines · Yosry Ahmed
Quoted 17 lines · Sean Christopherson
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c line 184
Quoted 11 lines · Yosry Ahmed
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +11
-0+int main (int argc,char *argv[]) +{ +struct kvm_pmu_event_filter *filter; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +gva_t svm_gva; + +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); +TEST_REQUIRE (kvm_is_pmu_enabled ()); +TEST_REQUIRE (get_kvm_amd_param_bool ("enable_mediated_pmu" ));This can and should be handled in a helper, using either
host_cpu_is_intelorhost_cpu_is_amd_compatibleto choose while vendor to query.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+
TEST_REQUIRE(host_cpu_is_amd && kvm_cpu_family() >= 0x17);Why the family check? Actually, why check
host_cpu_is_amdat all?I assume you mean that we don't need
host_cpu_is_amdbecause we requireX86_FEATURE_SVMabove?
Yes.
Quoted 2 lines
Unless H/G emulation doesn't really work for cross-vendor emulation? I honestly don't know.
Not just cross-vendor emulation, KVM and selftests also support Hygon CPUs.
Quoted 3 lines
I will defer to Jim for the family check, I took this as-is. My guess is that maybe older families did not support H/G bits or some of the other PMU features used by the test.
If that's true, then the mediated PMU shouldn't be enabled. If the mediated PMU _can_ be enabled for CPUs that do _and_ don't support H/G bits, then we need to figure out a way to enumerate H/G support to userspace. Because telling userspace to check F/M/S ain't going to fly.
Re: [PATCH v7 17/17] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits
Quoted 34 lines · Sean Christopherson
Quoted 18 lines · Yosry Ahmed
Quoted 17 lines · Sean Christopherson
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c line 184
Quoted 11 lines · Yosry Ahmed
tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c new file +11
-0+int main (int argc,char *argv[]) +{ +struct kvm_pmu_event_filter *filter; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; +gva_t svm_gva; + +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_SVM )); +TEST_REQUIRE (kvm_is_pmu_enabled ()); +TEST_REQUIRE (get_kvm_amd_param_bool ("enable_mediated_pmu" ));This can and should be handled in a helper, using either
host_cpu_is_intelorhost_cpu_is_amd_compatibleto choose while vendor to query.Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+
TEST_REQUIRE(host_cpu_is_amd && kvm_cpu_family() >= 0x17);Why the family check? Actually, why check
host_cpu_is_amdat all?I assume you mean that we don't need
host_cpu_is_amdbecause we requireX86_FEATURE_SVMabove?Yes.
Quoted 2 lines
Unless H/G emulation doesn't really work for cross-vendor emulation? I honestly don't know.
Not just cross-vendor emulation, KVM and selftests also support Hygon CPUs.
Quoted 8 lines
Quoted 3 lines
I will defer to Jim for the family check, I took this as-is. My guess is that maybe older families did not support H/G bits or some of the other PMU features used by the test.
If that's true, then the mediated PMU shouldn't be enabled. If the mediated PMU _can_ be enabled for CPUs that do _and_ don't support H/G bits, then we need to figure out a way to enumerate H/G support to userspace. Because telling userspace to check F/M/S ain't going to fly.
The test counts AMD_ZEN_INSTRUCTIONS_RETIRED, so I tested for "Zen."This is admittedly pedantic, as I don't think AMD has ever changed this event code. Blame AMD for not having any architected events. :)
Sean Christopherson
maintainerQuoted 16 lines · Yosry Ahmed
Jim Mattson (2):
KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated
PMU
KVM: selftests: Addsvm_pmu_host_guest_testfor Host-Only/Guest-Only
bitsYosry Ahmed (15):
KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails
KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN
KVM: nSVM: Move VMRUN instruction retirement after entering guest mode
KVM: x86: Move enable_pmu/enable_mediated_pmu topmu.handpmu.c
KVM: x86/pmu: Renamereprogram_counters()to clarify usage
KVM: x86/pmu: Do a single atomic OR when reprogramming counters
KVM: x86/pmu: Check mediated PMU counter enablement before event
filters
Quoted 8 lines
KVM: x86/pmu: Add support for
KVM_X86_PMU_OP_OPTIONAL_RET0
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in
SVM
KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions
KVM: selftests: Refactor allocating guest stack into a helper
KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
KVM: selftests: Drop L1-provided stacks for L2 guests on x86
This all looks good. I'll get the KVM changes applied and pushed (assuming they pass testing).
For the selftests, I'm going to hold off on applying them until after the initial pull requests for 7.2 are merged into Paolo's tree. The (AWESOME!!!) L2 stack changes are going to cause conflict hell, and it's easy enough for me to carry them out of tree for a few weeks.
I'll send a refreshed version of the selftest once kvm-x86 has settled down for 7.2, e.g. to account for the gpat test as well.
Quoted 30 lines · Sean Christopherson
Quoted 16 lines · Yosry Ahmed
Jim Mattson (2):
KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated
PMU
KVM: selftests: Addsvm_pmu_host_guest_testfor Host-Only/Guest-Only
bitsYosry Ahmed (15):
KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails
KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN
KVM: nSVM: Move VMRUN instruction retirement after entering guest mode
KVM: x86: Move enable_pmu/enable_mediated_pmu topmu.handpmu.c
KVM: x86/pmu: Renamereprogram_counters()to clarify usage
KVM: x86/pmu: Do a single atomic OR when reprogramming counters
KVM: x86/pmu: Check mediated PMU counter enablement before event
filtersQuoted 8 lines
KVM: x86/pmu: Add support for
KVM_X86_PMU_OP_OPTIONAL_RET0
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in
SVM
KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions
KVM: selftests: Refactor allocating guest stack into a helper
KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
KVM: selftests: Drop L1-provided stacks for L2 guests on x86This all looks good. I'll get the KVM changes applied and pushed (assuming they pass testing).
Quoted 7 lines
For the selftests, I'm going to hold off on applying them until after the initial pull requests for 7.2 are merged into Paolo's tree. The (AWESOME!!!) L2 stack changes are going to cause conflict hell, and it's easy enough for me to carry them out of tree for a few weeks.
I'll send a refreshed version of the selftest once kvm-x86 has settled down for 7.2, e.g. to account for the gpat test as well.
So do you plan on getting it merged later in the 7.2 merge window or for 7.3? I think the former but I am not quite sure.
Sean Christopherson
maintainerQuoted 36 lines · Yosry Ahmed
Quoted 35 lines · Sean Christopherson
Quoted 16 lines · Yosry Ahmed
Jim Mattson (2):
KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated
PMU
KVM: selftests: Addsvm_pmu_host_guest_testfor Host-Only/Guest-Only
bitsYosry Ahmed (15):
KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails
KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN
KVM: nSVM: Move VMRUN instruction retirement after entering guest mode
KVM: x86: Move enable_pmu/enable_mediated_pmu topmu.handpmu.c
KVM: x86/pmu: Renamereprogram_counters()to clarify usage
KVM: x86/pmu: Do a single atomic OR when reprogramming counters
KVM: x86/pmu: Check mediated PMU counter enablement before event
filtersQuoted 8 lines
KVM: x86/pmu: Add support for
KVM_X86_PMU_OP_OPTIONAL_RET0
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in
SVM
KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions
KVM: selftests: Refactor allocating guest stack into a helper
KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
KVM: selftests: Drop L1-provided stacks for L2 guests on x86This all looks good. I'll get the KVM changes applied and pushed (assuming they pass testing).
For the selftests, I'm going to hold off on applying them until after the initial pull requests for 7.2 are merged into Paolo's tree. The (AWESOME!!!) L2 stack changes are going to cause conflict hell, and it's easy enough for me to carry them out of tree for a few weeks.
Quoted 5 lines
Quoted 2 lines
I'll send a refreshed version of the selftest once kvm-x86 has settled down for 7.2, e.g. to account for the gpat test as well.
So do you plan on getting it merged later in the 7.2 merge window or for 7.3? I think the former but I am not quite sure.
Ideally, later in the 7.2 merge window. Worst case scenario, super duper early in kvm/next for 7.3, so that developers (and me) can build on the functionality.
Run below KVM PMU selftests on SPR for both mediated vPMU and legacy emulated vPMU modes, no issue is found.
- pmu_counters_test
- pmu_event_filter_test
- vmx_pmu_caps_test
I know the patches have been merged, so just post the test results here. Thanks.
Quoted 149 lines · Yosry Ahmed
v7 of Jim and myself's series adding support for AMD's Host-Only and Guest-Only performance counter eventsel bits in KVM's mediated PMU passthrough implementation. The series keeps growing in size over versions. This is Sean's fault (as usual), as he keeps finding subtle problems and making excellent suggestions.
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 nested transitions and EFER.SVME changes such that counters only count in the appropriate mode.
The series grew significantly after v4, as it now includes semi-related nSVM fixups and selftests cleanups needed for the series. I think parts of this series can land independently (patches 1-8 and patches 14-16), but then the remaining series would depend on both.
v6 -> v7:
- Instead of creating__kvm_skip_emulated_instruction(), callsvm_skip_emulated_instruction()directly when emulating VMRUN [Sean].
- Split bailing early on failed VMRUN into two patches, one for the actual fix (patch 2) and one for the code cleanup (patch 3).
- Add a Fixes tag for patch 3.
- Indentation fixes and changelog updates.v5 -> v6:
- Use a per-counter PMU callback that is called frompmc_is_locally_enabled(), rather than a single callback for all counters fromkvm_pmu_handle_event(). This is mainly to address the problem Sashiko found wherekvm_pmu_recalc_pmc_emulation()does not capture counter disablement due to Host-Only/Guest-Only bits [Sean].
- Move the bitmap tracking counters using Host-Only/Guest-Only bits from SVM code to the generic PMU code (and in its own patch), as it's now a generic optimization forpmc_is_locally_enabled()that is reused by SVM code on nested transitions [Sean].v4 -> v5:
- Dropped movingleave_guest_mode()andenter_guest_mode()definitions, since the calls to update the vPMU no longer happen within these functions.
- Add PMU helpers refactoring to facilitate SVM usage.
- Added nested SVM fixups to count VMRUN correctly in guest mode when Host-Only/Guest-Only support is enabled [Jim/Sean].
- Update the vPMU synchronously on nested VM-Enter/VM-Exit and EFER.SVME changes, such that counter enablement is reevaluated before the instructions are counted, as the vPMU counts based on the vCPU state at instruction retirement (e.g. using new EFER value when EFER.SVME changes) [Jim/Sean].
- Keep deferring vPMU updates usingKVM_REQ_PMUin thesvm_leave_nested()path to avoid KVM potentially consuming stale state [Sean].
- Use a single PMU callback for reprogramming counters instead of a per-counter callback [Sean].
- Move the bitmap tracking counters into SVM code. The generic vPMU code now only exposes an API to reprogram counters, and an SVM wrapper uses it on nested transitions [Sean].
- Drop the manual stack-alignment fixes in the vPMU selftest, instead rework L2 stack setup in all nested selftests to reuse the allocation and alignment logic used by L1, and completely drop L1-provided stacks for L2 [Sean].v6: https://lore.kernel.org/kvm/[email protected]/
v5: https://lore.kernel.org/kvm/[email protected]/
v4: 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: Addsvm_pmu_host_guest_testfor Host-Only/Guest-Only
bitsYosry Ahmed (15):
KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails
KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN
KVM: nSVM: Move VMRUN instruction retirement after entering guest mode
KVM: x86: Move enable_pmu/enable_mediated_pmu topmu.handpmu.c
KVM: x86/pmu: Renamereprogram_counters()to clarify usage
KVM: x86/pmu: Do a single atomic OR when reprogramming counters
KVM: x86/pmu: Check mediated PMU counter enablement before event
filters
KVM: x86/pmu: Add support forKVM_X86_PMU_OP_OPTIONAL_RET0
KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in
SVM
KVM: x86/pmu: Track mediated PMU counters with mode-specific enables
KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions
KVM: selftests: Refactor allocating guest stack into a helper
KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack
KVM: selftests: Drop L1-provided stacks for L2 guests on x86arch/x86/include/asm/kvm-x86-pmu-ops.h | 5 +- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/include/asm/perf_event.h | 2 + arch/x86/kvm/pmu.c | 21 +- arch/x86/kvm/pmu.h | 44 +++- arch/x86/kvm/svm/nested.c | 43 +++- arch/x86/kvm/svm/pmu.c | 42 ++++ arch/x86/kvm/svm/svm.c | 3 +- arch/x86/kvm/svm/svm.h | 24 ++ arch/x86/kvm/vmx/pmu_intel.c | 2 +- arch/x86/kvm/x86.c | 9 - arch/x86/kvm/x86.h | 3 - tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../selftests/kvm/include/x86/processor.h | 2 + .../selftests/kvm/include/x86/svm_util.h | 5 +- tools/testing/selftests/kvm/include/x86/vmx.h | 4 +- .../testing/selftests/kvm/lib/x86/memstress.c | 19 +- .../testing/selftests/kvm/lib/x86/processor.c | 45 ++-- tools/testing/selftests/kvm/lib/x86/svm.c | 6 +- tools/testing/selftests/kvm/lib/x86/vmx.c | 6 +- .../selftests/kvm/x86/aperfmperf_test.c | 9 +- .../kvm/x86/evmcs_smm_controls_test.c | 5 +- .../testing/selftests/kvm/x86/hyperv_evmcs.c | 6 +- .../selftests/kvm/x86/hyperv_svm_test.c | 6 +- .../selftests/kvm/x86/kvm_buslock_test.c | 9 +- .../selftests/kvm/x86/nested_close_kvm_test.c | 12 +- .../selftests/kvm/x86/nested_dirty_log_test.c | 8 +- .../selftests/kvm/x86/nested_emulation_test.c | 4 +- .../kvm/x86/nested_exceptions_test.c | 9 +- .../kvm/x86/nested_invalid_cr3_test.c | 10 +- .../selftests/kvm/x86/nested_tdp_fault_test.c | 9 +- .../kvm/x86/nested_tsc_adjust_test.c | 10 +- .../kvm/x86/nested_tsc_scaling_test.c | 10 +- .../kvm/x86/nested_vmsave_vmload_test.c | 6 +- tools/testing/selftests/kvm/x86/smm_test.c | 8 +- tools/testing/selftests/kvm/x86/state_test.c | 11 +- .../selftests/kvm/x86/svm_int_ctl_test.c | 5 +- .../selftests/kvm/x86/svm_lbr_nested_state.c | 6 +- .../kvm/x86/svm_nested_clear_efer_svme.c | 7 +- .../kvm/x86/svm_nested_shutdown_test.c | 5 +- .../kvm/x86/svm_nested_soft_inject_test.c | 6 +- .../selftests/kvm/x86/svm_nested_vmcb12_gpa.c | 13 +- .../kvm/x86/svm_pmu_host_guest_test.c | 216 ++++++++++++++++++ .../selftests/kvm/x86/svm_vmcall_test.c | 5 +- .../kvm/x86/triple_fault_event_test.c | 9 +- .../selftests/kvm/x86/vmx_apic_access_test.c | 5 +- .../kvm/x86/vmx_apicv_updates_test.c | 4 +- .../kvm/x86/vmx_invalid_nested_guest_state.c | 6 +- .../kvm/x86/vmx_nested_la57_state_test.c | 5 +- .../kvm/x86/vmx_preemption_timer_test.c | 5 +- 51 files changed, 479 insertions(+), 245 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.cbase-commit:
9f2a49c511cb05b85745e1578e4fd425bff87f58
Quoted 4 lines · Dapeng
Run below KVM PMU selftests on SPR for both mediated vPMU and legacy emulated vPMU modes, no issue is found.
Quoted 8 lines
-
pmu_counters_test-
pmu_event_filter_test-
vmx_pmu_caps_testI know the patches have been merged, so just post the test results here. Thanks.
Thank you for checking!
Sean Christopherson
maintainerQuoted 11 lines · Yosry Ahmed
v7 of Jim and myself's series adding support for AMD's Host-Only and Guest-Only performance counter eventsel bits in KVM's mediated PMU passthrough implementation. The series keeps growing in size over versions. This is Sean's fault (as usual), as he keeps finding subtle problems and making excellent suggestions.
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).
[...]
Applied the KVM changes to kvm-x86 svm. Please yell at me if you haven't seen new versions of the selftests after the 7.2 merge window has opened (assuming Paolo and I are somewhat on top of things and actually get the KVM x86 pull requests merged _before_ Linus opens the window).
[01/17] KVM: nSVM: Stop leaking single-stepping on VMRUN into L2
https://github.com/kvm-x86/linux/commit/8862376260c4 [02/17] KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails https://github.com/kvm-x86/linux/commit/42ff88db18a5 [03/17] KVM: nSVM: Unify RIP and PMU handling calls when emulating VMRUN https://github.com/kvm-x86/linux/commit/0a35c2a051f3 [04/17] KVM: nSVM: Move VMRUN instruction retirement after entering guest mode https://github.com/kvm-x86/linux/commit/9be579d22651 [05/17] KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c https://github.com/kvm-x86/linux/commit/277eb65b6cc3 [06/17] KVM: x86/pmu: Rename reprogram_counters() to clarify usage https://github.com/kvm-x86/linux/commit/103cd7deda58 [07/17] KVM: x86/pmu: Do a single atomic OR when reprogramming counters https://github.com/kvm-x86/linux/commit/e48810e06f45 [08/17] KVM: x86/pmu: Check mediated PMU counter enablement before event filters https://github.com/kvm-x86/linux/commit/232eadc3110b [09/17] KVM: x86/pmu: Add support for KVM_X86_PMU_OP_OPTIONAL_RET0 https://github.com/kvm-x86/linux/commit/f98ef41a5af4 [10/17] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM https://github.com/kvm-x86/linux/commit/2ed72677a5c1 [11/17] KVM: x86/pmu: Track mediated PMU counters with mode-specific enables https://github.com/kvm-x86/linux/commit/c0d4c9c54c25 [12/17] KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions https://github.com/kvm-x86/linux/commit/a02a25a65246 [13/17] KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated PMU https://github.com/kvm-x86/linux/commit/f4805dcf2d21