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 2/3] cpufreq: Use hardware feedback for cpuinfo_avg_freq
Date: Wed, 16 Sep 2026 22:26:22 +0800	[thread overview]
Message-ID: <20260916142623.359336-3-zhouchuyi@bytedance.com> (raw)
In-Reply-To: <20260916142623.359336-1-zhouchuyi@bytedance.com>

cpuinfo_avg_freq is documented as a frequency derived from hardware
feedback, but it uses arch_freq_get_on_cpu(), which supplies a fallback
on x86 when the cached sample is unavailable. Userspace cannot distinguish
that value from a measurement. With amd-pstate-epp, for example, the
fallback is policy->min even under the performance policy. A fallback
based on the requested frequency also does not establish the frequency
at which the hardware actually ran.

Use arch_freq_get_avg() for cpuinfo_avg_freq reads and support detection.
On x86, unavailable samples then return -EAGAIN instead of a fallback
frequency, and the attribute is omitted when APERF/MPERF is unsupported.

Provide a weak default returning -EOPNOTSUPP and reuse the existing ARM64
AMU implementation. Preserve the behavior of ARM64, scaling_cur_freq
and /proc/cpuinfo.

Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
---
 Documentation/admin-guide/pm/cpufreq.rst | 11 +++++++++--
 arch/arm64/kernel/topology.c             |  7 ++++++-
 drivers/cpufreq/cpufreq.c                | 19 +++++++++++++++++--
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/Documentation/admin-guide/pm/cpufreq.rst b/Documentation/admin-guide/pm/cpufreq.rst
index 34baf20cc202..3057829b01bd 100644
--- a/Documentation/admin-guide/pm/cpufreq.rst
+++ b/Documentation/admin-guide/pm/cpufreq.rst
@@ -255,12 +255,19 @@ are the following:
 
         This is expected to be based on the frequency the hardware actually runs
         at and, as such, might require specialised hardware support (such as AMU
-        extension on ARM). If one cannot be determined, this attribute should
-        not be present.
+        extension on ARM or APERF/MPERF on x86). This attribute is not present
+        when hardware feedback is unsupported.
 
         Note that failed attempt to retrieve current frequency for a given
         CPU(s) will result in an appropriate error, i.e.: EAGAIN for CPU that
         remains idle (raised on ARM).
+        The attribute remains present during temporary sampling gaps.
+
+        On x86, reads use cached APERF/MPERF samples without waking the target
+        CPU to collect new samples. An expired sample or a zero MPERF delta
+        results in ``EAGAIN`` instead of a fallback to a policy or reference
+        frequency. A CPU that has just entered idle can still have a usable
+        sample, while a busy CPU excluded from periodic sampling can lack one.
 
 ``cpuinfo_max_freq``
 	Maximum possible operating frequency the CPUs belonging to this policy
diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c
index d28438f8b83f..939d3e4ce5d2 100644
--- a/arch/arm64/kernel/topology.c
+++ b/arch/arm64/kernel/topology.c
@@ -181,7 +181,7 @@ void arch_cpu_idle_enter(void)
 
 #define AMU_SAMPLE_EXP_MS	20
 
-int arch_freq_get_on_cpu(int cpu)
+int arch_freq_get_avg(int cpu)
 {
 	struct amu_cntr_sample *amu_sample;
 	unsigned int start_cpu = cpu;
@@ -250,6 +250,11 @@ int arch_freq_get_on_cpu(int cpu)
 	return freq;
 }
 
+int arch_freq_get_on_cpu(int cpu)
+{
+	return arch_freq_get_avg(cpu);
+}
+
 static void amu_fie_setup(const struct cpumask *cpus)
 {
 	int cpu;
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 0d0df986fa3d..21bec6b7f538 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -705,9 +705,24 @@ __weak int arch_freq_get_on_cpu(int cpu)
 	return -EOPNOTSUPP;
 }
 
+/**
+ * arch_freq_get_avg() - Get an average frequency from hardware feedback
+ * @cpu: CPU to read.
+ *
+ * Provide cpuinfo_avg_freq with an average operating frequency derived
+ * from recent hardware feedback for @cpu or its frequency domain.
+ *
+ * Return: Frequency in kHz, -EOPNOTSUPP if feedback is unsupported for the
+ * CPU or policy under the current configuration.
+ */
+__weak int arch_freq_get_avg(int cpu)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline bool cpufreq_avg_freq_supported(struct cpufreq_policy *policy)
 {
-	return arch_freq_get_on_cpu(policy->cpu) != -EOPNOTSUPP;
+	return arch_freq_get_avg(policy->cpu) != -EOPNOTSUPP;
 }
 
 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
@@ -769,7 +784,7 @@ static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
 static ssize_t show_cpuinfo_avg_freq(struct cpufreq_policy *policy,
 				     char *buf)
 {
-	int avg_freq = arch_freq_get_on_cpu(policy->cpu);
+	int avg_freq = arch_freq_get_avg(policy->cpu);
 
 	if (avg_freq > 0)
 		return sysfs_emit(buf, "%u\n", avg_freq);
-- 
2.20.1

  parent 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 ` [PATCH 1/3] x86/aperfmperf: Separate hardware feedback from fallback reporting Chuyi Zhou
2026-09-16 14:26 ` Chuyi Zhou [this message]
2026-09-29 14:04   ` [PATCH 2/3] cpufreq: Use hardware feedback for cpuinfo_avg_freq 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-3-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®