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