]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/timer.c | |
3 | * | |
4a22f166 | 4 | * Kernel internal timers |
1da177e4 LT |
5 | * |
6 | * Copyright (C) 1991, 1992 Linus Torvalds | |
7 | * | |
8 | * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better. | |
9 | * | |
10 | * 1997-09-10 Updated NTP code according to technical memorandum Jan '96 | |
11 | * "A Kernel Model for Precision Timekeeping" by Dave Mills | |
12 | * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to | |
13 | * serialize accesses to xtime/lost_ticks). | |
14 | * Copyright (C) 1998 Andrea Arcangeli | |
15 | * 1999-03-10 Improved NTP compatibility by Ulrich Windl | |
16 | * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love | |
17 | * 2000-10-05 Implemented scalable SMP per-CPU timer handling. | |
18 | * Copyright (C) 2000, 2001, 2002 Ingo Molnar | |
19 | * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar | |
20 | */ | |
21 | ||
22 | #include <linux/kernel_stat.h> | |
9984de1a | 23 | #include <linux/export.h> |
1da177e4 LT |
24 | #include <linux/interrupt.h> |
25 | #include <linux/percpu.h> | |
26 | #include <linux/init.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/swap.h> | |
b488893a | 29 | #include <linux/pid_namespace.h> |
1da177e4 LT |
30 | #include <linux/notifier.h> |
31 | #include <linux/thread_info.h> | |
32 | #include <linux/time.h> | |
33 | #include <linux/jiffies.h> | |
34 | #include <linux/posix-timers.h> | |
35 | #include <linux/cpu.h> | |
36 | #include <linux/syscalls.h> | |
97a41e26 | 37 | #include <linux/delay.h> |
79bf2bb3 | 38 | #include <linux/tick.h> |
82f67cd9 | 39 | #include <linux/kallsyms.h> |
e360adbe | 40 | #include <linux/irq_work.h> |
eea08f32 | 41 | #include <linux/sched.h> |
cf4aebc2 | 42 | #include <linux/sched/sysctl.h> |
5a0e3ad6 | 43 | #include <linux/slab.h> |
1a0df594 | 44 | #include <linux/compat.h> |
1da177e4 LT |
45 | |
46 | #include <asm/uaccess.h> | |
47 | #include <asm/unistd.h> | |
48 | #include <asm/div64.h> | |
49 | #include <asm/timex.h> | |
50 | #include <asm/io.h> | |
51 | ||
c1ad348b TG |
52 | #include "tick-internal.h" |
53 | ||
2b022e3d XG |
54 | #define CREATE_TRACE_POINTS |
55 | #include <trace/events/timer.h> | |
56 | ||
40747ffa | 57 | __visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; |
ecea8d19 TG |
58 | |
59 | EXPORT_SYMBOL(jiffies_64); | |
60 | ||
1da177e4 LT |
61 | /* |
62 | * per-CPU timer vector definitions: | |
63 | */ | |
1da177e4 LT |
64 | #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6) |
65 | #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8) | |
66 | #define TVN_SIZE (1 << TVN_BITS) | |
67 | #define TVR_SIZE (1 << TVR_BITS) | |
68 | #define TVN_MASK (TVN_SIZE - 1) | |
69 | #define TVR_MASK (TVR_SIZE - 1) | |
26cff4e2 | 70 | #define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1)) |
1da177e4 | 71 | |
a6fa8e5a | 72 | struct tvec { |
1dabbcec | 73 | struct hlist_head vec[TVN_SIZE]; |
a6fa8e5a | 74 | }; |
1da177e4 | 75 | |
a6fa8e5a | 76 | struct tvec_root { |
1dabbcec | 77 | struct hlist_head vec[TVR_SIZE]; |
a6fa8e5a | 78 | }; |
1da177e4 | 79 | |
a6fa8e5a | 80 | struct tvec_base { |
3691c519 ON |
81 | spinlock_t lock; |
82 | struct timer_list *running_timer; | |
1da177e4 | 83 | unsigned long timer_jiffies; |
97fd9ed4 | 84 | unsigned long next_timer; |
99d5f3aa | 85 | unsigned long active_timers; |
fff42158 | 86 | unsigned long all_timers; |
d6f93829 | 87 | int cpu; |
a6fa8e5a PM |
88 | struct tvec_root tv1; |
89 | struct tvec tv2; | |
90 | struct tvec tv3; | |
91 | struct tvec tv4; | |
92 | struct tvec tv5; | |
6e453a67 | 93 | } ____cacheline_aligned; |
1da177e4 | 94 | |
e52b1db3 | 95 | |
0eeda71b | 96 | static DEFINE_PER_CPU(struct tvec_base, tvec_bases); |
6e453a67 | 97 | |
9c133c46 AS |
98 | static unsigned long round_jiffies_common(unsigned long j, int cpu, |
99 | bool force_up) | |
4c36a5de AV |
100 | { |
101 | int rem; | |
102 | unsigned long original = j; | |
103 | ||
104 | /* | |
105 | * We don't want all cpus firing their timers at once hitting the | |
106 | * same lock or cachelines, so we skew each extra cpu with an extra | |
107 | * 3 jiffies. This 3 jiffies came originally from the mm/ code which | |
108 | * already did this. | |
109 | * The skew is done by adding 3*cpunr, then round, then subtract this | |
110 | * extra offset again. | |
111 | */ | |
112 | j += cpu * 3; | |
113 | ||
114 | rem = j % HZ; | |
115 | ||
116 | /* | |
117 | * If the target jiffie is just after a whole second (which can happen | |
118 | * due to delays of the timer irq, long irq off times etc etc) then | |
119 | * we should round down to the whole second, not up. Use 1/4th second | |
120 | * as cutoff for this rounding as an extreme upper bound for this. | |
9c133c46 | 121 | * But never round down if @force_up is set. |
4c36a5de | 122 | */ |
9c133c46 | 123 | if (rem < HZ/4 && !force_up) /* round down */ |
4c36a5de AV |
124 | j = j - rem; |
125 | else /* round up */ | |
126 | j = j - rem + HZ; | |
127 | ||
128 | /* now that we have rounded, subtract the extra skew again */ | |
129 | j -= cpu * 3; | |
130 | ||
9e04d380 BVA |
131 | /* |
132 | * Make sure j is still in the future. Otherwise return the | |
133 | * unmodified value. | |
134 | */ | |
135 | return time_is_after_jiffies(j) ? j : original; | |
4c36a5de | 136 | } |
9c133c46 AS |
137 | |
138 | /** | |
139 | * __round_jiffies - function to round jiffies to a full second | |
140 | * @j: the time in (absolute) jiffies that should be rounded | |
141 | * @cpu: the processor number on which the timeout will happen | |
142 | * | |
143 | * __round_jiffies() rounds an absolute time in the future (in jiffies) | |
144 | * up or down to (approximately) full seconds. This is useful for timers | |
145 | * for which the exact time they fire does not matter too much, as long as | |
146 | * they fire approximately every X seconds. | |
147 | * | |
148 | * By rounding these timers to whole seconds, all such timers will fire | |
149 | * at the same time, rather than at various times spread out. The goal | |
150 | * of this is to have the CPU wake up less, which saves power. | |
151 | * | |
152 | * The exact rounding is skewed for each processor to avoid all | |
153 | * processors firing at the exact same time, which could lead | |
154 | * to lock contention or spurious cache line bouncing. | |
155 | * | |
156 | * The return value is the rounded version of the @j parameter. | |
157 | */ | |
158 | unsigned long __round_jiffies(unsigned long j, int cpu) | |
159 | { | |
160 | return round_jiffies_common(j, cpu, false); | |
161 | } | |
4c36a5de AV |
162 | EXPORT_SYMBOL_GPL(__round_jiffies); |
163 | ||
164 | /** | |
165 | * __round_jiffies_relative - function to round jiffies to a full second | |
166 | * @j: the time in (relative) jiffies that should be rounded | |
167 | * @cpu: the processor number on which the timeout will happen | |
168 | * | |
72fd4a35 | 169 | * __round_jiffies_relative() rounds a time delta in the future (in jiffies) |
4c36a5de AV |
170 | * up or down to (approximately) full seconds. This is useful for timers |
171 | * for which the exact time they fire does not matter too much, as long as | |
172 | * they fire approximately every X seconds. | |
173 | * | |
174 | * By rounding these timers to whole seconds, all such timers will fire | |
175 | * at the same time, rather than at various times spread out. The goal | |
176 | * of this is to have the CPU wake up less, which saves power. | |
177 | * | |
178 | * The exact rounding is skewed for each processor to avoid all | |
179 | * processors firing at the exact same time, which could lead | |
180 | * to lock contention or spurious cache line bouncing. | |
181 | * | |
72fd4a35 | 182 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
183 | */ |
184 | unsigned long __round_jiffies_relative(unsigned long j, int cpu) | |
185 | { | |
9c133c46 AS |
186 | unsigned long j0 = jiffies; |
187 | ||
188 | /* Use j0 because jiffies might change while we run */ | |
189 | return round_jiffies_common(j + j0, cpu, false) - j0; | |
4c36a5de AV |
190 | } |
191 | EXPORT_SYMBOL_GPL(__round_jiffies_relative); | |
192 | ||
193 | /** | |
194 | * round_jiffies - function to round jiffies to a full second | |
195 | * @j: the time in (absolute) jiffies that should be rounded | |
196 | * | |
72fd4a35 | 197 | * round_jiffies() rounds an absolute time in the future (in jiffies) |
4c36a5de AV |
198 | * up or down to (approximately) full seconds. This is useful for timers |
199 | * for which the exact time they fire does not matter too much, as long as | |
200 | * they fire approximately every X seconds. | |
201 | * | |
202 | * By rounding these timers to whole seconds, all such timers will fire | |
203 | * at the same time, rather than at various times spread out. The goal | |
204 | * of this is to have the CPU wake up less, which saves power. | |
205 | * | |
72fd4a35 | 206 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
207 | */ |
208 | unsigned long round_jiffies(unsigned long j) | |
209 | { | |
9c133c46 | 210 | return round_jiffies_common(j, raw_smp_processor_id(), false); |
4c36a5de AV |
211 | } |
212 | EXPORT_SYMBOL_GPL(round_jiffies); | |
213 | ||
214 | /** | |
215 | * round_jiffies_relative - function to round jiffies to a full second | |
216 | * @j: the time in (relative) jiffies that should be rounded | |
217 | * | |
72fd4a35 | 218 | * round_jiffies_relative() rounds a time delta in the future (in jiffies) |
4c36a5de AV |
219 | * up or down to (approximately) full seconds. This is useful for timers |
220 | * for which the exact time they fire does not matter too much, as long as | |
221 | * they fire approximately every X seconds. | |
222 | * | |
223 | * By rounding these timers to whole seconds, all such timers will fire | |
224 | * at the same time, rather than at various times spread out. The goal | |
225 | * of this is to have the CPU wake up less, which saves power. | |
226 | * | |
72fd4a35 | 227 | * The return value is the rounded version of the @j parameter. |
4c36a5de AV |
228 | */ |
229 | unsigned long round_jiffies_relative(unsigned long j) | |
230 | { | |
231 | return __round_jiffies_relative(j, raw_smp_processor_id()); | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(round_jiffies_relative); | |
234 | ||
9c133c46 AS |
235 | /** |
236 | * __round_jiffies_up - function to round jiffies up to a full second | |
237 | * @j: the time in (absolute) jiffies that should be rounded | |
238 | * @cpu: the processor number on which the timeout will happen | |
239 | * | |
240 | * This is the same as __round_jiffies() except that it will never | |
241 | * round down. This is useful for timeouts for which the exact time | |
242 | * of firing does not matter too much, as long as they don't fire too | |
243 | * early. | |
244 | */ | |
245 | unsigned long __round_jiffies_up(unsigned long j, int cpu) | |
246 | { | |
247 | return round_jiffies_common(j, cpu, true); | |
248 | } | |
249 | EXPORT_SYMBOL_GPL(__round_jiffies_up); | |
250 | ||
251 | /** | |
252 | * __round_jiffies_up_relative - function to round jiffies up to a full second | |
253 | * @j: the time in (relative) jiffies that should be rounded | |
254 | * @cpu: the processor number on which the timeout will happen | |
255 | * | |
256 | * This is the same as __round_jiffies_relative() except that it will never | |
257 | * round down. This is useful for timeouts for which the exact time | |
258 | * of firing does not matter too much, as long as they don't fire too | |
259 | * early. | |
260 | */ | |
261 | unsigned long __round_jiffies_up_relative(unsigned long j, int cpu) | |
262 | { | |
263 | unsigned long j0 = jiffies; | |
264 | ||
265 | /* Use j0 because jiffies might change while we run */ | |
266 | return round_jiffies_common(j + j0, cpu, true) - j0; | |
267 | } | |
268 | EXPORT_SYMBOL_GPL(__round_jiffies_up_relative); | |
269 | ||
270 | /** | |
271 | * round_jiffies_up - function to round jiffies up to a full second | |
272 | * @j: the time in (absolute) jiffies that should be rounded | |
273 | * | |
274 | * This is the same as round_jiffies() except that it will never | |
275 | * round down. This is useful for timeouts for which the exact time | |
276 | * of firing does not matter too much, as long as they don't fire too | |
277 | * early. | |
278 | */ | |
279 | unsigned long round_jiffies_up(unsigned long j) | |
280 | { | |
281 | return round_jiffies_common(j, raw_smp_processor_id(), true); | |
282 | } | |
283 | EXPORT_SYMBOL_GPL(round_jiffies_up); | |
284 | ||
285 | /** | |
286 | * round_jiffies_up_relative - function to round jiffies up to a full second | |
287 | * @j: the time in (relative) jiffies that should be rounded | |
288 | * | |
289 | * This is the same as round_jiffies_relative() except that it will never | |
290 | * round down. This is useful for timeouts for which the exact time | |
291 | * of firing does not matter too much, as long as they don't fire too | |
292 | * early. | |
293 | */ | |
294 | unsigned long round_jiffies_up_relative(unsigned long j) | |
295 | { | |
296 | return __round_jiffies_up_relative(j, raw_smp_processor_id()); | |
297 | } | |
298 | EXPORT_SYMBOL_GPL(round_jiffies_up_relative); | |
299 | ||
3bbb9ec9 AV |
300 | /** |
301 | * set_timer_slack - set the allowed slack for a timer | |
0caa6210 | 302 | * @timer: the timer to be modified |
3bbb9ec9 AV |
303 | * @slack_hz: the amount of time (in jiffies) allowed for rounding |
304 | * | |
305 | * Set the amount of time, in jiffies, that a certain timer has | |
306 | * in terms of slack. By setting this value, the timer subsystem | |
307 | * will schedule the actual timer somewhere between | |
308 | * the time mod_timer() asks for, and that time plus the slack. | |
309 | * | |
310 | * By setting the slack to -1, a percentage of the delay is used | |
311 | * instead. | |
312 | */ | |
313 | void set_timer_slack(struct timer_list *timer, int slack_hz) | |
314 | { | |
315 | timer->slack = slack_hz; | |
316 | } | |
317 | EXPORT_SYMBOL_GPL(set_timer_slack); | |
318 | ||
facbb4a7 TG |
319 | static void |
320 | __internal_add_timer(struct tvec_base *base, struct timer_list *timer) | |
1da177e4 LT |
321 | { |
322 | unsigned long expires = timer->expires; | |
323 | unsigned long idx = expires - base->timer_jiffies; | |
1dabbcec | 324 | struct hlist_head *vec; |
1da177e4 LT |
325 | |
326 | if (idx < TVR_SIZE) { | |
327 | int i = expires & TVR_MASK; | |
328 | vec = base->tv1.vec + i; | |
329 | } else if (idx < 1 << (TVR_BITS + TVN_BITS)) { | |
330 | int i = (expires >> TVR_BITS) & TVN_MASK; | |
331 | vec = base->tv2.vec + i; | |
332 | } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) { | |
333 | int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK; | |
334 | vec = base->tv3.vec + i; | |
335 | } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) { | |
336 | int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK; | |
337 | vec = base->tv4.vec + i; | |
338 | } else if ((signed long) idx < 0) { | |
339 | /* | |
340 | * Can happen if you add a timer with expires == jiffies, | |
341 | * or you set a timer to go off in the past | |
342 | */ | |
343 | vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); | |
344 | } else { | |
345 | int i; | |
26cff4e2 HC |
346 | /* If the timeout is larger than MAX_TVAL (on 64-bit |
347 | * architectures or with CONFIG_BASE_SMALL=1) then we | |
348 | * use the maximum timeout. | |
1da177e4 | 349 | */ |
26cff4e2 HC |
350 | if (idx > MAX_TVAL) { |
351 | idx = MAX_TVAL; | |
1da177e4 LT |
352 | expires = idx + base->timer_jiffies; |
353 | } | |
354 | i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK; | |
355 | vec = base->tv5.vec + i; | |
356 | } | |
1bd04bf6 | 357 | |
1dabbcec | 358 | hlist_add_head(&timer->entry, vec); |
1da177e4 LT |
359 | } |
360 | ||
facbb4a7 TG |
361 | static void internal_add_timer(struct tvec_base *base, struct timer_list *timer) |
362 | { | |
3bb475a3 TG |
363 | /* Advance base->jiffies, if the base is empty */ |
364 | if (!base->all_timers++) | |
365 | base->timer_jiffies = jiffies; | |
366 | ||
facbb4a7 TG |
367 | __internal_add_timer(base, timer); |
368 | /* | |
99d5f3aa | 369 | * Update base->active_timers and base->next_timer |
facbb4a7 | 370 | */ |
0eeda71b | 371 | if (!(timer->flags & TIMER_DEFERRABLE)) { |
aea369b9 ON |
372 | if (!base->active_timers++ || |
373 | time_before(timer->expires, base->next_timer)) | |
99d5f3aa | 374 | base->next_timer = timer->expires; |
99d5f3aa | 375 | } |
9f6d9baa VK |
376 | |
377 | /* | |
378 | * Check whether the other CPU is in dynticks mode and needs | |
379 | * to be triggered to reevaluate the timer wheel. | |
380 | * We are protected against the other CPU fiddling | |
381 | * with the timer by holding the timer base lock. This also | |
382 | * makes sure that a CPU on the way to stop its tick can not | |
383 | * evaluate the timer wheel. | |
384 | * | |
385 | * Spare the IPI for deferrable timers on idle targets though. | |
386 | * The next busy ticks will take care of it. Except full dynticks | |
387 | * require special care against races with idle_cpu(), lets deal | |
388 | * with that later. | |
389 | */ | |
0eeda71b | 390 | if (!(timer->flags & TIMER_DEFERRABLE) || tick_nohz_full_cpu(base->cpu)) |
9f6d9baa | 391 | wake_up_nohz_cpu(base->cpu); |
facbb4a7 TG |
392 | } |
393 | ||
82f67cd9 IM |
394 | #ifdef CONFIG_TIMER_STATS |
395 | void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr) | |
396 | { | |
397 | if (timer->start_site) | |
398 | return; | |
399 | ||
400 | timer->start_site = addr; | |
401 | memcpy(timer->start_comm, current->comm, TASK_COMM_LEN); | |
402 | timer->start_pid = current->pid; | |
403 | } | |
c5c061b8 VP |
404 | |
405 | static void timer_stats_account_timer(struct timer_list *timer) | |
406 | { | |
507e1231 HC |
407 | if (likely(!timer->start_site)) |
408 | return; | |
c5c061b8 VP |
409 | |
410 | timer_stats_update_stats(timer, timer->start_pid, timer->start_site, | |
c74441a1 TG |
411 | timer->function, timer->start_comm, |
412 | timer->flags); | |
c5c061b8 VP |
413 | } |
414 | ||
415 | #else | |
416 | static void timer_stats_account_timer(struct timer_list *timer) {} | |
82f67cd9 IM |
417 | #endif |
418 | ||
c6f3a97f TG |
419 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
420 | ||
421 | static struct debug_obj_descr timer_debug_descr; | |
422 | ||
99777288 SG |
423 | static void *timer_debug_hint(void *addr) |
424 | { | |
425 | return ((struct timer_list *) addr)->function; | |
426 | } | |
427 | ||
c6f3a97f TG |
428 | /* |
429 | * fixup_init is called when: | |
430 | * - an active object is initialized | |
55c888d6 | 431 | */ |
c6f3a97f TG |
432 | static int timer_fixup_init(void *addr, enum debug_obj_state state) |
433 | { | |
434 | struct timer_list *timer = addr; | |
435 | ||
436 | switch (state) { | |
437 | case ODEBUG_STATE_ACTIVE: | |
438 | del_timer_sync(timer); | |
439 | debug_object_init(timer, &timer_debug_descr); | |
440 | return 1; | |
441 | default: | |
442 | return 0; | |
443 | } | |
444 | } | |
445 | ||
fb16b8cf SB |
446 | /* Stub timer callback for improperly used timers. */ |
447 | static void stub_timer(unsigned long data) | |
448 | { | |
449 | WARN_ON(1); | |
450 | } | |
451 | ||
c6f3a97f TG |
452 | /* |
453 | * fixup_activate is called when: | |
454 | * - an active object is activated | |
455 | * - an unknown object is activated (might be a statically initialized object) | |
456 | */ | |
457 | static int timer_fixup_activate(void *addr, enum debug_obj_state state) | |
458 | { | |
459 | struct timer_list *timer = addr; | |
460 | ||
461 | switch (state) { | |
462 | ||
463 | case ODEBUG_STATE_NOTAVAILABLE: | |
464 | /* | |
465 | * This is not really a fixup. The timer was | |
466 | * statically initialized. We just make sure that it | |
467 | * is tracked in the object tracker. | |
468 | */ | |
1dabbcec TG |
469 | if (timer->entry.pprev == NULL && |
470 | timer->entry.next == TIMER_ENTRY_STATIC) { | |
c6f3a97f TG |
471 | debug_object_init(timer, &timer_debug_descr); |
472 | debug_object_activate(timer, &timer_debug_descr); | |
473 | return 0; | |
474 | } else { | |
fb16b8cf SB |
475 | setup_timer(timer, stub_timer, 0); |
476 | return 1; | |
c6f3a97f TG |
477 | } |
478 | return 0; | |
479 | ||
480 | case ODEBUG_STATE_ACTIVE: | |
481 | WARN_ON(1); | |
482 | ||
483 | default: | |
484 | return 0; | |
485 | } | |
486 | } | |
487 | ||
488 | /* | |
489 | * fixup_free is called when: | |
490 | * - an active object is freed | |
491 | */ | |
492 | static int timer_fixup_free(void *addr, enum debug_obj_state state) | |
493 | { | |
494 | struct timer_list *timer = addr; | |
495 | ||
496 | switch (state) { | |
497 | case ODEBUG_STATE_ACTIVE: | |
498 | del_timer_sync(timer); | |
499 | debug_object_free(timer, &timer_debug_descr); | |
500 | return 1; | |
501 | default: | |
502 | return 0; | |
503 | } | |
504 | } | |
505 | ||
dc4218bd CC |
506 | /* |
507 | * fixup_assert_init is called when: | |
508 | * - an untracked/uninit-ed object is found | |
509 | */ | |
510 | static int timer_fixup_assert_init(void *addr, enum debug_obj_state state) | |
511 | { | |
512 | struct timer_list *timer = addr; | |
513 | ||
514 | switch (state) { | |
515 | case ODEBUG_STATE_NOTAVAILABLE: | |
1dabbcec | 516 | if (timer->entry.next == TIMER_ENTRY_STATIC) { |
dc4218bd CC |
517 | /* |
518 | * This is not really a fixup. The timer was | |
519 | * statically initialized. We just make sure that it | |
520 | * is tracked in the object tracker. | |
521 | */ | |
522 | debug_object_init(timer, &timer_debug_descr); | |
523 | return 0; | |
524 | } else { | |
525 | setup_timer(timer, stub_timer, 0); | |
526 | return 1; | |
527 | } | |
528 | default: | |
529 | return 0; | |
530 | } | |
531 | } | |
532 | ||
c6f3a97f | 533 | static struct debug_obj_descr timer_debug_descr = { |
dc4218bd CC |
534 | .name = "timer_list", |
535 | .debug_hint = timer_debug_hint, | |
536 | .fixup_init = timer_fixup_init, | |
537 | .fixup_activate = timer_fixup_activate, | |
538 | .fixup_free = timer_fixup_free, | |
539 | .fixup_assert_init = timer_fixup_assert_init, | |
c6f3a97f TG |
540 | }; |
541 | ||
542 | static inline void debug_timer_init(struct timer_list *timer) | |
543 | { | |
544 | debug_object_init(timer, &timer_debug_descr); | |
545 | } | |
546 | ||
547 | static inline void debug_timer_activate(struct timer_list *timer) | |
548 | { | |
549 | debug_object_activate(timer, &timer_debug_descr); | |
550 | } | |
551 | ||
552 | static inline void debug_timer_deactivate(struct timer_list *timer) | |
553 | { | |
554 | debug_object_deactivate(timer, &timer_debug_descr); | |
555 | } | |
556 | ||
557 | static inline void debug_timer_free(struct timer_list *timer) | |
558 | { | |
559 | debug_object_free(timer, &timer_debug_descr); | |
560 | } | |
561 | ||
dc4218bd CC |
562 | static inline void debug_timer_assert_init(struct timer_list *timer) |
563 | { | |
564 | debug_object_assert_init(timer, &timer_debug_descr); | |
565 | } | |
566 | ||
fc683995 TH |
567 | static void do_init_timer(struct timer_list *timer, unsigned int flags, |
568 | const char *name, struct lock_class_key *key); | |
c6f3a97f | 569 | |
fc683995 TH |
570 | void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags, |
571 | const char *name, struct lock_class_key *key) | |
c6f3a97f TG |
572 | { |
573 | debug_object_init_on_stack(timer, &timer_debug_descr); | |
fc683995 | 574 | do_init_timer(timer, flags, name, key); |
c6f3a97f | 575 | } |
6f2b9b9a | 576 | EXPORT_SYMBOL_GPL(init_timer_on_stack_key); |
c6f3a97f TG |
577 | |
578 | void destroy_timer_on_stack(struct timer_list *timer) | |
579 | { | |
580 | debug_object_free(timer, &timer_debug_descr); | |
581 | } | |
582 | EXPORT_SYMBOL_GPL(destroy_timer_on_stack); | |
583 | ||
584 | #else | |
585 | static inline void debug_timer_init(struct timer_list *timer) { } | |
586 | static inline void debug_timer_activate(struct timer_list *timer) { } | |
587 | static inline void debug_timer_deactivate(struct timer_list *timer) { } | |
dc4218bd | 588 | static inline void debug_timer_assert_init(struct timer_list *timer) { } |
c6f3a97f TG |
589 | #endif |
590 | ||
2b022e3d XG |
591 | static inline void debug_init(struct timer_list *timer) |
592 | { | |
593 | debug_timer_init(timer); | |
594 | trace_timer_init(timer); | |
595 | } | |
596 | ||
597 | static inline void | |
598 | debug_activate(struct timer_list *timer, unsigned long expires) | |
599 | { | |
600 | debug_timer_activate(timer); | |
0eeda71b | 601 | trace_timer_start(timer, expires, timer->flags); |
2b022e3d XG |
602 | } |
603 | ||
604 | static inline void debug_deactivate(struct timer_list *timer) | |
605 | { | |
606 | debug_timer_deactivate(timer); | |
607 | trace_timer_cancel(timer); | |
608 | } | |
609 | ||
dc4218bd CC |
610 | static inline void debug_assert_init(struct timer_list *timer) |
611 | { | |
612 | debug_timer_assert_init(timer); | |
613 | } | |
614 | ||
fc683995 TH |
615 | static void do_init_timer(struct timer_list *timer, unsigned int flags, |
616 | const char *name, struct lock_class_key *key) | |
55c888d6 | 617 | { |
1dabbcec | 618 | timer->entry.pprev = NULL; |
0eeda71b | 619 | timer->flags = flags | raw_smp_processor_id(); |
3bbb9ec9 | 620 | timer->slack = -1; |
82f67cd9 IM |
621 | #ifdef CONFIG_TIMER_STATS |
622 | timer->start_site = NULL; | |
623 | timer->start_pid = -1; | |
624 | memset(timer->start_comm, 0, TASK_COMM_LEN); | |
625 | #endif | |
6f2b9b9a | 626 | lockdep_init_map(&timer->lockdep_map, name, key, 0); |
55c888d6 | 627 | } |
c6f3a97f TG |
628 | |
629 | /** | |
633fe795 | 630 | * init_timer_key - initialize a timer |
c6f3a97f | 631 | * @timer: the timer to be initialized |
fc683995 | 632 | * @flags: timer flags |
633fe795 RD |
633 | * @name: name of the timer |
634 | * @key: lockdep class key of the fake lock used for tracking timer | |
635 | * sync lock dependencies | |
c6f3a97f | 636 | * |
633fe795 | 637 | * init_timer_key() must be done to a timer prior calling *any* of the |
c6f3a97f TG |
638 | * other timer functions. |
639 | */ | |
fc683995 TH |
640 | void init_timer_key(struct timer_list *timer, unsigned int flags, |
641 | const char *name, struct lock_class_key *key) | |
c6f3a97f | 642 | { |
2b022e3d | 643 | debug_init(timer); |
fc683995 | 644 | do_init_timer(timer, flags, name, key); |
c6f3a97f | 645 | } |
6f2b9b9a | 646 | EXPORT_SYMBOL(init_timer_key); |
55c888d6 | 647 | |
ec44bc7a | 648 | static inline void detach_timer(struct timer_list *timer, bool clear_pending) |
55c888d6 | 649 | { |
1dabbcec | 650 | struct hlist_node *entry = &timer->entry; |
55c888d6 | 651 | |
2b022e3d | 652 | debug_deactivate(timer); |
c6f3a97f | 653 | |
1dabbcec | 654 | __hlist_del(entry); |
55c888d6 | 655 | if (clear_pending) |
1dabbcec TG |
656 | entry->pprev = NULL; |
657 | entry->next = LIST_POISON2; | |
55c888d6 ON |
658 | } |
659 | ||
99d5f3aa TG |
660 | static inline void |
661 | detach_expired_timer(struct timer_list *timer, struct tvec_base *base) | |
662 | { | |
663 | detach_timer(timer, true); | |
0eeda71b | 664 | if (!(timer->flags & TIMER_DEFERRABLE)) |
e52b1db3 | 665 | base->active_timers--; |
fff42158 | 666 | base->all_timers--; |
99d5f3aa TG |
667 | } |
668 | ||
ec44bc7a TG |
669 | static int detach_if_pending(struct timer_list *timer, struct tvec_base *base, |
670 | bool clear_pending) | |
671 | { | |
672 | if (!timer_pending(timer)) | |
673 | return 0; | |
674 | ||
675 | detach_timer(timer, clear_pending); | |
0eeda71b | 676 | if (!(timer->flags & TIMER_DEFERRABLE)) { |
e52b1db3 | 677 | base->active_timers--; |
99d5f3aa TG |
678 | if (timer->expires == base->next_timer) |
679 | base->next_timer = base->timer_jiffies; | |
680 | } | |
3bb475a3 TG |
681 | /* If this was the last timer, advance base->jiffies */ |
682 | if (!--base->all_timers) | |
683 | base->timer_jiffies = jiffies; | |
ec44bc7a TG |
684 | return 1; |
685 | } | |
686 | ||
55c888d6 | 687 | /* |
3691c519 | 688 | * We are using hashed locking: holding per_cpu(tvec_bases).lock |
55c888d6 ON |
689 | * means that all timers which are tied to this base via timer->base are |
690 | * locked, and the base itself is locked too. | |
691 | * | |
692 | * So __run_timers/migrate_timers can safely modify all timers which could | |
693 | * be found on ->tvX lists. | |
694 | * | |
0eeda71b TG |
695 | * When the timer's base is locked and removed from the list, the |
696 | * TIMER_MIGRATING flag is set, FIXME | |
55c888d6 | 697 | */ |
a6fa8e5a | 698 | static struct tvec_base *lock_timer_base(struct timer_list *timer, |
55c888d6 | 699 | unsigned long *flags) |
89e7e374 | 700 | __acquires(timer->base->lock) |
55c888d6 | 701 | { |
55c888d6 | 702 | for (;;) { |
0eeda71b TG |
703 | u32 tf = timer->flags; |
704 | struct tvec_base *base; | |
705 | ||
706 | if (!(tf & TIMER_MIGRATING)) { | |
707 | base = per_cpu_ptr(&tvec_bases, tf & TIMER_CPUMASK); | |
55c888d6 | 708 | spin_lock_irqsave(&base->lock, *flags); |
0eeda71b | 709 | if (timer->flags == tf) |
55c888d6 | 710 | return base; |
55c888d6 ON |
711 | spin_unlock_irqrestore(&base->lock, *flags); |
712 | } | |
713 | cpu_relax(); | |
714 | } | |
715 | } | |
716 | ||
74019224 | 717 | static inline int |
597d0275 AB |
718 | __mod_timer(struct timer_list *timer, unsigned long expires, |
719 | bool pending_only, int pinned) | |
1da177e4 | 720 | { |
a6fa8e5a | 721 | struct tvec_base *base, *new_base; |
1da177e4 | 722 | unsigned long flags; |
eea08f32 | 723 | int ret = 0 , cpu; |
1da177e4 | 724 | |
82f67cd9 | 725 | timer_stats_timer_set_start_info(timer); |
1da177e4 | 726 | BUG_ON(!timer->function); |
1da177e4 | 727 | |
55c888d6 ON |
728 | base = lock_timer_base(timer, &flags); |
729 | ||
ec44bc7a TG |
730 | ret = detach_if_pending(timer, base, false); |
731 | if (!ret && pending_only) | |
732 | goto out_unlock; | |
55c888d6 | 733 | |
2b022e3d | 734 | debug_activate(timer, expires); |
c6f3a97f | 735 | |
6201b4d6 | 736 | cpu = get_nohz_timer_target(pinned); |
0eeda71b | 737 | new_base = per_cpu_ptr(&tvec_bases, cpu); |
eea08f32 | 738 | |
3691c519 | 739 | if (base != new_base) { |
1da177e4 | 740 | /* |
55c888d6 ON |
741 | * We are trying to schedule the timer on the local CPU. |
742 | * However we can't change timer's base while it is running, | |
743 | * otherwise del_timer_sync() can't detect that the timer's | |
744 | * handler yet has not finished. This also guarantees that | |
745 | * the timer is serialized wrt itself. | |
1da177e4 | 746 | */ |
a2c348fe | 747 | if (likely(base->running_timer != timer)) { |
55c888d6 | 748 | /* See the comment in lock_timer_base() */ |
0eeda71b TG |
749 | timer->flags |= TIMER_MIGRATING; |
750 | ||
55c888d6 | 751 | spin_unlock(&base->lock); |
a2c348fe ON |
752 | base = new_base; |
753 | spin_lock(&base->lock); | |
0eeda71b | 754 | timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; |
1da177e4 LT |
755 | } |
756 | } | |
757 | ||
1da177e4 | 758 | timer->expires = expires; |
a2c348fe | 759 | internal_add_timer(base, timer); |
74019224 IM |
760 | |
761 | out_unlock: | |
a2c348fe | 762 | spin_unlock_irqrestore(&base->lock, flags); |
1da177e4 LT |
763 | |
764 | return ret; | |
765 | } | |
766 | ||
2aae4a10 | 767 | /** |
74019224 IM |
768 | * mod_timer_pending - modify a pending timer's timeout |
769 | * @timer: the pending timer to be modified | |
770 | * @expires: new timeout in jiffies | |
1da177e4 | 771 | * |
74019224 IM |
772 | * mod_timer_pending() is the same for pending timers as mod_timer(), |
773 | * but will not re-activate and modify already deleted timers. | |
774 | * | |
775 | * It is useful for unserialized use of timers. | |
1da177e4 | 776 | */ |
74019224 | 777 | int mod_timer_pending(struct timer_list *timer, unsigned long expires) |
1da177e4 | 778 | { |
597d0275 | 779 | return __mod_timer(timer, expires, true, TIMER_NOT_PINNED); |
1da177e4 | 780 | } |
74019224 | 781 | EXPORT_SYMBOL(mod_timer_pending); |
1da177e4 | 782 | |
3bbb9ec9 AV |
783 | /* |
784 | * Decide where to put the timer while taking the slack into account | |
785 | * | |
786 | * Algorithm: | |
787 | * 1) calculate the maximum (absolute) time | |
788 | * 2) calculate the highest bit where the expires and new max are different | |
789 | * 3) use this bit to make a mask | |
790 | * 4) use the bitmask to round down the maximum time, so that all last | |
791 | * bits are zeros | |
792 | */ | |
793 | static inline | |
794 | unsigned long apply_slack(struct timer_list *timer, unsigned long expires) | |
795 | { | |
796 | unsigned long expires_limit, mask; | |
797 | int bit; | |
798 | ||
8e63d779 | 799 | if (timer->slack >= 0) { |
f00e047e | 800 | expires_limit = expires + timer->slack; |
8e63d779 | 801 | } else { |
1c3cc116 SAS |
802 | long delta = expires - jiffies; |
803 | ||
804 | if (delta < 256) | |
805 | return expires; | |
3bbb9ec9 | 806 | |
1c3cc116 | 807 | expires_limit = expires + delta / 256; |
8e63d779 | 808 | } |
3bbb9ec9 | 809 | mask = expires ^ expires_limit; |
3bbb9ec9 AV |
810 | if (mask == 0) |
811 | return expires; | |
812 | ||
813 | bit = find_last_bit(&mask, BITS_PER_LONG); | |
814 | ||
98a01e77 | 815 | mask = (1UL << bit) - 1; |
3bbb9ec9 AV |
816 | |
817 | expires_limit = expires_limit & ~(mask); | |
818 | ||
819 | return expires_limit; | |
820 | } | |
821 | ||
2aae4a10 | 822 | /** |
1da177e4 LT |
823 | * mod_timer - modify a timer's timeout |
824 | * @timer: the timer to be modified | |
2aae4a10 | 825 | * @expires: new timeout in jiffies |
1da177e4 | 826 | * |
72fd4a35 | 827 | * mod_timer() is a more efficient way to update the expire field of an |
1da177e4 LT |
828 | * active timer (if the timer is inactive it will be activated) |
829 | * | |
830 | * mod_timer(timer, expires) is equivalent to: | |
831 | * | |
832 | * del_timer(timer); timer->expires = expires; add_timer(timer); | |
833 | * | |
834 | * Note that if there are multiple unserialized concurrent users of the | |
835 | * same timer, then mod_timer() is the only safe way to modify the timeout, | |
836 | * since add_timer() cannot modify an already running timer. | |
837 | * | |
838 | * The function returns whether it has modified a pending timer or not. | |
839 | * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an | |
840 | * active timer returns 1.) | |
841 | */ | |
842 | int mod_timer(struct timer_list *timer, unsigned long expires) | |
843 | { | |
1c3cc116 SAS |
844 | expires = apply_slack(timer, expires); |
845 | ||
1da177e4 LT |
846 | /* |
847 | * This is a common optimization triggered by the | |
848 | * networking code - if the timer is re-modified | |
849 | * to be the same thing then just return: | |
850 | */ | |
4841158b | 851 | if (timer_pending(timer) && timer->expires == expires) |
1da177e4 LT |
852 | return 1; |
853 | ||
597d0275 | 854 | return __mod_timer(timer, expires, false, TIMER_NOT_PINNED); |
1da177e4 | 855 | } |
1da177e4 LT |
856 | EXPORT_SYMBOL(mod_timer); |
857 | ||
597d0275 AB |
858 | /** |
859 | * mod_timer_pinned - modify a timer's timeout | |
860 | * @timer: the timer to be modified | |
861 | * @expires: new timeout in jiffies | |
862 | * | |
863 | * mod_timer_pinned() is a way to update the expire field of an | |
864 | * active timer (if the timer is inactive it will be activated) | |
048a0e8f PM |
865 | * and to ensure that the timer is scheduled on the current CPU. |
866 | * | |
867 | * Note that this does not prevent the timer from being migrated | |
868 | * when the current CPU goes offline. If this is a problem for | |
869 | * you, use CPU-hotplug notifiers to handle it correctly, for | |
870 | * example, cancelling the timer when the corresponding CPU goes | |
871 | * offline. | |
597d0275 AB |
872 | * |
873 | * mod_timer_pinned(timer, expires) is equivalent to: | |
874 | * | |
875 | * del_timer(timer); timer->expires = expires; add_timer(timer); | |
876 | */ | |
877 | int mod_timer_pinned(struct timer_list *timer, unsigned long expires) | |
878 | { | |
879 | if (timer->expires == expires && timer_pending(timer)) | |
880 | return 1; | |
881 | ||
882 | return __mod_timer(timer, expires, false, TIMER_PINNED); | |
883 | } | |
884 | EXPORT_SYMBOL(mod_timer_pinned); | |
885 | ||
74019224 IM |
886 | /** |
887 | * add_timer - start a timer | |
888 | * @timer: the timer to be added | |
889 | * | |
890 | * The kernel will do a ->function(->data) callback from the | |
891 | * timer interrupt at the ->expires point in the future. The | |
892 | * current time is 'jiffies'. | |
893 | * | |
894 | * The timer's ->expires, ->function (and if the handler uses it, ->data) | |
895 | * fields must be set prior calling this function. | |
896 | * | |
897 | * Timers with an ->expires field in the past will be executed in the next | |
898 | * timer tick. | |
899 | */ | |
900 | void add_timer(struct timer_list *timer) | |
901 | { | |
902 | BUG_ON(timer_pending(timer)); | |
903 | mod_timer(timer, timer->expires); | |
904 | } | |
905 | EXPORT_SYMBOL(add_timer); | |
906 | ||
907 | /** | |
908 | * add_timer_on - start a timer on a particular CPU | |
909 | * @timer: the timer to be added | |
910 | * @cpu: the CPU to start it on | |
911 | * | |
912 | * This is not very scalable on SMP. Double adds are not possible. | |
913 | */ | |
914 | void add_timer_on(struct timer_list *timer, int cpu) | |
915 | { | |
0eeda71b | 916 | struct tvec_base *base = per_cpu_ptr(&tvec_bases, cpu); |
74019224 IM |
917 | unsigned long flags; |
918 | ||
919 | timer_stats_timer_set_start_info(timer); | |
920 | BUG_ON(timer_pending(timer) || !timer->function); | |
921 | spin_lock_irqsave(&base->lock, flags); | |
0eeda71b | 922 | timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; |
2b022e3d | 923 | debug_activate(timer, timer->expires); |
74019224 | 924 | internal_add_timer(base, timer); |
74019224 IM |
925 | spin_unlock_irqrestore(&base->lock, flags); |
926 | } | |
a9862e05 | 927 | EXPORT_SYMBOL_GPL(add_timer_on); |
74019224 | 928 | |
2aae4a10 | 929 | /** |
1da177e4 LT |
930 | * del_timer - deactive a timer. |
931 | * @timer: the timer to be deactivated | |
932 | * | |
933 | * del_timer() deactivates a timer - this works on both active and inactive | |
934 | * timers. | |
935 | * | |
936 | * The function returns whether it has deactivated a pending timer or not. | |
937 | * (ie. del_timer() of an inactive timer returns 0, del_timer() of an | |
938 | * active timer returns 1.) | |
939 | */ | |
940 | int del_timer(struct timer_list *timer) | |
941 | { | |
a6fa8e5a | 942 | struct tvec_base *base; |
1da177e4 | 943 | unsigned long flags; |
55c888d6 | 944 | int ret = 0; |
1da177e4 | 945 | |
dc4218bd CC |
946 | debug_assert_init(timer); |
947 | ||
82f67cd9 | 948 | timer_stats_timer_clear_start_info(timer); |
55c888d6 ON |
949 | if (timer_pending(timer)) { |
950 | base = lock_timer_base(timer, &flags); | |
ec44bc7a | 951 | ret = detach_if_pending(timer, base, true); |
1da177e4 | 952 | spin_unlock_irqrestore(&base->lock, flags); |
1da177e4 | 953 | } |
1da177e4 | 954 | |
55c888d6 | 955 | return ret; |
1da177e4 | 956 | } |
1da177e4 LT |
957 | EXPORT_SYMBOL(del_timer); |
958 | ||
2aae4a10 REB |
959 | /** |
960 | * try_to_del_timer_sync - Try to deactivate a timer | |
961 | * @timer: timer do del | |
962 | * | |
fd450b73 ON |
963 | * This function tries to deactivate a timer. Upon successful (ret >= 0) |
964 | * exit the timer is not queued and the handler is not running on any CPU. | |
fd450b73 ON |
965 | */ |
966 | int try_to_del_timer_sync(struct timer_list *timer) | |
967 | { | |
a6fa8e5a | 968 | struct tvec_base *base; |
fd450b73 ON |
969 | unsigned long flags; |
970 | int ret = -1; | |
971 | ||
dc4218bd CC |
972 | debug_assert_init(timer); |
973 | ||
fd450b73 ON |
974 | base = lock_timer_base(timer, &flags); |
975 | ||
ec44bc7a TG |
976 | if (base->running_timer != timer) { |
977 | timer_stats_timer_clear_start_info(timer); | |
978 | ret = detach_if_pending(timer, base, true); | |
fd450b73 | 979 | } |
fd450b73 ON |
980 | spin_unlock_irqrestore(&base->lock, flags); |
981 | ||
982 | return ret; | |
983 | } | |
e19dff1f DH |
984 | EXPORT_SYMBOL(try_to_del_timer_sync); |
985 | ||
6f1bc451 | 986 | #ifdef CONFIG_SMP |
2aae4a10 | 987 | /** |
1da177e4 LT |
988 | * del_timer_sync - deactivate a timer and wait for the handler to finish. |
989 | * @timer: the timer to be deactivated | |
990 | * | |
991 | * This function only differs from del_timer() on SMP: besides deactivating | |
992 | * the timer it also makes sure the handler has finished executing on other | |
993 | * CPUs. | |
994 | * | |
72fd4a35 | 995 | * Synchronization rules: Callers must prevent restarting of the timer, |
1da177e4 | 996 | * otherwise this function is meaningless. It must not be called from |
c5f66e99 TH |
997 | * interrupt contexts unless the timer is an irqsafe one. The caller must |
998 | * not hold locks which would prevent completion of the timer's | |
999 | * handler. The timer's handler must not call add_timer_on(). Upon exit the | |
1000 | * timer is not queued and the handler is not running on any CPU. | |
1da177e4 | 1001 | * |
c5f66e99 TH |
1002 | * Note: For !irqsafe timers, you must not hold locks that are held in |
1003 | * interrupt context while calling this function. Even if the lock has | |
1004 | * nothing to do with the timer in question. Here's why: | |
48228f7b SR |
1005 | * |
1006 | * CPU0 CPU1 | |
1007 | * ---- ---- | |
1008 | * <SOFTIRQ> | |
1009 | * call_timer_fn(); | |
1010 | * base->running_timer = mytimer; | |
1011 | * spin_lock_irq(somelock); | |
1012 | * <IRQ> | |
1013 | * spin_lock(somelock); | |
1014 | * del_timer_sync(mytimer); | |
1015 | * while (base->running_timer == mytimer); | |
1016 | * | |
1017 | * Now del_timer_sync() will never return and never release somelock. | |
1018 | * The interrupt on the other CPU is waiting to grab somelock but | |
1019 | * it has interrupted the softirq that CPU0 is waiting to finish. | |
1020 | * | |
1da177e4 | 1021 | * The function returns whether it has deactivated a pending timer or not. |
1da177e4 LT |
1022 | */ |
1023 | int del_timer_sync(struct timer_list *timer) | |
1024 | { | |
6f2b9b9a | 1025 | #ifdef CONFIG_LOCKDEP |
f266a511 PZ |
1026 | unsigned long flags; |
1027 | ||
48228f7b SR |
1028 | /* |
1029 | * If lockdep gives a backtrace here, please reference | |
1030 | * the synchronization rules above. | |
1031 | */ | |
7ff20792 | 1032 | local_irq_save(flags); |
6f2b9b9a JB |
1033 | lock_map_acquire(&timer->lockdep_map); |
1034 | lock_map_release(&timer->lockdep_map); | |
7ff20792 | 1035 | local_irq_restore(flags); |
6f2b9b9a | 1036 | #endif |
466bd303 YZ |
1037 | /* |
1038 | * don't use it in hardirq context, because it | |
1039 | * could lead to deadlock. | |
1040 | */ | |
0eeda71b | 1041 | WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE)); |
fd450b73 ON |
1042 | for (;;) { |
1043 | int ret = try_to_del_timer_sync(timer); | |
1044 | if (ret >= 0) | |
1045 | return ret; | |
a0009652 | 1046 | cpu_relax(); |
fd450b73 | 1047 | } |
1da177e4 | 1048 | } |
55c888d6 | 1049 | EXPORT_SYMBOL(del_timer_sync); |
1da177e4 LT |
1050 | #endif |
1051 | ||
a6fa8e5a | 1052 | static int cascade(struct tvec_base *base, struct tvec *tv, int index) |
1da177e4 LT |
1053 | { |
1054 | /* cascade all the timers from tv up one level */ | |
1dabbcec TG |
1055 | struct timer_list *timer; |
1056 | struct hlist_node *tmp; | |
1057 | struct hlist_head tv_list; | |
3439dd86 | 1058 | |
1dabbcec | 1059 | hlist_move_list(tv->vec + index, &tv_list); |
1da177e4 | 1060 | |
1da177e4 | 1061 | /* |
3439dd86 P |
1062 | * We are removing _all_ timers from the list, so we |
1063 | * don't have to detach them individually. | |
1da177e4 | 1064 | */ |
1dabbcec | 1065 | hlist_for_each_entry_safe(timer, tmp, &tv_list, entry) { |
facbb4a7 TG |
1066 | /* No accounting, while moving them */ |
1067 | __internal_add_timer(base, timer); | |
1da177e4 | 1068 | } |
1da177e4 LT |
1069 | |
1070 | return index; | |
1071 | } | |
1072 | ||
576da126 TG |
1073 | static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long), |
1074 | unsigned long data) | |
1075 | { | |
4a2b4b22 | 1076 | int count = preempt_count(); |
576da126 TG |
1077 | |
1078 | #ifdef CONFIG_LOCKDEP | |
1079 | /* | |
1080 | * It is permissible to free the timer from inside the | |
1081 | * function that is called from it, this we need to take into | |
1082 | * account for lockdep too. To avoid bogus "held lock freed" | |
1083 | * warnings as well as problems when looking into | |
1084 | * timer->lockdep_map, make a copy and use that here. | |
1085 | */ | |
4d82a1de PZ |
1086 | struct lockdep_map lockdep_map; |
1087 | ||
1088 | lockdep_copy_map(&lockdep_map, &timer->lockdep_map); | |
576da126 TG |
1089 | #endif |
1090 | /* | |
1091 | * Couple the lock chain with the lock chain at | |
1092 | * del_timer_sync() by acquiring the lock_map around the fn() | |
1093 | * call here and in del_timer_sync(). | |
1094 | */ | |
1095 | lock_map_acquire(&lockdep_map); | |
1096 | ||
1097 | trace_timer_expire_entry(timer); | |
1098 | fn(data); | |
1099 | trace_timer_expire_exit(timer); | |
1100 | ||
1101 | lock_map_release(&lockdep_map); | |
1102 | ||
4a2b4b22 | 1103 | if (count != preempt_count()) { |
802702e0 | 1104 | WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n", |
4a2b4b22 | 1105 | fn, count, preempt_count()); |
802702e0 TG |
1106 | /* |
1107 | * Restore the preempt count. That gives us a decent | |
1108 | * chance to survive and extract information. If the | |
1109 | * callback kept a lock held, bad luck, but not worse | |
1110 | * than the BUG() we had. | |
1111 | */ | |
4a2b4b22 | 1112 | preempt_count_set(count); |
576da126 TG |
1113 | } |
1114 | } | |
1115 | ||
2aae4a10 REB |
1116 | #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) |
1117 | ||
1118 | /** | |
1da177e4 LT |
1119 | * __run_timers - run all expired timers (if any) on this CPU. |
1120 | * @base: the timer vector to be processed. | |
1121 | * | |
1122 | * This function cascades all vectors and executes all expired timer | |
1123 | * vectors. | |
1124 | */ | |
a6fa8e5a | 1125 | static inline void __run_timers(struct tvec_base *base) |
1da177e4 LT |
1126 | { |
1127 | struct timer_list *timer; | |
1128 | ||
3691c519 | 1129 | spin_lock_irq(&base->lock); |
3bb475a3 | 1130 | |
1da177e4 | 1131 | while (time_after_eq(jiffies, base->timer_jiffies)) { |
1dabbcec TG |
1132 | struct hlist_head work_list; |
1133 | struct hlist_head *head = &work_list; | |
3bb475a3 TG |
1134 | int index; |
1135 | ||
1136 | if (!base->all_timers) { | |
1137 | base->timer_jiffies = jiffies; | |
1138 | break; | |
1139 | } | |
1140 | ||
1141 | index = base->timer_jiffies & TVR_MASK; | |
626ab0e6 | 1142 | |
1da177e4 LT |
1143 | /* |
1144 | * Cascade timers: | |
1145 | */ | |
1146 | if (!index && | |
1147 | (!cascade(base, &base->tv2, INDEX(0))) && | |
1148 | (!cascade(base, &base->tv3, INDEX(1))) && | |
1149 | !cascade(base, &base->tv4, INDEX(2))) | |
1150 | cascade(base, &base->tv5, INDEX(3)); | |
626ab0e6 | 1151 | ++base->timer_jiffies; |
1dabbcec TG |
1152 | hlist_move_list(base->tv1.vec + index, head); |
1153 | while (!hlist_empty(head)) { | |
1da177e4 LT |
1154 | void (*fn)(unsigned long); |
1155 | unsigned long data; | |
c5f66e99 | 1156 | bool irqsafe; |
1da177e4 | 1157 | |
1dabbcec | 1158 | timer = hlist_entry(head->first, struct timer_list, entry); |
6819457d TG |
1159 | fn = timer->function; |
1160 | data = timer->data; | |
0eeda71b | 1161 | irqsafe = timer->flags & TIMER_IRQSAFE; |
1da177e4 | 1162 | |
82f67cd9 IM |
1163 | timer_stats_account_timer(timer); |
1164 | ||
6f1bc451 | 1165 | base->running_timer = timer; |
99d5f3aa | 1166 | detach_expired_timer(timer, base); |
6f2b9b9a | 1167 | |
c5f66e99 TH |
1168 | if (irqsafe) { |
1169 | spin_unlock(&base->lock); | |
1170 | call_timer_fn(timer, fn, data); | |
1171 | spin_lock(&base->lock); | |
1172 | } else { | |
1173 | spin_unlock_irq(&base->lock); | |
1174 | call_timer_fn(timer, fn, data); | |
1175 | spin_lock_irq(&base->lock); | |
1176 | } | |
1da177e4 LT |
1177 | } |
1178 | } | |
6f1bc451 | 1179 | base->running_timer = NULL; |
3691c519 | 1180 | spin_unlock_irq(&base->lock); |
1da177e4 LT |
1181 | } |
1182 | ||
3451d024 | 1183 | #ifdef CONFIG_NO_HZ_COMMON |
1da177e4 LT |
1184 | /* |
1185 | * Find out when the next timer event is due to happen. This | |
90cba64a RD |
1186 | * is used on S/390 to stop all activity when a CPU is idle. |
1187 | * This function needs to be called with interrupts disabled. | |
1da177e4 | 1188 | */ |
a6fa8e5a | 1189 | static unsigned long __next_timer_interrupt(struct tvec_base *base) |
1da177e4 | 1190 | { |
1cfd6849 | 1191 | unsigned long timer_jiffies = base->timer_jiffies; |
eaad084b | 1192 | unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA; |
1cfd6849 | 1193 | int index, slot, array, found = 0; |
1da177e4 | 1194 | struct timer_list *nte; |
a6fa8e5a | 1195 | struct tvec *varray[4]; |
1da177e4 LT |
1196 | |
1197 | /* Look for timer events in tv1. */ | |
1cfd6849 | 1198 | index = slot = timer_jiffies & TVR_MASK; |
1da177e4 | 1199 | do { |
1dabbcec | 1200 | hlist_for_each_entry(nte, base->tv1.vec + slot, entry) { |
0eeda71b | 1201 | if (nte->flags & TIMER_DEFERRABLE) |
6819457d | 1202 | continue; |
6e453a67 | 1203 | |
1cfd6849 | 1204 | found = 1; |
1da177e4 | 1205 | expires = nte->expires; |
1cfd6849 TG |
1206 | /* Look at the cascade bucket(s)? */ |
1207 | if (!index || slot < index) | |
1208 | goto cascade; | |
1209 | return expires; | |
1da177e4 | 1210 | } |
1cfd6849 TG |
1211 | slot = (slot + 1) & TVR_MASK; |
1212 | } while (slot != index); | |
1213 | ||
1214 | cascade: | |
1215 | /* Calculate the next cascade event */ | |
1216 | if (index) | |
1217 | timer_jiffies += TVR_SIZE - index; | |
1218 | timer_jiffies >>= TVR_BITS; | |
1da177e4 LT |
1219 | |
1220 | /* Check tv2-tv5. */ | |
1221 | varray[0] = &base->tv2; | |
1222 | varray[1] = &base->tv3; | |
1223 | varray[2] = &base->tv4; | |
1224 | varray[3] = &base->tv5; | |
1cfd6849 TG |
1225 | |
1226 | for (array = 0; array < 4; array++) { | |
a6fa8e5a | 1227 | struct tvec *varp = varray[array]; |
1cfd6849 TG |
1228 | |
1229 | index = slot = timer_jiffies & TVN_MASK; | |
1da177e4 | 1230 | do { |
1dabbcec | 1231 | hlist_for_each_entry(nte, varp->vec + slot, entry) { |
0eeda71b | 1232 | if (nte->flags & TIMER_DEFERRABLE) |
a0419888 JH |
1233 | continue; |
1234 | ||
1cfd6849 | 1235 | found = 1; |
1da177e4 LT |
1236 | if (time_before(nte->expires, expires)) |
1237 | expires = nte->expires; | |
1cfd6849 TG |
1238 | } |
1239 | /* | |
1240 | * Do we still search for the first timer or are | |
1241 | * we looking up the cascade buckets ? | |
1242 | */ | |
1243 | if (found) { | |
1244 | /* Look at the cascade bucket(s)? */ | |
1245 | if (!index || slot < index) | |
1246 | break; | |
1247 | return expires; | |
1248 | } | |
1249 | slot = (slot + 1) & TVN_MASK; | |
1250 | } while (slot != index); | |
1251 | ||
1252 | if (index) | |
1253 | timer_jiffies += TVN_SIZE - index; | |
1254 | timer_jiffies >>= TVN_BITS; | |
1da177e4 | 1255 | } |
1cfd6849 TG |
1256 | return expires; |
1257 | } | |
69239749 | 1258 | |
1cfd6849 TG |
1259 | /* |
1260 | * Check, if the next hrtimer event is before the next timer wheel | |
1261 | * event: | |
1262 | */ | |
c1ad348b | 1263 | static u64 cmp_next_hrtimer_event(u64 basem, u64 expires) |
1cfd6849 | 1264 | { |
c1ad348b | 1265 | u64 nextevt = hrtimer_get_next_event(); |
0662b713 | 1266 | |
9501b6cf | 1267 | /* |
c1ad348b TG |
1268 | * If high resolution timers are enabled |
1269 | * hrtimer_get_next_event() returns KTIME_MAX. | |
9501b6cf | 1270 | */ |
c1ad348b TG |
1271 | if (expires <= nextevt) |
1272 | return expires; | |
eaad084b TG |
1273 | |
1274 | /* | |
c1ad348b TG |
1275 | * If the next timer is already expired, return the tick base |
1276 | * time so the tick is fired immediately. | |
eaad084b | 1277 | */ |
c1ad348b TG |
1278 | if (nextevt <= basem) |
1279 | return basem; | |
eaad084b | 1280 | |
9501b6cf | 1281 | /* |
c1ad348b TG |
1282 | * Round up to the next jiffie. High resolution timers are |
1283 | * off, so the hrtimers are expired in the tick and we need to | |
1284 | * make sure that this tick really expires the timer to avoid | |
1285 | * a ping pong of the nohz stop code. | |
1286 | * | |
1287 | * Use DIV_ROUND_UP_ULL to prevent gcc calling __divdi3 | |
9501b6cf | 1288 | */ |
c1ad348b | 1289 | return DIV_ROUND_UP_ULL(nextevt, TICK_NSEC) * TICK_NSEC; |
1da177e4 | 1290 | } |
1cfd6849 TG |
1291 | |
1292 | /** | |
c1ad348b TG |
1293 | * get_next_timer_interrupt - return the time (clock mono) of the next timer |
1294 | * @basej: base time jiffies | |
1295 | * @basem: base time clock monotonic | |
1296 | * | |
1297 | * Returns the tick aligned clock monotonic time of the next pending | |
1298 | * timer or KTIME_MAX if no timer is pending. | |
1cfd6849 | 1299 | */ |
c1ad348b | 1300 | u64 get_next_timer_interrupt(unsigned long basej, u64 basem) |
1cfd6849 | 1301 | { |
0eeda71b | 1302 | struct tvec_base *base = this_cpu_ptr(&tvec_bases); |
c1ad348b TG |
1303 | u64 expires = KTIME_MAX; |
1304 | unsigned long nextevt; | |
1cfd6849 | 1305 | |
dbd87b5a HC |
1306 | /* |
1307 | * Pretend that there is no timer pending if the cpu is offline. | |
1308 | * Possible pending timers will be migrated later to an active cpu. | |
1309 | */ | |
1310 | if (cpu_is_offline(smp_processor_id())) | |
e40468a5 TG |
1311 | return expires; |
1312 | ||
1cfd6849 | 1313 | spin_lock(&base->lock); |
e40468a5 TG |
1314 | if (base->active_timers) { |
1315 | if (time_before_eq(base->next_timer, base->timer_jiffies)) | |
1316 | base->next_timer = __next_timer_interrupt(base); | |
c1ad348b TG |
1317 | nextevt = base->next_timer; |
1318 | if (time_before_eq(nextevt, basej)) | |
1319 | expires = basem; | |
1320 | else | |
1321 | expires = basem + (nextevt - basej) * TICK_NSEC; | |
e40468a5 | 1322 | } |
1cfd6849 TG |
1323 | spin_unlock(&base->lock); |
1324 | ||
c1ad348b | 1325 | return cmp_next_hrtimer_event(basem, expires); |
1cfd6849 | 1326 | } |
1da177e4 LT |
1327 | #endif |
1328 | ||
1da177e4 | 1329 | /* |
5b4db0c2 | 1330 | * Called from the timer interrupt handler to charge one tick to the current |
1da177e4 LT |
1331 | * process. user_tick is 1 if the tick is user time, 0 for system. |
1332 | */ | |
1333 | void update_process_times(int user_tick) | |
1334 | { | |
1335 | struct task_struct *p = current; | |
1da177e4 LT |
1336 | |
1337 | /* Note: this timer irq context must be accounted for as well. */ | |
fa13a5a1 | 1338 | account_process_tick(p, user_tick); |
1da177e4 | 1339 | run_local_timers(); |
c3377c2d | 1340 | rcu_check_callbacks(user_tick); |
e360adbe PZ |
1341 | #ifdef CONFIG_IRQ_WORK |
1342 | if (in_irq()) | |
76a33061 | 1343 | irq_work_tick(); |
e360adbe | 1344 | #endif |
1da177e4 | 1345 | scheduler_tick(); |
6819457d | 1346 | run_posix_cpu_timers(p); |
1da177e4 LT |
1347 | } |
1348 | ||
1da177e4 LT |
1349 | /* |
1350 | * This function runs timers and the timer-tq in bottom half context. | |
1351 | */ | |
1352 | static void run_timer_softirq(struct softirq_action *h) | |
1353 | { | |
0eeda71b | 1354 | struct tvec_base *base = this_cpu_ptr(&tvec_bases); |
1da177e4 LT |
1355 | |
1356 | if (time_after_eq(jiffies, base->timer_jiffies)) | |
1357 | __run_timers(base); | |
1358 | } | |
1359 | ||
1360 | /* | |
1361 | * Called by the local, per-CPU timer interrupt on SMP. | |
1362 | */ | |
1363 | void run_local_timers(void) | |
1364 | { | |
d3d74453 | 1365 | hrtimer_run_queues(); |
1da177e4 LT |
1366 | raise_softirq(TIMER_SOFTIRQ); |
1367 | } | |
1368 | ||
1da177e4 LT |
1369 | #ifdef __ARCH_WANT_SYS_ALARM |
1370 | ||
1371 | /* | |
1372 | * For backwards compatibility? This can be done in libc so Alpha | |
1373 | * and all newer ports shouldn't need it. | |
1374 | */ | |
58fd3aa2 | 1375 | SYSCALL_DEFINE1(alarm, unsigned int, seconds) |
1da177e4 | 1376 | { |
c08b8a49 | 1377 | return alarm_setitimer(seconds); |
1da177e4 LT |
1378 | } |
1379 | ||
1380 | #endif | |
1381 | ||
1da177e4 LT |
1382 | static void process_timeout(unsigned long __data) |
1383 | { | |
36c8b586 | 1384 | wake_up_process((struct task_struct *)__data); |
1da177e4 LT |
1385 | } |
1386 | ||
1387 | /** | |
1388 | * schedule_timeout - sleep until timeout | |
1389 | * @timeout: timeout value in jiffies | |
1390 | * | |
1391 | * Make the current task sleep until @timeout jiffies have | |
1392 | * elapsed. The routine will return immediately unless | |
1393 | * the current task state has been set (see set_current_state()). | |
1394 | * | |
1395 | * You can set the task state as follows - | |
1396 | * | |
1397 | * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to | |
1398 | * pass before the routine returns. The routine will return 0 | |
1399 | * | |
1400 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | |
1401 | * delivered to the current task. In this case the remaining time | |
1402 | * in jiffies will be returned, or 0 if the timer expired in time | |
1403 | * | |
1404 | * The current task state is guaranteed to be TASK_RUNNING when this | |
1405 | * routine returns. | |
1406 | * | |
1407 | * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule | |
1408 | * the CPU away without a bound on the timeout. In this case the return | |
1409 | * value will be %MAX_SCHEDULE_TIMEOUT. | |
1410 | * | |
1411 | * In all cases the return value is guaranteed to be non-negative. | |
1412 | */ | |
7ad5b3a5 | 1413 | signed long __sched schedule_timeout(signed long timeout) |
1da177e4 LT |
1414 | { |
1415 | struct timer_list timer; | |
1416 | unsigned long expire; | |
1417 | ||
1418 | switch (timeout) | |
1419 | { | |
1420 | case MAX_SCHEDULE_TIMEOUT: | |
1421 | /* | |
1422 | * These two special cases are useful to be comfortable | |
1423 | * in the caller. Nothing more. We could take | |
1424 | * MAX_SCHEDULE_TIMEOUT from one of the negative value | |
1425 | * but I' d like to return a valid offset (>=0) to allow | |
1426 | * the caller to do everything it want with the retval. | |
1427 | */ | |
1428 | schedule(); | |
1429 | goto out; | |
1430 | default: | |
1431 | /* | |
1432 | * Another bit of PARANOID. Note that the retval will be | |
1433 | * 0 since no piece of kernel is supposed to do a check | |
1434 | * for a negative retval of schedule_timeout() (since it | |
1435 | * should never happens anyway). You just have the printk() | |
1436 | * that will tell you if something is gone wrong and where. | |
1437 | */ | |
5b149bcc | 1438 | if (timeout < 0) { |
1da177e4 | 1439 | printk(KERN_ERR "schedule_timeout: wrong timeout " |
5b149bcc AM |
1440 | "value %lx\n", timeout); |
1441 | dump_stack(); | |
1da177e4 LT |
1442 | current->state = TASK_RUNNING; |
1443 | goto out; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | expire = timeout + jiffies; | |
1448 | ||
c6f3a97f | 1449 | setup_timer_on_stack(&timer, process_timeout, (unsigned long)current); |
597d0275 | 1450 | __mod_timer(&timer, expire, false, TIMER_NOT_PINNED); |
1da177e4 LT |
1451 | schedule(); |
1452 | del_singleshot_timer_sync(&timer); | |
1453 | ||
c6f3a97f TG |
1454 | /* Remove the timer from the object tracker */ |
1455 | destroy_timer_on_stack(&timer); | |
1456 | ||
1da177e4 LT |
1457 | timeout = expire - jiffies; |
1458 | ||
1459 | out: | |
1460 | return timeout < 0 ? 0 : timeout; | |
1461 | } | |
1da177e4 LT |
1462 | EXPORT_SYMBOL(schedule_timeout); |
1463 | ||
8a1c1757 AM |
1464 | /* |
1465 | * We can use __set_current_state() here because schedule_timeout() calls | |
1466 | * schedule() unconditionally. | |
1467 | */ | |
64ed93a2 NA |
1468 | signed long __sched schedule_timeout_interruptible(signed long timeout) |
1469 | { | |
a5a0d52c AM |
1470 | __set_current_state(TASK_INTERRUPTIBLE); |
1471 | return schedule_timeout(timeout); | |
64ed93a2 NA |
1472 | } |
1473 | EXPORT_SYMBOL(schedule_timeout_interruptible); | |
1474 | ||
294d5cc2 MW |
1475 | signed long __sched schedule_timeout_killable(signed long timeout) |
1476 | { | |
1477 | __set_current_state(TASK_KILLABLE); | |
1478 | return schedule_timeout(timeout); | |
1479 | } | |
1480 | EXPORT_SYMBOL(schedule_timeout_killable); | |
1481 | ||
64ed93a2 NA |
1482 | signed long __sched schedule_timeout_uninterruptible(signed long timeout) |
1483 | { | |
a5a0d52c AM |
1484 | __set_current_state(TASK_UNINTERRUPTIBLE); |
1485 | return schedule_timeout(timeout); | |
64ed93a2 NA |
1486 | } |
1487 | EXPORT_SYMBOL(schedule_timeout_uninterruptible); | |
1488 | ||
1da177e4 | 1489 | #ifdef CONFIG_HOTPLUG_CPU |
1dabbcec | 1490 | static void migrate_timer_list(struct tvec_base *new_base, struct hlist_head *head) |
1da177e4 LT |
1491 | { |
1492 | struct timer_list *timer; | |
0eeda71b | 1493 | int cpu = new_base->cpu; |
1da177e4 | 1494 | |
1dabbcec TG |
1495 | while (!hlist_empty(head)) { |
1496 | timer = hlist_entry(head->first, struct timer_list, entry); | |
99d5f3aa | 1497 | /* We ignore the accounting on the dying cpu */ |
ec44bc7a | 1498 | detach_timer(timer, false); |
0eeda71b | 1499 | timer->flags = (timer->flags & ~TIMER_BASEMASK) | cpu; |
1da177e4 | 1500 | internal_add_timer(new_base, timer); |
1da177e4 | 1501 | } |
1da177e4 LT |
1502 | } |
1503 | ||
0db0628d | 1504 | static void migrate_timers(int cpu) |
1da177e4 | 1505 | { |
a6fa8e5a PM |
1506 | struct tvec_base *old_base; |
1507 | struct tvec_base *new_base; | |
1da177e4 LT |
1508 | int i; |
1509 | ||
1510 | BUG_ON(cpu_online(cpu)); | |
0eeda71b TG |
1511 | old_base = per_cpu_ptr(&tvec_bases, cpu); |
1512 | new_base = this_cpu_ptr(&tvec_bases); | |
d82f0b0f ON |
1513 | /* |
1514 | * The caller is globally serialized and nobody else | |
1515 | * takes two locks at once, deadlock is not possible. | |
1516 | */ | |
1517 | spin_lock_irq(&new_base->lock); | |
0d180406 | 1518 | spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); |
3691c519 ON |
1519 | |
1520 | BUG_ON(old_base->running_timer); | |
1da177e4 | 1521 | |
1da177e4 | 1522 | for (i = 0; i < TVR_SIZE; i++) |
55c888d6 ON |
1523 | migrate_timer_list(new_base, old_base->tv1.vec + i); |
1524 | for (i = 0; i < TVN_SIZE; i++) { | |
1525 | migrate_timer_list(new_base, old_base->tv2.vec + i); | |
1526 | migrate_timer_list(new_base, old_base->tv3.vec + i); | |
1527 | migrate_timer_list(new_base, old_base->tv4.vec + i); | |
1528 | migrate_timer_list(new_base, old_base->tv5.vec + i); | |
1529 | } | |
1530 | ||
8def9060 VK |
1531 | old_base->active_timers = 0; |
1532 | old_base->all_timers = 0; | |
1533 | ||
0d180406 | 1534 | spin_unlock(&old_base->lock); |
d82f0b0f | 1535 | spin_unlock_irq(&new_base->lock); |
1da177e4 | 1536 | } |
1da177e4 | 1537 | |
0db0628d | 1538 | static int timer_cpu_notify(struct notifier_block *self, |
1da177e4 LT |
1539 | unsigned long action, void *hcpu) |
1540 | { | |
8def9060 | 1541 | switch (action) { |
1da177e4 | 1542 | case CPU_DEAD: |
8bb78442 | 1543 | case CPU_DEAD_FROZEN: |
8def9060 | 1544 | migrate_timers((long)hcpu); |
1da177e4 | 1545 | break; |
1da177e4 LT |
1546 | default: |
1547 | break; | |
1548 | } | |
3650b57f | 1549 | |
1da177e4 LT |
1550 | return NOTIFY_OK; |
1551 | } | |
1552 | ||
3650b57f PZ |
1553 | static inline void timer_register_cpu_notifier(void) |
1554 | { | |
1555 | cpu_notifier(timer_cpu_notify, 0); | |
1556 | } | |
1557 | #else | |
1558 | static inline void timer_register_cpu_notifier(void) { } | |
1559 | #endif /* CONFIG_HOTPLUG_CPU */ | |
1da177e4 | 1560 | |
0eeda71b | 1561 | static void __init init_timer_cpu(int cpu) |
8def9060 | 1562 | { |
0eeda71b | 1563 | struct tvec_base *base = per_cpu_ptr(&tvec_bases, cpu); |
3650b57f | 1564 | |
8def9060 | 1565 | base->cpu = cpu; |
8def9060 VK |
1566 | spin_lock_init(&base->lock); |
1567 | ||
8def9060 VK |
1568 | base->timer_jiffies = jiffies; |
1569 | base->next_timer = base->timer_jiffies; | |
1570 | } | |
1571 | ||
1572 | static void __init init_timer_cpus(void) | |
1da177e4 | 1573 | { |
8def9060 VK |
1574 | int cpu; |
1575 | ||
0eeda71b TG |
1576 | for_each_possible_cpu(cpu) |
1577 | init_timer_cpu(cpu); | |
8def9060 | 1578 | } |
e52b1db3 | 1579 | |
8def9060 VK |
1580 | void __init init_timers(void) |
1581 | { | |
8def9060 | 1582 | init_timer_cpus(); |
c24a4a36 | 1583 | init_timer_stats(); |
3650b57f | 1584 | timer_register_cpu_notifier(); |
962cf36c | 1585 | open_softirq(TIMER_SOFTIRQ, run_timer_softirq); |
1da177e4 LT |
1586 | } |
1587 | ||
1da177e4 LT |
1588 | /** |
1589 | * msleep - sleep safely even with waitqueue interruptions | |
1590 | * @msecs: Time in milliseconds to sleep for | |
1591 | */ | |
1592 | void msleep(unsigned int msecs) | |
1593 | { | |
1594 | unsigned long timeout = msecs_to_jiffies(msecs) + 1; | |
1595 | ||
75bcc8c5 NA |
1596 | while (timeout) |
1597 | timeout = schedule_timeout_uninterruptible(timeout); | |
1da177e4 LT |
1598 | } |
1599 | ||
1600 | EXPORT_SYMBOL(msleep); | |
1601 | ||
1602 | /** | |
96ec3efd | 1603 | * msleep_interruptible - sleep waiting for signals |
1da177e4 LT |
1604 | * @msecs: Time in milliseconds to sleep for |
1605 | */ | |
1606 | unsigned long msleep_interruptible(unsigned int msecs) | |
1607 | { | |
1608 | unsigned long timeout = msecs_to_jiffies(msecs) + 1; | |
1609 | ||
75bcc8c5 NA |
1610 | while (timeout && !signal_pending(current)) |
1611 | timeout = schedule_timeout_interruptible(timeout); | |
1da177e4 LT |
1612 | return jiffies_to_msecs(timeout); |
1613 | } | |
1614 | ||
1615 | EXPORT_SYMBOL(msleep_interruptible); | |
5e7f5a17 | 1616 | |
6deba083 | 1617 | static void __sched do_usleep_range(unsigned long min, unsigned long max) |
5e7f5a17 PP |
1618 | { |
1619 | ktime_t kmin; | |
1620 | unsigned long delta; | |
1621 | ||
1622 | kmin = ktime_set(0, min * NSEC_PER_USEC); | |
1623 | delta = (max - min) * NSEC_PER_USEC; | |
6deba083 | 1624 | schedule_hrtimeout_range(&kmin, delta, HRTIMER_MODE_REL); |
5e7f5a17 PP |
1625 | } |
1626 | ||
1627 | /** | |
1628 | * usleep_range - Drop in replacement for udelay where wakeup is flexible | |
1629 | * @min: Minimum time in usecs to sleep | |
1630 | * @max: Maximum time in usecs to sleep | |
1631 | */ | |
2ad5d327 | 1632 | void __sched usleep_range(unsigned long min, unsigned long max) |
5e7f5a17 PP |
1633 | { |
1634 | __set_current_state(TASK_UNINTERRUPTIBLE); | |
1635 | do_usleep_range(min, max); | |
1636 | } | |
1637 | EXPORT_SYMBOL(usleep_range); |