timer.h 6.7 KB

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