timer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef _LINUX_TIMER_H
  2. #define _LINUX_TIMER_H
  3. #include <linux/list.h>
  4. #include <linux/ktime.h>
  5. #include <linux/stddef.h>
  6. #include <linux/debugobjects.h>
  7. #include <linux/stringify.h>
  8. struct tvec_base;
  9. struct timer_list {
  10. /*
  11. * All fields that change during normal runtime grouped to the
  12. * same cacheline
  13. */
  14. struct list_head entry;
  15. unsigned long expires;
  16. struct tvec_base *base;
  17. void (*function)(unsigned long);
  18. unsigned long data;
  19. int slack;
  20. #ifdef CONFIG_TIMER_STATS
  21. int start_pid;
  22. void *start_site;
  23. char start_comm[16];
  24. #endif
  25. #ifdef CONFIG_LOCKDEP
  26. struct lockdep_map lockdep_map;
  27. #endif
  28. };
  29. extern struct tvec_base boot_tvec_bases;
  30. #ifdef CONFIG_LOCKDEP
  31. /*
  32. * NB: because we have to copy the lockdep_map, setting the lockdep_map key
  33. * (second argument) here is required, otherwise it could be initialised to
  34. * the copy of the lockdep_map later! We use the pointer to and the string
  35. * "<file>:<line>" as the key resp. the name of the lockdep_map.
  36. */
  37. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
  38. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
  39. #else
  40. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
  41. #endif
  42. /*
  43. * Note that all tvec_bases are at least 4 byte aligned and lower two bits
  44. * of base in timer_list is guaranteed to be zero. Use them for flags.
  45. *
  46. * A deferrable timer will work normally when the system is busy, but
  47. * will not cause a CPU to come out of idle just to service it; instead,
  48. * the timer will be serviced when the CPU eventually wakes up with a
  49. * subsequent non-deferrable timer.
  50. *
  51. * An irqsafe timer is executed with IRQ disabled and it's safe to wait for
  52. * the completion of the running instance from IRQ handlers, for example,
  53. * by calling del_timer_sync().
  54. *
  55. * Note: The irq disabled callback execution is a special case for
  56. * workqueue locking issues. It's not meant for executing random crap
  57. * with interrupts disabled. Abuse is monitored!
  58. */
  59. #define TIMER_DEFERRABLE 0x1LU
  60. #define TIMER_IRQSAFE 0x2LU
  61. #define TIMER_FLAG_MASK 0x3LU
  62. #define __TIMER_INITIALIZER(_function, _expires, _data, _flags) { \
  63. .entry = { .prev = TIMER_ENTRY_STATIC }, \
  64. .function = (_function), \
  65. .expires = (_expires), \
  66. .data = (_data), \
  67. .base = (void *)((unsigned long)&boot_tvec_bases + (_flags)), \
  68. .slack = -1, \
  69. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  70. __FILE__ ":" __stringify(__LINE__)) \
  71. }
  72. #define TIMER_INITIALIZER(_function, _expires, _data) \
  73. __TIMER_INITIALIZER((_function), (_expires), (_data), 0)
  74. #define TIMER_DEFERRED_INITIALIZER(_function, _expires, _data) \
  75. __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_DEFERRABLE)
  76. #define DEFINE_TIMER(_name, _function, _expires, _data) \
  77. struct timer_list _name = \
  78. TIMER_INITIALIZER(_function, _expires, _data)
  79. void init_timer_key(struct timer_list *timer, unsigned int flags,
  80. const char *name, struct lock_class_key *key);
  81. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  82. extern void init_timer_on_stack_key(struct timer_list *timer,
  83. unsigned int flags, const char *name,
  84. struct lock_class_key *key);
  85. extern void destroy_timer_on_stack(struct timer_list *timer);
  86. #else
  87. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  88. static inline void init_timer_on_stack_key(struct timer_list *timer,
  89. unsigned int flags, const char *name,
  90. struct lock_class_key *key)
  91. {
  92. init_timer_key(timer, flags, name, key);
  93. }
  94. #endif
  95. #ifdef CONFIG_LOCKDEP
  96. #define __init_timer(_timer, _flags) \
  97. do { \
  98. static struct lock_class_key __key; \
  99. init_timer_key((_timer), (_flags), #_timer, &__key); \
  100. } while (0)
  101. #define __init_timer_on_stack(_timer, _flags) \
  102. do { \
  103. static struct lock_class_key __key; \
  104. init_timer_on_stack_key((_timer), (_flags), #_timer, &__key); \
  105. } while (0)
  106. #else
  107. #define __init_timer(_timer, _flags) \
  108. init_timer_key((_timer), (_flags), NULL, NULL)
  109. #define __init_timer_on_stack(_timer, _flags) \
  110. init_timer_on_stack_key((_timer), (_flags), NULL, NULL)
  111. #endif
  112. #define init_timer(timer) \
  113. __init_timer((timer), 0)
  114. #define init_timer_deferrable(timer) \
  115. __init_timer((timer), TIMER_DEFERRABLE)
  116. #define init_timer_on_stack(timer) \
  117. __init_timer_on_stack((timer), 0)
  118. #define __setup_timer(_timer, _fn, _data, _flags) \
  119. do { \
  120. __init_timer((_timer), (_flags)); \
  121. (_timer)->function = (_fn); \
  122. (_timer)->data = (_data); \
  123. } while (0)
  124. #define __setup_timer_on_stack(_timer, _fn, _data, _flags) \
  125. do { \
  126. __init_timer_on_stack((_timer), (_flags)); \
  127. (_timer)->function = (_fn); \
  128. (_timer)->data = (_data); \
  129. } while (0)
  130. #define setup_timer(timer, fn, data) \
  131. __setup_timer((timer), (fn), (data), 0)
  132. #define setup_timer_on_stack(timer, fn, data) \
  133. __setup_timer_on_stack((timer), (fn), (data), 0)
  134. #define setup_deferrable_timer_on_stack(timer, fn, data) \
  135. __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE)
  136. /**
  137. * timer_pending - is a timer pending?
  138. * @timer: the timer in question
  139. *
  140. * timer_pending will tell whether a given timer is currently pending,
  141. * or not. Callers must ensure serialization wrt. other operations done
  142. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  143. *
  144. * return value: 1 if the timer is pending, 0 if not.
  145. */
  146. static inline int timer_pending(const struct timer_list * timer)
  147. {
  148. return timer->entry.next != NULL;
  149. }
  150. extern void add_timer_on(struct timer_list *timer, int cpu);
  151. extern int del_timer(struct timer_list * timer);
  152. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  153. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  154. extern int mod_timer_pinned(struct timer_list *timer, unsigned long expires);
  155. extern void set_timer_slack(struct timer_list *time, int slack_hz);
  156. #define TIMER_NOT_PINNED 0
  157. #define TIMER_PINNED 1
  158. /*
  159. * The jiffies value which is added to now, when there is no timer
  160. * in the timer wheel:
  161. */
  162. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  163. /*
  164. * Return when the next timer-wheel timeout occurs (in absolute jiffies),
  165. * locks the timer base and does the comparison against the given
  166. * jiffie.
  167. */
  168. extern unsigned long get_next_timer_interrupt(unsigned long now);
  169. /*
  170. * Timer-statistics info:
  171. */
  172. #ifdef CONFIG_TIMER_STATS
  173. extern int timer_stats_active;
  174. #define TIMER_STATS_FLAG_DEFERRABLE 0x1
  175. extern void init_timer_stats(void);
  176. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  177. void *timerf, char *comm,
  178. unsigned int timer_flag);
  179. extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
  180. void *addr);
  181. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  182. {
  183. if (likely(!timer_stats_active))
  184. return;
  185. __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
  186. }
  187. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  188. {
  189. timer->start_site = NULL;
  190. }
  191. #else
  192. static inline void init_timer_stats(void)
  193. {
  194. }
  195. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  196. {
  197. }
  198. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  199. {
  200. }
  201. #endif
  202. extern void add_timer(struct timer_list *timer);
  203. extern int try_to_del_timer_sync(struct timer_list *timer);
  204. #ifdef CONFIG_SMP
  205. extern int del_timer_sync(struct timer_list *timer);
  206. #else
  207. # define del_timer_sync(t) del_timer(t)
  208. #endif
  209. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  210. extern void init_timers(void);
  211. extern void run_local_timers(void);
  212. struct hrtimer;
  213. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  214. unsigned long __round_jiffies(unsigned long j, int cpu);
  215. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  216. unsigned long round_jiffies(unsigned long j);
  217. unsigned long round_jiffies_relative(unsigned long j);
  218. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  219. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  220. unsigned long round_jiffies_up(unsigned long j);
  221. unsigned long round_jiffies_up_relative(unsigned long j);
  222. #endif