tick-broadcast-hrtimer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/kernel/time/tick-broadcast-hrtimer.c
  4. * This file emulates a local clock event device
  5. * via a pseudo clock device.
  6. */
  7. #include <linux/cpu.h>
  8. #include <linux/err.h>
  9. #include <linux/hrtimer.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/percpu.h>
  12. #include <linux/profile.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/module.h>
  17. #include "tick-internal.h"
  18. static struct hrtimer bctimer;
  19. static int bc_shutdown(struct clock_event_device *evt)
  20. {
  21. /*
  22. * Note, we cannot cancel the timer here as we might
  23. * run into the following live lock scenario:
  24. *
  25. * cpu 0 cpu1
  26. * lock(broadcast_lock);
  27. * hrtimer_interrupt()
  28. * bc_handler()
  29. * tick_handle_oneshot_broadcast();
  30. * lock(broadcast_lock);
  31. * hrtimer_cancel()
  32. * wait_for_callback()
  33. */
  34. hrtimer_try_to_cancel(&bctimer);
  35. return 0;
  36. }
  37. /*
  38. * This is called from the guts of the broadcast code when the cpu
  39. * which is about to enter idle has the earliest broadcast timer event.
  40. */
  41. static int bc_set_next(ktime_t expires, struct clock_event_device *bc)
  42. {
  43. /*
  44. * This is called either from enter/exit idle code or from the
  45. * broadcast handler. In all cases tick_broadcast_lock is held.
  46. *
  47. * hrtimer_cancel() cannot be called here neither from the
  48. * broadcast handler nor from the enter/exit idle code. The idle
  49. * code can run into the problem described in bc_shutdown() and the
  50. * broadcast handler cannot wait for itself to complete for obvious
  51. * reasons.
  52. *
  53. * Each caller tries to arm the hrtimer on its own CPU, but if the
  54. * hrtimer callbback function is currently running, then
  55. * hrtimer_start() cannot move it and the timer stays on the CPU on
  56. * which it is assigned at the moment.
  57. *
  58. * As this can be called from idle code, the hrtimer_start()
  59. * invocation has to be wrapped with RCU_NONIDLE() as
  60. * hrtimer_start() can call into tracing.
  61. */
  62. RCU_NONIDLE( {
  63. hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED);
  64. /*
  65. * The core tick broadcast mode expects bc->bound_on to be set
  66. * correctly to prevent a CPU which has the broadcast hrtimer
  67. * armed from going deep idle.
  68. *
  69. * As tick_broadcast_lock is held, nothing can change the cpu
  70. * base which was just established in hrtimer_start() above. So
  71. * the below access is safe even without holding the hrtimer
  72. * base lock.
  73. */
  74. bc->bound_on = bctimer.base->cpu_base->cpu;
  75. } );
  76. return 0;
  77. }
  78. static struct clock_event_device ce_broadcast_hrtimer = {
  79. .name = "bc_hrtimer",
  80. .set_state_shutdown = bc_shutdown,
  81. .set_next_ktime = bc_set_next,
  82. .features = CLOCK_EVT_FEAT_ONESHOT |
  83. CLOCK_EVT_FEAT_KTIME |
  84. CLOCK_EVT_FEAT_HRTIMER,
  85. .rating = 0,
  86. .bound_on = -1,
  87. .min_delta_ns = 1,
  88. .max_delta_ns = KTIME_MAX,
  89. .min_delta_ticks = 1,
  90. .max_delta_ticks = ULONG_MAX,
  91. .mult = 1,
  92. .shift = 0,
  93. .cpumask = cpu_possible_mask,
  94. };
  95. static enum hrtimer_restart bc_handler(struct hrtimer *t)
  96. {
  97. ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer);
  98. return HRTIMER_NORESTART;
  99. }
  100. void tick_setup_hrtimer_broadcast(void)
  101. {
  102. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  103. bctimer.function = bc_handler;
  104. clockevents_register_device(&ce_broadcast_hrtimer);
  105. }