[RFC PATCH 03/22] x86/aperfmperf: Introduce restore_host_[am]perf()

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

From: Jim Mattson <jmattson@google.com>

Round out the {host,guest}[am]perf APIs by adding functions to restore
host values to the hardware MSRs. These functions:

1. Write the current host value (obtained via get_host_[am]perf()) to
the corresponding MSR
2. Clear the per-CPU offset used to track the difference between guest
and host values

Signed-off-by: Jim Mattson <jmattson@google.com>
---
arch/x86/include/asm/topology.h | 2 ++
arch/x86/kernel/cpu/aperfmperf.c | 16 ++++++++++++++++
2 files changed, 18 insertions(+)

diff --git a/arch/x86/include/asm/topology.h b/arch/x86/include/asm/topology.h
index fef5846c01976..8d4d4cd41bd84 100644
--- a/arch/x86/include/asm/topology.h
+++ b/arch/x86/include/asm/topology.h
@@ -309,6 +309,8 @@ extern u64 get_host_aperf(void);
extern u64 get_host_mperf(void);
extern void set_guest_aperf(u64 aperf);
extern void set_guest_mperf(u64 mperf);
+extern void restore_host_aperf(void);
+extern void restore_host_mperf(void);

extern void arch_scale_freq_tick(void);
#define arch_scale_freq_tick arch_scale_freq_tick
diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c
index 8b66872aa98c1..4d6c0b8b39452 100644
--- a/arch/x86/kernel/cpu/aperfmperf.c
+++ b/arch/x86/kernel/cpu/aperfmperf.c
@@ -135,6 +135,22 @@ void set_guest_mperf(u64 guest_mperf)
}
EXPORT_SYMBOL_GPL(set_guest_mperf);

+void restore_host_aperf(void)
+{
+ WARN_ON_ONCE(!irqs_disabled());
+ wrmsrl(MSR_IA32_APERF, get_host_aperf());
+ this_cpu_write(host_aperf_offset, 0);
+}
+EXPORT_SYMBOL_GPL(restore_host_aperf);
+
+void restore_host_mperf(void)
+{
+ WARN_ON_ONCE(!irqs_disabled());
+ wrmsrl(MSR_IA32_MPERF, get_host_mperf());
+ this_cpu_write(host_mperf_offset, 0);
+}
+EXPORT_SYMBOL_GPL(restore_host_mperf);
+
static bool __init turbo_disabled(void)
{
u64 misc_en;
--
2.47.0.371.ga323438b13-goog