Skip to content

Commit 860bcb1

Browse files
Dapeng Misean-jc
authored andcommitted
KVM: x86/pmu: Expose enable_mediated_pmu parameter to user space
Expose enable_mediated_pmu parameter to user space, i.e. allow userspace to enable/disable mediated vPMU support. Document the mediated versus perf-based behavior as part of the kernel-parameters.txt entry, and opportunistically add an entry for the core enable_pmu param as well. Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Signed-off-by: Mingwei Zhang <mizhang@google.com> Tested-by: Xudong Hao <xudong.hao@intel.com> Co-developed-by: Sean Christopherson <seanjc@google.com> Tested-by: Manali Shukla <manali.shukla@amd.com> Link: https://patch.msgid.link/20251206001720.468579-34-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 3b36160 commit 860bcb1

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,26 @@ Kernel parameters
30443044

30453045
Default is Y (on).
30463046

3047+
kvm.enable_pmu=[KVM,X86]
3048+
If enabled, KVM will virtualize PMU functionality based
3049+
on the virtual CPU model defined by userspace. This
3050+
can be overridden on a per-VM basis via
3051+
KVM_CAP_PMU_CAPABILITY.
3052+
3053+
If disabled, KVM will not virtualize PMU functionality,
3054+
e.g. MSRs, PMCs, PMIs, etc., even if userspace defines
3055+
a virtual CPU model that contains PMU assets.
3056+
3057+
Note, KVM's vPMU support implicitly requires running
3058+
with an in-kernel local APIC, e.g. to deliver PMIs to
3059+
the guest. Running without an in-kernel local APIC is
3060+
not supported, though KVM will allow such a combination
3061+
(with severely degraded functionality).
3062+
3063+
See also enable_mediated_pmu.
3064+
3065+
Default is Y (on).
3066+
30473067
kvm.enable_virt_at_load=[KVM,ARM64,LOONGARCH,MIPS,RISCV,X86]
30483068
If enabled, KVM will enable virtualization in hardware
30493069
when KVM is loaded, and disable virtualization when KVM
@@ -3090,6 +3110,35 @@ Kernel parameters
30903110
If the value is 0 (the default), KVM will pick a period based
30913111
on the ratio, such that a page is zapped after 1 hour on average.
30923112

3113+
kvm-{amd,intel}.enable_mediated_pmu=[KVM,AMD,INTEL]
3114+
If enabled, KVM will provide a mediated virtual PMU,
3115+
instead of the default perf-based virtual PMU (if
3116+
kvm.enable_pmu is true and PMU is enumerated via the
3117+
virtual CPU model).
3118+
3119+
With a perf-based vPMU, KVM operates as a user of perf,
3120+
i.e. emulates guest PMU counters using perf events.
3121+
KVM-created perf events are managed by perf as regular
3122+
(guest-only) events, e.g. are scheduled in/out, contend
3123+
for hardware resources, etc. Using a perf-based vPMU
3124+
allows guest and host usage of the PMU to co-exist, but
3125+
incurs non-trivial overhead and can result in silently
3126+
dropped guest events (due to resource contention).
3127+
3128+
With a mediated vPMU, hardware PMU state is context
3129+
switched around the world switch to/from the guest.
3130+
KVM mediates which events the guest can utilize, but
3131+
gives the guest direct access to all other PMU assets
3132+
when possible (KVM may intercept some accesses if the
3133+
virtual CPU model provides a subset of hardware PMU
3134+
functionality). Using a mediated vPMU significantly
3135+
reduces PMU virtualization overhead and eliminates lost
3136+
guest events, but is mutually exclusive with using perf
3137+
to profile KVM guests and adds latency to most VM-Exits
3138+
(to context switch PMU state).
3139+
3140+
Default is N (off).
3141+
30933142
kvm-amd.nested= [KVM,AMD] Control nested virtualization feature in
30943143
KVM/SVM. Default is 1 (enabled).
30953144

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ module_param(intercept_smi, bool, 0444);
170170
bool vnmi = true;
171171
module_param(vnmi, bool, 0444);
172172

173+
module_param(enable_mediated_pmu, bool, 0444);
174+
173175
static bool svm_gp_erratum_intercept = true;
174176

175177
static u8 rsm_ins_bytes[] = "\x0f\xaa";

arch/x86/kvm/vmx/vmx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
150150
extern bool __read_mostly allow_smaller_maxphyaddr;
151151
module_param(allow_smaller_maxphyaddr, bool, S_IRUGO);
152152

153+
module_param(enable_mediated_pmu, bool, 0444);
154+
153155
#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
154156
#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
155157
#define KVM_VM_CR0_ALWAYS_ON \

0 commit comments

Comments
 (0)