sched_clock.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic sched_clock() support, to extend low level hardware time
  4. * counters to full 64-bit ns values.
  5. */
  6. #include <linux/clocksource.h>
  7. #include <linux/init.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/ktime.h>
  10. #include <linux/kernel.h>
  11. #include <linux/math.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/clock.h>
  15. #include <linux/syscore_ops.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/sched_clock.h>
  18. #include <linux/seqlock.h>
  19. #include <linux/bitops.h>
  20. #include "timekeeping.h"
  21. /**
  22. * struct clock_data - all data needed for sched_clock() (including
  23. * registration of a new clock source)
  24. *
  25. * @seq: Sequence counter for protecting updates. The lowest
  26. * bit is the index for @read_data.
  27. * @read_data: Data required to read from sched_clock.
  28. * @wrap_kt: Duration for which clock can run before wrapping.
  29. * @rate: Tick rate of the registered clock.
  30. * @actual_read_sched_clock: Registered hardware level clock read function.
  31. *
  32. * The ordering of this structure has been chosen to optimize cache
  33. * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
  34. * into a single 64-byte cache line.
  35. */
  36. struct clock_data {
  37. seqcount_latch_t seq;
  38. struct clock_read_data read_data[2];
  39. ktime_t wrap_kt;
  40. unsigned long rate;
  41. u64 (*actual_read_sched_clock)(void);
  42. };
  43. static struct hrtimer sched_clock_timer;
  44. static int irqtime = -1;
  45. core_param(irqtime, irqtime, int, 0400);
  46. static u64 notrace jiffy_sched_clock_read(void)
  47. {
  48. /*
  49. * We don't need to use get_jiffies_64 on 32-bit arches here
  50. * because we register with BITS_PER_LONG
  51. */
  52. return (u64)(jiffies - INITIAL_JIFFIES);
  53. }
  54. static struct clock_data cd ____cacheline_aligned = {
  55. .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
  56. .read_sched_clock = jiffy_sched_clock_read, },
  57. .actual_read_sched_clock = jiffy_sched_clock_read,
  58. };
  59. static __always_inline u64 cyc_to_ns(u64 cyc, u32 mult, u32 shift)
  60. {
  61. return (cyc * mult) >> shift;
  62. }
  63. notrace struct clock_read_data *sched_clock_read_begin(unsigned int *seq)
  64. {
  65. *seq = raw_read_seqcount_latch(&cd.seq);
  66. return cd.read_data + (*seq & 1);
  67. }
  68. notrace int sched_clock_read_retry(unsigned int seq)
  69. {
  70. return raw_read_seqcount_latch_retry(&cd.seq, seq);
  71. }
  72. unsigned long long noinstr sched_clock_noinstr(void)
  73. {
  74. struct clock_read_data *rd;
  75. unsigned int seq;
  76. u64 cyc, res;
  77. do {
  78. seq = raw_read_seqcount_latch(&cd.seq);
  79. rd = cd.read_data + (seq & 1);
  80. cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
  81. rd->sched_clock_mask;
  82. res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
  83. } while (raw_read_seqcount_latch_retry(&cd.seq, seq));
  84. return res;
  85. }
  86. unsigned long long notrace sched_clock(void)
  87. {
  88. unsigned long long ns;
  89. preempt_disable_notrace();
  90. ns = sched_clock_noinstr();
  91. preempt_enable_notrace();
  92. return ns;
  93. }
  94. /*
  95. * Updating the data required to read the clock.
  96. *
  97. * sched_clock() will never observe mis-matched data even if called from
  98. * an NMI. We do this by maintaining an odd/even copy of the data and
  99. * steering sched_clock() to one or the other using a sequence counter.
  100. * In order to preserve the data cache profile of sched_clock() as much
  101. * as possible the system reverts back to the even copy when the update
  102. * completes; the odd copy is used *only* during an update.
  103. */
  104. static void update_clock_read_data(struct clock_read_data *rd)
  105. {
  106. /* update the backup (odd) copy with the new data */
  107. cd.read_data[1] = *rd;
  108. /* steer readers towards the odd copy */
  109. raw_write_seqcount_latch(&cd.seq);
  110. /* now its safe for us to update the normal (even) copy */
  111. cd.read_data[0] = *rd;
  112. /* switch readers back to the even copy */
  113. raw_write_seqcount_latch(&cd.seq);
  114. }
  115. /*
  116. * Atomically update the sched_clock() epoch.
  117. */
  118. static void update_sched_clock(void)
  119. {
  120. u64 cyc;
  121. u64 ns;
  122. struct clock_read_data rd;
  123. rd = cd.read_data[0];
  124. cyc = cd.actual_read_sched_clock();
  125. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  126. rd.epoch_ns = ns;
  127. rd.epoch_cyc = cyc;
  128. update_clock_read_data(&rd);
  129. }
  130. static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
  131. {
  132. update_sched_clock();
  133. hrtimer_forward_now(hrt, cd.wrap_kt);
  134. return HRTIMER_RESTART;
  135. }
  136. void __init
  137. sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
  138. {
  139. u64 res, wrap, new_mask, new_epoch, cyc, ns;
  140. u32 new_mult, new_shift;
  141. unsigned long r, flags;
  142. char r_unit;
  143. struct clock_read_data rd;
  144. if (cd.rate > rate)
  145. return;
  146. /* Cannot register a sched_clock with interrupts on */
  147. local_irq_save(flags);
  148. /* Calculate the mult/shift to convert counter ticks to ns. */
  149. clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
  150. new_mask = CLOCKSOURCE_MASK(bits);
  151. cd.rate = rate;
  152. /* Calculate how many nanosecs until we risk wrapping */
  153. wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
  154. cd.wrap_kt = ns_to_ktime(wrap);
  155. rd = cd.read_data[0];
  156. /* Update epoch for new counter and update 'epoch_ns' from old counter*/
  157. new_epoch = read();
  158. cyc = cd.actual_read_sched_clock();
  159. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  160. cd.actual_read_sched_clock = read;
  161. rd.read_sched_clock = read;
  162. rd.sched_clock_mask = new_mask;
  163. rd.mult = new_mult;
  164. rd.shift = new_shift;
  165. rd.epoch_cyc = new_epoch;
  166. rd.epoch_ns = ns;
  167. update_clock_read_data(&rd);
  168. if (sched_clock_timer.function != NULL) {
  169. /* update timeout for clock wrap */
  170. hrtimer_start(&sched_clock_timer, cd.wrap_kt,
  171. HRTIMER_MODE_REL_HARD);
  172. }
  173. r = rate;
  174. if (r >= 4000000) {
  175. r = DIV_ROUND_CLOSEST(r, 1000000);
  176. r_unit = 'M';
  177. } else if (r >= 4000) {
  178. r = DIV_ROUND_CLOSEST(r, 1000);
  179. r_unit = 'k';
  180. } else {
  181. r_unit = ' ';
  182. }
  183. /* Calculate the ns resolution of this counter */
  184. res = cyc_to_ns(1ULL, new_mult, new_shift);
  185. pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
  186. bits, r, r_unit, res, wrap);
  187. /* Enable IRQ time accounting if we have a fast enough sched_clock() */
  188. if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
  189. enable_sched_clock_irqtime();
  190. local_irq_restore(flags);
  191. pr_debug("Registered %pS as sched_clock source\n", read);
  192. }
  193. void __init generic_sched_clock_init(void)
  194. {
  195. /*
  196. * If no sched_clock() function has been provided at that point,
  197. * make it the final one.
  198. */
  199. if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
  200. sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
  201. update_sched_clock();
  202. /*
  203. * Start the timer to keep sched_clock() properly updated and
  204. * sets the initial epoch.
  205. */
  206. hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  207. sched_clock_timer.function = sched_clock_poll;
  208. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
  209. }
  210. /*
  211. * Clock read function for use when the clock is suspended.
  212. *
  213. * This function makes it appear to sched_clock() as if the clock
  214. * stopped counting at its last update.
  215. *
  216. * This function must only be called from the critical
  217. * section in sched_clock(). It relies on the read_seqcount_retry()
  218. * at the end of the critical section to be sure we observe the
  219. * correct copy of 'epoch_cyc'.
  220. */
  221. static u64 notrace suspended_sched_clock_read(void)
  222. {
  223. unsigned int seq = raw_read_seqcount_latch(&cd.seq);
  224. return cd.read_data[seq & 1].epoch_cyc;
  225. }
  226. int sched_clock_suspend(void)
  227. {
  228. struct clock_read_data *rd = &cd.read_data[0];
  229. update_sched_clock();
  230. hrtimer_cancel(&sched_clock_timer);
  231. rd->read_sched_clock = suspended_sched_clock_read;
  232. return 0;
  233. }
  234. void sched_clock_resume(void)
  235. {
  236. struct clock_read_data *rd = &cd.read_data[0];
  237. rd->epoch_cyc = cd.actual_read_sched_clock();
  238. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
  239. rd->read_sched_clock = cd.actual_read_sched_clock;
  240. }
  241. static struct syscore_ops sched_clock_ops = {
  242. .suspend = sched_clock_suspend,
  243. .resume = sched_clock_resume,
  244. };
  245. static int __init sched_clock_syscore_init(void)
  246. {
  247. register_syscore_ops(&sched_clock_ops);
  248. return 0;
  249. }
  250. device_initcall(sched_clock_syscore_init);