Skip to content

Commit 4732f24

Browse files
lkml-likexubonzini
authored andcommitted
KVM: x86: Making the module parameter of vPMU more common
The new module parameter to control PMU virtualization should apply to Intel as well as AMD, for situations where userspace is not trusted. If the module parameter allows PMU virtualization, there could be a new KVM_CAP or guest CPUID bits whereby userspace can enable/disable PMU virtualization on a per-VM basis. If the module parameter does not allow PMU virtualization, there should be no userspace override, since we have no precedent for authorizing that kind of override. If it's false, other counter-based profiling features (such as LBR including the associated CPUID bits if any) will not be exposed. Change its name from "pmu" to "enable_pmu" as we have temporary variables with the same name in our code like "struct kvm_pmu *pmu". Fixes: b1d66da ("KVM: x86/svm: Add module param to control PMU virtualization") Suggested-by : Jim Mattson <jmattson@google.com> Signed-off-by: Like Xu <likexu@tencent.com> Message-Id: <20220111073823.21885-1-likexu@tencent.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent ecebb96 commit 4732f24

8 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎arch/x86/kvm/cpuid.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
845845
perf_get_x86_pmu_capability(&cap);
846846

847847
/*
848-
* Only support guest architectural pmu on a host
849-
* with architectural pmu.
848+
* The guest architecture pmu is only supported if the architecture
849+
* pmu exists on the host and the module parameters allow it.
850850
*/
851-
if (!cap.version)
851+
if (!cap.version || !enable_pmu)
852852
memset(&cap, 0, sizeof(cap));
853853

854854
eax.split.version_id = min(cap.version, 2);

‎arch/x86/kvm/svm/pmu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
101101
{
102102
struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
103103

104-
if (!pmu)
104+
if (!enable_pmu)
105105
return NULL;
106106

107107
switch (msr) {

‎arch/x86/kvm/svm/svm.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,6 @@ module_param(vgif, int, 0444);
192192
static int lbrv = true;
193193
module_param(lbrv, int, 0444);
194194

195-
/* enable/disable PMU virtualization */
196-
bool pmu = true;
197-
module_param(pmu, bool, 0444);
198-
199195
static int tsc_scaling = true;
200196
module_param(tsc_scaling, int, 0444);
201197

@@ -957,7 +953,7 @@ static __init void svm_set_cpu_caps(void)
957953
kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
958954

959955
/* AMD PMU PERFCTR_CORE CPUID */
960-
if (pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
956+
if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
961957
kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
962958

963959
/* CPUID 0x8000001F (SME/SEV features) */
@@ -1093,7 +1089,7 @@ static __init int svm_hardware_setup(void)
10931089
pr_info("LBR virtualization supported\n");
10941090
}
10951091

1096-
if (!pmu)
1092+
if (!enable_pmu)
10971093
pr_info("PMU virtualization is disabled\n");
10981094

10991095
svm_set_cpu_caps();

‎arch/x86/kvm/svm/svm.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
3333
extern bool npt_enabled;
3434
extern bool intercept_smi;
35-
extern bool pmu;
3635

3736
/*
3837
* Clean bits in VMCB.

‎arch/x86/kvm/vmx/capabilities.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <asm/vmx.h>
66

77
#include "lapic.h"
8+
#include "x86.h"
89

910
extern bool __read_mostly enable_vpid;
1011
extern bool __read_mostly flexpriority_enabled;
@@ -389,6 +390,9 @@ static inline u64 vmx_get_perf_capabilities(void)
389390
{
390391
u64 perf_cap = 0;
391392

393+
if (!enable_pmu)
394+
return perf_cap;
395+
392396
if (boot_cpu_has(X86_FEATURE_PDCM))
393397
rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap);
394398

‎arch/x86/kvm/vmx/pmu_intel.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
487487
pmu->reserved_bits = 0xffffffff00200000ull;
488488

489489
entry = kvm_find_cpuid_entry(vcpu, 0xa, 0);
490-
if (!entry)
490+
if (!entry || !enable_pmu)
491491
return;
492492
eax.full = entry->eax;
493493
edx.full = entry->edx;

‎arch/x86/kvm/x86.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ module_param(force_emulation_prefix, bool, S_IRUGO);
187187
int __read_mostly pi_inject_timer = -1;
188188
module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
189189

190+
/* Enable/disable PMU virtualization */
191+
bool __read_mostly enable_pmu = true;
192+
EXPORT_SYMBOL_GPL(enable_pmu);
193+
module_param(enable_pmu, bool, 0444);
194+
190195
/*
191196
* Restoring the host value for MSRs that are only consumed when running in
192197
* usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU

‎arch/x86/kvm/x86.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ extern u64 host_xcr0;
336336
extern u64 supported_xcr0;
337337
extern u64 host_xss;
338338
extern u64 supported_xss;
339+
extern bool enable_pmu;
339340

340341
static inline bool kvm_mpx_supported(void)
341342
{

0 commit comments

Comments
 (0)