aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/timer.c')
-rw-r--r--kernel/timer.c118
1 files changed, 55 insertions, 63 deletions
diff --git a/kernel/timer.c b/kernel/timer.c
index 8c5e7b908c68..367d00858482 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -63,6 +63,7 @@ EXPORT_SYMBOL(jiffies_64);
63#define TVR_SIZE (1 << TVR_BITS) 63#define TVR_SIZE (1 << TVR_BITS)
64#define TVN_MASK (TVN_SIZE - 1) 64#define TVN_MASK (TVN_SIZE - 1)
65#define TVR_MASK (TVR_SIZE - 1) 65#define TVR_MASK (TVR_SIZE - 1)
66#define MAX_TVAL ((unsigned long)((1ULL << (TVR_BITS + 4*TVN_BITS)) - 1))
66 67
67struct tvec { 68struct tvec {
68 struct list_head vec[TVN_SIZE]; 69 struct list_head vec[TVN_SIZE];
@@ -92,24 +93,25 @@ static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
92/* Functions below help us manage 'deferrable' flag */ 93/* Functions below help us manage 'deferrable' flag */
93static inline unsigned int tbase_get_deferrable(struct tvec_base *base) 94static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
94{ 95{
95 return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG); 96 return ((unsigned int)(unsigned long)base & TIMER_DEFERRABLE);
96} 97}
97 98
98static inline struct tvec_base *tbase_get_base(struct tvec_base *base) 99static inline unsigned int tbase_get_irqsafe(struct tvec_base *base)
99{ 100{
100 return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG)); 101 return ((unsigned int)(unsigned long)base & TIMER_IRQSAFE);
101} 102}
102 103
103static inline void timer_set_deferrable(struct timer_list *timer) 104static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
104{ 105{
105 timer->base = TBASE_MAKE_DEFERRED(timer->base); 106 return ((struct tvec_base *)((unsigned long)base & ~TIMER_FLAG_MASK));
106} 107}
107 108
108static inline void 109static inline void
109timer_set_base(struct timer_list *timer, struct tvec_base *new_base) 110timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
110{ 111{
111 timer->base = (struct tvec_base *)((unsigned long)(new_base) | 112 unsigned long flags = (unsigned long)timer->base & TIMER_FLAG_MASK;
112 tbase_get_deferrable(timer->base)); 113
114 timer->base = (struct tvec_base *)((unsigned long)(new_base) | flags);
113} 115}
114 116
115static unsigned long round_jiffies_common(unsigned long j, int cpu, 117static unsigned long round_jiffies_common(unsigned long j, int cpu,
@@ -358,11 +360,12 @@ __internal_add_timer(struct tvec_base *base, struct timer_list *timer)
358 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); 360 vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
359 } else { 361 } else {
360 int i; 362 int i;
361 /* If the timeout is larger than 0xffffffff on 64-bit 363 /* If the timeout is larger than MAX_TVAL (on 64-bit
362 * architectures then we use the maximum timeout: 364 * architectures or with CONFIG_BASE_SMALL=1) then we
365 * use the maximum timeout.
363 */ 366 */
364 if (idx > 0xffffffffUL) { 367 if (idx > MAX_TVAL) {
365 idx = 0xffffffffUL; 368 idx = MAX_TVAL;
366 expires = idx + base->timer_jiffies; 369 expires = idx + base->timer_jiffies;
367 } 370 }
368 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK; 371 i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
@@ -563,16 +566,14 @@ static inline void debug_timer_assert_init(struct timer_list *timer)
563 debug_object_assert_init(timer, &timer_debug_descr); 566 debug_object_assert_init(timer, &timer_debug_descr);
564} 567}
565 568
566static void __init_timer(struct timer_list *timer, 569static void do_init_timer(struct timer_list *timer, unsigned int flags,
567 const char *name, 570 const char *name, struct lock_class_key *key);
568 struct lock_class_key *key);
569 571
570void init_timer_on_stack_key(struct timer_list *timer, 572void init_timer_on_stack_key(struct timer_list *timer, unsigned int flags,
571 const char *name, 573 const char *name, struct lock_class_key *key)
572 struct lock_class_key *key)
573{ 574{
574 debug_object_init_on_stack(timer, &timer_debug_descr); 575 debug_object_init_on_stack(timer, &timer_debug_descr);
575 __init_timer(timer, name, key); 576 do_init_timer(timer, flags, name, key);
576} 577}
577EXPORT_SYMBOL_GPL(init_timer_on_stack_key); 578EXPORT_SYMBOL_GPL(init_timer_on_stack_key);
578 579
@@ -613,12 +614,13 @@ static inline void debug_assert_init(struct timer_list *timer)
613 debug_timer_assert_init(timer); 614 debug_timer_assert_init(timer);
614} 615}
615 616
616static void __init_timer(struct timer_list *timer, 617static void do_init_timer(struct timer_list *timer, unsigned int flags,
617 const char *name, 618 const char *name, struct lock_class_key *key)
618 struct lock_class_key *key)
619{ 619{
620 struct tvec_base *base = __raw_get_cpu_var(tvec_bases);
621
620 timer->entry.next = NULL; 622 timer->entry.next = NULL;
621 timer->base = __raw_get_cpu_var(tvec_bases); 623 timer->base = (void *)((unsigned long)base | flags);
622 timer->slack = -1; 624 timer->slack = -1;
623#ifdef CONFIG_TIMER_STATS 625#ifdef CONFIG_TIMER_STATS
624 timer->start_site = NULL; 626 timer->start_site = NULL;
@@ -628,22 +630,10 @@ static void __init_timer(struct timer_list *timer,
628 lockdep_init_map(&timer->lockdep_map, name, key, 0); 630 lockdep_init_map(&timer->lockdep_map, name, key, 0);
629} 631}
630 632
631void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
632 const char *name,
633 struct lock_class_key *key,
634 void (*function)(unsigned long),
635 unsigned long data)
636{
637 timer->function = function;
638 timer->data = data;
639 init_timer_on_stack_key(timer, name, key);
640 timer_set_deferrable(timer);
641}
642EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
643
644/** 633/**
645 * init_timer_key - initialize a timer 634 * init_timer_key - initialize a timer
646 * @timer: the timer to be initialized 635 * @timer: the timer to be initialized
636 * @flags: timer flags
647 * @name: name of the timer 637 * @name: name of the timer
648 * @key: lockdep class key of the fake lock used for tracking timer 638 * @key: lockdep class key of the fake lock used for tracking timer
649 * sync lock dependencies 639 * sync lock dependencies
@@ -651,24 +641,14 @@ EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
651 * init_timer_key() must be done to a timer prior calling *any* of the 641 * init_timer_key() must be done to a timer prior calling *any* of the
652 * other timer functions. 642 * other timer functions.
653 */ 643 */
654void init_timer_key(struct timer_list *timer, 644void init_timer_key(struct timer_list *timer, unsigned int flags,
655 const char *name, 645 const char *name, struct lock_class_key *key)
656 struct lock_class_key *key)
657{ 646{
658 debug_init(timer); 647 debug_init(timer);
659 __init_timer(timer, name, key); 648 do_init_timer(timer, flags, name, key);
660} 649}
661EXPORT_SYMBOL(init_timer_key); 650EXPORT_SYMBOL(init_timer_key);
662 651
663void init_timer_deferrable_key(struct timer_list *timer,
664 const char *name,
665 struct lock_class_key *key)
666{
667 init_timer_key(timer, name, key);
668 timer_set_deferrable(timer);
669}
670EXPORT_SYMBOL(init_timer_deferrable_key);
671
672static inline void detach_timer(struct timer_list *timer, bool clear_pending) 652static inline void detach_timer(struct timer_list *timer, bool clear_pending)
673{ 653{
674 struct list_head *entry = &timer->entry; 654 struct list_head *entry = &timer->entry;
@@ -686,7 +666,7 @@ detach_expired_timer(struct timer_list *timer, struct tvec_base *base)
686{ 666{
687 detach_timer(timer, true); 667 detach_timer(timer, true);
688 if (!tbase_get_deferrable(timer->base)) 668 if (!tbase_get_deferrable(timer->base))
689 timer->base->active_timers--; 669 base->active_timers--;
690} 670}
691 671
692static int detach_if_pending(struct timer_list *timer, struct tvec_base *base, 672static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
@@ -697,7 +677,7 @@ static int detach_if_pending(struct timer_list *timer, struct tvec_base *base,
697 677
698 detach_timer(timer, clear_pending); 678 detach_timer(timer, clear_pending);
699 if (!tbase_get_deferrable(timer->base)) { 679 if (!tbase_get_deferrable(timer->base)) {
700 timer->base->active_timers--; 680 base->active_timers--;
701 if (timer->expires == base->next_timer) 681 if (timer->expires == base->next_timer)
702 base->next_timer = base->timer_jiffies; 682 base->next_timer = base->timer_jiffies;
703 } 683 }
@@ -1029,14 +1009,14 @@ EXPORT_SYMBOL(try_to_del_timer_sync);
1029 * 1009 *
1030 * Synchronization rules: Callers must prevent restarting of the timer, 1010 * Synchronization rules: Callers must prevent restarting of the timer,
1031 * otherwise this function is meaningless. It must not be called from 1011 * otherwise this function is meaningless. It must not be called from
1032 * interrupt contexts. The caller must not hold locks which would prevent 1012 * interrupt contexts unless the timer is an irqsafe one. The caller must
1033 * completion of the timer's handler. The timer's handler must not call 1013 * not hold locks which would prevent completion of the timer's
1034 * add_timer_on(). Upon exit the timer is not queued and the handler is 1014 * handler. The timer's handler must not call add_timer_on(). Upon exit the
1035 * not running on any CPU. 1015 * timer is not queued and the handler is not running on any CPU.
1036 * 1016 *
1037 * Note: You must not hold locks that are held in interrupt context 1017 * Note: For !irqsafe timers, you must not hold locks that are held in
1038 * while calling this function. Even if the lock has nothing to do 1018 * interrupt context while calling this function. Even if the lock has
1039 * with the timer in question. Here's why: 1019 * nothing to do with the timer in question. Here's why:
1040 * 1020 *
1041 * CPU0 CPU1 1021 * CPU0 CPU1
1042 * ---- ---- 1022 * ---- ----
@@ -1073,7 +1053,7 @@ int del_timer_sync(struct timer_list *timer)
1073 * don't use it in hardirq context, because it 1053 * don't use it in hardirq context, because it
1074 * could lead to deadlock. 1054 * could lead to deadlock.
1075 */ 1055 */
1076 WARN_ON(in_irq()); 1056 WARN_ON(in_irq() && !tbase_get_irqsafe(timer->base));
1077 for (;;) { 1057 for (;;) {
1078 int ret = try_to_del_timer_sync(timer); 1058 int ret = try_to_del_timer_sync(timer);
1079 if (ret >= 0) 1059 if (ret >= 0)
@@ -1180,19 +1160,27 @@ static inline void __run_timers(struct tvec_base *base)
1180 while (!list_empty(head)) { 1160 while (!list_empty(head)) {
1181 void (*fn)(unsigned long); 1161 void (*fn)(unsigned long);
1182 unsigned long data; 1162 unsigned long data;
1163 bool irqsafe;
1183 1164
1184 timer = list_first_entry(head, struct timer_list,entry); 1165 timer = list_first_entry(head, struct timer_list,entry);
1185 fn = timer->function; 1166 fn = timer->function;
1186 data = timer->data; 1167 data = timer->data;
1168 irqsafe = tbase_get_irqsafe(timer->base);
1187 1169
1188 timer_stats_account_timer(timer); 1170 timer_stats_account_timer(timer);
1189 1171
1190 base->running_timer = timer; 1172 base->running_timer = timer;
1191 detach_expired_timer(timer, base); 1173 detach_expired_timer(timer, base);
1192 1174
1193 spin_unlock_irq(&base->lock); 1175 if (irqsafe) {
1194 call_timer_fn(timer, fn, data); 1176 spin_unlock(&base->lock);
1195 spin_lock_irq(&base->lock); 1177 call_timer_fn(timer, fn, data);
1178 spin_lock(&base->lock);
1179 } else {
1180 spin_unlock_irq(&base->lock);
1181 call_timer_fn(timer, fn, data);
1182 spin_lock_irq(&base->lock);
1183 }
1196 } 1184 }
1197 } 1185 }
1198 base->running_timer = NULL; 1186 base->running_timer = NULL;
@@ -1791,9 +1779,13 @@ static struct notifier_block __cpuinitdata timers_nb = {
1791 1779
1792void __init init_timers(void) 1780void __init init_timers(void)
1793{ 1781{
1794 int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, 1782 int err;
1795 (void *)(long)smp_processor_id()); 1783
1784 /* ensure there are enough low bits for flags in timer->base pointer */
1785 BUILD_BUG_ON(__alignof__(struct tvec_base) & TIMER_FLAG_MASK);
1796 1786
1787 err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
1788 (void *)(long)smp_processor_id());
1797 init_timer_stats(); 1789 init_timer_stats();
1798 1790
1799 BUG_ON(err != NOTIFY_OK); 1791 BUG_ON(err != NOTIFY_OK);