Skip to content

Commit 615cd03

Browse files
Peter Zijlstraingomolnar
authored andcommitted
x86/tsc: Fix sched_clock() sync
While looking through the code I noticed that we initialize the cyc2ns fields with a different cycle value for each CPU, resulting in a slightly different 0 point for each CPU. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 59eaef7 commit 615cd03

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎arch/x86/kernel/tsc.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ static inline unsigned long long cycles_2_ns(unsigned long long cyc)
137137
return ns;
138138
}
139139

140-
static void set_cyc2ns_scale(unsigned long khz, int cpu)
140+
static void __set_cyc2ns_scale(unsigned long khz, int cpu, unsigned long long tsc_now)
141141
{
142-
unsigned long long tsc_now, ns_now;
142+
unsigned long long ns_now;
143143
struct cyc2ns_data data;
144144
struct cyc2ns *c2n;
145145
unsigned long flags;
@@ -150,7 +150,6 @@ static void set_cyc2ns_scale(unsigned long khz, int cpu)
150150
if (!khz)
151151
goto done;
152152

153-
tsc_now = rdtsc();
154153
ns_now = cycles_2_ns(tsc_now);
155154

156155
/*
@@ -186,6 +185,12 @@ static void set_cyc2ns_scale(unsigned long khz, int cpu)
186185
sched_clock_idle_wakeup_event(0);
187186
local_irq_restore(flags);
188187
}
188+
189+
static void set_cyc2ns_scale(unsigned long khz, int cpu)
190+
{
191+
__set_cyc2ns_scale(khz, cpu, rdtsc());
192+
}
193+
189194
/*
190195
* Scheduler clock - returns current time in nanosec units.
191196
*/
@@ -1251,7 +1256,7 @@ device_initcall(init_tsc_clocksource);
12511256

12521257
void __init tsc_init(void)
12531258
{
1254-
u64 lpj;
1259+
u64 lpj, cyc;
12551260
int cpu;
12561261

12571262
if (!boot_cpu_has(X86_FEATURE_TSC)) {
@@ -1291,9 +1296,10 @@ void __init tsc_init(void)
12911296
* speed as the bootup CPU. (cpufreq notifiers will fix this
12921297
* up if their speed diverges)
12931298
*/
1299+
cyc = rdtsc();
12941300
for_each_possible_cpu(cpu) {
12951301
cyc2ns_init(cpu);
1296-
set_cyc2ns_scale(tsc_khz, cpu);
1302+
__set_cyc2ns_scale(tsc_khz, cpu, cyc);
12971303
}
12981304

12991305
if (tsc_disabled > 0)

0 commit comments

Comments
 (0)