tsc_sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * check TSC synchronization.
  3. *
  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. */
  17. #include <linux/topology.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/kernel.h>
  20. #include <linux/smp.h>
  21. #include <linux/nmi.h>
  22. #include <asm/tsc.h>
  23. struct tsc_adjust {
  24. s64 bootval;
  25. s64 adjusted;
  26. unsigned long nextcheck;
  27. bool warned;
  28. };
  29. static DEFINE_PER_CPU(struct tsc_adjust, tsc_adjust);
  30. void tsc_verify_tsc_adjust(bool resume)
  31. {
  32. struct tsc_adjust *adj = this_cpu_ptr(&tsc_adjust);
  33. s64 curval;
  34. if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
  35. return;
  36. /* Rate limit the MSR check */
  37. if (!resume && time_before(jiffies, adj->nextcheck))
  38. return;
  39. adj->nextcheck = jiffies + HZ;
  40. rdmsrl(MSR_IA32_TSC_ADJUST, curval);
  41. if (adj->adjusted == curval)
  42. return;
  43. /* Restore the original value */
  44. wrmsrl(MSR_IA32_TSC_ADJUST, adj->adjusted);
  45. if (!adj->warned || resume) {
  46. pr_warn(FW_BUG "TSC ADJUST differs: CPU%u %lld --> %lld. Restoring\n",
  47. smp_processor_id(), adj->adjusted, curval);
  48. adj->warned = true;
  49. }
  50. }
  51. static void tsc_sanitize_first_cpu(struct tsc_adjust *cur, s64 bootval,
  52. unsigned int cpu, bool bootcpu)
  53. {
  54. /*
  55. * First online CPU in a package stores the boot value in the
  56. * adjustment value. This value might change later via the sync
  57. * mechanism. If that fails we still can yell about boot values not
  58. * being consistent.
  59. *
  60. * On the boot cpu we just force set the ADJUST value to 0 if it's
  61. * non zero. We don't do that on non boot cpus because physical
  62. * hotplug should have set the ADJUST register to a value > 0 so
  63. * the TSC is in sync with the already running cpus.
  64. */
  65. if (bootcpu && bootval != 0) {
  66. pr_warn(FW_BUG "TSC ADJUST: CPU%u: %lld force to 0\n", cpu,
  67. bootval);
  68. wrmsrl(MSR_IA32_TSC_ADJUST, 0);
  69. bootval = 0;
  70. }
  71. cur->adjusted = bootval;
  72. }
  73. #ifndef CONFIG_SMP
  74. bool __init tsc_store_and_check_tsc_adjust(bool bootcpu)
  75. {
  76. struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
  77. s64 bootval;
  78. if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
  79. return false;
  80. rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
  81. cur->bootval = bootval;
  82. cur->nextcheck = jiffies + HZ;
  83. tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(), bootcpu);
  84. return false;
  85. }
  86. #else /* !CONFIG_SMP */
  87. /*
  88. * Store and check the TSC ADJUST MSR if available
  89. */
  90. bool tsc_store_and_check_tsc_adjust(bool bootcpu)
  91. {
  92. struct tsc_adjust *ref, *cur = this_cpu_ptr(&tsc_adjust);
  93. unsigned int refcpu, cpu = smp_processor_id();
  94. struct cpumask *mask;
  95. s64 bootval;
  96. if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
  97. return false;
  98. rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
  99. cur->bootval = bootval;
  100. cur->nextcheck = jiffies + HZ;
  101. cur->warned = false;
  102. /*
  103. * Check whether this CPU is the first in a package to come up. In
  104. * this case do not check the boot value against another package
  105. * because the new package might have been physically hotplugged,
  106. * where TSC_ADJUST is expected to be different. When called on the
  107. * boot CPU topology_core_cpumask() might not be available yet.
  108. */
  109. mask = topology_core_cpumask(cpu);
  110. refcpu = mask ? cpumask_any_but(mask, cpu) : nr_cpu_ids;
  111. if (refcpu >= nr_cpu_ids) {
  112. tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(),
  113. bootcpu);
  114. return false;
  115. }
  116. ref = per_cpu_ptr(&tsc_adjust, refcpu);
  117. /*
  118. * Compare the boot value and complain if it differs in the
  119. * package.
  120. */
  121. if (bootval != ref->bootval) {
  122. pr_warn(FW_BUG "TSC ADJUST differs: Reference CPU%u: %lld CPU%u: %lld\n",
  123. refcpu, ref->bootval, cpu, bootval);
  124. }
  125. /*
  126. * The TSC_ADJUST values in a package must be the same. If the boot
  127. * value on this newly upcoming CPU differs from the adjustment
  128. * value of the already online CPU in this package, set it to that
  129. * adjusted value.
  130. */
  131. if (bootval != ref->adjusted) {
  132. pr_warn("TSC ADJUST synchronize: Reference CPU%u: %lld CPU%u: %lld\n",
  133. refcpu, ref->adjusted, cpu, bootval);
  134. cur->adjusted = ref->adjusted;
  135. wrmsrl(MSR_IA32_TSC_ADJUST, ref->adjusted);
  136. }
  137. /*
  138. * We have the TSCs forced to be in sync on this package. Skip sync
  139. * test:
  140. */
  141. return true;
  142. }
  143. /*
  144. * Entry/exit counters that make sure that both CPUs
  145. * run the measurement code at once:
  146. */
  147. static atomic_t start_count;
  148. static atomic_t stop_count;
  149. static atomic_t skip_test;
  150. static atomic_t test_runs;
  151. /*
  152. * We use a raw spinlock in this exceptional case, because
  153. * we want to have the fastest, inlined, non-debug version
  154. * of a critical section, to be able to prove TSC time-warps:
  155. */
  156. static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  157. static cycles_t last_tsc;
  158. static cycles_t max_warp;
  159. static int nr_warps;
  160. static int random_warps;
  161. /*
  162. * TSC-warp measurement loop running on both CPUs. This is not called
  163. * if there is no TSC.
  164. */
  165. static cycles_t check_tsc_warp(unsigned int timeout)
  166. {
  167. cycles_t start, now, prev, end, cur_max_warp = 0;
  168. int i, cur_warps = 0;
  169. start = rdtsc_ordered();
  170. /*
  171. * The measurement runs for 'timeout' msecs:
  172. */
  173. end = start + (cycles_t) tsc_khz * timeout;
  174. now = start;
  175. for (i = 0; ; i++) {
  176. /*
  177. * We take the global lock, measure TSC, save the
  178. * previous TSC that was measured (possibly on
  179. * another CPU) and update the previous TSC timestamp.
  180. */
  181. arch_spin_lock(&sync_lock);
  182. prev = last_tsc;
  183. now = rdtsc_ordered();
  184. last_tsc = now;
  185. arch_spin_unlock(&sync_lock);
  186. /*
  187. * Be nice every now and then (and also check whether
  188. * measurement is done [we also insert a 10 million
  189. * loops safety exit, so we dont lock up in case the
  190. * TSC readout is totally broken]):
  191. */
  192. if (unlikely(!(i & 7))) {
  193. if (now > end || i > 10000000)
  194. break;
  195. cpu_relax();
  196. touch_nmi_watchdog();
  197. }
  198. /*
  199. * Outside the critical section we can now see whether
  200. * we saw a time-warp of the TSC going backwards:
  201. */
  202. if (unlikely(prev > now)) {
  203. arch_spin_lock(&sync_lock);
  204. max_warp = max(max_warp, prev - now);
  205. cur_max_warp = max_warp;
  206. /*
  207. * Check whether this bounces back and forth. Only
  208. * one CPU should observe time going backwards.
  209. */
  210. if (cur_warps != nr_warps)
  211. random_warps++;
  212. nr_warps++;
  213. cur_warps = nr_warps;
  214. arch_spin_unlock(&sync_lock);
  215. }
  216. }
  217. WARN(!(now-start),
  218. "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
  219. now-start, end-start);
  220. return cur_max_warp;
  221. }
  222. /*
  223. * If the target CPU coming online doesn't have any of its core-siblings
  224. * online, a timeout of 20msec will be used for the TSC-warp measurement
  225. * loop. Otherwise a smaller timeout of 2msec will be used, as we have some
  226. * information about this socket already (and this information grows as we
  227. * have more and more logical-siblings in that socket).
  228. *
  229. * Ideally we should be able to skip the TSC sync check on the other
  230. * core-siblings, if the first logical CPU in a socket passed the sync test.
  231. * But as the TSC is per-logical CPU and can potentially be modified wrongly
  232. * by the bios, TSC sync test for smaller duration should be able
  233. * to catch such errors. Also this will catch the condition where all the
  234. * cores in the socket doesn't get reset at the same time.
  235. */
  236. static inline unsigned int loop_timeout(int cpu)
  237. {
  238. return (cpumask_weight(topology_core_cpumask(cpu)) > 1) ? 2 : 20;
  239. }
  240. /*
  241. * Source CPU calls into this - it waits for the freshly booted
  242. * target CPU to arrive and then starts the measurement:
  243. */
  244. void check_tsc_sync_source(int cpu)
  245. {
  246. int cpus = 2;
  247. /*
  248. * No need to check if we already know that the TSC is not
  249. * synchronized or if we have no TSC.
  250. */
  251. if (unsynchronized_tsc())
  252. return;
  253. /*
  254. * Set the maximum number of test runs to
  255. * 1 if the CPU does not provide the TSC_ADJUST MSR
  256. * 3 if the MSR is available, so the target can try to adjust
  257. */
  258. if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
  259. atomic_set(&test_runs, 1);
  260. else
  261. atomic_set(&test_runs, 3);
  262. retry:
  263. /*
  264. * Wait for the target to start or to skip the test:
  265. */
  266. while (atomic_read(&start_count) != cpus - 1) {
  267. if (atomic_read(&skip_test) > 0) {
  268. atomic_set(&skip_test, 0);
  269. return;
  270. }
  271. cpu_relax();
  272. }
  273. /*
  274. * Trigger the target to continue into the measurement too:
  275. */
  276. atomic_inc(&start_count);
  277. check_tsc_warp(loop_timeout(cpu));
  278. while (atomic_read(&stop_count) != cpus-1)
  279. cpu_relax();
  280. /*
  281. * If the test was successful set the number of runs to zero and
  282. * stop. If not, decrement the number of runs an check if we can
  283. * retry. In case of random warps no retry is attempted.
  284. */
  285. if (!nr_warps) {
  286. atomic_set(&test_runs, 0);
  287. pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
  288. smp_processor_id(), cpu);
  289. } else if (atomic_dec_and_test(&test_runs) || random_warps) {
  290. /* Force it to 0 if random warps brought us here */
  291. atomic_set(&test_runs, 0);
  292. pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n",
  293. smp_processor_id(), cpu);
  294. pr_warning("Measured %Ld cycles TSC warp between CPUs, "
  295. "turning off TSC clock.\n", max_warp);
  296. if (random_warps)
  297. pr_warning("TSC warped randomly between CPUs\n");
  298. mark_tsc_unstable("check_tsc_sync_source failed");
  299. }
  300. /*
  301. * Reset it - just in case we boot another CPU later:
  302. */
  303. atomic_set(&start_count, 0);
  304. random_warps = 0;
  305. nr_warps = 0;
  306. max_warp = 0;
  307. last_tsc = 0;
  308. /*
  309. * Let the target continue with the bootup:
  310. */
  311. atomic_inc(&stop_count);
  312. /*
  313. * Retry, if there is a chance to do so.
  314. */
  315. if (atomic_read(&test_runs) > 0)
  316. goto retry;
  317. }
  318. /*
  319. * Freshly booted CPUs call into this:
  320. */
  321. void check_tsc_sync_target(void)
  322. {
  323. struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
  324. unsigned int cpu = smp_processor_id();
  325. cycles_t cur_max_warp, gbl_max_warp;
  326. int cpus = 2;
  327. /* Also aborts if there is no TSC. */
  328. if (unsynchronized_tsc())
  329. return;
  330. /*
  331. * Store, verify and sanitize the TSC adjust register. If
  332. * successful skip the test.
  333. *
  334. * The test is also skipped when the TSC is marked reliable. This
  335. * is true for SoCs which have no fallback clocksource. On these
  336. * SoCs the TSC is frequency synchronized, but still the TSC ADJUST
  337. * register might have been wreckaged by the BIOS..
  338. */
  339. if (tsc_store_and_check_tsc_adjust(false) || tsc_clocksource_reliable) {
  340. atomic_inc(&skip_test);
  341. return;
  342. }
  343. retry:
  344. /*
  345. * Register this CPU's participation and wait for the
  346. * source CPU to start the measurement:
  347. */
  348. atomic_inc(&start_count);
  349. while (atomic_read(&start_count) != cpus)
  350. cpu_relax();
  351. cur_max_warp = check_tsc_warp(loop_timeout(cpu));
  352. /*
  353. * Store the maximum observed warp value for a potential retry:
  354. */
  355. gbl_max_warp = max_warp;
  356. /*
  357. * Ok, we are done:
  358. */
  359. atomic_inc(&stop_count);
  360. /*
  361. * Wait for the source CPU to print stuff:
  362. */
  363. while (atomic_read(&stop_count) != cpus)
  364. cpu_relax();
  365. /*
  366. * Reset it for the next sync test:
  367. */
  368. atomic_set(&stop_count, 0);
  369. /*
  370. * Check the number of remaining test runs. If not zero, the test
  371. * failed and a retry with adjusted TSC is possible. If zero the
  372. * test was either successful or failed terminally.
  373. */
  374. if (!atomic_read(&test_runs))
  375. return;
  376. /*
  377. * If the warp value of this CPU is 0, then the other CPU
  378. * observed time going backwards so this TSC was ahead and
  379. * needs to move backwards.
  380. */
  381. if (!cur_max_warp)
  382. cur_max_warp = -gbl_max_warp;
  383. /*
  384. * Add the result to the previous adjustment value.
  385. *
  386. * The adjustement value is slightly off by the overhead of the
  387. * sync mechanism (observed values are ~200 TSC cycles), but this
  388. * really depends on CPU, node distance and frequency. So
  389. * compensating for this is hard to get right. Experiments show
  390. * that the warp is not longer detectable when the observed warp
  391. * value is used. In the worst case the adjustment needs to go
  392. * through a 3rd run for fine tuning.
  393. */
  394. cur->adjusted += cur_max_warp;
  395. pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n",
  396. cpu, cur_max_warp, cur->adjusted);
  397. wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted);
  398. goto retry;
  399. }
  400. #endif /* CONFIG_SMP */