[RFC PATCH 13/22] KVM: x86: Load guest [am]perf when leaving halt state

Mingwei Zhang
2024-11-21T18:53:41Z

From: Jim Mattson <jmattson@google.com>

When a vCPU transitions from HALTED to RUNNABLE, it resumes time keeping:
its virtual IA32_MPERF MSR should start accumulating C0 cycles again.
Load the guest values into the hardware MSRs for direct guest access.

Background cycle accumulation is unnecessary at this point since the
vCPU has been in C1, so the guest's IA32_MPERF has been stopped.

Signed-off-by: Jim Mattson <jmattson@google.com>
---
arch/x86/kvm/x86.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index b914578718d9c..acfa9ecc5bc36 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11204,9 +11204,16 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)

void kvm_vcpu_make_runnable(struct kvm_vcpu *vcpu)
{
- if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED ||
- vcpu->arch.mp_state == KVM_MP_STATE_AP_RESET_HOLD)
+ switch (vcpu->arch.mp_state) {
+ case KVM_MP_STATE_HALTED:
+ if (guest_can_use(vcpu, X86_FEATURE_APERFMPERF) &&
+ vcpu->wants_to_run)
+ kvm_load_guest_aperfmperf(vcpu, false);
+ fallthrough;
+ case KVM_MP_STATE_AP_RESET_HOLD:
vcpu->arch.pv.pv_unhalted = false;
+ break;
+ }
vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
vcpu->arch.apf.halted = false;
}
--
2.47.0.371.ga323438b13-goog