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