tsc_sync.c 13 KB

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