aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/timer.c')
-rw-r--r--kernel/timer.c205
1 files changed, 154 insertions, 51 deletions
diff --git a/kernel/timer.c b/kernel/timer.c
index aeb6a54f2771..97bf05baade7 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -90,8 +90,13 @@ static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases;
90 90
91/* 91/*
92 * Note that all tvec_bases are 2 byte aligned and lower bit of 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 for 93 * base in timer_list is guaranteed to be zero. Use the LSB to
94 * the new flag to indicate whether the timer is deferrable 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.
95 */ 100 */
96#define TBASE_DEFERRABLE_FLAG (0x1) 101#define TBASE_DEFERRABLE_FLAG (0x1)
97 102
@@ -319,6 +324,25 @@ unsigned long round_jiffies_up_relative(unsigned long j)
319} 324}
320EXPORT_SYMBOL_GPL(round_jiffies_up_relative); 325EXPORT_SYMBOL_GPL(round_jiffies_up_relative);
321 326
327/**
328 * set_timer_slack - set the allowed slack for a timer
329 * @timer: the timer to be modified
330 * @slack_hz: the amount of time (in jiffies) allowed for rounding
331 *
332 * Set the amount of time, in jiffies, that a certain timer has
333 * in terms of slack. By setting this value, the timer subsystem
334 * will schedule the actual timer somewhere between
335 * the time mod_timer() asks for, and that time plus the slack.
336 *
337 * By setting the slack to -1, a percentage of the delay is used
338 * instead.
339 */
340void set_timer_slack(struct timer_list *timer, int slack_hz)
341{
342 timer->slack = slack_hz;
343}
344EXPORT_SYMBOL_GPL(set_timer_slack);
345
322 346
323static inline void set_running_timer(struct tvec_base *base, 347static inline void set_running_timer(struct tvec_base *base,
324 struct timer_list *timer) 348 struct timer_list *timer)
@@ -550,6 +574,7 @@ static void __init_timer(struct timer_list *timer,
550{ 574{
551 timer->entry.next = NULL; 575 timer->entry.next = NULL;
552 timer->base = __raw_get_cpu_var(tvec_bases); 576 timer->base = __raw_get_cpu_var(tvec_bases);
577 timer->slack = -1;
553#ifdef CONFIG_TIMER_STATS 578#ifdef CONFIG_TIMER_STATS
554 timer->start_site = NULL; 579 timer->start_site = NULL;
555 timer->start_pid = -1; 580 timer->start_pid = -1;
@@ -558,6 +583,19 @@ static void __init_timer(struct timer_list *timer,
558 lockdep_init_map(&timer->lockdep_map, name, key, 0); 583 lockdep_init_map(&timer->lockdep_map, name, key, 0);
559} 584}
560 585
586void setup_deferrable_timer_on_stack_key(struct timer_list *timer,
587 const char *name,
588 struct lock_class_key *key,
589 void (*function)(unsigned long),
590 unsigned long data)
591{
592 timer->function = function;
593 timer->data = data;
594 init_timer_on_stack_key(timer, name, key);
595 timer_set_deferrable(timer);
596}
597EXPORT_SYMBOL_GPL(setup_deferrable_timer_on_stack_key);
598
561/** 599/**
562 * init_timer_key - initialize a timer 600 * init_timer_key - initialize a timer
563 * @timer: the timer to be initialized 601 * @timer: the timer to be initialized
@@ -660,12 +698,8 @@ __mod_timer(struct timer_list *timer, unsigned long expires,
660 cpu = smp_processor_id(); 698 cpu = smp_processor_id();
661 699
662#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP) 700#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP)
663 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu)) { 701 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu))
664 int preferred_cpu = get_nohz_load_balancer(); 702 cpu = get_nohz_timer_target();
665
666 if (preferred_cpu >= 0)
667 cpu = preferred_cpu;
668 }
669#endif 703#endif
670 new_base = per_cpu(tvec_bases, cpu); 704 new_base = per_cpu(tvec_bases, cpu);
671 705
@@ -715,6 +749,46 @@ int mod_timer_pending(struct timer_list *timer, unsigned long expires)
715} 749}
716EXPORT_SYMBOL(mod_timer_pending); 750EXPORT_SYMBOL(mod_timer_pending);
717 751
752/*
753 * Decide where to put the timer while taking the slack into account
754 *
755 * Algorithm:
756 * 1) calculate the maximum (absolute) time
757 * 2) calculate the highest bit where the expires and new max are different
758 * 3) use this bit to make a mask
759 * 4) use the bitmask to round down the maximum time, so that all last
760 * bits are zeros
761 */
762static inline
763unsigned long apply_slack(struct timer_list *timer, unsigned long expires)
764{
765 unsigned long expires_limit, mask;
766 int bit;
767
768 expires_limit = expires;
769
770 if (timer->slack >= 0) {
771 expires_limit = expires + timer->slack;
772 } else {
773 unsigned long now = jiffies;
774
775 /* No slack, if already expired else auto slack 0.4% */
776 if (time_after(expires, now))
777 expires_limit = expires + (expires - now)/256;
778 }
779 mask = expires ^ expires_limit;
780 if (mask == 0)
781 return expires;
782
783 bit = find_last_bit(&mask, BITS_PER_LONG);
784
785 mask = (1 << bit) - 1;
786
787 expires_limit = expires_limit & ~(mask);
788
789 return expires_limit;
790}
791
718/** 792/**
719 * mod_timer - modify a timer's timeout 793 * mod_timer - modify a timer's timeout
720 * @timer: the timer to be modified 794 * @timer: the timer to be modified
@@ -745,6 +819,8 @@ int mod_timer(struct timer_list *timer, unsigned long expires)
745 if (timer_pending(timer) && timer->expires == expires) 819 if (timer_pending(timer) && timer->expires == expires)
746 return 1; 820 return 1;
747 821
822 expires = apply_slack(timer, expires);
823
748 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED); 824 return __mod_timer(timer, expires, false, TIMER_NOT_PINNED);
749} 825}
750EXPORT_SYMBOL(mod_timer); 826EXPORT_SYMBOL(mod_timer);
@@ -955,6 +1031,47 @@ static int cascade(struct tvec_base *base, struct tvec *tv, int index)
955 return index; 1031 return index;
956} 1032}
957 1033
1034static void call_timer_fn(struct timer_list *timer, void (*fn)(unsigned long),
1035 unsigned long data)
1036{
1037 int preempt_count = preempt_count();
1038
1039#ifdef CONFIG_LOCKDEP
1040 /*
1041 * It is permissible to free the timer from inside the
1042 * function that is called from it, this we need to take into
1043 * account for lockdep too. To avoid bogus "held lock freed"
1044 * warnings as well as problems when looking into
1045 * timer->lockdep_map, make a copy and use that here.
1046 */
1047 struct lockdep_map lockdep_map = timer->lockdep_map;
1048#endif
1049 /*
1050 * Couple the lock chain with the lock chain at
1051 * del_timer_sync() by acquiring the lock_map around the fn()
1052 * call here and in del_timer_sync().
1053 */
1054 lock_map_acquire(&lockdep_map);
1055
1056 trace_timer_expire_entry(timer);
1057 fn(data);
1058 trace_timer_expire_exit(timer);
1059
1060 lock_map_release(&lockdep_map);
1061
1062 if (preempt_count != preempt_count()) {
1063 WARN_ONCE(1, "timer: %pF preempt leak: %08x -> %08x\n",
1064 fn, preempt_count, preempt_count());
1065 /*
1066 * Restore the preempt count. That gives us a decent
1067 * chance to survive and extract information. If the
1068 * callback kept a lock held, bad luck, but not worse
1069 * than the BUG() we had.
1070 */
1071 preempt_count() = preempt_count;
1072 }
1073}
1074
958#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) 1075#define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK)
959 1076
960/** 1077/**
@@ -998,45 +1115,7 @@ static inline void __run_timers(struct tvec_base *base)
998 detach_timer(timer, 1); 1115 detach_timer(timer, 1);
999 1116
1000 spin_unlock_irq(&base->lock); 1117 spin_unlock_irq(&base->lock);
1001 { 1118 call_timer_fn(timer, fn, data);
1002 int preempt_count = preempt_count();
1003
1004#ifdef CONFIG_LOCKDEP
1005 /*
1006 * It is permissible to free the timer from
1007 * inside the function that is called from
1008 * it, this we need to take into account for
1009 * lockdep too. To avoid bogus "held lock
1010 * freed" warnings as well as problems when
1011 * looking into timer->lockdep_map, make a
1012 * copy and use that here.
1013 */
1014 struct lockdep_map lockdep_map =
1015 timer->lockdep_map;
1016#endif
1017 /*
1018 * Couple the lock chain with the lock chain at
1019 * del_timer_sync() by acquiring the lock_map
1020 * around the fn() call here and in
1021 * del_timer_sync().
1022 */
1023 lock_map_acquire(&lockdep_map);
1024
1025 trace_timer_expire_entry(timer);
1026 fn(data);
1027 trace_timer_expire_exit(timer);
1028
1029 lock_map_release(&lockdep_map);
1030
1031 if (preempt_count != preempt_count()) {
1032 printk(KERN_ERR "huh, entered %p "
1033 "with preempt_count %08x, exited"
1034 " with %08x?\n",
1035 fn, preempt_count,
1036 preempt_count());
1037 BUG();
1038 }
1039 }
1040 spin_lock_irq(&base->lock); 1119 spin_lock_irq(&base->lock);
1041 } 1120 }
1042 } 1121 }
@@ -1225,7 +1304,6 @@ void run_local_timers(void)
1225{ 1304{
1226 hrtimer_run_queues(); 1305 hrtimer_run_queues();
1227 raise_softirq(TIMER_SOFTIRQ); 1306 raise_softirq(TIMER_SOFTIRQ);
1228 softlockup_tick();
1229} 1307}
1230 1308
1231/* 1309/*
@@ -1620,11 +1698,14 @@ static int __cpuinit timer_cpu_notify(struct notifier_block *self,
1620 unsigned long action, void *hcpu) 1698 unsigned long action, void *hcpu)
1621{ 1699{
1622 long cpu = (long)hcpu; 1700 long cpu = (long)hcpu;
1701 int err;
1702
1623 switch(action) { 1703 switch(action) {
1624 case CPU_UP_PREPARE: 1704 case CPU_UP_PREPARE:
1625 case CPU_UP_PREPARE_FROZEN: 1705 case CPU_UP_PREPARE_FROZEN:
1626 if (init_timers_cpu(cpu) < 0) 1706 err = init_timers_cpu(cpu);
1627 return NOTIFY_BAD; 1707 if (err < 0)
1708 return notifier_from_errno(err);
1628 break; 1709 break;
1629#ifdef CONFIG_HOTPLUG_CPU 1710#ifdef CONFIG_HOTPLUG_CPU
1630 case CPU_DEAD: 1711 case CPU_DEAD:
@@ -1650,7 +1731,7 @@ void __init init_timers(void)
1650 1731
1651 init_timer_stats(); 1732 init_timer_stats();
1652 1733
1653 BUG_ON(err == NOTIFY_BAD); 1734 BUG_ON(err != NOTIFY_OK);
1654 register_cpu_notifier(&timers_nb); 1735 register_cpu_notifier(&timers_nb);
1655 open_softirq(TIMER_SOFTIRQ, run_timer_softirq); 1736 open_softirq(TIMER_SOFTIRQ, run_timer_softirq);
1656} 1737}
@@ -1683,3 +1764,25 @@ unsigned long msleep_interruptible(unsigned int msecs)
1683} 1764}
1684 1765
1685EXPORT_SYMBOL(msleep_interruptible); 1766EXPORT_SYMBOL(msleep_interruptible);
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);