[PATCH 1/3] x86/aperfmperf: Separate hardware feedback from fallback reporting
From: Chuyi Zhou
Date: Wed Sep 16 2026 - 11:00:08 EST
arch_freq_get_on_cpu() reads cached APERF/MPERF samples and falls back to
cpufreq_quick_get() or cpu_khz when no usable sample is available. Its
callers cannot distinguish that fallback from a measured frequency.
This prevents a hardware feedback consumer from reporting missing
samples while preserving the fallback used by existing interfaces.
Extract the sample lookup and calculation into arch_freq_get_avg().
Return -EOPNOTSUPP when APERF/MPERF is unavailable and -EAGAIN when the
sample is too old or has a zero MPERF delta. Keep the existing seqcount
read, age limit and frequency calculation.
No functional change intended. This prepares cpuinfo_avg_freq to use
hardware feedback without fallback values.
Signed-off-by: Chuyi Zhou <zhouchuyi@xxxxxxxxxxxxx>
---
arch/x86/kernel/cpu/aperfmperf.c | 25 ++++++++++++++++++++-----
include/linux/cpufreq.h | 1 +
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/arch/x86/kernel/cpu/aperfmperf.c b/arch/x86/kernel/cpu/aperfmperf.c
index 7ffc78d5ebf2..de6297b1284b 100644
--- a/arch/x86/kernel/cpu/aperfmperf.c
+++ b/arch/x86/kernel/cpu/aperfmperf.c
@@ -503,15 +503,22 @@ void arch_scale_freq_tick(void)
*/
#define MAX_SAMPLE_AGE ((unsigned long)HZ / 50)
-int arch_freq_get_on_cpu(int cpu)
+/**
+ * arch_freq_get_avg() - Read cached APERF/MPERF frequency feedback
+ * @cpu: CPU to read.
+ *
+ * Return: Frequency in kHz, -EAGAIN if no usable sample is available, or
+ * -EOPNOTSUPP if APERF/MPERF is unsupported.
+ */
+int arch_freq_get_avg(int cpu)
{
struct aperfmperf *s = per_cpu_ptr(&cpu_samples, cpu);
- unsigned int seq, freq;
unsigned long last;
+ unsigned int seq;
u64 acnt, mcnt;
if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
- goto fallback;
+ return -EOPNOTSUPP;
do {
seq = raw_read_seqcount_begin(&s->seq);
@@ -525,11 +532,19 @@ int arch_freq_get_on_cpu(int cpu)
* which covers idle and NOHZ full CPUs.
*/
if (!mcnt || (jiffies - last) > MAX_SAMPLE_AGE)
- goto fallback;
+ return -EAGAIN;
return div64_u64((cpu_khz * acnt), mcnt);
+}
+
+int arch_freq_get_on_cpu(int cpu)
+{
+ int freq = arch_freq_get_avg(cpu);
+
+ if (freq >= 0)
+ return freq;
-fallback:
+ /* Preserve the fallback used by scaling_cur_freq and /proc/cpuinfo. */
freq = cpufreq_quick_get(cpu);
return freq ? freq : cpu_khz;
}
diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
index 35ce665edfd8..dab1d921183d 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -1224,6 +1224,7 @@ static inline int of_perf_domain_get_sharing_cpumask(int pcpu, const char *list_
}
#endif
+extern int arch_freq_get_avg(int cpu);
extern int arch_freq_get_on_cpu(int cpu);
#ifndef arch_set_freq_scale
--
2.20.1