sched_clock.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/moduleparam.h>
  12. #include <linux/sched.h>
  13. #include <linux/sched/clock.h>
  14. #include <linux/syscore_ops.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/sched_clock.h>
  17. #include <linux/seqlock.h>
  18. #include <linux/bitops.h>
  19. #include <trace/hooks/epoch.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 inline u64 notrace 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 read_seqcount_latch_retry(&cd.seq, seq);
  71. }
  72. unsigned long long notrace sched_clock(void)
  73. {
  74. u64 cyc, res;
  75. unsigned int seq;
  76. struct clock_read_data *rd;
  77. do {
  78. rd = sched_clock_read_begin(&seq);
  79. cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
  80. rd->sched_clock_mask;
  81. res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
  82. } while (sched_clock_read_retry(seq));
  83. return res;
  84. }
  85. /*
  86. * Updating the data required to read the clock.
  87. *
  88. * sched_clock() will never observe mis-matched data even if called from
  89. * an NMI. We do this by maintaining an odd/even copy of the data and
  90. * steering sched_clock() to one or the other using a sequence counter.
  91. * In order to preserve the data cache profile of sched_clock() as much
  92. * as possible the system reverts back to the even copy when the update
  93. * completes; the odd copy is used *only* during an update.
  94. */
  95. static void update_clock_read_data(struct clock_read_data *rd)
  96. {
  97. /* update the backup (odd) copy with the new data */
  98. cd.read_data[1] = *rd;
  99. /* steer readers towards the odd copy */
  100. raw_write_seqcount_latch(&cd.seq);
  101. /* now its safe for us to update the normal (even) copy */
  102. cd.read_data[0] = *rd;
  103. /* switch readers back to the even copy */
  104. raw_write_seqcount_latch(&cd.seq);
  105. }
  106. /*
  107. * Atomically update the sched_clock() epoch.
  108. */
  109. static void update_sched_clock(void)
  110. {
  111. u64 cyc;
  112. u64 ns;
  113. struct clock_read_data rd;
  114. rd = cd.read_data[0];
  115. cyc = cd.actual_read_sched_clock();
  116. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  117. rd.epoch_ns = ns;
  118. rd.epoch_cyc = cyc;
  119. update_clock_read_data(&rd);
  120. }
  121. static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
  122. {
  123. update_sched_clock();
  124. hrtimer_forward_now(hrt, cd.wrap_kt);
  125. return HRTIMER_RESTART;
  126. }
  127. void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
  128. {
  129. u64 res, wrap, new_mask, new_epoch, cyc, ns;
  130. u32 new_mult, new_shift;
  131. unsigned long r, flags;
  132. char r_unit;
  133. struct clock_read_data rd;
  134. if (cd.rate > rate)
  135. return;
  136. /* Cannot register a sched_clock with interrupts on */
  137. local_irq_save(flags);
  138. /* Calculate the mult/shift to convert counter ticks to ns. */
  139. clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
  140. new_mask = CLOCKSOURCE_MASK(bits);
  141. cd.rate = rate;
  142. /* Calculate how many nanosecs until we risk wrapping */
  143. wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
  144. cd.wrap_kt = ns_to_ktime(wrap);
  145. rd = cd.read_data[0];
  146. /* Update epoch for new counter and update 'epoch_ns' from old counter*/
  147. new_epoch = read();
  148. cyc = cd.actual_read_sched_clock();
  149. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  150. cd.actual_read_sched_clock = read;
  151. rd.read_sched_clock = read;
  152. rd.sched_clock_mask = new_mask;
  153. rd.mult = new_mult;
  154. rd.shift = new_shift;
  155. rd.epoch_cyc = new_epoch;
  156. rd.epoch_ns = ns;
  157. update_clock_read_data(&rd);
  158. if (sched_clock_timer.function != NULL) {
  159. /* update timeout for clock wrap */
  160. hrtimer_start(&sched_clock_timer, cd.wrap_kt,
  161. HRTIMER_MODE_REL_HARD);
  162. }
  163. r = rate;
  164. if (r >= 4000000) {
  165. r /= 1000000;
  166. r_unit = 'M';
  167. } else {
  168. if (r >= 1000) {
  169. r /= 1000;
  170. r_unit = 'k';
  171. } else {
  172. r_unit = ' ';
  173. }
  174. }
  175. /* Calculate the ns resolution of this counter */
  176. res = cyc_to_ns(1ULL, new_mult, new_shift);
  177. pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
  178. bits, r, r_unit, res, wrap);
  179. /* Enable IRQ time accounting if we have a fast enough sched_clock() */
  180. if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
  181. enable_sched_clock_irqtime();
  182. local_irq_restore(flags);
  183. pr_debug("Registered %pS as sched_clock source\n", read);
  184. }
  185. EXPORT_SYMBOL_GPL(sched_clock_register);
  186. void __init generic_sched_clock_init(void)
  187. {
  188. /*
  189. * If no sched_clock() function has been provided at that point,
  190. * make it the final one.
  191. */
  192. if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
  193. sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
  194. update_sched_clock();
  195. /*
  196. * Start the timer to keep sched_clock() properly updated and
  197. * sets the initial epoch.
  198. */
  199. hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  200. sched_clock_timer.function = sched_clock_poll;
  201. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
  202. }
  203. /*
  204. * Clock read function for use when the clock is suspended.
  205. *
  206. * This function makes it appear to sched_clock() as if the clock
  207. * stopped counting at its last update.
  208. *
  209. * This function must only be called from the critical
  210. * section in sched_clock(). It relies on the read_seqcount_retry()
  211. * at the end of the critical section to be sure we observe the
  212. * correct copy of 'epoch_cyc'.
  213. */
  214. static u64 notrace suspended_sched_clock_read(void)
  215. {
  216. unsigned int seq = raw_read_seqcount_latch(&cd.seq);
  217. return cd.read_data[seq & 1].epoch_cyc;
  218. }
  219. int sched_clock_suspend(void)
  220. {
  221. struct clock_read_data *rd = &cd.read_data[0];
  222. update_sched_clock();
  223. hrtimer_cancel(&sched_clock_timer);
  224. rd->read_sched_clock = suspended_sched_clock_read;
  225. trace_android_vh_show_suspend_epoch_val(rd->epoch_ns, rd->epoch_cyc);
  226. return 0;
  227. }
  228. void sched_clock_resume(void)
  229. {
  230. struct clock_read_data *rd = &cd.read_data[0];
  231. rd->epoch_cyc = cd.actual_read_sched_clock();
  232. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
  233. rd->read_sched_clock = cd.actual_read_sched_clock;
  234. trace_android_vh_show_resume_epoch_val(rd->epoch_cyc);
  235. }
  236. static struct syscore_ops sched_clock_ops = {
  237. .suspend = sched_clock_suspend,
  238. .resume = sched_clock_resume,
  239. };
  240. static int __init sched_clock_syscore_init(void)
  241. {
  242. register_syscore_ops(&sched_clock_ops);
  243. return 0;
  244. }
  245. device_initcall(sched_clock_syscore_init);