timer.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TIMER_H
  3. #define _LINUX_TIMER_H
  4. #include <linux/list.h>
  5. #include <linux/ktime.h>
  6. #include <linux/stddef.h>
  7. #include <linux/debugobjects.h>
  8. #include <linux/stringify.h>
  9. struct tvec_base;
  10. struct timer_list {
  11. /*
  12. * All fields that change during normal runtime grouped to the
  13. * same cacheline
  14. */
  15. struct hlist_node entry;
  16. unsigned long expires;
  17. void (*function)(unsigned long);
  18. unsigned long data;
  19. u32 flags;
  20. #ifdef CONFIG_LOCKDEP
  21. struct lockdep_map lockdep_map;
  22. #endif
  23. };
  24. #ifdef CONFIG_LOCKDEP
  25. /*
  26. * NB: because we have to copy the lockdep_map, setting the lockdep_map key
  27. * (second argument) here is required, otherwise it could be initialised to
  28. * the copy of the lockdep_map later! We use the pointer to and the string
  29. * "<file>:<line>" as the key resp. the name of the lockdep_map.
  30. */
  31. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn) \
  32. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
  33. #else
  34. #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
  35. #endif
  36. /*
  37. * A deferrable timer will work normally when the system is busy, but
  38. * will not cause a CPU to come out of idle just to service it; instead,
  39. * the timer will be serviced when the CPU eventually wakes up with a
  40. * subsequent non-deferrable timer.
  41. *
  42. * An irqsafe timer is executed with IRQ disabled and it's safe to wait for
  43. * the completion of the running instance from IRQ handlers, for example,
  44. * by calling del_timer_sync().
  45. *
  46. * Note: The irq disabled callback execution is a special case for
  47. * workqueue locking issues. It's not meant for executing random crap
  48. * with interrupts disabled. Abuse is monitored!
  49. */
  50. #define TIMER_CPUMASK 0x0003FFFF
  51. #define TIMER_MIGRATING 0x00040000
  52. #define TIMER_BASEMASK (TIMER_CPUMASK | TIMER_MIGRATING)
  53. #define TIMER_DEFERRABLE 0x00080000
  54. #define TIMER_PINNED 0x00100000
  55. #define TIMER_IRQSAFE 0x00200000
  56. #define TIMER_ARRAYSHIFT 22
  57. #define TIMER_ARRAYMASK 0xFFC00000
  58. #define TIMER_TRACE_FLAGMASK (TIMER_MIGRATING | TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE)
  59. #define TIMER_DATA_TYPE unsigned long
  60. #define TIMER_FUNC_TYPE void (*)(TIMER_DATA_TYPE)
  61. #define __TIMER_INITIALIZER(_function, _data, _flags) { \
  62. .entry = { .next = TIMER_ENTRY_STATIC }, \
  63. .function = (_function), \
  64. .data = (_data), \
  65. .flags = (_flags), \
  66. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  67. __FILE__ ":" __stringify(__LINE__)) \
  68. }
  69. #define DEFINE_TIMER(_name, _function) \
  70. struct timer_list _name = \
  71. __TIMER_INITIALIZER((TIMER_FUNC_TYPE)_function, 0, 0)
  72. void init_timer_key(struct timer_list *timer, unsigned int flags,
  73. const char *name, struct lock_class_key *key);
  74. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  75. extern void init_timer_on_stack_key(struct timer_list *timer,
  76. unsigned int flags, const char *name,
  77. struct lock_class_key *key);
  78. extern void destroy_timer_on_stack(struct timer_list *timer);
  79. #else
  80. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  81. static inline void init_timer_on_stack_key(struct timer_list *timer,
  82. unsigned int flags, const char *name,
  83. struct lock_class_key *key)
  84. {
  85. init_timer_key(timer, flags, name, key);
  86. }
  87. #endif
  88. #ifdef CONFIG_LOCKDEP
  89. #define __init_timer(_timer, _flags) \
  90. do { \
  91. static struct lock_class_key __key; \
  92. init_timer_key((_timer), (_flags), #_timer, &__key); \
  93. } while (0)
  94. #define __init_timer_on_stack(_timer, _flags) \
  95. do { \
  96. static struct lock_class_key __key; \
  97. init_timer_on_stack_key((_timer), (_flags), #_timer, &__key); \
  98. } while (0)
  99. #else
  100. #define __init_timer(_timer, _flags) \
  101. init_timer_key((_timer), (_flags), NULL, NULL)
  102. #define __init_timer_on_stack(_timer, _flags) \
  103. init_timer_on_stack_key((_timer), (_flags), NULL, NULL)
  104. #endif
  105. #define __setup_timer(_timer, _fn, _data, _flags) \
  106. do { \
  107. __init_timer((_timer), (_flags)); \
  108. (_timer)->function = (_fn); \
  109. (_timer)->data = (_data); \
  110. } while (0)
  111. #define __setup_timer_on_stack(_timer, _fn, _data, _flags) \
  112. do { \
  113. __init_timer_on_stack((_timer), (_flags)); \
  114. (_timer)->function = (_fn); \
  115. (_timer)->data = (_data); \
  116. } while (0)
  117. #define setup_timer(timer, fn, data) \
  118. __setup_timer((timer), (fn), (data), 0)
  119. #define setup_pinned_timer(timer, fn, data) \
  120. __setup_timer((timer), (fn), (data), TIMER_PINNED)
  121. #define setup_deferrable_timer(timer, fn, data) \
  122. __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE)
  123. #define setup_pinned_deferrable_timer(timer, fn, data) \
  124. __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE | TIMER_PINNED)
  125. #define setup_timer_on_stack(timer, fn, data) \
  126. __setup_timer_on_stack((timer), (fn), (data), 0)
  127. #define setup_pinned_timer_on_stack(timer, fn, data) \
  128. __setup_timer_on_stack((timer), (fn), (data), TIMER_PINNED)
  129. #define setup_deferrable_timer_on_stack(timer, fn, data) \
  130. __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE)
  131. #define setup_pinned_deferrable_timer_on_stack(timer, fn, data) \
  132. __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE | TIMER_PINNED)
  133. #ifndef CONFIG_LOCKDEP
  134. static inline void timer_setup(struct timer_list *timer,
  135. void (*callback)(struct timer_list *),
  136. unsigned int flags)
  137. {
  138. __setup_timer(timer, (TIMER_FUNC_TYPE)callback,
  139. (TIMER_DATA_TYPE)timer, flags);
  140. }
  141. static inline void timer_setup_on_stack(struct timer_list *timer,
  142. void (*callback)(struct timer_list *),
  143. unsigned int flags)
  144. {
  145. __setup_timer_on_stack(timer, (TIMER_FUNC_TYPE)callback,
  146. (TIMER_DATA_TYPE)timer, flags);
  147. }
  148. #else
  149. /*
  150. * Under LOCKDEP, the timer lock_class_key (set up in __init_timer) needs
  151. * to be tied to the caller's context, so an inline (above) won't work. We
  152. * do want to keep the inline for argument type checking, though.
  153. */
  154. # define timer_setup(timer, callback, flags) \
  155. __setup_timer((timer), (TIMER_FUNC_TYPE)(callback), \
  156. (TIMER_DATA_TYPE)(timer), (flags))
  157. # define timer_setup_on_stack(timer, callback, flags) \
  158. __setup_timer_on_stack((timer), \
  159. (TIMER_FUNC_TYPE)(callback), \
  160. (TIMER_DATA_TYPE)(timer), (flags))
  161. #endif
  162. #define from_timer(var, callback_timer, timer_fieldname) \
  163. container_of(callback_timer, typeof(*var), timer_fieldname)
  164. /**
  165. * timer_pending - is a timer pending?
  166. * @timer: the timer in question
  167. *
  168. * timer_pending will tell whether a given timer is currently pending,
  169. * or not. Callers must ensure serialization wrt. other operations done
  170. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  171. *
  172. * return value: 1 if the timer is pending, 0 if not.
  173. */
  174. static inline int timer_pending(const struct timer_list * timer)
  175. {
  176. return timer->entry.pprev != NULL;
  177. }
  178. extern void add_timer_on(struct timer_list *timer, int cpu);
  179. extern int del_timer(struct timer_list * timer);
  180. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  181. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  182. extern int timer_reduce(struct timer_list *timer, unsigned long expires);
  183. /*
  184. * The jiffies value which is added to now, when there is no timer
  185. * in the timer wheel:
  186. */
  187. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  188. extern void add_timer(struct timer_list *timer);
  189. extern int try_to_del_timer_sync(struct timer_list *timer);
  190. #ifdef CONFIG_SMP
  191. extern int del_timer_sync(struct timer_list *timer);
  192. #else
  193. # define del_timer_sync(t) del_timer(t)
  194. #endif
  195. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  196. extern void init_timers(void);
  197. extern void run_local_timers(void);
  198. struct hrtimer;
  199. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  200. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  201. struct ctl_table;
  202. extern unsigned int sysctl_timer_migration;
  203. int timer_migration_handler(struct ctl_table *table, int write,
  204. void __user *buffer, size_t *lenp,
  205. loff_t *ppos);
  206. #endif
  207. unsigned long __round_jiffies(unsigned long j, int cpu);
  208. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  209. unsigned long round_jiffies(unsigned long j);
  210. unsigned long round_jiffies_relative(unsigned long j);
  211. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  212. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  213. unsigned long round_jiffies_up(unsigned long j);
  214. unsigned long round_jiffies_up_relative(unsigned long j);
  215. #ifdef CONFIG_HOTPLUG_CPU
  216. int timers_dead_cpu(unsigned int cpu);
  217. #else
  218. #define timers_dead_cpu NULL
  219. #endif
  220. #endif