timer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_INITIALIZER(_function, _flags) { \
  59. .entry = { .next = TIMER_ENTRY_STATIC }, \
  60. .function = (_function), \
  61. .flags = (_flags), \
  62. __TIMER_LOCKDEP_MAP_INITIALIZER( \
  63. __FILE__ ":" __stringify(__LINE__)) \
  64. }
  65. #define DEFINE_TIMER(_name, _function) \
  66. struct timer_list _name = \
  67. __TIMER_INITIALIZER(_function, 0)
  68. /*
  69. * LOCKDEP and DEBUG timer interfaces.
  70. */
  71. void init_timer_key(struct timer_list *timer,
  72. void (*func)(struct timer_list *), 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. void (*func)(struct timer_list *),
  77. unsigned int flags, const char *name,
  78. struct lock_class_key *key);
  79. #else
  80. static inline void init_timer_on_stack_key(struct timer_list *timer,
  81. void (*func)(struct timer_list *),
  82. unsigned int flags,
  83. const char *name,
  84. struct lock_class_key *key)
  85. {
  86. init_timer_key(timer, func, flags, name, key);
  87. }
  88. #endif
  89. #ifdef CONFIG_LOCKDEP
  90. #define __init_timer(_timer, _fn, _flags) \
  91. do { \
  92. static struct lock_class_key __key; \
  93. init_timer_key((_timer), (_fn), (_flags), #_timer, &__key);\
  94. } while (0)
  95. #define __init_timer_on_stack(_timer, _fn, _flags) \
  96. do { \
  97. static struct lock_class_key __key; \
  98. init_timer_on_stack_key((_timer), (_fn), (_flags), \
  99. #_timer, &__key); \
  100. } while (0)
  101. #else
  102. #define __init_timer(_timer, _fn, _flags) \
  103. init_timer_key((_timer), (_fn), (_flags), NULL, NULL)
  104. #define __init_timer_on_stack(_timer, _fn, _flags) \
  105. init_timer_on_stack_key((_timer), (_fn), (_flags), NULL, NULL)
  106. #endif
  107. /**
  108. * timer_setup - prepare a timer for first use
  109. * @timer: the timer in question
  110. * @callback: the function to call when timer expires
  111. * @flags: any TIMER_* flags
  112. *
  113. * Regular timer initialization should use either DEFINE_TIMER() above,
  114. * or timer_setup(). For timers on the stack, timer_setup_on_stack() must
  115. * be used and must be balanced with a call to destroy_timer_on_stack().
  116. */
  117. #define timer_setup(timer, callback, flags) \
  118. __init_timer((timer), (callback), (flags))
  119. #define timer_setup_on_stack(timer, callback, flags) \
  120. __init_timer_on_stack((timer), (callback), (flags))
  121. #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
  122. extern void destroy_timer_on_stack(struct timer_list *timer);
  123. #else
  124. static inline void destroy_timer_on_stack(struct timer_list *timer) { }
  125. #endif
  126. #define from_timer(var, callback_timer, timer_fieldname) \
  127. container_of(callback_timer, typeof(*var), timer_fieldname)
  128. /**
  129. * timer_pending - is a timer pending?
  130. * @timer: the timer in question
  131. *
  132. * timer_pending will tell whether a given timer is currently pending,
  133. * or not. Callers must ensure serialization wrt. other operations done
  134. * to this timer, eg. interrupt contexts, or other CPUs on SMP.
  135. *
  136. * return value: 1 if the timer is pending, 0 if not.
  137. */
  138. static inline int timer_pending(const struct timer_list * timer)
  139. {
  140. return timer->entry.pprev != NULL;
  141. }
  142. extern void add_timer_on(struct timer_list *timer, int cpu);
  143. extern int del_timer(struct timer_list * timer);
  144. extern int mod_timer(struct timer_list *timer, unsigned long expires);
  145. extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
  146. extern int timer_reduce(struct timer_list *timer, unsigned long expires);
  147. /*
  148. * The jiffies value which is added to now, when there is no timer
  149. * in the timer wheel:
  150. */
  151. #define NEXT_TIMER_MAX_DELTA ((1UL << 30) - 1)
  152. extern void add_timer(struct timer_list *timer);
  153. extern int try_to_del_timer_sync(struct timer_list *timer);
  154. #ifdef CONFIG_SMP
  155. extern int del_timer_sync(struct timer_list *timer);
  156. #else
  157. # define del_timer_sync(t) del_timer(t)
  158. #endif
  159. #define del_singleshot_timer_sync(t) del_timer_sync(t)
  160. extern void init_timers(void);
  161. extern void run_local_timers(void);
  162. struct hrtimer;
  163. extern enum hrtimer_restart it_real_fn(struct hrtimer *);
  164. #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
  165. struct ctl_table;
  166. extern unsigned int sysctl_timer_migration;
  167. int timer_migration_handler(struct ctl_table *table, int write,
  168. void __user *buffer, size_t *lenp,
  169. loff_t *ppos);
  170. #endif
  171. unsigned long __round_jiffies(unsigned long j, int cpu);
  172. unsigned long __round_jiffies_relative(unsigned long j, int cpu);
  173. unsigned long round_jiffies(unsigned long j);
  174. unsigned long round_jiffies_relative(unsigned long j);
  175. unsigned long __round_jiffies_up(unsigned long j, int cpu);
  176. unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
  177. unsigned long round_jiffies_up(unsigned long j);
  178. unsigned long round_jiffies_up_relative(unsigned long j);
  179. #ifdef CONFIG_HOTPLUG_CPU
  180. int timers_dead_cpu(unsigned int cpu);
  181. #else
  182. #define timers_dead_cpu NULL
  183. #endif
  184. #endif