timer.h 8.8 KB

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