blob: 94f2ce5fb159a5f42e0abdbd932c2fc77e1ad4a7 [file] [log] [blame]
Thomas Gleixner250c2272007-10-11 11:17:24 +02001/*
Dave Jones835c34a2007-10-12 21:10:53 -04002 * check TSC synchronization.
Thomas Gleixner250c2272007-10-11 11:17:24 +02003 *
4 * Copyright (C) 2006, Red Hat, Inc., Ingo Molnar
5 *
6 * We check whether all boot CPUs have their TSC's synchronized,
7 * print a warning if not and turn off the TSC clock-source.
8 *
9 * The warp-check is point-to-point between two CPUs, the CPU
10 * initiating the bootup is the 'source CPU', the freshly booting
11 * CPU is the 'target CPU'.
12 *
13 * Only two CPUs may participate - they can enter in any order.
14 * ( The serial nature of the boot logic and the CPU hotplug lock
15 * protects against more than 2 CPUs entering this code. )
16 */
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000017#include <linux/topology.h>
Thomas Gleixner250c2272007-10-11 11:17:24 +020018#include <linux/spinlock.h>
19#include <linux/kernel.h>
Thomas Gleixner250c2272007-10-11 11:17:24 +020020#include <linux/smp.h>
21#include <linux/nmi.h>
22#include <asm/tsc.h>
23
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000024struct tsc_adjust {
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000025 s64 bootval;
26 s64 adjusted;
27 unsigned long nextcheck;
28 bool warned;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000029};
30
31static DEFINE_PER_CPU(struct tsc_adjust, tsc_adjust);
32
Thomas Gleixner6a369582016-12-13 13:14:17 +000033void tsc_verify_tsc_adjust(bool resume)
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000034{
35 struct tsc_adjust *adj = this_cpu_ptr(&tsc_adjust);
36 s64 curval;
37
38 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
39 return;
40
41 /* Rate limit the MSR check */
Thomas Gleixner6a369582016-12-13 13:14:17 +000042 if (!resume && time_before(jiffies, adj->nextcheck))
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000043 return;
44
45 adj->nextcheck = jiffies + HZ;
46
47 rdmsrl(MSR_IA32_TSC_ADJUST, curval);
48 if (adj->adjusted == curval)
49 return;
50
51 /* Restore the original value */
52 wrmsrl(MSR_IA32_TSC_ADJUST, adj->adjusted);
53
Thomas Gleixner6a369582016-12-13 13:14:17 +000054 if (!adj->warned || resume) {
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000055 pr_warn(FW_BUG "TSC ADJUST differs: CPU%u %lld --> %lld. Restoring\n",
56 smp_processor_id(), adj->adjusted, curval);
57 adj->warned = true;
58 }
59}
60
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000061#ifndef CONFIG_SMP
Thomas Gleixnera36f5132016-11-19 13:47:39 +000062bool __init tsc_store_and_check_tsc_adjust(void)
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000063{
Thomas Gleixnerb8365542016-11-29 20:28:31 +010064 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000065 s64 bootval;
66
67 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
Thomas Gleixnera36f5132016-11-19 13:47:39 +000068 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000069
70 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
71 cur->bootval = bootval;
72 cur->adjusted = bootval;
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000073 cur->nextcheck = jiffies + HZ;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000074 pr_info("TSC ADJUST: Boot CPU0: %lld\n", bootval);
Thomas Gleixnera36f5132016-11-19 13:47:39 +000075 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000076}
77
78#else /* !CONFIG_SMP */
79
80/*
81 * Store and check the TSC ADJUST MSR if available
82 */
Thomas Gleixnera36f5132016-11-19 13:47:39 +000083bool tsc_store_and_check_tsc_adjust(void)
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000084{
85 struct tsc_adjust *ref, *cur = this_cpu_ptr(&tsc_adjust);
86 unsigned int refcpu, cpu = smp_processor_id();
Thomas Gleixner31f8a652016-12-01 13:26:58 +010087 struct cpumask *mask;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000088 s64 bootval;
89
90 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
Thomas Gleixnera36f5132016-11-19 13:47:39 +000091 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000092
93 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
94 cur->bootval = bootval;
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000095 cur->nextcheck = jiffies + HZ;
96 cur->warned = false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +000097
98 /*
99 * Check whether this CPU is the first in a package to come up. In
100 * this case do not check the boot value against another package
101 * because the package might have been physically hotplugged, where
Thomas Gleixner31f8a652016-12-01 13:26:58 +0100102 * TSC_ADJUST is expected to be different. When called on the boot
103 * CPU topology_core_cpumask() might not be available yet.
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000104 */
Thomas Gleixner31f8a652016-12-01 13:26:58 +0100105 mask = topology_core_cpumask(cpu);
106 refcpu = mask ? cpumask_any_but(mask, cpu) : nr_cpu_ids;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000107
108 if (refcpu >= nr_cpu_ids) {
109 /*
110 * First online CPU in a package stores the boot value in
111 * the adjustment value. This value might change later via
112 * the sync mechanism. If that fails we still can yell
113 * about boot values not being consistent.
114 */
115 cur->adjusted = bootval;
116 pr_info_once("TSC ADJUST: Boot CPU%u: %lld\n", cpu, bootval);
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000117 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000118 }
119
120 ref = per_cpu_ptr(&tsc_adjust, refcpu);
121 /*
122 * Compare the boot value and complain if it differs in the
123 * package.
124 */
125 if (bootval != ref->bootval) {
126 pr_warn("TSC ADJUST differs: Reference CPU%u: %lld CPU%u: %lld\n",
127 refcpu, ref->bootval, cpu, bootval);
128 }
129 /*
130 * The TSC_ADJUST values in a package must be the same. If the boot
131 * value on this newly upcoming CPU differs from the adjustment
132 * value of the already online CPU in this package, set it to that
133 * adjusted value.
134 */
135 if (bootval != ref->adjusted) {
136 pr_warn("TSC ADJUST synchronize: Reference CPU%u: %lld CPU%u: %lld\n",
137 refcpu, ref->adjusted, cpu, bootval);
138 cur->adjusted = ref->adjusted;
139 wrmsrl(MSR_IA32_TSC_ADJUST, ref->adjusted);
140 }
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000141 /*
142 * We have the TSCs forced to be in sync on this package. Skip sync
143 * test:
144 */
145 return true;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000146}
147
Thomas Gleixner250c2272007-10-11 11:17:24 +0200148/*
149 * Entry/exit counters that make sure that both CPUs
150 * run the measurement code at once:
151 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400152static atomic_t start_count;
153static atomic_t stop_count;
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000154static atomic_t skip_test;
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000155static atomic_t test_runs;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200156
157/*
158 * We use a raw spinlock in this exceptional case, because
159 * we want to have the fastest, inlined, non-debug version
160 * of a critical section, to be able to prove TSC time-warps:
161 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400162static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar643bec92009-05-07 09:12:50 +0200163
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400164static cycles_t last_tsc;
165static cycles_t max_warp;
166static int nr_warps;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000167static int random_warps;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200168
169/*
Andy Lutomirskieee69462015-06-25 18:44:09 +0200170 * TSC-warp measurement loop running on both CPUs. This is not called
171 * if there is no TSC.
Thomas Gleixner250c2272007-10-11 11:17:24 +0200172 */
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000173static cycles_t check_tsc_warp(unsigned int timeout)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200174{
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000175 cycles_t start, now, prev, end, cur_max_warp = 0;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000176 int i, cur_warps = 0;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200177
Andy Lutomirskieee69462015-06-25 18:44:09 +0200178 start = rdtsc_ordered();
Thomas Gleixner250c2272007-10-11 11:17:24 +0200179 /*
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800180 * The measurement runs for 'timeout' msecs:
Thomas Gleixner250c2272007-10-11 11:17:24 +0200181 */
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800182 end = start + (cycles_t) tsc_khz * timeout;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200183 now = start;
184
185 for (i = 0; ; i++) {
186 /*
187 * We take the global lock, measure TSC, save the
188 * previous TSC that was measured (possibly on
189 * another CPU) and update the previous TSC timestamp.
190 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100191 arch_spin_lock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200192 prev = last_tsc;
Andy Lutomirskieee69462015-06-25 18:44:09 +0200193 now = rdtsc_ordered();
Thomas Gleixner250c2272007-10-11 11:17:24 +0200194 last_tsc = now;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100195 arch_spin_unlock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200196
197 /*
198 * Be nice every now and then (and also check whether
Ingo Molnardf435102008-01-30 13:33:23 +0100199 * measurement is done [we also insert a 10 million
Thomas Gleixner250c2272007-10-11 11:17:24 +0200200 * loops safety exit, so we dont lock up in case the
201 * TSC readout is totally broken]):
202 */
203 if (unlikely(!(i & 7))) {
Ingo Molnardf435102008-01-30 13:33:23 +0100204 if (now > end || i > 10000000)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200205 break;
206 cpu_relax();
207 touch_nmi_watchdog();
208 }
209 /*
210 * Outside the critical section we can now see whether
211 * we saw a time-warp of the TSC going backwards:
212 */
213 if (unlikely(prev > now)) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100214 arch_spin_lock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200215 max_warp = max(max_warp, prev - now);
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000216 cur_max_warp = max_warp;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000217 /*
218 * Check whether this bounces back and forth. Only
219 * one CPU should observe time going backwards.
220 */
221 if (cur_warps != nr_warps)
222 random_warps++;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200223 nr_warps++;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000224 cur_warps = nr_warps;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100225 arch_spin_unlock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200226 }
Ingo Molnarad8ca492008-01-30 13:33:24 +0100227 }
Arjan van de Venbde78a72008-07-08 09:51:56 -0700228 WARN(!(now-start),
229 "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
Ingo Molnarad8ca492008-01-30 13:33:24 +0100230 now-start, end-start);
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000231 return cur_max_warp;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200232}
233
234/*
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800235 * If the target CPU coming online doesn't have any of its core-siblings
236 * online, a timeout of 20msec will be used for the TSC-warp measurement
237 * loop. Otherwise a smaller timeout of 2msec will be used, as we have some
238 * information about this socket already (and this information grows as we
239 * have more and more logical-siblings in that socket).
240 *
241 * Ideally we should be able to skip the TSC sync check on the other
242 * core-siblings, if the first logical CPU in a socket passed the sync test.
243 * But as the TSC is per-logical CPU and can potentially be modified wrongly
244 * by the bios, TSC sync test for smaller duration should be able
245 * to catch such errors. Also this will catch the condition where all the
246 * cores in the socket doesn't get reset at the same time.
247 */
248static inline unsigned int loop_timeout(int cpu)
249{
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200250 return (cpumask_weight(topology_core_cpumask(cpu)) > 1) ? 2 : 20;
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800251}
252
253/*
Thomas Gleixner250c2272007-10-11 11:17:24 +0200254 * Source CPU calls into this - it waits for the freshly booted
255 * target CPU to arrive and then starts the measurement:
256 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400257void check_tsc_sync_source(int cpu)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200258{
259 int cpus = 2;
260
261 /*
262 * No need to check if we already know that the TSC is not
Andy Lutomirskieee69462015-06-25 18:44:09 +0200263 * synchronized or if we have no TSC.
Thomas Gleixner250c2272007-10-11 11:17:24 +0200264 */
265 if (unsynchronized_tsc())
266 return;
267
Suresh Siddha28a00182011-11-04 15:42:17 -0700268 if (tsc_clocksource_reliable) {
Mike Travis9b3660a2009-11-17 18:22:16 -0600269 if (cpu == (nr_cpu_ids-1) || system_state != SYSTEM_BOOTING)
270 pr_info(
271 "Skipped synchronization checks as TSC is reliable.\n");
Alok Katariaeca0cd02008-10-31 12:01:58 -0700272 return;
273 }
274
Thomas Gleixner250c2272007-10-11 11:17:24 +0200275 /*
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000276 * Set the maximum number of test runs to
277 * 1 if the CPU does not provide the TSC_ADJUST MSR
278 * 3 if the MSR is available, so the target can try to adjust
279 */
280 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
281 atomic_set(&test_runs, 1);
282 else
283 atomic_set(&test_runs, 3);
284retry:
285 /*
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000286 * Wait for the target to start or to skip the test:
Thomas Gleixner250c2272007-10-11 11:17:24 +0200287 */
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000288 while (atomic_read(&start_count) != cpus - 1) {
289 if (atomic_read(&skip_test) > 0) {
290 atomic_set(&skip_test, 0);
291 return;
292 }
Thomas Gleixner250c2272007-10-11 11:17:24 +0200293 cpu_relax();
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000294 }
295
Thomas Gleixner250c2272007-10-11 11:17:24 +0200296 /*
297 * Trigger the target to continue into the measurement too:
298 */
299 atomic_inc(&start_count);
300
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800301 check_tsc_warp(loop_timeout(cpu));
Thomas Gleixner250c2272007-10-11 11:17:24 +0200302
303 while (atomic_read(&stop_count) != cpus-1)
304 cpu_relax();
305
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000306 /*
307 * If the test was successful set the number of runs to zero and
308 * stop. If not, decrement the number of runs an check if we can
309 * retry. In case of random warps no retry is attempted.
310 */
311 if (!nr_warps) {
312 atomic_set(&test_runs, 0);
313
314 pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
315 smp_processor_id(), cpu);
316
317 } else if (atomic_dec_and_test(&test_runs) || random_warps) {
318 /* Force it to 0 if random warps brought us here */
319 atomic_set(&test_runs, 0);
320
Mike Travis9b3660a2009-11-17 18:22:16 -0600321 pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n",
322 smp_processor_id(), cpu);
Ingo Molnar643bec92009-05-07 09:12:50 +0200323 pr_warning("Measured %Ld cycles TSC warp between CPUs, "
324 "turning off TSC clock.\n", max_warp);
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000325 if (random_warps)
326 pr_warning("TSC warped randomly between CPUs\n");
Thomas Gleixner250c2272007-10-11 11:17:24 +0200327 mark_tsc_unstable("check_tsc_sync_source failed");
Thomas Gleixner250c2272007-10-11 11:17:24 +0200328 }
329
330 /*
Mike Galbraith4c6b8b42008-01-30 13:30:04 +0100331 * Reset it - just in case we boot another CPU later:
332 */
333 atomic_set(&start_count, 0);
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000334 random_warps = 0;
Mike Galbraith4c6b8b42008-01-30 13:30:04 +0100335 nr_warps = 0;
336 max_warp = 0;
337 last_tsc = 0;
338
339 /*
Thomas Gleixner250c2272007-10-11 11:17:24 +0200340 * Let the target continue with the bootup:
341 */
342 atomic_inc(&stop_count);
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000343
344 /*
345 * Retry, if there is a chance to do so.
346 */
347 if (atomic_read(&test_runs) > 0)
348 goto retry;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200349}
350
351/*
352 * Freshly booted CPUs call into this:
353 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400354void check_tsc_sync_target(void)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200355{
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000356 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
357 unsigned int cpu = smp_processor_id();
358 cycles_t cur_max_warp, gbl_max_warp;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200359 int cpus = 2;
360
Andy Lutomirskieee69462015-06-25 18:44:09 +0200361 /* Also aborts if there is no TSC. */
Suresh Siddha28a00182011-11-04 15:42:17 -0700362 if (unsynchronized_tsc() || tsc_clocksource_reliable)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200363 return;
364
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000365 /*
366 * Store, verify and sanitize the TSC adjust register. If
367 * successful skip the test.
368 */
369 if (tsc_store_and_check_tsc_adjust()) {
370 atomic_inc(&skip_test);
371 return;
372 }
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000373
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000374retry:
Thomas Gleixner250c2272007-10-11 11:17:24 +0200375 /*
376 * Register this CPU's participation and wait for the
377 * source CPU to start the measurement:
378 */
379 atomic_inc(&start_count);
380 while (atomic_read(&start_count) != cpus)
381 cpu_relax();
382
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000383 cur_max_warp = check_tsc_warp(loop_timeout(cpu));
384
385 /*
386 * Store the maximum observed warp value for a potential retry:
387 */
388 gbl_max_warp = max_warp;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200389
390 /*
391 * Ok, we are done:
392 */
393 atomic_inc(&stop_count);
394
395 /*
396 * Wait for the source CPU to print stuff:
397 */
398 while (atomic_read(&stop_count) != cpus)
399 cpu_relax();
Thomas Gleixner4c5e3c62016-11-19 13:47:40 +0000400
401 /*
402 * Reset it for the next sync test:
403 */
404 atomic_set(&stop_count, 0);
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000405
406 /*
407 * Check the number of remaining test runs. If not zero, the test
408 * failed and a retry with adjusted TSC is possible. If zero the
409 * test was either successful or failed terminally.
410 */
411 if (!atomic_read(&test_runs))
412 return;
413
414 /*
415 * If the warp value of this CPU is 0, then the other CPU
416 * observed time going backwards so this TSC was ahead and
417 * needs to move backwards.
418 */
419 if (!cur_max_warp)
420 cur_max_warp = -gbl_max_warp;
421
422 /*
423 * Add the result to the previous adjustment value.
424 *
425 * The adjustement value is slightly off by the overhead of the
426 * sync mechanism (observed values are ~200 TSC cycles), but this
427 * really depends on CPU, node distance and frequency. So
428 * compensating for this is hard to get right. Experiments show
429 * that the warp is not longer detectable when the observed warp
430 * value is used. In the worst case the adjustment needs to go
431 * through a 3rd run for fine tuning.
432 */
433 cur->adjusted += cur_max_warp;
434
435 pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n",
436 cpu, cur_max_warp, cur->adjusted);
437
438 wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted);
439 goto retry;
440
Thomas Gleixner250c2272007-10-11 11:17:24 +0200441}
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000442
443#endif /* CONFIG_SMP */