hrtimer.h 13 KB

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