timer.h 7.5 KB

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