mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Chuyi Zhou" <zhouchuyi@bytedance.com>
To: <rafael@kernel.org>, <viresh.kumar@linaro.org>,
	 <catalin.marinas@arm.com>, <will@kernel.org>, <tglx@kernel.org>,
	 <mingo@redhat.com>, <bp@alien8.de>,
	<dave.hansen@linux.intel.com>,  <beata.michalska@arm.com>
Cc: <linux-kernel@vger.kernel.org>, "Chuyi Zhou" <zhouchuyi@bytedance.com>
Subject: [PATCH 1/3] x86/aperfmperf: Separate hardware feedback from fallback reporting
Date: Wed, 16 Sep 2026 22:26:21 +0800	[thread overview]
Message-ID: <20260916142623.359336-2-zhouchuyi@bytedance.com> (raw)
In-Reply-To: <20260916142623.359336-1-zhouchuyi@bytedance.com>

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@bytedance.com>
---
 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

  reply	other threads:[~2026-09-16 14:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-16 14:26 [PATCH 0/3] cpufreq: Avoid fallback values in cpuinfo_avg_freq Chuyi Zhou
2026-09-16 14:26 ` Chuyi Zhou [this message]
2026-09-16 14:26 ` [PATCH 2/3] cpufreq: Use hardware feedback for cpuinfo_avg_freq Chuyi Zhou
2026-09-29 14:04   ` Beata Michalska
2026-09-16 14:26 ` [PATCH 3/3] x86/aperfmperf: Clean up includes after removing IPI sampling Chuyi Zhou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260916142623.359336-2-zhouchuyi@bytedance.com \
    --to=zhouchuyi@bytedance.com \
    --cc=beata.michalska@arm.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rafael@kernel.org \
    --cc=tglx@kernel.org \
    --cc=viresh.kumar@linaro.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®