timer.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 hlist_node entry;
  15. unsigned long expires;
  16. void (*function)(unsigned long);
  17. unsigned long data;
  18. u32 flags;
  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. #ifdef CONFIG_LOCKDEP
  30. /*
  31. * NB: because we have to copy the lockdep_map, setting the lockdep_map key
  32. * (second argument) here is required, otherwise it could be initialised to
  33. * the copy of the lockdep_map later! We use the pointer to and the string
  34. * "<file>:<line>" as the key resp. the name of the lockdep_map.
  35. */
  36. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
  37. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
  38. #else
  39. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
  40. #endif
  41. /*
  42. * A deferrable timer will work normally when the system is busy, but
  43. * will not cause a CPU to come out of idle just to service it; instead,
  44. * the timer will be serviced when the CPU eventually wakes up with a
  45. * subsequent non-deferrable timer.
  46. *
  47. * An irqsafe timer is executed with IRQ disabled and it's safe to wait for
  48. * the completion of the running instance from IRQ handlers, for example,
  49. * by calling del_timer_sync().
  50. *
  51. * Note: The irq disabled callback execution is a special case for
  52. * workqueue locking issues. It's not meant for executing random crap
  53. * with interrupts disabled. Abuse is monitored!
  54. */
  55. #define TIMER_CPUMASK 0x0003FFFF
  56. #define TIMER_MIGRATING 0x00040000
  57. #define TIMER_BASEMASK (TIMER_CPUMASK | TIMER_MIGRATING)
  58. #define TIMER_DEFERRABLE 0x00080000
  59. #define TIMER_PINNED 0x00100000
  60. #define TIMER_IRQSAFE 0x00200000
  61. #define TIMER_ARRAYSHIFT 22
  62. #define TIMER_ARRAYMASK 0xFFC00000
  63. #define __TIMER_INITIALIZER(_function, _expires, _data, _flags) { \
  64. .entry = { .next = TIMER_ENTRY_STATIC }, \
  65. .function = (_function), \
  66. .expires = (_expires), \
  67. .data = (_data), \
  68. .flags = (_flags), \
  69. .slack = -1, \
  70. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  71. __FILE__ ":" __stringify(__LINE__)) \
  72. }
  73. #define TIMER_INITIALIZER(_function, _expires, _data) \
  74. __TIMER_INITIALIZER((_function), (_expires), (_data), 0)
  75. #define TIMER_PINNED_INITIALIZER(_function, _expires, _data) \
  76. __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_PINNED)
  77. #define TIMER_DEFERRED_INITIALIZER(_function, _expires, _data) \
  78. __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_DEFERRABLE)
  79. #define TIMER_PINNED_DEFERRED_INITIALIZER(_function, _expires, _data) \
  80. __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_DEFERRABLE | TIMER_PINNED)
  81. #define DEFINE_TIMER(_name, _function, _expires, _data) \
  82. struct timer_list _name = \
  83. TIMER_INITIALIZER(_function, _expires, _data)
  84. void init_timer_key(struct timer_list *timer, unsigned int flags,
  85. const char *name, struct lock_class_key *key);
  86. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  87. extern void init_timer_on_stack_key(struct timer_list *timer,
  88. unsigned int flags, const char *name,
  89. struct lock_class_key *key);
  90. extern void destroy_timer_on_stack(struct timer_list *timer);
  91. #else
  92. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  93. static inline void init_timer_on_stack_key(struct timer_list *timer,
  94. unsigned int flags, const char *name,
  95. struct lock_class_key *key)
  96. {
  97. init_timer_key(timer, flags, name, key);
  98. }
  99. #endif
  100. #ifdef CONFIG_LOCKDEP
  101. #define __init_timer(_timer, _flags) \
  102. do { \
  103. static struct lock_class_key __key; \
  104. init_timer_key((_timer), (_flags), #_timer, &__key); \
  105. } while (0)
  106. #define __init_timer_on_stack(_timer, _flags) \
  107. do { \
  108. static struct lock_class_key __key; \
  109. init_timer_on_stack_key((_timer), (_flags), #_timer, &__key); \
  110. } while (0)
  111. #else
  112. #define __init_timer(_timer, _flags) \
  113. init_timer_key((_timer), (_flags), NULL, NULL)
  114. #define __init_timer_on_stack(_timer, _flags) \
  115. init_timer_on_stack_key((_timer), (_flags), NULL, NULL)
  116. #endif
  117. #define init_timer(timer) \
  118. __init_timer((timer), 0)
  119. #define init_timer_pinned(timer) \
  120. __init_timer((timer), TIMER_PINNED)
  121. #define init_timer_deferrable(timer) \
  122. __init_timer((timer), TIMER_DEFERRABLE)
  123. #define init_timer_pinned_deferrable(timer) \
  124. __init_timer((timer), TIMER_DEFERRABLE | TIMER_PINNED)
  125. #define init_timer_on_stack(timer) \
  126. __init_timer_on_stack((timer), 0)
  127. #define __setup_timer(_timer, _fn, _data, _flags) \
  128. do { \
  129. __init_timer((_timer), (_flags)); \
  130. (_timer)->function = (_fn); \
  131. (_timer)->data = (_data); \
  132. } while (0)
  133. #define __setup_timer_on_stack(_timer, _fn, _data, _flags) \
  134. do { \
  135. __init_timer_on_stack((_timer), (_flags)); \
  136. (_timer)->function = (_fn); \
  137. (_timer)->data = (_data); \
  138. } while (0)
  139. #define setup_timer(timer, fn, data) \
  140. __setup_timer((timer), (fn), (data), 0)
  141. #define setup_pinned_timer(timer, fn, data) \
  142. __setup_timer((timer), (fn), (data), TIMER_PINNED)
  143. #define setup_deferrable_timer(timer, fn, data) \
  144. __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE)
  145. #define setup_pinned_deferrable_timer(timer, fn, data) \
  146. __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE | TIMER_PINNED)
  147. #define setup_timer_on_stack(timer, fn, data) \
  148. __setup_timer_on_stack((timer), (fn), (data), 0)
  149. #define setup_pinned_timer_on_stack(timer, fn, data) \
  150. __setup_timer_on_stack((timer), (fn), (data), TIMER_PINNED)
  151. #define setup_deferrable_timer_on_stack(timer, fn, data) \
  152. __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE)
  153. #define setup_pinned_deferrable_timer_on_stack(timer, fn, data) \
  154. __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE | TIMER_PINNED)
  155. /**
  156. * timer_pending - is a timer pending?
  157. * @timer: the timer in question
  158. *
  159. * timer_pending will tell whether a given timer is currently pending,
  160. * or not. Callers must ensure serialization wrt. other operations done
  161. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  162. *
  163. * return value: 1 if the timer is pending, 0 if not.
  164. */
  165. static inline int timer_pending(const struct timer_list * timer)
  166. {
  167. return timer->entry.pprev != NULL;
  168. }
  169. extern void add_timer_on(struct timer_list *timer, int cpu);
  170. extern int del_timer(struct timer_list * timer);
  171. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  172. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  173. extern void set_timer_slack(struct timer_list *time, int slack_hz);
  174. /*
  175. * The jiffies value which is added to now, when there is no timer
  176. * in the timer wheel:
  177. */
  178. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  179. /*
  180. * Timer-statistics info:
  181. */
  182. #ifdef CONFIG_TIMER_STATS
  183. extern int timer_stats_active;
  184. extern void init_timer_stats(void);
  185. extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
  186. void *timerf, char *comm, u32 flags);
  187. extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
  188. void *addr);
  189. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  190. {
  191. if (likely(!timer_stats_active))
  192. return;
  193. __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
  194. }
  195. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  196. {
  197. timer->start_site = NULL;
  198. }
  199. #else
  200. static inline void init_timer_stats(void)
  201. {
  202. }
  203. static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
  204. {
  205. }
  206. static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
  207. {
  208. }
  209. #endif
  210. extern void add_timer(struct timer_list *timer);
  211. extern int try_to_del_timer_sync(struct timer_list *timer);
  212. #ifdef CONFIG_SMP
  213. extern int del_timer_sync(struct timer_list *timer);
  214. #else
  215. # define del_timer_sync(t) del_timer(t)
  216. #endif
  217. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  218. extern void init_timers(void);
  219. extern void run_local_timers(void);
  220. struct hrtimer;
  221. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  222. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  223. #include <linux/sysctl.h>
  224. extern unsigned int sysctl_timer_migration;
  225. int timer_migration_handler(struct ctl_table *table, int write,
  226. void __user *buffer, size_t *lenp,
  227. loff_t *ppos);
  228. #endif
  229. unsigned long __round_jiffies(unsigned long j, int cpu);
  230. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  231. unsigned long round_jiffies(unsigned long j);
  232. unsigned long round_jiffies_relative(unsigned long j);
  233. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  234. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  235. unsigned long round_jiffies_up(unsigned long j);
  236. unsigned long round_jiffies_up_relative(unsigned long j);
  237. #endif