hrtimer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. struct hrtimer_clock_base;
  24. struct hrtimer_cpu_base;
  25. /*
  26. * Mode arguments of xxx_hrtimer functions:
  27. */
  28. enum hrtimer_mode {
  29. HRTIMER_MODE_ABS, /* Time value is absolute */
  30. HRTIMER_MODE_REL, /* Time value is relative to now */
  31. };
  32. /*
  33. * Return values for the callback function
  34. */
  35. enum hrtimer_restart {
  36. HRTIMER_NORESTART, /* Timer is not restarted */
  37. HRTIMER_RESTART, /* Timer must be restarted */
  38. };
  39. /*
  40. * hrtimer callback modes:
  41. *
  42. * HRTIMER_CB_SOFTIRQ: Callback must run in softirq context
  43. * HRTIMER_CB_IRQSAFE: Callback may run in hardirq context
  44. * HRTIMER_CB_IRQSAFE_NO_RESTART: Callback may run in hardirq context and
  45. * does not restart the timer
  46. * HRTIMER_CB_IRQSAFE_PERCPU: Callback must run in hardirq context
  47. * Special mode for tick emulation and
  48. * scheduler timer. Such timers are per
  49. * cpu and not allowed to be migrated on
  50. * cpu unplug.
  51. * HRTIMER_CB_IRQSAFE_UNLOCKED: Callback should run in hardirq context
  52. * with timer->base lock unlocked
  53. * used for timers which call wakeup to
  54. * avoid lock order problems with rq->lock
  55. */
  56. enum hrtimer_cb_mode {
  57. HRTIMER_CB_SOFTIRQ,
  58. HRTIMER_CB_IRQSAFE,
  59. HRTIMER_CB_IRQSAFE_NO_RESTART,
  60. HRTIMER_CB_IRQSAFE_PERCPU,
  61. HRTIMER_CB_IRQSAFE_UNLOCKED,
  62. };
  63. /*
  64. * Values to track state of the timer
  65. *
  66. * Possible states:
  67. *
  68. * 0x00 inactive
  69. * 0x01 enqueued into rbtree
  70. * 0x02 callback function running
  71. * 0x04 callback pending (high resolution mode)
  72. *
  73. * Special cases:
  74. * 0x03 callback function running and enqueued
  75. * (was requeued on another CPU)
  76. * 0x09 timer was migrated on CPU hotunplug
  77. * The "callback function running and enqueued" status is only possible on
  78. * SMP. It happens for example when a posix timer expired and the callback
  79. * queued a signal. Between dropping the lock which protects the posix timer
  80. * and reacquiring the base lock of the hrtimer, another CPU can deliver the
  81. * signal and rearm the timer. We have to preserve the callback running state,
  82. * as otherwise the timer could be removed before the softirq code finishes the
  83. * the handling of the timer.
  84. *
  85. * The HRTIMER_STATE_ENQUEUED bit is always or'ed to the current state to
  86. * preserve the HRTIMER_STATE_CALLBACK bit in the above scenario.
  87. *
  88. * All state transitions are protected by cpu_base->lock.
  89. */
  90. #define HRTIMER_STATE_INACTIVE 0x00
  91. #define HRTIMER_STATE_ENQUEUED 0x01
  92. #define HRTIMER_STATE_CALLBACK 0x02
  93. #define HRTIMER_STATE_PENDING 0x04
  94. #define HRTIMER_STATE_MIGRATE 0x08
  95. /**
  96. * struct hrtimer - the basic hrtimer structure
  97. * @node: red black tree node for time ordered insertion
  98. * @_expires: the absolute expiry time in the hrtimers internal
  99. * representation. The time is related to the clock on
  100. * which the timer is based. Is setup by adding
  101. * slack to the _softexpires value. For non range timers
  102. * identical to _softexpires.
  103. * @_softexpires: the absolute earliest expiry time of the hrtimer.
  104. * The time which was given as expiry time when the timer
  105. * was armed.
  106. * @function: timer expiry callback function
  107. * @base: pointer to the timer base (per cpu and per clock)
  108. * @state: state information (See bit values above)
  109. * @cb_mode: high resolution timer feature to select the callback execution
  110. * mode
  111. * @cb_entry: list head to enqueue an expired timer into the callback list
  112. * @start_site: timer statistics field to store the site where the timer
  113. * was started
  114. * @start_comm: timer statistics field to store the name of the process which
  115. * started the timer
  116. * @start_pid: timer statistics field to store the pid of the task which
  117. * started the timer
  118. *
  119. * The hrtimer structure must be initialized by hrtimer_init()
  120. */
  121. struct hrtimer {
  122. struct rb_node node;
  123. ktime_t _expires;
  124. ktime_t _softexpires;
  125. enum hrtimer_restart (*function)(struct hrtimer *);
  126. struct hrtimer_clock_base *base;
  127. unsigned long state;
  128. enum hrtimer_cb_mode cb_mode;
  129. struct list_head cb_entry;
  130. #ifdef CONFIG_TIMER_STATS
  131. void *start_site;
  132. char start_comm[16];
  133. int start_pid;
  134. #endif
  135. };
  136. /**
  137. * struct hrtimer_sleeper - simple sleeper structure
  138. * @timer: embedded timer structure
  139. * @task: task to wake up
  140. *
  141. * task is set to NULL, when the timer expires.
  142. */
  143. struct hrtimer_sleeper {
  144. struct hrtimer timer;
  145. struct task_struct *task;
  146. };
  147. /**
  148. * struct hrtimer_clock_base - the timer base for a specific clock
  149. * @cpu_base: per cpu clock base
  150. * @index: clock type index for per_cpu support when moving a
  151. * timer to a base on another cpu.
  152. * @active: red black tree root node for the active timers
  153. * @first: pointer to the timer node which expires first
  154. * @resolution: the resolution of the clock, in nanoseconds
  155. * @get_time: function to retrieve the current time of the clock
  156. * @get_softirq_time: function to retrieve the current time from the softirq
  157. * @softirq_time: the time when running the hrtimer queue in the softirq
  158. * @offset: offset of this clock to the monotonic base
  159. * @reprogram: function to reprogram the timer event
  160. */
  161. struct hrtimer_clock_base {
  162. struct hrtimer_cpu_base *cpu_base;
  163. clockid_t index;
  164. struct rb_root active;
  165. struct rb_node *first;
  166. ktime_t resolution;
  167. ktime_t (*get_time)(void);
  168. ktime_t (*get_softirq_time)(void);
  169. ktime_t softirq_time;
  170. #ifdef CONFIG_HIGH_RES_TIMERS
  171. ktime_t offset;
  172. int (*reprogram)(struct hrtimer *t,
  173. struct hrtimer_clock_base *b,
  174. ktime_t n);
  175. #endif
  176. };
  177. #define HRTIMER_MAX_CLOCK_BASES 2
  178. /*
  179. * struct hrtimer_cpu_base - the per cpu clock bases
  180. * @lock: lock protecting the base and associated clock bases
  181. * and timers
  182. * @clock_base: array of clock bases for this cpu
  183. * @curr_timer: the timer which is executing a callback right now
  184. * @expires_next: absolute time of the next event which was scheduled
  185. * via clock_set_next_event()
  186. * @hres_active: State of high resolution mode
  187. * @check_clocks: Indictator, when set evaluate time source and clock
  188. * event devices whether high resolution mode can be
  189. * activated.
  190. * @cb_pending: Expired timers are moved from the rbtree to this
  191. * list in the timer interrupt. The list is processed
  192. * in the softirq.
  193. * @nr_events: Total number of timer interrupt events
  194. */
  195. struct hrtimer_cpu_base {
  196. spinlock_t lock;
  197. struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
  198. struct list_head cb_pending;
  199. #ifdef CONFIG_HIGH_RES_TIMERS
  200. ktime_t expires_next;
  201. int hres_active;
  202. unsigned long nr_events;
  203. #endif
  204. };
  205. static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
  206. {
  207. timer->_expires = time;
  208. timer->_softexpires = time;
  209. }
  210. static inline void hrtimer_set_expires_range(struct hrtimer *timer, ktime_t time, ktime_t delta)
  211. {
  212. timer->_softexpires = time;
  213. timer->_expires = ktime_add_safe(time, delta);
  214. }
  215. static inline void hrtimer_set_expires_range_ns(struct hrtimer *timer, ktime_t time, unsigned long delta)
  216. {
  217. timer->_softexpires = time;
  218. timer->_expires = ktime_add_safe(time, ns_to_ktime(delta));
  219. }
  220. static inline void hrtimer_set_expires_tv64(struct hrtimer *timer, s64 tv64)
  221. {
  222. timer->_expires.tv64 = tv64;
  223. timer->_softexpires.tv64 = tv64;
  224. }
  225. static inline void hrtimer_add_expires(struct hrtimer *timer, ktime_t time)
  226. {
  227. timer->_expires = ktime_add_safe(timer->_expires, time);
  228. timer->_softexpires = ktime_add_safe(timer->_softexpires, time);
  229. }
  230. static inline void hrtimer_add_expires_ns(struct hrtimer *timer, unsigned long ns)
  231. {
  232. timer->_expires = ktime_add_ns(timer->_expires, ns);
  233. timer->_softexpires = ktime_add_ns(timer->_softexpires, ns);
  234. }
  235. static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
  236. {
  237. return timer->_expires;
  238. }
  239. static inline ktime_t hrtimer_get_softexpires(const struct hrtimer *timer)
  240. {
  241. return timer->_softexpires;
  242. }
  243. static inline s64 hrtimer_get_expires_tv64(const struct hrtimer *timer)
  244. {
  245. return timer->_expires.tv64;
  246. }
  247. static inline s64 hrtimer_get_softexpires_tv64(const struct hrtimer *timer)
  248. {
  249. return timer->_softexpires.tv64;
  250. }
  251. static inline s64 hrtimer_get_expires_ns(const struct hrtimer *timer)
  252. {
  253. return ktime_to_ns(timer->_expires);
  254. }
  255. static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
  256. {
  257. return ktime_sub(timer->_expires, timer->base->get_time());
  258. }
  259. #ifdef CONFIG_HIGH_RES_TIMERS
  260. struct clock_event_device;
  261. extern void clock_was_set(void);
  262. extern void hres_timers_resume(void);
  263. extern void hrtimer_interrupt(struct clock_event_device *dev);
  264. /*
  265. * In high resolution mode the time reference must be read accurate
  266. */
  267. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  268. {
  269. return timer->base->get_time();
  270. }
  271. static inline int hrtimer_is_hres_active(struct hrtimer *timer)
  272. {
  273. return timer->base->cpu_base->hres_active;
  274. }
  275. extern void hrtimer_peek_ahead_timers(void);
  276. /*
  277. * The resolution of the clocks. The resolution value is returned in
  278. * the clock_getres() system call to give application programmers an
  279. * idea of the (in)accuracy of timers. Timer values are rounded up to
  280. * this resolution values.
  281. */
  282. # define HIGH_RES_NSEC 1
  283. # define KTIME_HIGH_RES (ktime_t) { .tv64 = HIGH_RES_NSEC }
  284. # define MONOTONIC_RES_NSEC HIGH_RES_NSEC
  285. # define KTIME_MONOTONIC_RES KTIME_HIGH_RES
  286. #else
  287. # define MONOTONIC_RES_NSEC LOW_RES_NSEC
  288. # define KTIME_MONOTONIC_RES KTIME_LOW_RES
  289. /*
  290. * clock_was_set() is a NOP for non- high-resolution systems. The
  291. * time-sorted order guarantees that a timer does not expire early and
  292. * is expired in the next softirq when the clock was advanced.
  293. */
  294. static inline void clock_was_set(void) { }
  295. static inline void hrtimer_peek_ahead_timers(void) { }
  296. static inline void hres_timers_resume(void) { }
  297. /*
  298. * In non high resolution mode the time reference is taken from
  299. * the base softirq time variable.
  300. */
  301. static inline ktime_t hrtimer_cb_get_time(struct hrtimer *timer)
  302. {
  303. return timer->base->softirq_time;
  304. }
  305. static inline int hrtimer_is_hres_active(struct hrtimer *timer)
  306. {
  307. return 0;
  308. }
  309. #endif
  310. extern ktime_t ktime_get(void);
  311. extern ktime_t ktime_get_real(void);
  312. DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
  313. /* Exported timer functions: */
  314. /* Initialize timers: */
  315. extern void hrtimer_init(struct hrtimer *timer, clockid_t which_clock,
  316. enum hrtimer_mode mode);
  317. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  318. extern void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t which_clock,
  319. enum hrtimer_mode mode);
  320. extern void destroy_hrtimer_on_stack(struct hrtimer *timer);
  321. #else
  322. static inline void hrtimer_init_on_stack(struct hrtimer *timer,
  323. clockid_t which_clock,
  324. enum hrtimer_mode mode)
  325. {
  326. hrtimer_init(timer, which_clock, mode);
  327. }
  328. static inline void destroy_hrtimer_on_stack(struct hrtimer *timer) { }
  329. #endif
  330. /* Basic timer operations: */
  331. extern int hrtimer_start(struct hrtimer *timer, ktime_t tim,
  332. const enum hrtimer_mode mode);
  333. extern int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
  334. unsigned long range_ns, const enum hrtimer_mode mode);
  335. extern int hrtimer_cancel(struct hrtimer *timer);
  336. extern int hrtimer_try_to_cancel(struct hrtimer *timer);
  337. static inline int hrtimer_start_expires(struct hrtimer *timer,
  338. enum hrtimer_mode mode)
  339. {
  340. unsigned long delta;
  341. ktime_t soft, hard;
  342. soft = hrtimer_get_softexpires(timer);
  343. hard = hrtimer_get_expires(timer);
  344. delta = ktime_to_ns(ktime_sub(hard, soft));
  345. return hrtimer_start_range_ns(timer, soft, delta, mode);
  346. }
  347. static inline int hrtimer_restart(struct hrtimer *timer)
  348. {
  349. return hrtimer_start_expires(timer, HRTIMER_MODE_ABS);
  350. }
  351. /* Query timers: */
  352. extern ktime_t hrtimer_get_remaining(const struct hrtimer *timer);
  353. extern int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp);
  354. extern ktime_t hrtimer_get_next_event(void);
  355. /*
  356. * A timer is active, when it is enqueued into the rbtree or the callback
  357. * function is running.
  358. */
  359. static inline int hrtimer_active(const struct hrtimer *timer)
  360. {
  361. return timer->state != HRTIMER_STATE_INACTIVE;
  362. }
  363. /*
  364. * Helper function to check, whether the timer is on one of the queues
  365. */
  366. static inline int hrtimer_is_queued(struct hrtimer *timer)
  367. {
  368. return timer->state &
  369. (HRTIMER_STATE_ENQUEUED | HRTIMER_STATE_PENDING);
  370. }
  371. /*
  372. * Helper function to check, whether the timer is running the callback
  373. * function
  374. */
  375. static inline int hrtimer_callback_running(struct hrtimer *timer)
  376. {
  377. return timer->state & HRTIMER_STATE_CALLBACK;
  378. }
  379. /* Forward a hrtimer so it expires after now: */
  380. extern u64
  381. hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval);
  382. /* Forward a hrtimer so it expires after the hrtimer's current now */
  383. static inline u64 hrtimer_forward_now(struct hrtimer *timer,
  384. ktime_t interval)
  385. {
  386. return hrtimer_forward(timer, timer->base->get_time(), interval);
  387. }
  388. /* Precise sleep: */
  389. extern long hrtimer_nanosleep(struct timespec *rqtp,
  390. struct timespec __user *rmtp,
  391. const enum hrtimer_mode mode,
  392. const clockid_t clockid);
  393. extern long hrtimer_nanosleep_restart(struct restart_block *restart_block);
  394. extern void hrtimer_init_sleeper(struct hrtimer_sleeper *sl,
  395. struct task_struct *tsk);
  396. extern int schedule_hrtimeout_range(ktime_t *expires, unsigned long delta,
  397. const enum hrtimer_mode mode);
  398. extern int schedule_hrtimeout(ktime_t *expires, const enum hrtimer_mode mode);
  399. /* Soft interrupt function to run the hrtimer queues: */
  400. extern void hrtimer_run_queues(void);
  401. extern void hrtimer_run_pending(void);
  402. /* Bootup initialization: */
  403. extern void __init hrtimers_init(void);
  404. #if BITS_PER_LONG < 64
  405. extern u64 ktime_divns(const ktime_t kt, s64 div);
  406. #else /* BITS_PER_LONG < 64 */
  407. # define ktime_divns(kt, div) (u64)((kt).tv64 / (div))
  408. #endif
  409. /* Show pending timers: */
  410. extern void sysrq_timer_list_show(void);
  411. /*
  412. * Timer-statistics info:
  413. */
  414. #ifdef CONFIG_TIMER_STATS
  415. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  416. void *timerf, char *comm,
  417. unsigned int timer_flag);
  418. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  419. {
  420. timer_stats_update_stats(timer, timer->start_pid, timer->start_site,
  421. timer->function, timer->start_comm, 0);
  422. }
  423. extern void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer,
  424. void *addr);
  425. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  426. {
  427. __timer_stats_hrtimer_set_start_info(timer, __builtin_return_address(0));
  428. }
  429. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  430. {
  431. timer->start_site = NULL;
  432. }
  433. #else
  434. static inline void timer_stats_account_hrtimer(struct hrtimer *timer)
  435. {
  436. }
  437. static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
  438. {
  439. }
  440. static inline void timer_stats_hrtimer_clear_start_info(struct hrtimer *timer)
  441. {
  442. }
  443. #endif
  444. #endif