hrtimer.h 14 KB

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