* [PATCH v6] arm64: topology: fix arch_freq_get_on_cpu() overflow above 4.19 GHz
@ 2026-09-23 12:56 Oleg Keri
2026-09-23 14:47 ` Catalin Marinas
0 siblings, 1 reply; 2+ messages in thread
From: Oleg Keri @ 2026-09-23 12:56 UTC (permalink / raw)
To: Catalin Marinas, Will Deacon, Mark Rutland, Prasanna Kumar T S M,
Beata Michalska, Sumit Gupta
Cc: linux-arm-kernel, linux-kernel, Dietmar Eggemann, Peter Zijlstra,
Vincent Guittot, Ananthu C V, linux-pm
arch_freq_get_on_cpu() computes the product of the frequency scale and
the reference frequency as a u64, but assigns it to an unsigned int
before shifting it back down:
freq = scale * arch_scale_freq_ref(cpu);
freq >>= SCHED_CAPACITY_SHIFT;
The product is truncated to 32 bits before the shift, so the result
wraps once arch_scale_freq_ref() exceeds 2^32 / SCHED_CAPACITY_SCALE,
i.e. 4194304 kHz.
On a Snapdragon X2 Elite (Glymur) laptop, whose boost OPP is 4723200
kHz, cpuinfo_avg_freq reports 524283 kHz instead of ~4723200 kHz while
the CPU demonstrably runs at the boost frequency: a fixed workload
completes in 1.72 s at the 4723200 kHz OPP versus 2.01 s at 4032000
kHz, matching the 1.171 frequency ratio.
Shift the u64 product and narrow only at the return.
Fixes: 16d1e27475f6 ("arm64: Provide an AMU-based version of arch_freq_get_on_cpu")
Signed-off-by: Oleg Keri <okerixx@gmail.com>
---
Changes in v6:
- Drop the cap_scale() change. As a u64 static inline in a shared
header it would cost future 32-bit callers a 64-bit multiply that the
macro did not (Will); the truncation is the unsigned int temporary, so
drop that and leave kernel/sched untouched. Will's Ack on v5 not
carried since the arm64 hunk changed.
- v5: https://lore.kernel.org/all/20260917182059.2851-1-okerixx@gmail.com/
Changes in v5:
- cap_scale() as a static inline taking u64 in <linux/sched/topology.h>.
- Patch 2/2 of v4 dropped in favour of Ananthu C V's series.
arch/arm64/kernel/topology.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/kernel/topology.c b/arch/arm64/kernel/topology.c
index 55bd8fa3e95c..928079332fe6 100644
--- a/arch/arm64/kernel/topology.c
+++ b/arch/arm64/kernel/topology.c
@@ -186,7 +186,6 @@ int arch_freq_get_on_cpu(int cpu)
struct amu_cntr_sample *amu_sample;
unsigned int start_cpu = cpu;
unsigned long last_update;
- unsigned int freq = 0;
u64 scale;
if (!amu_fie_cpu_supported(cpu) || !arch_scale_freq_ref(cpu))
@@ -245,9 +244,8 @@ int arch_freq_get_on_cpu(int cpu)
* (see amu_scale_freq_tick for details)
*/
scale = arch_scale_freq_capacity(cpu);
- freq = scale * arch_scale_freq_ref(cpu);
- freq >>= SCHED_CAPACITY_SHIFT;
- return freq;
+
+ return (scale * arch_scale_freq_ref(cpu)) >> SCHED_CAPACITY_SHIFT;
}
static void amu_fie_setup(const struct cpumask *cpus)
base-commit: a8c591ed6b672915e0be57843f943a2a723aff40
--
2.55.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v6] arm64: topology: fix arch_freq_get_on_cpu() overflow above 4.19 GHz
2026-09-23 12:56 [PATCH v6] arm64: topology: fix arch_freq_get_on_cpu() overflow above 4.19 GHz Oleg Keri
@ 2026-09-23 14:47 ` Catalin Marinas
0 siblings, 0 replies; 2+ messages in thread
From: Catalin Marinas @ 2026-09-23 14:47 UTC (permalink / raw)
To: Oleg Keri
Cc: Will Deacon, Mark Rutland, Prasanna Kumar T S M, Beata Michalska,
Sumit Gupta, linux-arm-kernel, linux-kernel, Dietmar Eggemann,
Peter Zijlstra, Vincent Guittot, Ananthu C V, linux-pm
On Wed, Sep 23, 2026 at 02:56:04PM +0200, Oleg Keri wrote:
> arch_freq_get_on_cpu() computes the product of the frequency scale and
> the reference frequency as a u64, but assigns it to an unsigned int
> before shifting it back down:
>
> freq = scale * arch_scale_freq_ref(cpu);
> freq >>= SCHED_CAPACITY_SHIFT;
>
> The product is truncated to 32 bits before the shift, so the result
> wraps once arch_scale_freq_ref() exceeds 2^32 / SCHED_CAPACITY_SCALE,
> i.e. 4194304 kHz.
>
> On a Snapdragon X2 Elite (Glymur) laptop, whose boost OPP is 4723200
> kHz, cpuinfo_avg_freq reports 524283 kHz instead of ~4723200 kHz while
> the CPU demonstrably runs at the boost frequency: a fixed workload
> completes in 1.72 s at the 4723200 kHz OPP versus 2.01 s at 4032000
> kHz, matching the 1.171 frequency ratio.
>
> Shift the u64 product and narrow only at the return.
>
> Fixes: 16d1e27475f6 ("arm64: Provide an AMU-based version of arch_freq_get_on_cpu")
> Signed-off-by: Oleg Keri <okerixx@gmail.com>
> ---
> Changes in v6:
> - Drop the cap_scale() change. As a u64 static inline in a shared
> header it would cost future 32-bit callers a 64-bit multiply that the
> macro did not (Will); the truncation is the unsigned int temporary, so
> drop that and leave kernel/sched untouched. Will's Ack on v5 not
> carried since the arm64 hunk changed.
> - v5: https://lore.kernel.org/all/20260917182059.2851-1-okerixx@gmail.com/
I assume Dietmar and Will's acks still stand. I can add them from v5
(let me know if you disagree).
--
Catalin
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-09-23 14:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-23 12:56 [PATCH v6] arm64: topology: fix arch_freq_get_on_cpu() overflow above 4.19 GHz Oleg Keri
2026-09-23 14:47 ` Catalin Marinas
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®