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