arm64: topology: fix arch_freq_get_on_cpu() overflow above 4.19 GHz
PATCH v5 1/1 arm64: topology: fix arch_freq_get_on_cpu() overflow above 4.19 GHz
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.
Compute it with cap_scale(), turned into a static inline taking u64 and moved to <linux/sched/topology.h> so it is usable outside kernel/sched.
---
Changes in v5:
- cap_scale() becomes a static inline taking u64 arguments instead of a macro, as Dietmar proposed and Peter agreed, after Peter pointed out that the macro relies on one operand being u64. It now lives in <linux/sched/topology.h>, where SCHED_CAPACITY_SHIFT is visible, rather than <linux/topology.h>.
- Patch 2/2 of v4 is dropped: it duplicated Ananthu C V's series [1], which I tested instead. This fix is needed with that series, since it puts the reference above 4194304 kHz from boot.
- v4: https://lore.kernel.org/all/[email protected]/
arch/arm64/kernel/topology.c | 6 ++---- include/linux/sched/topology.h | 5 +++++ kernel/sched/sched.h | 2 -- 3 files changed, 7 insertions(+), 6 deletions(-)
arch/arm64/kernel/topology.c +2 -4
@@ -19,6 +19,7 @@#include <linux/init.h> #include <linux/percpu.h> #include <linux/sched/isolation.h> +#include <linux/sched/topology.h> #include <linux/xarray.h> #include <asm/cpu.h>
@@ -186,7 +187,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 +245,7 @@ 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 cap_scale (arch_scale_freq_ref (cpu), scale); }static void amu_fie_setup (const struct cpumask *cpus)
include/linux/sched/topology.h +5 -0
@@ -234,6 +234,11 @@ static inline void rebuild_sched_domains_energy(void) }#endif +static inline u64 cap_scale (u64 value,u64 scale) +{ +return value * scale >>SCHED_CAPACITY_SHIFT ; +} +#ifndef arch_scale_cpu_capacity/** * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU.
kernel/sched/sched.h +0 -2
@@ -236,8 +236,6 @@ static inline int task_has_dl_policy(struct task_struct *p)return dl_policy (p->policy); } -#define cap_scale (v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) -static inline void update_avg (u64 *avg,u64 sample) {s64 diff = sample - *avg;
base-commit: 0d9d0dbf2fddcff5859d623e90ca73c4054276e1
Signature · 1 line
2.55.0
Quoted 17 lines · Oleg Keri
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_freqreports 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.
Quoted 5 lines
Compute it with
cap_scale(), turned into a static inline taking u64 and moved to <linux/sched/topology.h> so it is usable outside kernel/sched.Fixes 16d1e27475f6 ("arm64: Provide an AMU-based version of arch_freq_get_on_cpu")Signed-off-by Oleg Keri <[email protected]>
Quoted 81 lines
---
Changes in v5:
-cap_scale()becomes a static inline taking u64 arguments instead of a macro, as Dietmar proposed and Peter agreed, after Peter pointed out that the macro relies on one operand being u64. It now lives in <linux/sched/topology.h>, whereSCHED_CAPACITY_SHIFTis visible, rather than <linux/topology.h>.
- Patch 2/2 of v4 is dropped: it duplicated Ananthu C V's series [1], which I tested instead. This fix is needed with that series, since it puts the reference above 4194304 kHz from boot.
- v4: https://lore.kernel.org/all/[email protected]/arch/arm64/kernel/topology.c | 6 ++---- include/linux/sched/topology.h | 5 +++++ kernel/sched/sched.h | 2 -- 3 files changed, 7 insertions(+), 6 deletions(-)arch/arm64/kernel/topology.c +2
-4@@ -19,6 +19,7 @@#include <linux/init.h> #include <linux/percpu.h> #include <linux/sched/isolation.h> +#include <linux/sched/topology.h> #include <linux/xarray.h> #include <asm/cpu.h> @@ -186,7 +187,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 +245,7 @@ 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 cap_scale (arch_scale_freq_ref (cpu), scale); }static void amu_fie_setup (const struct cpumask *cpus)include/linux/sched/topology.h +5
-0@@ -234,6 +234,11 @@ static inline void rebuild_sched_domains_energy(void) }#endif +static inline u64 cap_scale (u64 value,u64 scale) +{ +return value * scale >>SCHED_CAPACITY_SHIFT ; +} +#ifndef arch_scale_cpu_capacity/** * arch_scale_cpu_capacity - get the capacity scale factor of a given CPU. kernel/sched/sched.h +0
-2@@ -236,8 +236,6 @@ static inline int task_has_dl_policy(struct task_struct *p)return dl_policy (p->policy); } -#define cap_scale (v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT) -static inline void update_avg (u64 *avg,u64 sample) {s64 diff = sample - *avg;base-commit:
0d9d0dbf2fddcff5859d623e90ca73c4054276e1
Quoted 62 lines · Oleg Keri
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_freqreports 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.Compute it with
cap_scale(), turned into a static inline taking u64 and moved to <linux/sched/topology.h> so it is usable outside kernel/sched.Fixes 16d1e27475f6 ("arm64: Provide an AMU-based version of arch_freq_get_on_cpu")Signed-off-by Oleg Keri <[email protected]>---
Changes in v5:
-cap_scale()becomes a static inline taking u64 arguments instead of a macro, as Dietmar proposed and Peter agreed, after Peter pointed out that the macro relies on one operand being u64. It now lives in <linux/sched/topology.h>, whereSCHED_CAPACITY_SHIFTis visible, rather than <linux/topology.h>.
- Patch 2/2 of v4 is dropped: it duplicated Ananthu C V's series [1], which I tested instead. This fix is needed with that series, since it puts the reference above 4194304 kHz from boot.
- v4: https://lore.kernel.org/all/[email protected]/arch/arm64/kernel/topology.c | 6 ++---- include/linux/sched/topology.h | 5 +++++ kernel/sched/sched.h | 2 -- 3 files changed, 7 insertions(+), 6 deletions(-)arch/arm64/kernel/topology.c +1
-1@@ -19,6 +19,7 @@#include <linux/init.h> #include <linux/percpu.h> #include <linux/sched/isolation.h> +#include <linux/sched/topology.h> #include <linux/xarray.h> #include <asm/cpu.h> @@ -186,7 +187,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))
arch/arm64/kernel/topology.c int arch_freq_get_on_cpu(int cpu) · line 251
Quoted 10 lines
arch/arm64/kernel/topology.c +1
-3@@ -245,9 +245,7 @@ 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 cap_scale (arch_scale_freq_ref (cpu), scale); }static void amu_fie_setup (const struct cpumask *cpus)
The arm64 part looks fine to me, so for that:
However...
include/linux/sched/topology.h static inline void rebuild_sched_domains_energy(void) · line 240
Quoted 12 lines
include/linux/sched/topology.h +4
-0@@ -234,6 +234,11 @@ static inline void rebuild_sched_domains_energy(void) }#endif +static inline u64 cap_scale (u64 value,u64 scale) +{ +return value * scale >>SCHED_CAPACITY_SHIFT ; +}
... does this introduce unnecessary 64-bit arithmetic for 32-bit architectures that currently pass 'unsigned long' to the existing macro?
Will
Hi Will,
Quoted 2 lines · Will Deacon
... does this introduce unnecessary 64-bit arithmetic for 32-bit architectures that currently pass 'unsigned long' to the existing macro?
Good catch. The truncation happens in the unsigned int temporary in arch_freq_get_on_cpu(), so v6 will drop that temporary and leave kernel/sched alone.
Thanks,
Oleg