Skip to content

Commit

Permalink
KVM: x86: Making the module parameter of vPMU more common
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Like Xu authored and Paolo Bonzini committed Jan 17, 2022
1 parent ecebb96 commit 4732f24
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 12 deletions.
6 changes: 3 additions & 3 deletions arch/x86/kvm/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
perf_get_x86_pmu_capability(&cap);

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

eax.split.version_id = min(cap.version, 2);
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kvm/svm/pmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ 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)
if (!enable_pmu)
return NULL;

switch (msr) {
Expand Down
8 changes: 2 additions & 6 deletions arch/x86/kvm/svm/svm.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ 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);

Expand Down Expand Up @@ -957,7 +953,7 @@ static __init void svm_set_cpu_caps(void)
kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);

/* AMD PMU PERFCTR_CORE CPUID */
if (pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);

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

if (!pmu)
if (!enable_pmu)
pr_info("PMU virtualization is disabled\n");

svm_set_cpu_caps();
Expand Down
1 change: 0 additions & 1 deletion arch/x86/kvm/svm/svm.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
extern bool npt_enabled;
extern bool intercept_smi;
extern bool pmu;

/*
* Clean bits in VMCB.
Expand Down
4 changes: 4 additions & 0 deletions arch/x86/kvm/vmx/capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <asm/vmx.h>

#include "lapic.h"
#include "x86.h"

extern bool __read_mostly enable_vpid;
extern bool __read_mostly flexpriority_enabled;
Expand Down Expand Up @@ -389,6 +390,9 @@ static inline u64 vmx_get_perf_capabilities(void)
{
u64 perf_cap = 0;

if (!enable_pmu)
return perf_cap;

if (boot_cpu_has(X86_FEATURE_PDCM))
rdmsrl(MSR_IA32_PERF_CAPABILITIES, perf_cap);

Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kvm/vmx/pmu_intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
pmu->reserved_bits = 0xffffffff00200000ull;

entry = kvm_find_cpuid_entry(vcpu, 0xa, 0);
if (!entry)
if (!entry || !enable_pmu)
return;
eax.full = entry->eax;
edx.full = entry->edx;
Expand Down
5 changes: 5 additions & 0 deletions arch/x86/kvm/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ module_param(force_emulation_prefix, bool, S_IRUGO);
int __read_mostly pi_inject_timer = -1;
module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);

/* Enable/disable PMU virtualization */
bool __read_mostly enable_pmu = true;
EXPORT_SYMBOL_GPL(enable_pmu);
module_param(enable_pmu, bool, 0444);

/*
* Restoring the host value for MSRs that are only consumed when running in
* usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
Expand Down
1 change: 1 addition & 0 deletions arch/x86/kvm/x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ extern u64 host_xcr0;
extern u64 supported_xcr0;
extern u64 host_xss;
extern u64 supported_xss;
extern bool enable_pmu;

static inline bool kvm_mpx_supported(void)
{
Expand Down

0 comments on commit 4732f24

Please sign in to comment.