aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/timer.c')
-rw-r--r--kernel/timer.c151
1 files changed, 110 insertions, 41 deletions
diff --git a/kernel/timer.c b/kernel/timer.c
index c61a7949387f..2454172a80d3 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -39,6 +39,7 @@
39#include <linux/kallsyms.h> 39#include <linux/kallsyms.h>
40#include <linux/perf_event.h> 40#include <linux/perf_event.h>
41#include <linux/sched.h> 41#include <linux/sched.h>
42#include <linux/slab.h>
42 43
43#include <asm/uaccess.h> 44#include <asm/uaccess.h>
44#include <asm/unistd.h> 45#include <asm/unistd.h>
@@ -318,6 +319,24 @@ unsigned long round_jiffies_up_relative(unsigned long j)
318} 319}
319EXPORT_SYMBOL_GPL(round_jiffies_up_relative); 320EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
320 321
322/**
323 * set_timer_slack - set the allowed slack for a timer
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
321 340
322static inline void set_running_timer(struct tvec_base *base, 341static inline void set_running_timer(struct tvec_base *base,
323 struct timer_list *timer) 342 struct timer_list *timer)
@@ -549,6 +568,7 @@ static void __init_timer(struct timer_list *timer,
549{ 568{
550 timer->entry.next = NULL; 569 timer->entry.next = NULL;
551 timer->base = __raw_get_cpu_var(tvec_bases); 570 timer->base = __raw_get_cpu_var(tvec_bases);
571 timer->slack = -1;
552#ifdef CONFIG_TIMER_STATS 572#ifdef CONFIG_TIMER_STATS
553 timer->start_site = NULL; 573 timer->start_site = NULL;
554 timer->start_pid = -1; 574 timer->start_pid = -1;
@@ -714,6 +734,46 @@ int mod_timer_pending(struct timer_list *timer, unsigned long expires)
714} 734}
715EXPORT_SYMBOL(mod_timer_pending); 735EXPORT_SYMBOL(mod_timer_pending);
716 736
737/*
738 * Decide where to put the timer while taking the slack into account
739 *
740 * Algorithm:
741 * 1) calculate the maximum (absolute) time
742 * 2) calculate the highest bit where the expires and new max are different
743 * 3) use this bit to make a mask
744 * 4) use the bitmask to round down the maximum time, so that all last
745 * bits are zeros
746 */
747static inline
748unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
749{
750 unsigned long expires_limit, mask;
751 int bit;
752
753 expires_limit = expires;
754
755 if (timer->slack >= 0) {
756 expires_limit = expires + timer->slack;
757 } else {
758 unsigned long now = jiffies;
759
760 /* No slack, if already expired else auto slack 0.4% */
761 if (time_after(expires, now))
762 expires_limit = expires + (expires - now)/256;
763 }
764 mask = expires ^ expires_limit;
765 if (mask == 0)
766 return expires;
767
768 bit = find_last_bit(&mask, BITS_PER_LONG);
769
770 mask = (1 << bit) - 1;
771
772 expires_limit = expires_limit & ~(mask);
773
774 return expires_limit;
775}
776
717/** 777/**
718 * mod_timer - modify a timer's timeout 778 * mod_timer - modify a timer's timeout
719 * @timer: the timer to be modified 779 * @timer: the timer to be modified
@@ -744,6 +804,8 @@ int mod_timer(struct timer_list *timer, unsigned long expires)
744 if (timer_pending(timer) && timer->expires == expires) 804 if (timer_pending(timer) && timer->expires == expires)
745 return 1; 805 return 1;
746 806
807 expires = apply_slack(timer, expires);
808
747 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED); 809 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
748} 810}
749EXPORT_SYMBOL(mod_timer); 811EXPORT_SYMBOL(mod_timer);
@@ -880,6 +942,7 @@ int try_to_del_timer_sync(struct timer_list *timer)
880 if (base->running_timer == timer) 942 if (base->running_timer == timer)
881 goto out; 943 goto out;
882 944
945 timer_stats_timer_clear_start_info(timer);
883 ret = 0; 946 ret = 0;
884 if (timer_pending(timer)) { 947 if (timer_pending(timer)) {
885 detach_timer(timer, 1); 948 detach_timer(timer, 1);
@@ -953,6 +1016,47 @@ static int cascade(struct tvec_base *base, struct tvec *tv, int index)
953 return index; 1016 return index;
954} 1017}
955 1018
1019static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1020 unsigned long data)
1021{
1022 int preempt_count = preempt_count();
1023
1024#ifdef CONFIG_LOCKDEP
1025 /*
1026 * It is permissible to free the timer from inside the
1027 * function that is called from it, this we need to take into
1028 * account for lockdep too. To avoid bogus "held lock freed"
1029 * warnings as well as problems when looking into
1030 * timer->lockdep_map, make a copy and use that here.
1031 */
1032 struct lockdep_map lockdep_map = timer->lockdep_map;
1033#endif
1034 /*
1035 * Couple the lock chain with the lock chain at
1036 * del_timer_sync() by acquiring the lock_map around the fn()
1037 * call here and in del_timer_sync().
1038 */
1039 lock_map_acquire(&lockdep_map);
1040
1041 trace_timer_expire_entry(timer);
1042 fn(data);
1043 trace_timer_expire_exit(timer);
1044
1045 lock_map_release(&lockdep_map);
1046
1047 if (preempt_count != preempt_count()) {
1048 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1049 fn, preempt_count, preempt_count());
1050 /*
1051 * Restore the preempt count. That gives us a decent
1052 * chance to survive and extract information. If the
1053 * callback kept a lock held, bad luck, but not worse
1054 * than the BUG() we had.
1055 */
1056 preempt_count() = preempt_count;
1057 }
1058}
1059
956#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) 1060#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
957 1061
958/** 1062/**
@@ -996,45 +1100,7 @@ static inline void __run_timers(struct tvec_base *base)
996 detach_timer(timer, 1); 1100 detach_timer(timer, 1);
997 1101
998 spin_unlock_irq(&base->lock); 1102 spin_unlock_irq(&base->lock);
999 { 1103 call_timer_fn(timer, fn, data);
1000 int preempt_count = preempt_count();
1001
1002#ifdef CONFIG_LOCKDEP
1003 /*
1004 * It is permissible to free the timer from
1005 * inside the function that is called from
1006 * it, this we need to take into account for
1007 * lockdep too. To avoid bogus "held lock
1008 * freed" warnings as well as problems when
1009 * looking into timer->lockdep_map, make a
1010 * copy and use that here.
1011 */
1012 struct lockdep_map lockdep_map =
1013 timer->lockdep_map;
1014#endif
1015 /*
1016 * Couple the lock chain with the lock chain at
1017 * del_timer_sync() by acquiring the lock_map
1018 * around the fn() call here and in
1019 * del_timer_sync().
1020 */
1021 lock_map_acquire(&lockdep_map);
1022
1023 trace_timer_expire_entry(timer);
1024 fn(data);
1025 trace_timer_expire_exit(timer);
1026
1027 lock_map_release(&lockdep_map);
1028
1029 if (preempt_count != preempt_count()) {
1030 printk(KERN_ERR "huh, entered %p "
1031 "with preempt_count %08x, exited"
1032 " with %08x?\n",
1033 fn, preempt_count,
1034 preempt_count());
1035 BUG();
1036 }
1037 }
1038 spin_lock_irq(&base->lock); 1104 spin_lock_irq(&base->lock);
1039 } 1105 }
1040 } 1106 }
@@ -1618,11 +1684,14 @@ static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1618 unsigned long action, void *hcpu) 1684 unsigned long action, void *hcpu)
1619{ 1685{
1620 long cpu = (long)hcpu; 1686 long cpu = (long)hcpu;
1687 int err;
1688
1621 switch(action) { 1689 switch(action) {
1622 case CPU_UP_PREPARE: 1690 case CPU_UP_PREPARE:
1623 case CPU_UP_PREPARE_FROZEN: 1691 case CPU_UP_PREPARE_FROZEN:
1624 if (init_timers_cpu(cpu) < 0) 1692 err = init_timers_cpu(cpu);
1625 return NOTIFY_BAD; 1693 if (err < 0)
1694 return notifier_from_errno(err);
1626 break; 1695 break;
1627#ifdef CONFIG_HOTPLUG_CPU 1696#ifdef CONFIG_HOTPLUG_CPU
1628 case CPU_DEAD: 1697 case CPU_DEAD: