Skip to contentSkip to conversation

cpufreq: Avoid fallback values in cpuinfo_avg_freq

PATCH New Jackie Liu 2 participants 2 messages Oct 19 – Oct 22

Subsystems (2)
lore
Share & tools
lore

Jackie Liu

author
#0 lore raw

PATCH 1/1 cpufreq: Avoid fallback values in cpuinfo_avg_freq

From: Jackie Liu <[email protected]>

On x86, arch_freq_get_on_cpu() can return the requested or nominal frequency when hardware feedback is unavailable. cpuinfo_avg_freq then reports fallback values as hardware averages, even without APERF/MPERF.

Use a separate sampling hook for the attribute and its support probe:
return -EOPNOTSUPP without APERF/MPERF and -EAGAIN for zero MPERF deltas or stale samples. Preserve the scaling_cur_freq and /proc/cpuinfo fallback and the existing ARM64 AMU implementation.

Fixes fbb4a4759b54 ("cpufreq: Introduce an optional cpuinfo_avg_freq sysfs entry")
---
 arch/x86/kernel/cpu/aperfmperf.c | 18 +++++++++++++-----
 drivers/cpufreq/cpufreq.c        |  9 +++++++--
 include/linux/cpufreq.h          |  1 +
 3 files changed, 21 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/aperfmperf.c +13 -5
index 7ffc78d5ebf2..fa8376d9ce94 100644
@@ -503,15 +503,15 @@ void arch_scale_freq_tick(void)
  */
 #define MAX_SAMPLE_AGE	((unsigned long)HZ / 50)
 
-int arch_freq_get_on_cpu(int cpu)
+int arch_freq_get_avg_on_cpu(int cpu)
 {
 	struct aperfmperf *s = per_cpu_ptr(&cpu_samples, cpu);
-	unsigned int seq, freq;
+	unsigned int seq;
 	unsigned long last;
 	u64 acnt, mcnt;
 
 	if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
-		goto fallback;
+		return -EOPNOTSUPP;
 
 	do {
 		seq = raw_read_seqcount_begin(&s->seq);
@@ -525,11 +525,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 avg_freq = arch_freq_get_avg_on_cpu(cpu);
+	unsigned int freq;
+
+	if (avg_freq >= 0)
+		return avg_freq;
 
-fallback:
 	freq = cpufreq_quick_get(cpu);
 	return freq ? freq : cpu_khz;
 }
drivers/cpufreq/cpufreq.c +7 -2
index 54dde8419bdc..2e44b6ee14c9 100644
@@ -705,9 +705,14 @@ __weak int arch_freq_get_on_cpu(int cpu)
 	return -EOPNOTSUPP;
 }
 
+__weak int arch_freq_get_avg_on_cpu(int cpu)
+{
+	return arch_freq_get_on_cpu(cpu);
+}
+
 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_on_cpu(policy->cpu) != -EOPNOTSUPP;
 }
 
 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
@@ -769,7 +774,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_on_cpu(policy->cpu);
 
 	if (avg_freq > 0)
 		return sysfs_emit(buf, "%u\n", avg_freq);
include/linux/cpufreq.h +1 -0
index d3d0d9d02aa4..925a3d3b6c31 100644
@@ -1228,6 +1228,7 @@ static inline int of_perf_domain_get_sharing_cpumask(int pcpu, const char *list_
 #endif
 
 extern int arch_freq_get_on_cpu(int cpu);
+extern int arch_freq_get_avg_on_cpu(int cpu);
 
 #ifndef arch_set_freq_scale
 static __always_inline
Signature · 1 line
2.56.0
#1 lore raw
kernel test robot noticed "ltp.runpwtests03.fail" on:

Hello,

kernel test robot noticed "ltp.runpwtests03.fail" on:

commit: e5f9d99762e285664eea5ecbab004c8444930f22 ("[PATCH] cpufreq: Avoid fallback values in cpuinfo_avg_freq")
url: https://github.com/intel-lab-lkp/linux/commits/Jackie-Liu/cpufreq-Avoid-fallback-values-in-cpuinfo_avg_freq/20261019-145358
base: https://git.kernel.org/cgit/linux/kernel/git/rafael/linux-pm.git linux-next patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH] cpufreq: Avoid fallback values in cpuinfo_avg_freq

in testcase: ltp
version: ltp-x86_64-1255a3be5-1_20261017 with following parameters:

test: power_management_tests

config: x86_64-rhel-9.4-ltp
compiler: gcc-14
test machine: 8 threads 1 sockets Intel(R) Core(TM) i7-4790T CPU @ 2.70GHz (Haswell) with 16G memory

(please refer to attached dmesg/kmsg for entire log/backtrace)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]

