[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241121185315.3416855-16-mizhang@google.com>
Date: Thu, 21 Nov 2024 18:53:07 +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 15/22] KVM: x86: Restore host IA32_[AM]PERF on userspace return
From: Jim Mattson <jmattson@...gle.com>
Add support for restoring host IA32_APERF and IA32_MPERF values when
returning to userspace. While not strictly necessary since reads of
/dev/cpu/*/msr now reconstruct host values, restoring the host values
maintains cleaner system state.
Leverage KVM's existing user return notifier infrastructure but add a
separate flag since these MSRs require dynamic value restoration
rather than static value restoration. Restoration is only performed
when guest values have been loaded into the hardware MSRs.
Signed-off-by: Jim Mattson <jmattson@...gle.com>
---
arch/x86/kvm/x86.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 6df8f21b83eb1..ad5351673362c 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -207,6 +207,7 @@ module_param(mitigate_smt_rsb, bool, 0444);
struct kvm_user_return_msrs {
struct user_return_notifier urn;
bool registered;
+ bool restore_aperfmperf;
struct kvm_user_return_msr_values {
u64 host;
u64 curr;
@@ -571,6 +572,11 @@ static void kvm_on_user_return(struct user_return_notifier *urn)
* interrupted and executed through kvm_arch_disable_virtualization_cpu()
*/
local_irq_save(flags);
+ if (msrs->restore_aperfmperf) {
+ restore_host_aperf();
+ restore_host_mperf();
+ msrs->restore_aperfmperf = false;
+ }
if (msrs->registered) {
msrs->registered = false;
user_return_notifier_unregister(urn);
@@ -5003,6 +5009,7 @@ static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
static void kvm_load_guest_aperfmperf(struct kvm_vcpu *vcpu, bool update_mperf)
{
+ struct kvm_user_return_msrs *msrs;
unsigned long flags;
local_irq_save(flags);
@@ -5011,6 +5018,9 @@ static void kvm_load_guest_aperfmperf(struct kvm_vcpu *vcpu, bool update_mperf)
set_guest_aperf(vcpu->arch.aperfmperf.guest_aperf);
set_guest_mperf(vcpu->arch.aperfmperf.guest_mperf);
vcpu->arch.aperfmperf.loaded_while_running = true;
+ msrs = this_cpu_ptr(user_return_msrs);
+ kvm_user_return_notifier_register(msrs);
+ msrs->restore_aperfmperf = true;
local_irq_restore(flags);
}
--
2.47.0.371.ga323438b13-goog
Powered by blists - more mailing lists