timer.h 7.3 KB

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