hrtimer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * include/linux/hrtimer.h
  3. *
  4. * hrtimers - High-resolution kernel timers
  5. *
  6. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar
  8. *
  9. * data type definitions, declarations, prototypes
  10. *
  11. * Started by: Thomas Gleixner and Ingo Molnar
  12. *
  13. * For licencing details see kernel-base/COPYING
  14. */
  15. #ifndef _LINUX_HRTIMER_H
  16. #define _LINUX_HRTIMER_H
  17. #include <linux/rbtree.h>
  18. #include <linux/ktime.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/percpu.h>
  22. #include <linux/timer.h>
  23. #include <linux/timerqueue.h>
  24. struct hrtimer_clock_base;
  25. struct hrtimer_cpu_base;
  26. /*
  27. * Mode arguments of xxx_hrtimer functions:
  28. *
  29. * HRTIMER_MODE_ABS - Time value is absolute
  30. * HRTIMER_MODE_REL - Time value is relative to now
  31. * HRTIMER_MODE_PINNED - Timer is bound to CPU (is only considered
  32. * when starting the timer)
  33. */
  34. enum hrtimer_mode {
  35. HRTIMER_MODE_ABS = 0x00,
  36. HRTIMER_MODE_REL = 0x01,
  37. HRTIMER_MODE_PINNED = 0x02,
  38. HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
  39. HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,
  40. };
  41. /*
  42. * Return values for the callback function
  43. */
  44. enum hrtimer_restart {
  45. HRTIMER_NORESTART, /* Timer is not restarted */
  46. HRTIMER_RESTART, /* Timer must be restarted */
  47. };
  48. /*
  49. * Values to track state of the timer
  50. *
  51. * Possible states:
  52. *
  53. * 0x00 inactive
  54. * 0x01 enqueued into rbtree
  55. *
  56. * The callback state is not part of the timer->state because clearing it would
  57. * mean touching the timer after the callback, this makes it impossible to free
  58. * the timer from the callback function.
  59. *
  60. * Therefore we track the callback state in:
  61. *
  62. * timer->base->cpu_base->running == timer
  63. *
  64. * On SMP it is possible to have a "callback function running and enqueued"
  65. * status. It happens for example when a posix timer expired and the callback
  66. * queued a signal. Between dropping the lock which protects the posix timer
  67. * and reacquiring the base lock of the hrtimer, another CPU can deliver the
  68. * signal and rearm the timer.
  69. *
  70. * All state transitions are protected by cpu_base->lock.
  71. */
  72. #define HRTIMER_STATE_INACTIVE 0x00
  73. #define HRTIMER_STATE_ENQUEUED 0x01
  74. /**
  75. * struct hrtimer - the basic hrtimer structure
  76. * @node: timerqueue node, which also manages node.expires,
  77. * the absolute expiry time in the hrtimers internal
  78. * representation. The time is related to the clock on
  79. * which the timer is based. Is setup by adding
  80. * slack to the _softexpires value. For non range timers
  81. * identical to _softexpires.
  82. * @_softexpires: the absolute earliest expiry time of the hrtimer.
  83. * The time which was given as expiry time when the timer
  84. * was armed.
  85. * @function: timer expiry callback function
  86. * @base: pointer to the timer base (per cpu and per clock)
  87. * @state: state information (See bit values above)
  88. * @is_rel: Set if the timer was armed relative
  89. *
  90. * The hrtimer structure must be initialized by hrtimer_init()
  91. */
  92. struct hrtimer {
  93. struct timerqueue_node node;
  94. ktime_t _softexpires;
  95. enum hrtimer_restart (*function)(struct hrtimer *);
  96. struct hrtimer_clock_base *base;
  97. u8 state;
  98. u8 is_rel;
  99. };
  100. /**
  101. * struct hrtimer_sleeper - simple sleeper structure
  102. * @timer: embedded timer structure
  103. * @task: task to wake up
  104. *
  105. * task is set to NULL, when the timer expires.
  106. */
  107. struct hrtimer_sleeper {
  108. struct hrtimer timer;
  109. struct task_struct *task;
  110. };
  111. #ifdef CONFIG_64BIT
  112. # define HRTIMER_CLOCK_BASE_ALIGN 64
  113. #else
  114. # define HRTIMER_CLOCK_BASE_ALIGN 32
  115. #endif
  116. /**
  117. * struct hrtimer_clock_base - the timer base for a specific clock
  118. * @cpu_base: per cpu clock base
  119. * @index: clock type index for per_cpu support when moving a
  120. * timer to a base on another cpu.
  121. * @clockid: clock id for per_cpu support
  122. * @active: red black tree root node for the active timers
  123. * @get_time: function to retrieve the current time of the clock
  124. * @offset: offset of this clock to the monotonic base
  125. */
  126. struct hrtimer_clock_base {
  127. struct hrtimer_cpu_base *cpu_base;
  128. int index;
  129. clockid_t clockid;
  130. struct timerqueue_head active;
  131. ktime_t (*get_time)(void);
  132. ktime_t offset;
  133. } __attribute__((__aligned__(HRTIMER_CLOCK_BASE_ALIGN)));
  134. enum hrtimer_base_type {
  135. HRTIMER_BASE_MONOTONIC,
  136. HRTIMER_BASE_REALTIME,
  137. HRTIMER_BASE_BOOTTIME,
  138. HRTIMER_BASE_TAI,
  139. HRTIMER_MAX_CLOCK_BASES,
  140. };
  141. /**
  142. * struct hrtimer_cpu_base - the per cpu clock bases
  143. * @lock: lock protecting the base and associated clock bases
  144. * and timers
  145. * @seq: seqcount around __run_hrtimer
  146. * @running: pointer to the currently running hrtimer
  147. * @cpu: cpu number
  148. * @active_bases: Bitfield to mark bases with active timers
  149. * @clock_was_set_seq: Sequence counter of clock was set events
  150. * @in_hrtirq: hrtimer_interrupt() is currently executing
  151. * @hres_active: State of high resolution mode
  152. * @hang_detected: The last hrtimer interrupt detected a hang
  153. * @expires_next: absolute time of the next event, is required for remote
  154. * hrtimer enqueue
  155. * @next_timer: Pointer to the first expiring timer
  156. * @nr_events: Total number of hrtimer interrupt events
  157. * @nr_retries: Total number of hrtimer interrupt retries
  158. * @nr_hangs: Total number of hrtimer interrupt hangs
  159. * @max_hang_time: Maximum time spent in hrtimer_interrupt
  160. * @clock_base: array of clock bases for this cpu
  161. *
  162. * Note: next_timer is just an optimization for __remove_hrtimer().
  163. * Do not dereference the pointer because it is not reliable on
  164. * cross cpu removals.
  165. */
  166. struct hrtimer_cpu_base {
  167. raw_spinlock_t lock;
  168. seqcount_t seq;
  169. struct hrtimer *running;
  170. unsigned int cpu;
  171. unsigned int active_bases;
  172. unsigned int clock_was_set_seq;
  173. #ifdef CONFIG_HIGH_RES_TIMERS
  174. unsigned int in_hrtirq : 1,
  175. hres_active : 1,
  176. hang_detected : 1;
  177. ktime_t expires_next;
  178. struct hrtimer *next_timer;
  179. unsigned int nr_events;
  180. unsigned int nr_retries;
  181. unsigned int nr_hangs;
  182. unsigned int max_hang_time;
  183. #endif
  184. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  185. } ____cacheline_aligned;
  186. static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
  187. {
  188. BUILD_BUG_ON(sizeof(struct hrtimer_clock_base) > HRTIMER_CLOCK_BASE_ALIGN);
  189. timer->node.expires = time;
  190. timer->_softexpires = time;
  191. }
  192. static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
  193. {
  194. timer->_softexpires = time;
  195. timer->node.expires = ktime_add_safe(time, delta);
  196. }
  197. static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, u64 delta)
  198. {
  199. timer->_softexpires = time;
  200. timer->node.expires = ktime_add_safe(time, ns_to_ktime(delta));
  201. }
  202. static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
  203. {
  204. timer->node.expires = tv64;
  205. timer->_softexpires = tv64;
  206. }
  207. static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
  208. {
  209. timer->node.expires = ktime_add_safe(timer->node.expires, time);
  210. timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
  211. }
  212. static inline void hrtimer_add_expires_ns(struct hrtimer *timer, u64 ns)
  213. {
  214. timer->node.expires = ktime_add_ns(timer->node.expires, ns);
  215. timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
  216. }
  217. static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
  218. {
  219. return timer->node.expires;
  220. }
  221. static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
  222. {
  223. return timer->_softexpires;
  224. }
  225. static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
  226. {
  227. return timer->node.expires;
  228. }
  229. static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
  230. {
  231. return timer->_softexpires;
  232. }
  233. static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
  234. {
  235. return ktime_to_ns(timer->node.expires);
  236. }
  237. static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
  238. {
  239. return ktime_sub(timer->node.expires, timer->base->get_time());
  240. }
  241. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  242. {
  243. return timer->base->get_time();
  244. }
  245. #ifdef CONFIG_HIGH_RES_TIMERS
  246. struct clock_event_device;
  247. extern void hrtimer_interrupt(struct clock_event_device *dev);
  248. static inline int hrtimer_is_hres_active(struct hrtimer *timer)
  249. {
  250. return timer->base->cpu_base->hres_active;
  251. }
  252. /*
  253. * The resolution of the clocks. The resolution value is returned in
  254. * the clock_getres() system call to give application programmers an
  255. * idea of the (in)accuracy of timers. Timer values are rounded up to
  256. * this resolution values.
  257. */
  258. # define HIGH_RES_NSEC 1
  259. # define KTIME_HIGH_RES (HIGH_RES_NSEC)
  260. # define MONOTONIC_RES_NSEC HIGH_RES_NSEC
  261. # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
  262. extern void clock_was_set_delayed(void);
  263. extern unsigned int hrtimer_resolution;
  264. #else
  265. # define MONOTONIC_RES_NSEC LOW_RES_NSEC
  266. # define KTIME_MONOTONIC_RES KTIME_LOW_RES
  267. #define hrtimer_resolution (unsigned int)LOW_RES_NSEC
  268. static inline int hrtimer_is_hres_active(struct hrtimer *timer)
  269. {
  270. return 0;
  271. }
  272. static inline void clock_was_set_delayed(void) { }
  273. #endif
  274. static inline ktime_t
  275. __hrtimer_expires_remaining_adjusted(const struct hrtimer *timer, ktime_t now)
  276. {
  277. ktime_t rem = ktime_sub(timer->node.expires, now);
  278. /*
  279. * Adjust relative timers for the extra we added in
  280. * hrtimer_start_range_ns() to prevent short timeouts.
  281. */
  282. if (IS_ENABLED(CONFIG_TIME_LOW_RES) && timer->is_rel)
  283. rem -= hrtimer_resolution;
  284. return rem;
  285. }
  286. static inline ktime_t
  287. hrtimer_expires_remaining_adjusted(const struct hrtimer *timer)
  288. {
  289. return __hrtimer_expires_remaining_adjusted(timer,
  290. timer->base->get_time());
  291. }
  292. extern void clock_was_set(void);
  293. #ifdef CONFIG_TIMERFD
  294. extern void timerfd_clock_was_set(void);
  295. #else
  296. static inline void timerfd_clock_was_set(void) { }
  297. #endif
  298. extern void hrtimers_resume(void);
  299. DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
  300. /* Exported timer functions: */
  301. /* Initialize timers: */
  302. extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
  303. enum hrtimer_mode mode);
  304. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  305. extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
  306. enum hrtimer_mode mode);
  307. extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
  308. #else
  309. static inline void hrtimer_init_on_stack(struct hrtimer *timer,
  310. clockid_t which_clock,
  311. enum hrtimer_mode mode)
  312. {
  313. hrtimer_init(timer, which_clock, mode);
  314. }
  315. static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
  316. #endif
  317. /* Basic timer operations: */
  318. extern void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  319. u64 range_ns, const enum hrtimer_mode mode);
  320. /**
  321. * hrtimer_start - (re)start an hrtimer
  322. * @timer: the timer to be added
  323. * @tim: expiry time
  324. * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or
  325. * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED)
  326. */
  327. static inline void hrtimer_start(struct hrtimer *timer, ktime_t tim,
  328. const enum hrtimer_mode mode)
  329. {
  330. hrtimer_start_range_ns(timer, tim, 0, mode);
  331. }
  332. extern int hrtimer_cancel(struct hrtimer *timer);
  333. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  334. static inline void hrtimer_start_expires(struct hrtimer *timer,
  335. enum hrtimer_mode mode)
  336. {
  337. u64 delta;
  338. ktime_t soft, hard;
  339. soft = hrtimer_get_softexpires(timer);
  340. hard = hrtimer_get_expires(timer);
  341. delta = ktime_to_ns(ktime_sub(hard, soft));
  342. hrtimer_start_range_ns(timer, soft, delta, mode);
  343. }
  344. static inline void hrtimer_restart(struct hrtimer *timer)
  345. {
  346. hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  347. }
  348. /* Query timers: */
  349. extern ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust);
  350. static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
  351. {
  352. return __hrtimer_get_remaining(timer, false);
  353. }
  354. extern u64 hrtimer_get_next_event(void);
  355. extern bool hrtimer_active(const struct hrtimer *timer);
  356. /*
  357. * Helper function to check, whether the timer is on one of the queues
  358. */
  359. static inline int hrtimer_is_queued(struct hrtimer *timer)
  360. {
  361. return timer->state & HRTIMER_STATE_ENQUEUED;
  362. }
  363. /*
  364. * Helper function to check, whether the timer is running the callback
  365. * function
  366. */
  367. static inline int hrtimer_callback_running(struct hrtimer *timer)
  368. {
  369. return timer->base->cpu_base->running == timer;
  370. }
  371. /* Forward a hrtimer so it expires after now: */
  372. extern u64
  373. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
  374. /**
  375. * hrtimer_forward_now - forward the timer expiry so it expires after now
  376. * @timer: hrtimer to forward
  377. * @interval: the interval to forward
  378. *
  379. * Forward the timer expiry so it will expire after the current time
  380. * of the hrtimer clock base. Returns the number of overruns.
  381. *
  382. * Can be safely called from the callback function of @timer. If
  383. * called from other contexts @timer must neither be enqueued nor
  384. * running the callback and the caller needs to take care of
  385. * serialization.
  386. *
  387. * Note: This only updates the timer expiry value and does not requeue
  388. * the timer.
  389. */
  390. static inline u64 hrtimer_forward_now(struct hrtimer *timer,
  391. ktime_t interval)
  392. {
  393. return hrtimer_forward(timer, timer->base->get_time(), interval);
  394. }
  395. /* Precise sleep: */
  396. extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
  397. extern long hrtimer_nanosleep(const struct timespec64 *rqtp,
  398. const enum hrtimer_mode mode,
  399. const clockid_t clockid);
  400. extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  401. struct task_struct *tsk);
  402. extern int schedule_hrtimeout_range(ktime_t *expires, u64 delta,
  403. const enum hrtimer_mode mode);
  404. extern int schedule_hrtimeout_range_clock(ktime_t *expires,
  405. u64 delta,
  406. const enum hrtimer_mode mode,
  407. clockid_t clock_id);
  408. extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
  409. /* Soft interrupt function to run the hrtimer queues: */
  410. extern void hrtimer_run_queues(void);
  411. /* Bootup initialization: */
  412. extern void __init hrtimers_init(void);
  413. /* Show pending timers: */
  414. extern void sysrq_timer_list_show(void);
  415. int hrtimers_prepare_cpu(unsigned int cpu);
  416. #ifdef CONFIG_HOTPLUG_CPU
  417. int hrtimers_dead_cpu(unsigned int cpu);
  418. #else
  419. #define hrtimers_dead_cpu NULL
  420. #endif
  421. #endif