aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/timer.c')
-rw-r--r--kernel/timer.c112
1 files changed, 58 insertions, 54 deletions
diff --git a/kernel/timer.c b/kernel/timer.c
index 97bf05baade7..8cff36119e4d 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -37,7 +37,7 @@
37#include <linux/delay.h> 37#include <linux/delay.h>
38#include <linux/tick.h> 38#include <linux/tick.h>
39#include <linux/kallsyms.h> 39#include <linux/kallsyms.h>
40#include <linux/perf_event.h> 40#include <linux/irq_work.h>
41#include <linux/sched.h> 41#include <linux/sched.h>
42#include <linux/slab.h> 42#include <linux/slab.h>
43 43
@@ -88,18 +88,6 @@ struct tvec_base boot_tvec_bases;
88EXPORT_SYMBOL(boot_tvec_bases); 88EXPORT_SYMBOL(boot_tvec_bases);
89static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases; 89static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
90 90
91/*
92 * Note that all tvec_bases are 2 byte aligned and lower bit of
93 * base in timer_list is guaranteed to be zero. Use the LSB to
94 * indicate whether the timer is deferrable.
95 *
96 * A deferrable timer will work normally when the system is busy, but
97 * will not cause a CPU to come out of idle just to service it; instead,
98 * the timer will be serviced when the CPU eventually wakes up with a
99 * subsequent non-deferrable timer.
100 */
101#define TBASE_DEFERRABLE_FLAG (0x1)
102
103/* Functions below help us manage 'deferrable' flag */ 91/* Functions below help us manage 'deferrable' flag */
104static inline unsigned int tbase_get_deferrable(struct tvec_base *base) 92static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
105{ 93{
@@ -113,8 +101,7 @@ static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
113 101
114static inline void timer_set_deferrable(struct timer_list *timer) 102static inline void timer_set_deferrable(struct timer_list *timer)
115{ 103{
116 timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | 104 timer->base = TBASE_MAKE_DEFERRED(timer->base);
117 TBASE_DEFERRABLE_FLAG));
118} 105}
119 106
120static inline void 107static inline void
@@ -343,15 +330,6 @@ void set_timer_slack(struct timer_list *timer, int slack_hz)
343} 330}
344EXPORT_SYMBOL_GPL(set_timer_slack); 331EXPORT_SYMBOL_GPL(set_timer_slack);
345 332
346
347static inline void set_running_timer(struct tvec_base *base,
348 struct timer_list *timer)
349{
350#ifdef CONFIG_SMP
351 base->running_timer = timer;
352#endif
353}
354
355static void internal_add_timer(struct tvec_base *base, struct timer_list *timer) 333static void internal_add_timer(struct tvec_base *base, struct timer_list *timer)
356{ 334{
357 unsigned long expires = timer->expires; 335 unsigned long expires = timer->expires;
@@ -426,6 +404,11 @@ static void timer_stats_account_timer(struct timer_list *timer) {}
426 404
427static struct debug_obj_descr timer_debug_descr; 405static struct debug_obj_descr timer_debug_descr;
428 406
407static void *timer_debug_hint(void *addr)
408{
409 return ((struct timer_list *) addr)->function;
410}
411
429/* 412/*
430 * fixup_init is called when: 413 * fixup_init is called when:
431 * - an active object is initialized 414 * - an active object is initialized
@@ -499,6 +482,7 @@ static int timer_fixup_free(void *addr, enum debug_obj_state state)
499 482
500static struct debug_obj_descr timer_debug_descr = { 483static struct debug_obj_descr timer_debug_descr = {
501 .name = "timer_list", 484 .name = "timer_list",
485 .debug_hint = timer_debug_hint,
502 .fixup_init = timer_fixup_init, 486 .fixup_init = timer_fixup_init,
503 .fixup_activate = timer_fixup_activate, 487 .fixup_activate = timer_fixup_activate,
504 .fixup_free = timer_fixup_free, 488 .fixup_free = timer_fixup_free,
@@ -765,16 +749,15 @@ unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
765 unsigned long expires_limit, mask; 749 unsigned long expires_limit, mask;
766 int bit; 750 int bit;
767 751
768 expires_limit = expires;
769
770 if (timer->slack >= 0) { 752 if (timer->slack >= 0) {
771 expires_limit = expires + timer->slack; 753 expires_limit = expires + timer->slack;
772 } else { 754 } else {
773 unsigned long now = jiffies; 755 long delta = expires - jiffies;
756
757 if (delta < 256)
758 return expires;
774 759
775 /* No slack, if already expired else auto slack 0.4% */ 760 expires_limit = expires + delta / 256;
776 if (time_after(expires, now))
777 expires_limit = expires + (expires - now)/256;
778 } 761 }
779 mask = expires ^ expires_limit; 762 mask = expires ^ expires_limit;
780 if (mask == 0) 763 if (mask == 0)
@@ -811,6 +794,8 @@ unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
811 */ 794 */
812int mod_timer(struct timer_list *timer, unsigned long expires) 795int mod_timer(struct timer_list *timer, unsigned long expires)
813{ 796{
797 expires = apply_slack(timer, expires);
798
814 /* 799 /*
815 * This is a common optimization triggered by the 800 * This is a common optimization triggered by the
816 * networking code - if the timer is re-modified 801 * networking code - if the timer is re-modified
@@ -819,8 +804,6 @@ int mod_timer(struct timer_list *timer, unsigned long expires)
819 if (timer_pending(timer) && timer->expires == expires) 804 if (timer_pending(timer) && timer->expires == expires)
820 return 1; 805 return 1;
821 806
822 expires = apply_slack(timer, expires);
823
824 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED); 807 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
825} 808}
826EXPORT_SYMBOL(mod_timer); 809EXPORT_SYMBOL(mod_timer);
@@ -936,15 +919,12 @@ int del_timer(struct timer_list *timer)
936} 919}
937EXPORT_SYMBOL(del_timer); 920EXPORT_SYMBOL(del_timer);
938 921
939#ifdef CONFIG_SMP
940/** 922/**
941 * try_to_del_timer_sync - Try to deactivate a timer 923 * try_to_del_timer_sync - Try to deactivate a timer
942 * @timer: timer do del 924 * @timer: timer do del
943 * 925 *
944 * This function tries to deactivate a timer. Upon successful (ret >= 0) 926 * This function tries to deactivate a timer. Upon successful (ret >= 0)
945 * exit the timer is not queued and the handler is not running on any CPU. 927 * exit the timer is not queued and the handler is not running on any CPU.
946 *
947 * It must not be called from interrupt contexts.
948 */ 928 */
949int try_to_del_timer_sync(struct timer_list *timer) 929int try_to_del_timer_sync(struct timer_list *timer)
950{ 930{
@@ -973,6 +953,7 @@ out:
973} 953}
974EXPORT_SYMBOL(try_to_del_timer_sync); 954EXPORT_SYMBOL(try_to_del_timer_sync);
975 955
956#ifdef CONFIG_SMP
976/** 957/**
977 * del_timer_sync - deactivate a timer and wait for the handler to finish. 958 * del_timer_sync - deactivate a timer and wait for the handler to finish.
978 * @timer: the timer to be deactivated 959 * @timer: the timer to be deactivated
@@ -988,6 +969,25 @@ EXPORT_SYMBOL(try_to_del_timer_sync);
988 * add_timer_on(). Upon exit the timer is not queued and the handler is 969 * add_timer_on(). Upon exit the timer is not queued and the handler is
989 * not running on any CPU. 970 * not running on any CPU.
990 * 971 *
972 * Note: You must not hold locks that are held in interrupt context
973 * while calling this function. Even if the lock has nothing to do
974 * with the timer in question. Here's why:
975 *
976 * CPU0 CPU1
977 * ---- ----
978 * <SOFTIRQ>
979 * call_timer_fn();
980 * base->running_timer = mytimer;
981 * spin_lock_irq(somelock);
982 * <IRQ>
983 * spin_lock(somelock);
984 * del_timer_sync(mytimer);
985 * while (base->running_timer == mytimer);
986 *
987 * Now del_timer_sync() will never return and never release somelock.
988 * The interrupt on the other CPU is waiting to grab somelock but
989 * it has interrupted the softirq that CPU0 is waiting to finish.
990 *
991 * The function returns whether it has deactivated a pending timer or not. 991 * The function returns whether it has deactivated a pending timer or not.
992 */ 992 */
993int del_timer_sync(struct timer_list *timer) 993int del_timer_sync(struct timer_list *timer)
@@ -995,12 +995,20 @@ int del_timer_sync(struct timer_list *timer)
995#ifdef CONFIG_LOCKDEP 995#ifdef CONFIG_LOCKDEP
996 unsigned long flags; 996 unsigned long flags;
997 997
998 /*
999 * If lockdep gives a backtrace here, please reference
1000 * the synchronization rules above.
1001 */
998 local_irq_save(flags); 1002 local_irq_save(flags);
999 lock_map_acquire(&timer->lockdep_map); 1003 lock_map_acquire(&timer->lockdep_map);
1000 lock_map_release(&timer->lockdep_map); 1004 lock_map_release(&timer->lockdep_map);
1001 local_irq_restore(flags); 1005 local_irq_restore(flags);
1002#endif 1006#endif
1003 1007 /*
1008 * don't use it in hardirq context, because it
1009 * could lead to deadlock.
1010 */
1011 WARN_ON(in_irq());
1004 for (;;) { 1012 for (;;) {
1005 int ret = try_to_del_timer_sync(timer); 1013 int ret = try_to_del_timer_sync(timer);
1006 if (ret >= 0) 1014 if (ret >= 0)
@@ -1111,7 +1119,7 @@ static inline void __run_timers(struct tvec_base *base)
1111 1119
1112 timer_stats_account_timer(timer); 1120 timer_stats_account_timer(timer);
1113 1121
1114 set_running_timer(base, timer); 1122 base->running_timer = timer;
1115 detach_timer(timer, 1); 1123 detach_timer(timer, 1);
1116 1124
1117 spin_unlock_irq(&base->lock); 1125 spin_unlock_irq(&base->lock);
@@ -1119,7 +1127,7 @@ static inline void __run_timers(struct tvec_base *base)
1119 spin_lock_irq(&base->lock); 1127 spin_lock_irq(&base->lock);
1120 } 1128 }
1121 } 1129 }
1122 set_running_timer(base, NULL); 1130 base->running_timer = NULL;
1123 spin_unlock_irq(&base->lock); 1131 spin_unlock_irq(&base->lock);
1124} 1132}
1125 1133
@@ -1249,9 +1257,15 @@ static unsigned long cmp_next_hrtimer_event(unsigned long now,
1249 */ 1257 */
1250unsigned long get_next_timer_interrupt(unsigned long now) 1258unsigned long get_next_timer_interrupt(unsigned long now)
1251{ 1259{
1252 struct tvec_base *base = __get_cpu_var(tvec_bases); 1260 struct tvec_base *base = __this_cpu_read(tvec_bases);
1253 unsigned long expires; 1261 unsigned long expires;
1254 1262
1263 /*
1264 * Pretend that there is no timer pending if the cpu is offline.
1265 * Possible pending timers will be migrated later to an active cpu.
1266 */
1267 if (cpu_is_offline(smp_processor_id()))
1268 return now + NEXT_TIMER_MAX_DELTA;
1255 spin_lock(&base->lock); 1269 spin_lock(&base->lock);
1256 if (time_before_eq(base->next_timer, base->timer_jiffies)) 1270 if (time_before_eq(base->next_timer, base->timer_jiffies))
1257 base->next_timer = __next_timer_interrupt(base); 1271 base->next_timer = __next_timer_interrupt(base);
@@ -1279,7 +1293,10 @@ void update_process_times(int user_tick)
1279 run_local_timers(); 1293 run_local_timers();
1280 rcu_check_callbacks(cpu, user_tick); 1294 rcu_check_callbacks(cpu, user_tick);
1281 printk_tick(); 1295 printk_tick();
1282 perf_event_do_pending(); 1296#ifdef CONFIG_IRQ_WORK
1297 if (in_irq())
1298 irq_work_run();
1299#endif
1283 scheduler_tick(); 1300 scheduler_tick();
1284 run_posix_cpu_timers(p); 1301 run_posix_cpu_timers(p);
1285} 1302}
@@ -1289,7 +1306,7 @@ void update_process_times(int user_tick)
1289 */ 1306 */
1290static void run_timer_softirq(struct softirq_action *h) 1307static void run_timer_softirq(struct softirq_action *h)
1291{ 1308{
1292 struct tvec_base *base = __get_cpu_var(tvec_bases); 1309 struct tvec_base *base = __this_cpu_read(tvec_bases);
1293 1310
1294 hrtimer_run_pending(); 1311 hrtimer_run_pending();
1295 1312
@@ -1306,19 +1323,6 @@ void run_local_timers(void)
1306 raise_softirq(TIMER_SOFTIRQ); 1323 raise_softirq(TIMER_SOFTIRQ);
1307} 1324}
1308 1325
1309/*
1310 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1311 * without sampling the sequence number in xtime_lock.
1312 * jiffies is defined in the linker script...
1313 */
1314
1315void do_timer(unsigned long ticks)
1316{
1317 jiffies_64 += ticks;
1318 update_wall_time();
1319 calc_global_load();
1320}
1321
1322#ifdef __ARCH_WANT_SYS_ALARM 1326#ifdef __ARCH_WANT_SYS_ALARM
1323 1327
1324/* 1328/*