blob: 3bdb983fd11b15bb385e2aaa966d84499e3356b7 [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
mike.travis@hpe.com341102c2017-10-12 11:32:02 -050033/*
34 * TSC's on different sockets may be reset asynchronously.
35 * This may cause the TSC ADJUST value on socket 0 to be NOT 0.
36 */
37bool __read_mostly tsc_async_resets;
38
39void mark_tsc_async_resets(char *reason)
40{
41 if (tsc_async_resets)
42 return;
43 tsc_async_resets = true;
44 pr_info("tsc: Marking TSC async resets true due to %s\n", reason);
45}
46
Thomas Gleixner6a369582016-12-13 13:14:17 +000047void tsc_verify_tsc_adjust(bool resume)
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000048{
49 struct tsc_adjust *adj = this_cpu_ptr(&tsc_adjust);
50 s64 curval;
51
52 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
53 return;
54
mike.travis@hpe.com9514ece2017-10-12 11:32:03 -050055 /* Skip unnecessary error messages if TSC already unstable */
56 if (check_tsc_unstable())
57 return;
58
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000059 /* Rate limit the MSR check */
Thomas Gleixner6a369582016-12-13 13:14:17 +000060 if (!resume && time_before(jiffies, adj->nextcheck))
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000061 return;
62
63 adj->nextcheck = jiffies + HZ;
64
65 rdmsrl(MSR_IA32_TSC_ADJUST, curval);
66 if (adj->adjusted == curval)
67 return;
68
69 /* Restore the original value */
70 wrmsrl(MSR_IA32_TSC_ADJUST, adj->adjusted);
71
Thomas Gleixner6a369582016-12-13 13:14:17 +000072 if (!adj->warned || resume) {
Thomas Gleixner1d0095f2016-11-19 13:47:37 +000073 pr_warn(FW_BUG "TSC ADJUST differs: CPU%u %lld --> %lld. Restoring\n",
74 smp_processor_id(), adj->adjusted, curval);
75 adj->warned = true;
76 }
77}
78
Thomas Gleixner5bae1562016-12-13 13:14:17 +000079static void tsc_sanitize_first_cpu(struct tsc_adjust *cur, s64 bootval,
80 unsigned int cpu, bool bootcpu)
81{
82 /*
83 * First online CPU in a package stores the boot value in the
84 * adjustment value. This value might change later via the sync
85 * mechanism. If that fails we still can yell about boot values not
86 * being consistent.
87 *
88 * On the boot cpu we just force set the ADJUST value to 0 if it's
89 * non zero. We don't do that on non boot cpus because physical
90 * hotplug should have set the ADJUST register to a value > 0 so
91 * the TSC is in sync with the already running cpus.
mike.travis@hpe.com341102c2017-10-12 11:32:02 -050092 *
93 * Also don't force the ADJUST value to zero if that is a valid value
94 * for socket 0 as determined by the system arch. This is required
95 * when multiple sockets are reset asynchronously with each other
96 * and socket 0 may not have an TSC ADJUST value of 0.
Thomas Gleixner5bae1562016-12-13 13:14:17 +000097 */
Peter Zijlstra855615e2017-05-31 17:52:04 +020098 if (bootcpu && bootval != 0) {
mike.travis@hpe.com341102c2017-10-12 11:32:02 -050099 if (likely(!tsc_async_resets)) {
100 pr_warn(FW_BUG "TSC ADJUST: CPU%u: %lld force to 0\n",
101 cpu, bootval);
102 wrmsrl(MSR_IA32_TSC_ADJUST, 0);
103 bootval = 0;
104 } else {
105 pr_info("TSC ADJUST: CPU%u: %lld NOT forced to 0\n",
106 cpu, bootval);
107 }
Thomas Gleixner5bae1562016-12-13 13:14:17 +0000108 }
109 cur->adjusted = bootval;
110}
111
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000112#ifndef CONFIG_SMP
Thomas Gleixner5bae1562016-12-13 13:14:17 +0000113bool __init tsc_store_and_check_tsc_adjust(bool bootcpu)
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000114{
Thomas Gleixnerb8365542016-11-29 20:28:31 +0100115 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000116 s64 bootval;
117
118 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000119 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000120
mike.travis@hpe.com9514ece2017-10-12 11:32:03 -0500121 /* Skip unnecessary error messages if TSC already unstable */
122 if (check_tsc_unstable())
123 return false;
124
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000125 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
126 cur->bootval = bootval;
Thomas Gleixner1d0095f2016-11-19 13:47:37 +0000127 cur->nextcheck = jiffies + HZ;
Thomas Gleixner5bae1562016-12-13 13:14:17 +0000128 tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(), bootcpu);
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000129 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000130}
131
132#else /* !CONFIG_SMP */
133
134/*
135 * Store and check the TSC ADJUST MSR if available
136 */
Thomas Gleixner5bae1562016-12-13 13:14:17 +0000137bool tsc_store_and_check_tsc_adjust(bool bootcpu)
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000138{
139 struct tsc_adjust *ref, *cur = this_cpu_ptr(&tsc_adjust);
140 unsigned int refcpu, cpu = smp_processor_id();
Thomas Gleixner31f8a652016-12-01 13:26:58 +0100141 struct cpumask *mask;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000142 s64 bootval;
143
144 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000145 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000146
147 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
148 cur->bootval = bootval;
Thomas Gleixner1d0095f2016-11-19 13:47:37 +0000149 cur->nextcheck = jiffies + HZ;
150 cur->warned = false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000151
152 /*
mike.travis@hpe.com341102c2017-10-12 11:32:02 -0500153 * If a non-zero TSC value for socket 0 may be valid then the default
154 * adjusted value cannot assumed to be zero either.
155 */
156 if (tsc_async_resets)
157 cur->adjusted = bootval;
158
159 /*
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000160 * Check whether this CPU is the first in a package to come up. In
161 * this case do not check the boot value against another package
Thomas Gleixner5bae1562016-12-13 13:14:17 +0000162 * because the new package might have been physically hotplugged,
163 * where TSC_ADJUST is expected to be different. When called on the
164 * boot CPU topology_core_cpumask() might not be available yet.
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000165 */
Thomas Gleixner31f8a652016-12-01 13:26:58 +0100166 mask = topology_core_cpumask(cpu);
167 refcpu = mask ? cpumask_any_but(mask, cpu) : nr_cpu_ids;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000168
169 if (refcpu >= nr_cpu_ids) {
Thomas Gleixner5bae1562016-12-13 13:14:17 +0000170 tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(),
171 bootcpu);
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000172 return false;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000173 }
174
175 ref = per_cpu_ptr(&tsc_adjust, refcpu);
176 /*
177 * Compare the boot value and complain if it differs in the
178 * package.
179 */
180 if (bootval != ref->bootval) {
Thomas Gleixner16588f62016-12-18 15:06:27 +0100181 pr_warn(FW_BUG "TSC ADJUST differs: Reference CPU%u: %lld CPU%u: %lld\n",
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000182 refcpu, ref->bootval, cpu, bootval);
183 }
184 /*
185 * The TSC_ADJUST values in a package must be the same. If the boot
186 * value on this newly upcoming CPU differs from the adjustment
187 * value of the already online CPU in this package, set it to that
188 * adjusted value.
189 */
190 if (bootval != ref->adjusted) {
191 pr_warn("TSC ADJUST synchronize: Reference CPU%u: %lld CPU%u: %lld\n",
192 refcpu, ref->adjusted, cpu, bootval);
193 cur->adjusted = ref->adjusted;
194 wrmsrl(MSR_IA32_TSC_ADJUST, ref->adjusted);
195 }
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000196 /*
197 * We have the TSCs forced to be in sync on this package. Skip sync
198 * test:
199 */
200 return true;
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000201}
202
Thomas Gleixner250c2272007-10-11 11:17:24 +0200203/*
204 * Entry/exit counters that make sure that both CPUs
205 * run the measurement code at once:
206 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400207static atomic_t start_count;
208static atomic_t stop_count;
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000209static atomic_t skip_test;
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000210static atomic_t test_runs;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200211
212/*
213 * We use a raw spinlock in this exceptional case, because
214 * we want to have the fastest, inlined, non-debug version
215 * of a critical section, to be able to prove TSC time-warps:
216 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400217static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar643bec92009-05-07 09:12:50 +0200218
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400219static cycles_t last_tsc;
220static cycles_t max_warp;
221static int nr_warps;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000222static int random_warps;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200223
224/*
Andy Lutomirskieee69462015-06-25 18:44:09 +0200225 * TSC-warp measurement loop running on both CPUs. This is not called
226 * if there is no TSC.
Thomas Gleixner250c2272007-10-11 11:17:24 +0200227 */
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000228static cycles_t check_tsc_warp(unsigned int timeout)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200229{
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000230 cycles_t start, now, prev, end, cur_max_warp = 0;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000231 int i, cur_warps = 0;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200232
Andy Lutomirskieee69462015-06-25 18:44:09 +0200233 start = rdtsc_ordered();
Thomas Gleixner250c2272007-10-11 11:17:24 +0200234 /*
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800235 * The measurement runs for 'timeout' msecs:
Thomas Gleixner250c2272007-10-11 11:17:24 +0200236 */
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800237 end = start + (cycles_t) tsc_khz * timeout;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200238 now = start;
239
240 for (i = 0; ; i++) {
241 /*
242 * We take the global lock, measure TSC, save the
243 * previous TSC that was measured (possibly on
244 * another CPU) and update the previous TSC timestamp.
245 */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100246 arch_spin_lock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200247 prev = last_tsc;
Andy Lutomirskieee69462015-06-25 18:44:09 +0200248 now = rdtsc_ordered();
Thomas Gleixner250c2272007-10-11 11:17:24 +0200249 last_tsc = now;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100250 arch_spin_unlock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200251
252 /*
253 * Be nice every now and then (and also check whether
Ingo Molnardf435102008-01-30 13:33:23 +0100254 * measurement is done [we also insert a 10 million
Thomas Gleixner250c2272007-10-11 11:17:24 +0200255 * loops safety exit, so we dont lock up in case the
256 * TSC readout is totally broken]):
257 */
258 if (unlikely(!(i & 7))) {
Ingo Molnardf435102008-01-30 13:33:23 +0100259 if (now > end || i > 10000000)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200260 break;
261 cpu_relax();
262 touch_nmi_watchdog();
263 }
264 /*
265 * Outside the critical section we can now see whether
266 * we saw a time-warp of the TSC going backwards:
267 */
268 if (unlikely(prev > now)) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100269 arch_spin_lock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200270 max_warp = max(max_warp, prev - now);
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000271 cur_max_warp = max_warp;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000272 /*
273 * Check whether this bounces back and forth. Only
274 * one CPU should observe time going backwards.
275 */
276 if (cur_warps != nr_warps)
277 random_warps++;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200278 nr_warps++;
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000279 cur_warps = nr_warps;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100280 arch_spin_unlock(&sync_lock);
Thomas Gleixner250c2272007-10-11 11:17:24 +0200281 }
Ingo Molnarad8ca492008-01-30 13:33:24 +0100282 }
Arjan van de Venbde78a72008-07-08 09:51:56 -0700283 WARN(!(now-start),
284 "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
Ingo Molnarad8ca492008-01-30 13:33:24 +0100285 now-start, end-start);
Thomas Gleixner76d3b85152016-11-19 13:47:41 +0000286 return cur_max_warp;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200287}
288
289/*
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800290 * If the target CPU coming online doesn't have any of its core-siblings
291 * online, a timeout of 20msec will be used for the TSC-warp measurement
292 * loop. Otherwise a smaller timeout of 2msec will be used, as we have some
293 * information about this socket already (and this information grows as we
294 * have more and more logical-siblings in that socket).
295 *
296 * Ideally we should be able to skip the TSC sync check on the other
297 * core-siblings, if the first logical CPU in a socket passed the sync test.
298 * But as the TSC is per-logical CPU and can potentially be modified wrongly
299 * by the bios, TSC sync test for smaller duration should be able
300 * to catch such errors. Also this will catch the condition where all the
301 * cores in the socket doesn't get reset at the same time.
302 */
303static inline unsigned int loop_timeout(int cpu)
304{
Bartosz Golaszewski7d79a7b2015-05-26 15:11:35 +0200305 return (cpumask_weight(topology_core_cpumask(cpu)) > 1) ? 2 : 20;
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800306}
307
308/*
Thomas Gleixner250c2272007-10-11 11:17:24 +0200309 * Source CPU calls into this - it waits for the freshly booted
310 * target CPU to arrive and then starts the measurement:
311 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400312void check_tsc_sync_source(int cpu)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200313{
314 int cpus = 2;
315
316 /*
317 * No need to check if we already know that the TSC is not
Andy Lutomirskieee69462015-06-25 18:44:09 +0200318 * synchronized or if we have no TSC.
Thomas Gleixner250c2272007-10-11 11:17:24 +0200319 */
320 if (unsynchronized_tsc())
321 return;
322
Thomas Gleixner250c2272007-10-11 11:17:24 +0200323 /*
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000324 * Set the maximum number of test runs to
325 * 1 if the CPU does not provide the TSC_ADJUST MSR
326 * 3 if the MSR is available, so the target can try to adjust
327 */
328 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
329 atomic_set(&test_runs, 1);
330 else
331 atomic_set(&test_runs, 3);
332retry:
333 /*
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000334 * Wait for the target to start or to skip the test:
Thomas Gleixner250c2272007-10-11 11:17:24 +0200335 */
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000336 while (atomic_read(&start_count) != cpus - 1) {
337 if (atomic_read(&skip_test) > 0) {
338 atomic_set(&skip_test, 0);
339 return;
340 }
Thomas Gleixner250c2272007-10-11 11:17:24 +0200341 cpu_relax();
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000342 }
343
Thomas Gleixner250c2272007-10-11 11:17:24 +0200344 /*
345 * Trigger the target to continue into the measurement too:
346 */
347 atomic_inc(&start_count);
348
Suresh Siddhab0e5c772012-02-06 18:32:20 -0800349 check_tsc_warp(loop_timeout(cpu));
Thomas Gleixner250c2272007-10-11 11:17:24 +0200350
351 while (atomic_read(&stop_count) != cpus-1)
352 cpu_relax();
353
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000354 /*
355 * If the test was successful set the number of runs to zero and
356 * stop. If not, decrement the number of runs an check if we can
357 * retry. In case of random warps no retry is attempted.
358 */
359 if (!nr_warps) {
360 atomic_set(&test_runs, 0);
361
362 pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
363 smp_processor_id(), cpu);
364
365 } else if (atomic_dec_and_test(&test_runs) || random_warps) {
366 /* Force it to 0 if random warps brought us here */
367 atomic_set(&test_runs, 0);
368
Mike Travis9b3660a2009-11-17 18:22:16 -0600369 pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n",
370 smp_processor_id(), cpu);
Ingo Molnar643bec92009-05-07 09:12:50 +0200371 pr_warning("Measured %Ld cycles TSC warp between CPUs, "
372 "turning off TSC clock.\n", max_warp);
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000373 if (random_warps)
374 pr_warning("TSC warped randomly between CPUs\n");
Thomas Gleixner250c2272007-10-11 11:17:24 +0200375 mark_tsc_unstable("check_tsc_sync_source failed");
Thomas Gleixner250c2272007-10-11 11:17:24 +0200376 }
377
378 /*
Mike Galbraith4c6b8b42008-01-30 13:30:04 +0100379 * Reset it - just in case we boot another CPU later:
380 */
381 atomic_set(&start_count, 0);
Thomas Gleixnerbec85202016-11-19 13:47:35 +0000382 random_warps = 0;
Mike Galbraith4c6b8b42008-01-30 13:30:04 +0100383 nr_warps = 0;
384 max_warp = 0;
385 last_tsc = 0;
386
387 /*
Thomas Gleixner250c2272007-10-11 11:17:24 +0200388 * Let the target continue with the bootup:
389 */
390 atomic_inc(&stop_count);
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000391
392 /*
393 * Retry, if there is a chance to do so.
394 */
395 if (atomic_read(&test_runs) > 0)
396 goto retry;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200397}
398
399/*
400 * Freshly booted CPUs call into this:
401 */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400402void check_tsc_sync_target(void)
Thomas Gleixner250c2272007-10-11 11:17:24 +0200403{
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000404 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
405 unsigned int cpu = smp_processor_id();
406 cycles_t cur_max_warp, gbl_max_warp;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200407 int cpus = 2;
408
Andy Lutomirskieee69462015-06-25 18:44:09 +0200409 /* Also aborts if there is no TSC. */
Thomas Gleixner5f2e71e2017-02-09 16:08:42 +0100410 if (unsynchronized_tsc())
Thomas Gleixner250c2272007-10-11 11:17:24 +0200411 return;
412
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000413 /*
414 * Store, verify and sanitize the TSC adjust register. If
415 * successful skip the test.
Thomas Gleixner5f2e71e2017-02-09 16:08:42 +0100416 *
417 * The test is also skipped when the TSC is marked reliable. This
418 * is true for SoCs which have no fallback clocksource. On these
419 * SoCs the TSC is frequency synchronized, but still the TSC ADJUST
420 * register might have been wreckaged by the BIOS..
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000421 */
Thomas Gleixner5f2e71e2017-02-09 16:08:42 +0100422 if (tsc_store_and_check_tsc_adjust(false) || tsc_clocksource_reliable) {
Thomas Gleixnera36f5132016-11-19 13:47:39 +0000423 atomic_inc(&skip_test);
424 return;
425 }
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000426
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000427retry:
Thomas Gleixner250c2272007-10-11 11:17:24 +0200428 /*
429 * Register this CPU's participation and wait for the
430 * source CPU to start the measurement:
431 */
432 atomic_inc(&start_count);
433 while (atomic_read(&start_count) != cpus)
434 cpu_relax();
435
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000436 cur_max_warp = check_tsc_warp(loop_timeout(cpu));
437
438 /*
439 * Store the maximum observed warp value for a potential retry:
440 */
441 gbl_max_warp = max_warp;
Thomas Gleixner250c2272007-10-11 11:17:24 +0200442
443 /*
444 * Ok, we are done:
445 */
446 atomic_inc(&stop_count);
447
448 /*
449 * Wait for the source CPU to print stuff:
450 */
451 while (atomic_read(&stop_count) != cpus)
452 cpu_relax();
Thomas Gleixner4c5e3c62016-11-19 13:47:40 +0000453
454 /*
455 * Reset it for the next sync test:
456 */
457 atomic_set(&stop_count, 0);
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000458
459 /*
460 * Check the number of remaining test runs. If not zero, the test
461 * failed and a retry with adjusted TSC is possible. If zero the
462 * test was either successful or failed terminally.
463 */
464 if (!atomic_read(&test_runs))
465 return;
466
467 /*
468 * If the warp value of this CPU is 0, then the other CPU
469 * observed time going backwards so this TSC was ahead and
470 * needs to move backwards.
471 */
472 if (!cur_max_warp)
473 cur_max_warp = -gbl_max_warp;
474
475 /*
476 * Add the result to the previous adjustment value.
477 *
478 * The adjustement value is slightly off by the overhead of the
479 * sync mechanism (observed values are ~200 TSC cycles), but this
480 * really depends on CPU, node distance and frequency. So
481 * compensating for this is hard to get right. Experiments show
482 * that the warp is not longer detectable when the observed warp
483 * value is used. In the worst case the adjustment needs to go
484 * through a 3rd run for fine tuning.
485 */
486 cur->adjusted += cur_max_warp;
Thomas Gleixner8c9b9d82016-12-18 15:09:29 +0100487
Thomas Gleixnercc4db262016-11-19 13:47:43 +0000488 pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n",
489 cpu, cur_max_warp, cur->adjusted);
490
491 wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted);
492 goto retry;
493
Thomas Gleixner250c2272007-10-11 11:17:24 +0200494}
Thomas Gleixner8b223bc2016-11-19 13:47:36 +0000495
496#endif /* CONFIG_SMP */