timers: Switch to a non-cascading wheel
[cascardo/linux.git] / include / linux / timer.h
1 #ifndef _LINUX_TIMER_H
2 #define _LINUX_TIMER_H
3
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
10 struct tvec_base;
11
12 struct timer_list {
13         /*
14          * All fields that change during normal runtime grouped to the
15          * same cacheline
16          */
17         struct hlist_node       entry;
18         unsigned long           expires;
19         void                    (*function)(unsigned long);
20         unsigned long           data;
21         u32                     flags;
22         int                     slack;
23
24 #ifdef CONFIG_TIMER_STATS
25         int                     start_pid;
26         void                    *start_site;
27         char                    start_comm[16];
28 #endif
29 #ifdef CONFIG_LOCKDEP
30         struct lockdep_map      lockdep_map;
31 #endif
32 };
33
34 #ifdef CONFIG_LOCKDEP
35 /*
36  * NB: because we have to copy the lockdep_map, setting the lockdep_map key
37  * (second argument) here is required, otherwise it could be initialised to
38  * the copy of the lockdep_map later! We use the pointer to and the string
39  * "<file>:<line>" as the key resp. the name of the lockdep_map.
40  */
41 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)                            \
42         .lockdep_map = STATIC_LOCKDEP_MAP_INIT(_kn, &_kn),
43 #else
44 #define __TIMER_LOCKDEP_MAP_INITIALIZER(_kn)
45 #endif
46
47 /*
48  * A deferrable timer will work normally when the system is busy, but
49  * will not cause a CPU to come out of idle just to service it; instead,
50  * the timer will be serviced when the CPU eventually wakes up with a
51  * subsequent non-deferrable timer.
52  *
53  * An irqsafe timer is executed with IRQ disabled and it's safe to wait for
54  * the completion of the running instance from IRQ handlers, for example,
55  * by calling del_timer_sync().
56  *
57  * Note: The irq disabled callback execution is a special case for
58  * workqueue locking issues. It's not meant for executing random crap
59  * with interrupts disabled. Abuse is monitored!
60  */
61 #define TIMER_CPUMASK           0x0003FFFF
62 #define TIMER_MIGRATING         0x00040000
63 #define TIMER_BASEMASK          (TIMER_CPUMASK | TIMER_MIGRATING)
64 #define TIMER_DEFERRABLE        0x00080000
65 #define TIMER_PINNED            0x00100000
66 #define TIMER_IRQSAFE           0x00200000
67 #define TIMER_ARRAYSHIFT        22
68 #define TIMER_ARRAYMASK         0xFFC00000
69
70 #define __TIMER_INITIALIZER(_function, _expires, _data, _flags) { \
71                 .entry = { .next = TIMER_ENTRY_STATIC },        \
72                 .function = (_function),                        \
73                 .expires = (_expires),                          \
74                 .data = (_data),                                \
75                 .flags = (_flags),                              \
76                 .slack = -1,                                    \
77                 __TIMER_LOCKDEP_MAP_INITIALIZER(                \
78                         __FILE__ ":" __stringify(__LINE__))     \
79         }
80
81 #define TIMER_INITIALIZER(_function, _expires, _data)           \
82         __TIMER_INITIALIZER((_function), (_expires), (_data), 0)
83
84 #define TIMER_PINNED_INITIALIZER(_function, _expires, _data)    \
85         __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_PINNED)
86
87 #define TIMER_DEFERRED_INITIALIZER(_function, _expires, _data)  \
88         __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_DEFERRABLE)
89
90 #define TIMER_PINNED_DEFERRED_INITIALIZER(_function, _expires, _data)   \
91         __TIMER_INITIALIZER((_function), (_expires), (_data), TIMER_DEFERRABLE | TIMER_PINNED)
92
93 #define DEFINE_TIMER(_name, _function, _expires, _data)         \
94         struct timer_list _name =                               \
95                 TIMER_INITIALIZER(_function, _expires, _data)
96
97 void init_timer_key(struct timer_list *timer, unsigned int flags,
98                     const char *name, struct lock_class_key *key);
99
100 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
101 extern void init_timer_on_stack_key(struct timer_list *timer,
102                                     unsigned int flags, const char *name,
103                                     struct lock_class_key *key);
104 extern void destroy_timer_on_stack(struct timer_list *timer);
105 #else
106 static inline void destroy_timer_on_stack(struct timer_list *timer) { }
107 static inline void init_timer_on_stack_key(struct timer_list *timer,
108                                            unsigned int flags, const char *name,
109                                            struct lock_class_key *key)
110 {
111         init_timer_key(timer, flags, name, key);
112 }
113 #endif
114
115 #ifdef CONFIG_LOCKDEP
116 #define __init_timer(_timer, _flags)                                    \
117         do {                                                            \
118                 static struct lock_class_key __key;                     \
119                 init_timer_key((_timer), (_flags), #_timer, &__key);    \
120         } while (0)
121
122 #define __init_timer_on_stack(_timer, _flags)                           \
123         do {                                                            \
124                 static struct lock_class_key __key;                     \
125                 init_timer_on_stack_key((_timer), (_flags), #_timer, &__key); \
126         } while (0)
127 #else
128 #define __init_timer(_timer, _flags)                                    \
129         init_timer_key((_timer), (_flags), NULL, NULL)
130 #define __init_timer_on_stack(_timer, _flags)                           \
131         init_timer_on_stack_key((_timer), (_flags), NULL, NULL)
132 #endif
133
134 #define init_timer(timer)                                               \
135         __init_timer((timer), 0)
136 #define init_timer_pinned(timer)                                        \
137         __init_timer((timer), TIMER_PINNED)
138 #define init_timer_deferrable(timer)                                    \
139         __init_timer((timer), TIMER_DEFERRABLE)
140 #define init_timer_pinned_deferrable(timer)                             \
141         __init_timer((timer), TIMER_DEFERRABLE | TIMER_PINNED)
142 #define init_timer_on_stack(timer)                                      \
143         __init_timer_on_stack((timer), 0)
144
145 #define __setup_timer(_timer, _fn, _data, _flags)                       \
146         do {                                                            \
147                 __init_timer((_timer), (_flags));                       \
148                 (_timer)->function = (_fn);                             \
149                 (_timer)->data = (_data);                               \
150         } while (0)
151
152 #define __setup_timer_on_stack(_timer, _fn, _data, _flags)              \
153         do {                                                            \
154                 __init_timer_on_stack((_timer), (_flags));              \
155                 (_timer)->function = (_fn);                             \
156                 (_timer)->data = (_data);                               \
157         } while (0)
158
159 #define setup_timer(timer, fn, data)                                    \
160         __setup_timer((timer), (fn), (data), 0)
161 #define setup_pinned_timer(timer, fn, data)                             \
162         __setup_timer((timer), (fn), (data), TIMER_PINNED)
163 #define setup_deferrable_timer(timer, fn, data)                         \
164         __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE)
165 #define setup_pinned_deferrable_timer(timer, fn, data)                  \
166         __setup_timer((timer), (fn), (data), TIMER_DEFERRABLE | TIMER_PINNED)
167 #define setup_timer_on_stack(timer, fn, data)                           \
168         __setup_timer_on_stack((timer), (fn), (data), 0)
169 #define setup_pinned_timer_on_stack(timer, fn, data)                    \
170         __setup_timer_on_stack((timer), (fn), (data), TIMER_PINNED)
171 #define setup_deferrable_timer_on_stack(timer, fn, data)                \
172         __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE)
173 #define setup_pinned_deferrable_timer_on_stack(timer, fn, data)         \
174         __setup_timer_on_stack((timer), (fn), (data), TIMER_DEFERRABLE | TIMER_PINNED)
175
176 /**
177  * timer_pending - is a timer pending?
178  * @timer: the timer in question
179  *
180  * timer_pending will tell whether a given timer is currently pending,
181  * or not. Callers must ensure serialization wrt. other operations done
182  * to this timer, eg. interrupt contexts, or other CPUs on SMP.
183  *
184  * return value: 1 if the timer is pending, 0 if not.
185  */
186 static inline int timer_pending(const struct timer_list * timer)
187 {
188         return timer->entry.pprev != NULL;
189 }
190
191 extern void add_timer_on(struct timer_list *timer, int cpu);
192 extern int del_timer(struct timer_list * timer);
193 extern int mod_timer(struct timer_list *timer, unsigned long expires);
194 extern int mod_timer_pending(struct timer_list *timer, unsigned long expires);
195
196 extern void set_timer_slack(struct timer_list *time, int slack_hz);
197
198 /*
199  * The jiffies value which is added to now, when there is no timer
200  * in the timer wheel:
201  */
202 #define NEXT_TIMER_MAX_DELTA    ((1UL << 30) - 1)
203
204 /*
205  * Timer-statistics info:
206  */
207 #ifdef CONFIG_TIMER_STATS
208
209 extern int timer_stats_active;
210
211 extern void init_timer_stats(void);
212
213 extern void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
214                                      void *timerf, char *comm, u32 flags);
215
216 extern void __timer_stats_timer_set_start_info(struct timer_list *timer,
217                                                void *addr);
218
219 static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
220 {
221         if (likely(!timer_stats_active))
222                 return;
223         __timer_stats_timer_set_start_info(timer, __builtin_return_address(0));
224 }
225
226 static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
227 {
228         timer->start_site = NULL;
229 }
230 #else
231 static inline void init_timer_stats(void)
232 {
233 }
234
235 static inline void timer_stats_timer_set_start_info(struct timer_list *timer)
236 {
237 }
238
239 static inline void timer_stats_timer_clear_start_info(struct timer_list *timer)
240 {
241 }
242 #endif
243
244 extern void add_timer(struct timer_list *timer);
245
246 extern int try_to_del_timer_sync(struct timer_list *timer);
247
248 #ifdef CONFIG_SMP
249   extern int del_timer_sync(struct timer_list *timer);
250 #else
251 # define del_timer_sync(t)              del_timer(t)
252 #endif
253
254 #define del_singleshot_timer_sync(t) del_timer_sync(t)
255
256 extern void init_timers(void);
257 extern void run_local_timers(void);
258 struct hrtimer;
259 extern enum hrtimer_restart it_real_fn(struct hrtimer *);
260
261 #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON)
262 #include <linux/sysctl.h>
263
264 extern unsigned int sysctl_timer_migration;
265 int timer_migration_handler(struct ctl_table *table, int write,
266                             void __user *buffer, size_t *lenp,
267                             loff_t *ppos);
268 #endif
269
270 unsigned long __round_jiffies(unsigned long j, int cpu);
271 unsigned long __round_jiffies_relative(unsigned long j, int cpu);
272 unsigned long round_jiffies(unsigned long j);
273 unsigned long round_jiffies_relative(unsigned long j);
274
275 unsigned long __round_jiffies_up(unsigned long j, int cpu);
276 unsigned long __round_jiffies_up_relative(unsigned long j, int cpu);
277 unsigned long round_jiffies_up(unsigned long j);
278 unsigned long round_jiffies_up_relative(unsigned long j);
279
280 #endif