PATH=/lkp/benchmarks/ltp:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/lkp/lkp/src/bin

2026-10-21 16:24:03 cd /lkp/benchmarks/ltp
2026-10-21 16:24:03 export LTP_RUNTIME_MUL=2
2026-10-21 16:24:03 export LTPROOT=/lkp/benchmarks/ltp
2026-10-21 16:24:03 kirk -f power_management_tests Host information
	Hostname:   lkp-hsw-d05
	Python:     3.13.5 (main, Jun 13 2026, 14:18:01) [GCC 14.2.0]
	Directory:  /tmp/kirk.root/tmp_2s1ns12

Connecting to SUT: default

Suite: power_management_tests

─────────────────────────────
 [1;37mcpufreq_intel:  [0m [1;32mpass [0m |  [1;33mtainted [0m  (0.282s)
 [1;37mrfim01:  [0m [1;36mskip [0m |  [1;33mtainted [0m  (0.017s)
 [1;37mhigh_freq_hwp_cap_cppc:  [0m [1;36mskip [0m |  [1;33mtainted [0m  (0.015s)
 [1;37mrunpwtests03:  [0m [1;31mfail [0m |  [1;33mtainted [0m  (0.532s)
 [1;37mrunpwtests04:  [0m [1;32mpass [0m |  [1;33mtainted [0m  (0.011s)
 [1;37mrunpwtests06:  [0m [1;32mpass [0m |  [1;33mtainted [0m  (0.048s)

Execution time: 1.103s

Disconnecting from SUT: default

Target information

──────────────────
Kernel:   Linux 7.3.0-rc7+ #1 SMP PREEMPT_DYNAMIC Thu Oct 22 00:04:36 CST 2026
Cmdline:  ip=::::lkp-hsw-d05::dhcp
          root=/dev/ram0
          RESULT_ROOT=/result/ltp/power_management_tests/lkp-hsw-d05/debian-13-x86_64-20250902.cgz/x86_64-rhel-9.4-ltp/gcc-14/e5f9d99762e285664eea5ecbab004c8444930f22/0
          BOOT_IMAGE=/pkg/linux/x86_64-rhel-9.4-ltp/gcc-14/e5f9d99762e285664eea5ecbab004c8444930f22/vmlinuz-7.3.0-rc7+
          branch=internal-devel/devel-hourly-20261020-060911
          job=/lkp/jobs/scheduled/lkp-hsw-d05/ltp-power_management_tests-debian-13-x86_64-20250902.cgz-e5f9d99762e2-20261022-15031-ddtgwr-1.yaml
          user=lkp
          ARCH=x86_64
          kconfig=x86_64-rhel-9.4-ltp
          commit=e5f9d99762e285664eea5ecbab004c8444930f22
          intremap=posted_msi
          max_uptime=7200
          LKP_SERVER=internal-lkp-server
          nokaslr
          selinux=0
          debug
          apic=debug
          sysrq_always_enabled
          rcupdate.rcu_cpu_stall_timeout=100
          net.ifnames=0
          printk.devkmsg=on
          panic=-1
          softlockup_panic=1
          nmi_watchdog=panic
          oops=panic
          load_ramdisk=2
          prompt_ramdisk=0
          drbd.minor_count=8
          systemd.log_level=err
          ignore_loglevel
          console=tty0
          earlyprintk=ttyS0,115200
          console=ttyS0,115200
          vga=normal
          rw
          keep_initrds=/osimage/pkg/debian-13-x86_64-20250902.cgz/ltp-x86_64-1255a3be5-1_20261017.cgz
Machine:  unknown
Arch:     x86_64
RAM:      14035028 kB
Swap:     0 kB
Distro:   debian 13

────────────────────────
TEST SUMMARY

────────────────────────
Suite:   power_management_tests
Runtime: 0.905s
Runs:    6

Results:

    Passed:   194
    Failed:   1
    Broken:   0
    Skipped:  2

Warnings: 0

[1;31mFailures: [0m
• runpwtests03

Session stopped
LTP test details
----------------
runpwtests03: Power_Management03 1 TFAIL: CPUFREQ sysfs tests

The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20261022/[email protected]

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki