Skip to content

RFC KVM: x86: Virtualize IA32_APERF and IA32_MPERF MSRs

Mingwei Zhang kvm linux-pm lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest22 patches 12 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/22x86/aperfmperf: Introduce get_host_[am]perf()arch/x86/include/asm/topology.h arch/x86/kernel/cpu/aperfmperf.c drivers/cpufreq/amd-pstate.c +1 more+26 -80 replies
2/22x86/aperfmperf: Introduce set_guest_[am]perf()arch/x86/include/asm/topology.h arch/x86/kernel/cpu/aperfmperf.c+34 -20 replies
3/22x86/aperfmperf: Introduce restore_host_[am]perf()arch/x86/include/asm/topology.h arch/x86/kernel/cpu/aperfmperf.c+18 -00 replies
4/22x86/msr: Adjust remote reads of IA32_[AM]PERF by the per-cpu host offsetarch/x86/lib/msr-smp.c+11 -00 replies
5/22KVM: x86: Introduce kvm_vcpu_make_runnable()arch/x86/include/asm/kvm_host.h arch/x86/kvm/x86.c+13 -50 replies
6/22KVM: x86: INIT may transition from HALTED to RUNNABLEarch/x86/kvm/lapic.c+2 -31 reply
7/22KVM: nSVM: Nested #VMEXIT may transition from HALTED to RUNNABLEarch/x86/kvm/svm/nested.c+1 -10 replies
8/22KVM: nVMX: Nested VM-exit may transition from HALTED to RUNNABLEarch/x86/kvm/vmx/nested.c+1 -10 replies
9/22KVM: x86: Introduce KVM_X86_FEATURE_APERFMPERFarch/x86/kvm/reverse_cpuid.h+6 -00 replies
10/22KVM: x86: Make APERFMPERF a governed featurearch/x86/kvm/governed_features.h+1 -00 replies
11/22KVM: x86: Initialize guest [am]perf at vcpu power-onarch/x86/include/asm/kvm_host.h arch/x86/kvm/x86.c+16 -00 replies
12/22KVM: x86: Load guest [am]perf into hardware MSRs at vcpu_load()arch/x86/kvm/x86.c+35 -00 replies
13/22KVM: x86: Load guest [am]perf when leaving halt statearch/x86/kvm/x86.c+9 -20 replies
14/22KVM: x86: Introduce kvm_user_return_notifier_register()arch/x86/kvm/x86.c+10 -50 replies
15/22KVM: x86: Restore host IA32_[AM]PERF on userspace returnarch/x86/kvm/x86.c+10 -00 replies
16/22KVM: x86: Save guest [am]perf checkpoint on HLTarch/x86/kvm/x86.c+20 -20 replies
17/22KVM: x86: Save guest [am]perf checkpoint on vcpu_put()arch/x86/kvm/x86.c+5 -00 replies
18/22KVM: x86: Update aperfmperf on host-initiated MP_STATE transitionsarch/x86/kvm/x86.c+12 -00 replies
19/22KVM: x86: Allow host and guest access to IA32_[AM]PERFarch/x86/kvm/x86.c+42 -00 replies
20/22KVM: VMX: Pass through guest reads of IA32_[AM]PERFarch/x86/kvm/vmx/vmx.c arch/x86/kvm/vmx/vmx.h+8 -10 replies
21/22KVM: SVM: Pass through guest reads of IA32_[AM]PERFarch/x86/kvm/svm/svm.c arch/x86/kvm/svm/svm.h+8 -10 replies
22/22KVM: x86: Enable guest usage of X86_FEATURE_APERFMPERFarch/x86/kvm/cpuid.c+11 -10 replies

Reviews trailers per patch

6 of 22 patches have a Reviewed-by or Acked-by.

PatchMingwei Zhang
1/22R-b
2/22R-b
3/22
4/22R-b
5/22R-b
6/22
7/22
8/22
9/22
10/22R-b
11/22
12/22
13/22
14/22
15/22
16/22
17/22
18/22R-b
19/22
20/22
21/22
22/22

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [RFC PATCH 00/22] KVM: x86: Virtualize IA32_APERF and IA32_MPERF MSRs

Linux guests read IA32_APERF and IA32_MPERF on every scheduler tick (250 Hz by default) to measure their effective CPU frequency. To avoid the overhead of intercepting these frequent MSR reads, allow the guest to read them directly by loading guest values into the hardware MSRs.

These MSRs are continuously running counters whose values must be carefully tracked during all vCPU state transitions:
- Guest IA32_APERF advances only during guest execution
- Guest IA32_MPERF advances at the TSC frequency whenever the vCPU is in C0 state, even when not actively running
- Host kernel access is redirected through get_host_[am]perf() which adds per-CPU offsets to the hardware MSR values
- Remote MSR reads through /dev/cpu/*/msr also account for these offsets

Guest values persist in hardware while the vCPU is loaded and running. Host MSR values are restored on vcpu_put (either at KVM_RUN completion or when preempted) and when transitioning to halt state.

Note that guest TSC scaling via KVM_SET_TSC_KHZ is not supported, as it would require either intercepting MPERF reads on Intel (where MPERF ticks at host rate regardless of guest TSC scaling) or significantly complicating the cycle accounting on AMD.

The host must have both CONSTANT_TSC and NONSTOP_TSC capabilities since these ensure stable TSC frequency across C-states and P-states, which is required for accurate background MPERF accounting.

Jim Mattson (14):
x86/aperfmperf: Introduce get_host_[am]perf() x86/aperfmperf: Introduce set_guest_[am]perf() x86/aperfmperf: Introduce restore_host_[am]perf() x86/msr: Adjust remote reads of IA32_[AM]PERF by the per-cpu host
offset
KVM: x86: Introduce kvm_vcpu_make_runnable()
KVM: x86: INIT may transition from HALTED to RUNNABLE
KVM: nSVM: Nested #VMEXIT may transition from HALTED to RUNNABLE
KVM: nVMX: Nested VM-exit may transition from HALTED to RUNNABLE
KVM: x86: Make APERFMPERF a governed feature
KVM: x86: Initialize guest [am]perf at vcpu power-on
KVM: x86: Load guest [am]perf when leaving halt state
KVM: x86: Introduce kvm_user_return_notifier_register()
KVM: x86: Restore host IA32_[AM]PERF on userspace return
KVM: x86: Update aperfmperf on host-initiated MP_STATE transitions

Mingwei Zhang (8):
KVM: x86: Introduce KVM_X86_FEATURE_APERFMPERF
KVM: x86: Load guest [am]perf into hardware MSRs at vcpu_load()
KVM: x86: Save guest [am]perf checkpoint on HLT
KVM: x86: Save guest [am]perf checkpoint on vcpu_put()
KVM: x86: Allow host and guest access to IA32_[AM]PERF
KVM: VMX: Pass through guest reads of IA32_[AM]PERF
KVM: SVM: Pass through guest reads of IA32_[AM]PERF
KVM: x86: Enable guest usage of X86_FEATURE_APERFMPERF

 arch/x86/include/asm/kvm_host.h  |  11 ++
 arch/x86/include/asm/topology.h  |  10 ++
 arch/x86/kernel/cpu/aperfmperf.c |  65 +++++++++++-
 arch/x86/kvm/cpuid.c             |  12 ++-
 arch/x86/kvm/governed_features.h |   1 +
 arch/x86/kvm/lapic.c             |   5 +-
 arch/x86/kvm/reverse_cpuid.h     |   6 ++
 arch/x86/kvm/svm/nested.c        |   2 +-
 arch/x86/kvm/svm/svm.c           |   7 ++
 arch/x86/kvm/svm/svm.h           |   2 +-
 arch/x86/kvm/vmx/nested.c        |   2 +-
 arch/x86/kvm/vmx/vmx.c           |   7 ++
 arch/x86/kvm/vmx/vmx.h           |   2 +-
 arch/x86/kvm/x86.c               | 171 ++++++++++++++++++++++++++++---
 arch/x86/lib/msr-smp.c           |  11 ++
 drivers/cpufreq/amd-pstate.c     |   4 +-
 drivers/cpufreq/intel_pstate.c   |   5 +-
 17 files changed, 295 insertions(+), 28 deletions(-)

base-commit: 0a9b9d17f3a781dea03baca01c835deaa07f7cc3

Signature · 1 line
2.47.0.371.ga323438b13-goog