Skip to content

Commit e2b0d61

Browse files
gghhPeter Zijlstra
authored andcommitted
x86, sched: check for counters overflow in frequency invariant accounting
The product mcnt * arch_max_freq_ratio can overflows u64. For context, a large value for arch_max_freq_ratio would be 5000, corresponding to a turbo_freq/base_freq ratio of 5 (normally it's more like 1500-2000). A large increment frequency for the MPERF counter would be 5GHz (the base clock of all CPUs on the market today is less than that). With these figures, a CPU would need to go without a scheduler tick for around 8 days for the u64 overflow to happen. It is unlikely, but the check is warranted. Under similar conditions, the difference acnt of two consecutive APERF readings can overflow as well. In these circumstances is appropriate to disable frequency invariant accounting: the feature relies on measures of the clock frequency done at every scheduler tick, which need to be "fresh" to be at all meaningful. A note on i386: prior to version 5.1, the GCC compiler didn't have the builtin function __builtin_mul_overflow. In these GCC versions the macro check_mul_overflow needs __udivdi3() to do (u64)a/b, which the kernel doesn't provide. For this reason this change fails to build on i386 if GCC<5.1, and we protect the entire frequency invariant code behind CONFIG_X86_64 (special thanks to "kbuild test robot" <[email protected]>). Fixes: 1567c3e ("x86, sched: Add support for frequency invariance") Signed-off-by: Giovanni Gherdovich <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Rafael J. Wysocki <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 4581bea commit e2b0d61

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

arch/x86/include/asm/topology.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static inline void sched_clear_itmt_support(void)
193193
}
194194
#endif /* CONFIG_SCHED_MC_PRIO */
195195

196-
#ifdef CONFIG_SMP
196+
#if defined(CONFIG_SMP) && defined(CONFIG_X86_64)
197197
#include <asm/cpufeature.h>
198198

199199
DECLARE_STATIC_KEY_FALSE(arch_scale_freq_key);

arch/x86/kernel/smpboot.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <linux/cpuidle.h>
5757
#include <linux/numa.h>
5858
#include <linux/pgtable.h>
59+
#include <linux/overflow.h>
5960

6061
#include <asm/acpi.h>
6162
#include <asm/desc.h>
@@ -1777,6 +1778,7 @@ void native_play_dead(void)
17771778

17781779
#endif
17791780

1781+
#ifdef CONFIG_X86_64
17801782
/*
17811783
* APERF/MPERF frequency ratio computation.
17821784
*
@@ -2048,11 +2050,19 @@ static void init_freq_invariance(bool secondary)
20482050
}
20492051
}
20502052

2053+
static void disable_freq_invariance_workfn(struct work_struct *work)
2054+
{
2055+
static_branch_disable(&arch_scale_freq_key);
2056+
}
2057+
2058+
static DECLARE_WORK(disable_freq_invariance_work,
2059+
disable_freq_invariance_workfn);
2060+
20512061
DEFINE_PER_CPU(unsigned long, arch_freq_scale) = SCHED_CAPACITY_SCALE;
20522062

20532063
void arch_scale_freq_tick(void)
20542064
{
2055-
u64 freq_scale;
2065+
u64 freq_scale = SCHED_CAPACITY_SCALE;
20562066
u64 aperf, mperf;
20572067
u64 acnt, mcnt;
20582068

@@ -2064,19 +2074,32 @@ void arch_scale_freq_tick(void)
20642074

20652075
acnt = aperf - this_cpu_read(arch_prev_aperf);
20662076
mcnt = mperf - this_cpu_read(arch_prev_mperf);
2067-
if (!mcnt)
2068-
return;
20692077

20702078
this_cpu_write(arch_prev_aperf, aperf);
20712079
this_cpu_write(arch_prev_mperf, mperf);
20722080

2073-
acnt <<= 2*SCHED_CAPACITY_SHIFT;
2074-
mcnt *= arch_max_freq_ratio;
2081+
if (check_shl_overflow(acnt, 2*SCHED_CAPACITY_SHIFT, &acnt))
2082+
goto error;
2083+
2084+
if (check_mul_overflow(mcnt, arch_max_freq_ratio, &mcnt) || !mcnt)
2085+
goto error;
20752086

20762087
freq_scale = div64_u64(acnt, mcnt);
2088+
if (!freq_scale)
2089+
goto error;
20772090

20782091
if (freq_scale > SCHED_CAPACITY_SCALE)
20792092
freq_scale = SCHED_CAPACITY_SCALE;
20802093

20812094
this_cpu_write(arch_freq_scale, freq_scale);
2095+
return;
2096+
2097+
error:
2098+
pr_warn("Scheduler frequency invariance went wobbly, disabling!\n");
2099+
schedule_work(&disable_freq_invariance_work);
2100+
}
2101+
#else
2102+
static inline void init_freq_invariance(bool secondary)
2103+
{
20822104
}
2105+
#endif /* CONFIG_X86_64 */

0 commit comments

Comments
 (0)