tsc_sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. if (tsc_clocksource_reliable) {
  259. if (cpu == (nr_cpu_ids-1) || system_state != SYSTEM_BOOTING)
  260. pr_info(
  261. "Skipped synchronization checks as TSC is reliable.\n");
  262. return;
  263. }
  264. /*
  265. * Set the maximum number of test runs to
  266. * 1 if the CPU does not provide the TSC_ADJUST MSR
  267. * 3 if the MSR is available, so the target can try to adjust
  268. */
  269. if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
  270. atomic_set(&test_runs, 1);
  271. else
  272. atomic_set(&test_runs, 3);
  273. retry:
  274. /*
  275. * Wait for the target to start or to skip the test:
  276. */
  277. while (atomic_read(&start_count) != cpus - 1) {
  278. if (atomic_read(&skip_test) > 0) {
  279. atomic_set(&skip_test, 0);
  280. return;
  281. }
  282. cpu_relax();
  283. }
  284. /*
  285. * Trigger the target to continue into the measurement too:
  286. */
  287. atomic_inc(&start_count);
  288. check_tsc_warp(loop_timeout(cpu));
  289. while (atomic_read(&stop_count) != cpus-1)
  290. cpu_relax();
  291. /*
  292. * If the test was successful set the number of runs to zero and
  293. * stop. If not, decrement the number of runs an check if we can
  294. * retry. In case of random warps no retry is attempted.
  295. */
  296. if (!nr_warps) {
  297. atomic_set(&test_runs, 0);
  298. pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
  299. smp_processor_id(), cpu);
  300. } else if (atomic_dec_and_test(&test_runs) || random_warps) {
  301. /* Force it to 0 if random warps brought us here */
  302. atomic_set(&test_runs, 0);
  303. pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n",
  304. smp_processor_id(), cpu);
  305. pr_warning("Measured %Ld cycles TSC warp between CPUs, "
  306. "turning off TSC clock.\n", max_warp);
  307. if (random_warps)
  308. pr_warning("TSC warped randomly between CPUs\n");
  309. mark_tsc_unstable("check_tsc_sync_source failed");
  310. }
  311. /*
  312. * Reset it - just in case we boot another CPU later:
  313. */
  314. atomic_set(&start_count, 0);
  315. random_warps = 0;
  316. nr_warps = 0;
  317. max_warp = 0;
  318. last_tsc = 0;
  319. /*
  320. * Let the target continue with the bootup:
  321. */
  322. atomic_inc(&stop_count);
  323. /*
  324. * Retry, if there is a chance to do so.
  325. */
  326. if (atomic_read(&test_runs) > 0)
  327. goto retry;
  328. }
  329. /*
  330. * Freshly booted CPUs call into this:
  331. */
  332. void check_tsc_sync_target(void)
  333. {
  334. struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
  335. unsigned int cpu = smp_processor_id();
  336. cycles_t cur_max_warp, gbl_max_warp;
  337. int cpus = 2;
  338. /* Also aborts if there is no TSC. */
  339. if (unsynchronized_tsc() || tsc_clocksource_reliable)
  340. return;
  341. /*
  342. * Store, verify and sanitize the TSC adjust register. If
  343. * successful skip the test.
  344. */
  345. if (tsc_store_and_check_tsc_adjust(false)) {
  346. atomic_inc(&skip_test);
  347. return;
  348. }
  349. retry:
  350. /*
  351. * Register this CPU's participation and wait for the
  352. * source CPU to start the measurement:
  353. */
  354. atomic_inc(&start_count);
  355. while (atomic_read(&start_count) != cpus)
  356. cpu_relax();
  357. cur_max_warp = check_tsc_warp(loop_timeout(cpu));
  358. /*
  359. * Store the maximum observed warp value for a potential retry:
  360. */
  361. gbl_max_warp = max_warp;
  362. /*
  363. * Ok, we are done:
  364. */
  365. atomic_inc(&stop_count);
  366. /*
  367. * Wait for the source CPU to print stuff:
  368. */
  369. while (atomic_read(&stop_count) != cpus)
  370. cpu_relax();
  371. /*
  372. * Reset it for the next sync test:
  373. */
  374. atomic_set(&stop_count, 0);
  375. /*
  376. * Check the number of remaining test runs. If not zero, the test
  377. * failed and a retry with adjusted TSC is possible. If zero the
  378. * test was either successful or failed terminally.
  379. */
  380. if (!atomic_read(&test_runs))
  381. return;
  382. /*
  383. * If the warp value of this CPU is 0, then the other CPU
  384. * observed time going backwards so this TSC was ahead and
  385. * needs to move backwards.
  386. */
  387. if (!cur_max_warp)
  388. cur_max_warp = -gbl_max_warp;
  389. /*
  390. * Add the result to the previous adjustment value.
  391. *
  392. * The adjustement value is slightly off by the overhead of the
  393. * sync mechanism (observed values are ~200 TSC cycles), but this
  394. * really depends on CPU, node distance and frequency. So
  395. * compensating for this is hard to get right. Experiments show
  396. * that the warp is not longer detectable when the observed warp
  397. * value is used. In the worst case the adjustment needs to go
  398. * through a 3rd run for fine tuning.
  399. */
  400. cur->adjusted += cur_max_warp;
  401. /*
  402. * TSC deadline timer stops working or creates an interrupt storm
  403. * with adjust values < 0 and > x07ffffff.
  404. *
  405. * To allow adjust values > 0x7FFFFFFF we need to disable the
  406. * deadline timer and use the local APIC timer, but that requires
  407. * more intrusive changes and we do not have any useful information
  408. * from Intel about the underlying HW wreckage yet.
  409. */
  410. if (cur->adjusted < 0)
  411. cur->adjusted = 0;
  412. if (cur->adjusted > 0x7FFFFFFF)
  413. cur->adjusted = 0x7FFFFFFF;
  414. pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n",
  415. cpu, cur_max_warp, cur->adjusted);
  416. wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted);
  417. goto retry;
  418. }
  419. #endif /* CONFIG_SMP */