hrtimer.h 15 KB

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