[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241121185315.3416855-19-mizhang@google.com>
Date: Thu, 21 Nov 2024 18:53:10 +0000
From: Mingwei Zhang <mizhang@...gle.com>
To: Sean Christopherson <seanjc@...gle.com>, Paolo Bonzini <pbonzini@...hat.com>,
Huang Rui <ray.huang@....com>, "Gautham R. Shenoy" <gautham.shenoy@....com>,
Mario Limonciello <mario.limonciello@....com>, "Rafael J. Wysocki" <rafael@...nel.org>,
Viresh Kumar <viresh.kumar@...aro.org>,
Srinivas Pandruvada <srinivas.pandruvada@...ux.intel.com>, Len Brown <lenb@...nel.org>
Cc: "H. Peter Anvin" <hpa@...or.com>, Perry Yuan <perry.yuan@....com>, kvm@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-pm@...r.kernel.org,
Jim Mattson <jmattson@...gle.com>, Mingwei Zhang <mizhang@...gle.com>
Subject: [RFC PATCH 18/22] KVM: x86: Update aperfmperf on host-initiated
MP_STATE transitions
From: Jim Mattson <jmattson@...gle.com>
When the host modifies a vCPU's MP_STATE after the vCPU has started
running, maintain the accuracy of guest aperfmperf tracking:
1. For transitions from !HALTED to HALTED, add any accumulated
"background" TSC ticks to the guest_mperf checkpoint before
stopping the counter.
2. For transitions from HALTED to !HALTED, record the current TSC in
host_tsc to begin accumulating background cycles in guest_mperf.
This ensures the guest MPERF counter properly reflects time spent in
C0 vs C1 states, even when state transitions are initiated by the host
rather than the guest.
Signed-off-by: Jim Mattson <jmattson@...gle.com>
Reviewed-by: Mingwei Zhang <mizhang@...gle.com>
Signed-off-by: Mingwei Zhang <mizhang@...gle.com>
---
arch/x86/kvm/x86.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 7c22bda3b1f7b..cd1f1ae86f83f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11904,6 +11904,18 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
goto out;
+ if (kvm_vcpu_has_run(vcpu) &&
+ guest_can_use(vcpu, X86_FEATURE_APERFMPERF)) {
+ if (mp_state->mp_state == KVM_MP_STATE_HALTED &&
+ vcpu->arch.mp_state != KVM_MP_STATE_HALTED) {
+ kvm_accumulate_background_guest_mperf(vcpu);
+ vcpu->arch.aperfmperf.loaded_while_running = false;
+ } else if (mp_state->mp_state != KVM_MP_STATE_HALTED &&
+ vcpu->arch.mp_state == KVM_MP_STATE_HALTED) {
+ vcpu->arch.aperfmperf.host_tsc = rdtsc();
+ }
+ }
+
if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
--
2.47.0.371.ga323438b13-goog
Powered by blists - more mailing lists