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