KVM: x86/svm: Add module param to control PMU virtualization
authorLike Xu <likexu@tencent.com>
Wed, 17 Nov 2021 08:03:04 +0000 (16:03 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 8 Dec 2021 09:24:58 +0000 (04:24 -0500)
For Intel, the guest PMU can be disabled via clearing the PMU CPUID.
For AMD, all hw implementations support the base set of four
performance counters, with current mainstream hardware indicating
the presence of two additional counters via X86_FEATURE_PERFCTR_CORE.

In the virtualized world, the AMD guest driver may detect
the presence of at least one counter MSR. Most hypervisor
vendors would introduce a module param (like lbrv for svm)
to disable PMU for all guests.

Another control proposal per-VM is to pass PMU disable information
via MSR_IA32_PERF_CAPABILITIES or one bit in CPUID Fn4000_00[FF:00].
Both of methods require some guest-side changes, so a module
parameter may not be sufficiently granular, but practical enough.

Signed-off-by: Like Xu <likexu@tencent.com>
Message-Id: <20211117080304.38989-1-likexu@tencent.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/cpuid.c
arch/x86/kvm/svm/pmu.c
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index 07e9215e911d74b911efb98a8eb863a7cc4b8a6b..0b920e12bb6df6732d0dd5b8fe00338f25b1990e 100644 (file)
@@ -523,7 +523,7 @@ void kvm_set_cpu_caps(void)
                F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
                F(3DNOWPREFETCH) | F(OSVW) | 0 /* IBS */ | F(XOP) |
                0 /* SKINIT, WDT, LWP */ | F(FMA4) | F(TBM) |
-               F(TOPOEXT) | F(PERFCTR_CORE)
+               F(TOPOEXT) | 0 /* PERFCTR_CORE */
        );
 
        kvm_cpu_cap_mask(CPUID_8000_0001_EDX,
index b4095dfeeee62fa1702c3aa48d2af059d03e4d28..0cf05e4caa4c70a28b074bee55397f4bacf2aa5e 100644 (file)
@@ -16,6 +16,7 @@
 #include "cpuid.h"
 #include "lapic.h"
 #include "pmu.h"
+#include "svm.h"
 
 enum pmu_type {
        PMU_TYPE_COUNTER = 0,
@@ -100,6 +101,9 @@ static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
 {
        struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
 
+       if (!pmu)
+               return NULL;
+
        switch (msr) {
        case MSR_F15H_PERF_CTL0:
        case MSR_F15H_PERF_CTL1:
index 907ba85609a2f6b832e9f74f70e4a9113697e069..64a10cdb2356abf298ea390411861ac6d1f5b5ea 100644 (file)
@@ -192,6 +192,10 @@ module_param(vgif, int, 0444);
 static int lbrv = true;
 module_param(lbrv, int, 0444);
 
+/* enable/disable PMU virtualization */
+bool pmu = true;
+module_param(pmu, bool, 0444);
+
 static int tsc_scaling = true;
 module_param(tsc_scaling, int, 0444);
 
@@ -954,6 +958,10 @@ static __init void svm_set_cpu_caps(void)
            boot_cpu_has(X86_FEATURE_AMD_SSBD))
                kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
 
+       /* AMD PMU PERFCTR_CORE CPUID */
+       if (pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
+               kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
+
        /* CPUID 0x8000001F (SME/SEV features) */
        sev_set_cpu_caps();
 }
@@ -1087,6 +1095,9 @@ static __init int svm_hardware_setup(void)
                        pr_info("LBR virtualization supported\n");
        }
 
+       if (!pmu)
+               pr_info("PMU virtualization is disabled\n");
+
        svm_set_cpu_caps();
 
        /*
index 929bd60d754d38acc7cd3f51eb7fa7898e3a97f9..a57390473013da68616ee234c553005396d34e8c 100644 (file)
@@ -32,6 +32,7 @@
 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
 extern bool npt_enabled;
 extern bool intercept_smi;
+extern bool pmu;
 
 /*
  * Clean bits in VMCB.