[PATCH v2 48/54] KVM: x86/pmu/svm: Set enable_passthrough_pmu module parameter

Mingwei Zhang
2024-05-06T05:31:58Z

From: Sandipan Das <sandipan.das@amd.com>

Since passthrough PMU can be also used on some AMD platforms, set the
"enable_passthrough_pmu" KVM kernel module parameter to true when the
following conditions are met.
- parameter is set to true when module loaded
- enable_pmu is true
- is running on and AMD CPU
- CPU supports PerfMonV2
- host PMU supports passthrough mode

Signed-off-by: Sandipan Das <sandipan.das@amd.com>
---
arch/x86/kvm/pmu.h | 20 ++++++++++++--------
arch/x86/kvm/svm/svm.c | 2 ++
2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 10553bc1ae1d..02ea90431033 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -196,6 +196,7 @@ extern struct kvm_pmu_emulated_event_selectors kvm_pmu_eventsel;
static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
{
bool is_intel = boot_cpu_data.x86_vendor == X86_VENDOR_INTEL;
+ bool is_amd = boot_cpu_data.x86_vendor == X86_VENDOR_AMD;
int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS;

/*
@@ -223,18 +224,21 @@ static inline void kvm_init_pmu_capability(const struct kvm_pmu_ops *pmu_ops)
enable_pmu = false;
}

- /* Pass-through vPMU is only supported in Intel CPUs. */
- if (!is_intel)
+ /* Pass-through vPMU is only supported in Intel and AMD CPUs. */
+ if (!is_intel && !is_amd)
enable_passthrough_pmu = false;

/*
- * Pass-through vPMU requires at least PerfMon version 4 because the
- * implementation requires the usage of MSR_CORE_PERF_GLOBAL_STATUS_SET
- * for counter emulation as well as PMU context switch. In addition, it
- * requires host PMU support on passthrough mode. Disable pass-through
- * vPMU if any condition fails.
+ * On Intel platforms, pass-through vPMU requires at least PerfMon
+ * version 4 because the implementation requires the usage of
+ * MSR_CORE_PERF_GLOBAL_STATUS_SET for counter emulation as well as
+ * PMU context switch. In addition, it requires host PMU support on
+ * passthrough mode. Disable pass-through vPMU if any condition fails.
+ *
+ * On AMD platforms, pass-through vPMU requires at least PerfMonV2
+ * because MSR_PERF_CNTR_GLOBAL_STATUS_SET is required.
*/
- if (!enable_pmu || kvm_pmu_cap.version < 4 || !kvm_pmu_cap.passthrough)
+ if (!enable_pmu || !kvm_pmu_cap.passthrough || (is_intel && kvm_pmu_cap.version < 4) || (is_amd && kvm_pmu_cap.version < 2))
enable_passthrough_pmu = false;

if (!enable_pmu) {
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index d1a9f9951635..88648b3a9cdd 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -238,6 +238,8 @@ module_param(intercept_smi, bool, 0444);
bool vnmi = true;
module_param(vnmi, bool, 0444);

+module_param(enable_passthrough_pmu, bool, 0444);
+
static bool svm_gp_erratum_intercept = true;

static u8 rsm_ins_bytes[] = "\x0f\xaa";
--
2.45.0.rc1.225.g2a3ae87e7f-goog