| 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> | 
|  | 23 | #include <linux/module.h> | 
|  | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | #include <asm/uaccess.h> | 
|  | 42 | #include <asm/unistd.h> | 
|  | 43 | #include <asm/div64.h> | 
|  | 44 | #include <asm/timex.h> | 
|  | 45 | #include <asm/io.h> | 
|  | 46 |  | 
| Thomas Gleixner | ecea8d1 | 2005-10-30 15:03:00 -0800 | [diff] [blame] | 47 | u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; | 
|  | 48 |  | 
|  | 49 | EXPORT_SYMBOL(jiffies_64); | 
|  | 50 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* | 
|  | 52 | * per-CPU timer vector definitions: | 
|  | 53 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6) | 
|  | 55 | #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8) | 
|  | 56 | #define TVN_SIZE (1 << TVN_BITS) | 
|  | 57 | #define TVR_SIZE (1 << TVR_BITS) | 
|  | 58 | #define TVN_MASK (TVN_SIZE - 1) | 
|  | 59 | #define TVR_MASK (TVR_SIZE - 1) | 
|  | 60 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 61 | struct tvec { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct list_head vec[TVN_SIZE]; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 63 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 65 | struct tvec_root { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct list_head vec[TVR_SIZE]; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 67 | }; | 
| 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_base { | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 70 | spinlock_t lock; | 
|  | 71 | struct timer_list *running_timer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | unsigned long timer_jiffies; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 73 | struct tvec_root tv1; | 
|  | 74 | struct tvec tv2; | 
|  | 75 | struct tvec tv3; | 
|  | 76 | struct tvec tv4; | 
|  | 77 | struct tvec tv5; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 78 | } ____cacheline_aligned; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 80 | struct tvec_base boot_tvec_bases; | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 81 | EXPORT_SYMBOL(boot_tvec_bases); | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 82 | static DEFINE_PER_CPU(struct tvec_base *, tvec_bases) = &boot_tvec_bases; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 |  | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 84 | /* | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 85 | * Note that all tvec_bases are 2 byte aligned and lower bit of | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 86 | * base in timer_list is guaranteed to be zero. Use the LSB for | 
|  | 87 | * the new flag to indicate whether the timer is deferrable | 
|  | 88 | */ | 
|  | 89 | #define TBASE_DEFERRABLE_FLAG		(0x1) | 
|  | 90 |  | 
|  | 91 | /* Functions below help us manage 'deferrable' flag */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 92 | static inline unsigned int tbase_get_deferrable(struct tvec_base *base) | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 93 | { | 
| akpm@linux-foundation.org | e991084 | 2007-05-10 03:16:01 -0700 | [diff] [blame] | 94 | return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 97 | static inline struct tvec_base *tbase_get_base(struct tvec_base *base) | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 98 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 99 | return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG)); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
|  | 102 | static inline void timer_set_deferrable(struct timer_list *timer) | 
|  | 103 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 104 | timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 105 | TBASE_DEFERRABLE_FLAG)); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
|  | 108 | static inline void | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 109 | timer_set_base(struct timer_list *timer, struct tvec_base *new_base) | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 110 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 111 | timer->base = (struct tvec_base *)((unsigned long)(new_base) | | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 112 | tbase_get_deferrable(timer->base)); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Arjan van de Ven | 4c36a5d | 2006-12-10 02:21:24 -0800 | [diff] [blame] | 115 | /** | 
|  | 116 | * __round_jiffies - function to round jiffies to a full second | 
|  | 117 | * @j: the time in (absolute) jiffies that should be rounded | 
|  | 118 | * @cpu: the processor number on which the timeout will happen | 
|  | 119 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 120 | * __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] | 121 | * up or down to (approximately) full seconds. This is useful for timers | 
|  | 122 | * for which the exact time they fire does not matter too much, as long as | 
|  | 123 | * they fire approximately every X seconds. | 
|  | 124 | * | 
|  | 125 | * By rounding these timers to whole seconds, all such timers will fire | 
|  | 126 | * at the same time, rather than at various times spread out. The goal | 
|  | 127 | * of this is to have the CPU wake up less, which saves power. | 
|  | 128 | * | 
|  | 129 | * The exact rounding is skewed for each processor to avoid all | 
|  | 130 | * processors firing at the exact same time, which could lead | 
|  | 131 | * to lock contention or spurious cache line bouncing. | 
|  | 132 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 133 | * 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] | 134 | */ | 
|  | 135 | unsigned long __round_jiffies(unsigned long j, int cpu) | 
|  | 136 | { | 
|  | 137 | int rem; | 
|  | 138 | unsigned long original = j; | 
|  | 139 |  | 
|  | 140 | /* | 
|  | 141 | * We don't want all cpus firing their timers at once hitting the | 
|  | 142 | * same lock or cachelines, so we skew each extra cpu with an extra | 
|  | 143 | * 3 jiffies. This 3 jiffies came originally from the mm/ code which | 
|  | 144 | * already did this. | 
|  | 145 | * The skew is done by adding 3*cpunr, then round, then subtract this | 
|  | 146 | * extra offset again. | 
|  | 147 | */ | 
|  | 148 | j += cpu * 3; | 
|  | 149 |  | 
|  | 150 | rem = j % HZ; | 
|  | 151 |  | 
|  | 152 | /* | 
|  | 153 | * If the target jiffie is just after a whole second (which can happen | 
|  | 154 | * due to delays of the timer irq, long irq off times etc etc) then | 
|  | 155 | * we should round down to the whole second, not up. Use 1/4th second | 
|  | 156 | * as cutoff for this rounding as an extreme upper bound for this. | 
|  | 157 | */ | 
|  | 158 | if (rem < HZ/4) /* round down */ | 
|  | 159 | j = j - rem; | 
|  | 160 | else /* round up */ | 
|  | 161 | j = j - rem + HZ; | 
|  | 162 |  | 
|  | 163 | /* now that we have rounded, subtract the extra skew again */ | 
|  | 164 | j -= cpu * 3; | 
|  | 165 |  | 
|  | 166 | if (j <= jiffies) /* rounding ate our timeout entirely; */ | 
|  | 167 | return original; | 
|  | 168 | return j; | 
|  | 169 | } | 
|  | 170 | EXPORT_SYMBOL_GPL(__round_jiffies); | 
|  | 171 |  | 
|  | 172 | /** | 
|  | 173 | * __round_jiffies_relative - function to round jiffies to a full second | 
|  | 174 | * @j: the time in (relative) jiffies that should be rounded | 
|  | 175 | * @cpu: the processor number on which the timeout will happen | 
|  | 176 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 177 | * __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] | 178 | * up or down to (approximately) full seconds. This is useful for timers | 
|  | 179 | * for which the exact time they fire does not matter too much, as long as | 
|  | 180 | * they fire approximately every X seconds. | 
|  | 181 | * | 
|  | 182 | * By rounding these timers to whole seconds, all such timers will fire | 
|  | 183 | * at the same time, rather than at various times spread out. The goal | 
|  | 184 | * of this is to have the CPU wake up less, which saves power. | 
|  | 185 | * | 
|  | 186 | * The exact rounding is skewed for each processor to avoid all | 
|  | 187 | * processors firing at the exact same time, which could lead | 
|  | 188 | * to lock contention or spurious cache line bouncing. | 
|  | 189 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 190 | * 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] | 191 | */ | 
|  | 192 | unsigned long __round_jiffies_relative(unsigned long j, int cpu) | 
|  | 193 | { | 
|  | 194 | /* | 
|  | 195 | * In theory the following code can skip a jiffy in case jiffies | 
|  | 196 | * increments right between the addition and the later subtraction. | 
|  | 197 | * However since the entire point of this function is to use approximate | 
|  | 198 | * timeouts, it's entirely ok to not handle that. | 
|  | 199 | */ | 
|  | 200 | return  __round_jiffies(j + jiffies, cpu) - jiffies; | 
|  | 201 | } | 
|  | 202 | EXPORT_SYMBOL_GPL(__round_jiffies_relative); | 
|  | 203 |  | 
|  | 204 | /** | 
|  | 205 | * round_jiffies - function to round jiffies to a full second | 
|  | 206 | * @j: the time in (absolute) jiffies that should be rounded | 
|  | 207 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 208 | * 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] | 209 | * up or down to (approximately) full seconds. This is useful for timers | 
|  | 210 | * for which the exact time they fire does not matter too much, as long as | 
|  | 211 | * they fire approximately every X seconds. | 
|  | 212 | * | 
|  | 213 | * By rounding these timers to whole seconds, all such timers will fire | 
|  | 214 | * at the same time, rather than at various times spread out. The goal | 
|  | 215 | * of this is to have the CPU wake up less, which saves power. | 
|  | 216 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 217 | * The return value is the rounded version of the @j parameter. | 
| Arjan van de Ven | 4c36a5d | 2006-12-10 02:21:24 -0800 | [diff] [blame] | 218 | */ | 
|  | 219 | unsigned long round_jiffies(unsigned long j) | 
|  | 220 | { | 
|  | 221 | return __round_jiffies(j, raw_smp_processor_id()); | 
|  | 222 | } | 
|  | 223 | EXPORT_SYMBOL_GPL(round_jiffies); | 
|  | 224 |  | 
|  | 225 | /** | 
|  | 226 | * round_jiffies_relative - function to round jiffies to a full second | 
|  | 227 | * @j: the time in (relative) jiffies that should be rounded | 
|  | 228 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 229 | * 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] | 230 | * up or down to (approximately) full seconds. This is useful for timers | 
|  | 231 | * for which the exact time they fire does not matter too much, as long as | 
|  | 232 | * they fire approximately every X seconds. | 
|  | 233 | * | 
|  | 234 | * By rounding these timers to whole seconds, all such timers will fire | 
|  | 235 | * at the same time, rather than at various times spread out. The goal | 
|  | 236 | * of this is to have the CPU wake up less, which saves power. | 
|  | 237 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 238 | * 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] | 239 | */ | 
|  | 240 | unsigned long round_jiffies_relative(unsigned long j) | 
|  | 241 | { | 
|  | 242 | return __round_jiffies_relative(j, raw_smp_processor_id()); | 
|  | 243 | } | 
|  | 244 | EXPORT_SYMBOL_GPL(round_jiffies_relative); | 
|  | 245 |  | 
|  | 246 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 247 | static inline void set_running_timer(struct tvec_base *base, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | struct timer_list *timer) | 
|  | 249 | { | 
|  | 250 | #ifdef CONFIG_SMP | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 251 | base->running_timer = timer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | #endif | 
|  | 253 | } | 
|  | 254 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 255 | static void internal_add_timer(struct tvec_base *base, struct timer_list *timer) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { | 
|  | 257 | unsigned long expires = timer->expires; | 
|  | 258 | unsigned long idx = expires - base->timer_jiffies; | 
|  | 259 | struct list_head *vec; | 
|  | 260 |  | 
|  | 261 | if (idx < TVR_SIZE) { | 
|  | 262 | int i = expires & TVR_MASK; | 
|  | 263 | vec = base->tv1.vec + i; | 
|  | 264 | } else if (idx < 1 << (TVR_BITS + TVN_BITS)) { | 
|  | 265 | int i = (expires >> TVR_BITS) & TVN_MASK; | 
|  | 266 | vec = base->tv2.vec + i; | 
|  | 267 | } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) { | 
|  | 268 | int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK; | 
|  | 269 | vec = base->tv3.vec + i; | 
|  | 270 | } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) { | 
|  | 271 | int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK; | 
|  | 272 | vec = base->tv4.vec + i; | 
|  | 273 | } else if ((signed long) idx < 0) { | 
|  | 274 | /* | 
|  | 275 | * Can happen if you add a timer with expires == jiffies, | 
|  | 276 | * or you set a timer to go off in the past | 
|  | 277 | */ | 
|  | 278 | vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); | 
|  | 279 | } else { | 
|  | 280 | int i; | 
|  | 281 | /* If the timeout is larger than 0xffffffff on 64-bit | 
|  | 282 | * architectures then we use the maximum timeout: | 
|  | 283 | */ | 
|  | 284 | if (idx > 0xffffffffUL) { | 
|  | 285 | idx = 0xffffffffUL; | 
|  | 286 | expires = idx + base->timer_jiffies; | 
|  | 287 | } | 
|  | 288 | i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK; | 
|  | 289 | vec = base->tv5.vec + i; | 
|  | 290 | } | 
|  | 291 | /* | 
|  | 292 | * Timers are FIFO: | 
|  | 293 | */ | 
|  | 294 | list_add_tail(&timer->entry, vec); | 
|  | 295 | } | 
|  | 296 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 297 | #ifdef CONFIG_TIMER_STATS | 
|  | 298 | void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr) | 
|  | 299 | { | 
|  | 300 | if (timer->start_site) | 
|  | 301 | return; | 
|  | 302 |  | 
|  | 303 | timer->start_site = addr; | 
|  | 304 | memcpy(timer->start_comm, current->comm, TASK_COMM_LEN); | 
|  | 305 | timer->start_pid = current->pid; | 
|  | 306 | } | 
| Venki Pallipadi | c5c061b8 | 2007-07-15 23:40:30 -0700 | [diff] [blame] | 307 |  | 
|  | 308 | static void timer_stats_account_timer(struct timer_list *timer) | 
|  | 309 | { | 
|  | 310 | unsigned int flag = 0; | 
|  | 311 |  | 
|  | 312 | if (unlikely(tbase_get_deferrable(timer->base))) | 
|  | 313 | flag |= TIMER_STATS_FLAG_DEFERRABLE; | 
|  | 314 |  | 
|  | 315 | timer_stats_update_stats(timer, timer->start_pid, timer->start_site, | 
|  | 316 | timer->function, timer->start_comm, flag); | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | #else | 
|  | 320 | static void timer_stats_account_timer(struct timer_list *timer) {} | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 321 | #endif | 
|  | 322 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 323 | /** | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 324 | * init_timer - initialize a timer. | 
|  | 325 | * @timer: the timer to be initialized | 
|  | 326 | * | 
|  | 327 | * init_timer() must be done to a timer prior calling *any* of the | 
|  | 328 | * other timer functions. | 
|  | 329 | */ | 
| Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 330 | void init_timer(struct timer_list *timer) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 331 | { | 
|  | 332 | timer->entry.next = NULL; | 
| Paul Mackerras | bfe5d83 | 2006-06-25 05:47:14 -0700 | [diff] [blame] | 333 | timer->base = __raw_get_cpu_var(tvec_bases); | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 334 | #ifdef CONFIG_TIMER_STATS | 
|  | 335 | timer->start_site = NULL; | 
|  | 336 | timer->start_pid = -1; | 
|  | 337 | memset(timer->start_comm, 0, TASK_COMM_LEN); | 
|  | 338 | #endif | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 339 | } | 
|  | 340 | EXPORT_SYMBOL(init_timer); | 
|  | 341 |  | 
| Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 342 | void init_timer_deferrable(struct timer_list *timer) | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 343 | { | 
|  | 344 | init_timer(timer); | 
|  | 345 | timer_set_deferrable(timer); | 
|  | 346 | } | 
|  | 347 | EXPORT_SYMBOL(init_timer_deferrable); | 
|  | 348 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 349 | static inline void detach_timer(struct timer_list *timer, | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 350 | int clear_pending) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 351 | { | 
|  | 352 | struct list_head *entry = &timer->entry; | 
|  | 353 |  | 
|  | 354 | __list_del(entry->prev, entry->next); | 
|  | 355 | if (clear_pending) | 
|  | 356 | entry->next = NULL; | 
|  | 357 | entry->prev = LIST_POISON2; | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | /* | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 361 | * We are using hashed locking: holding per_cpu(tvec_bases).lock | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 362 | * means that all timers which are tied to this base via timer->base are | 
|  | 363 | * locked, and the base itself is locked too. | 
|  | 364 | * | 
|  | 365 | * So __run_timers/migrate_timers can safely modify all timers which could | 
|  | 366 | * be found on ->tvX lists. | 
|  | 367 | * | 
|  | 368 | * When the timer's base is locked, and the timer removed from list, it is | 
|  | 369 | * possible to set timer->base = NULL and drop the lock: the timer remains | 
|  | 370 | * locked. | 
|  | 371 | */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 372 | static struct tvec_base *lock_timer_base(struct timer_list *timer, | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 373 | unsigned long *flags) | 
| Josh Triplett | 89e7e374 | 2006-09-29 01:59:36 -0700 | [diff] [blame] | 374 | __acquires(timer->base->lock) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 375 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 376 | struct tvec_base *base; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 377 |  | 
|  | 378 | for (;;) { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 379 | struct tvec_base *prelock_base = timer->base; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 380 | base = tbase_get_base(prelock_base); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 381 | if (likely(base != NULL)) { | 
|  | 382 | spin_lock_irqsave(&base->lock, *flags); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 383 | if (likely(prelock_base == timer->base)) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 384 | return base; | 
|  | 385 | /* The timer has migrated to another CPU */ | 
|  | 386 | spin_unlock_irqrestore(&base->lock, *flags); | 
|  | 387 | } | 
|  | 388 | cpu_relax(); | 
|  | 389 | } | 
|  | 390 | } | 
|  | 391 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | int __mod_timer(struct timer_list *timer, unsigned long expires) | 
|  | 393 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 394 | struct tvec_base *base, *new_base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | unsigned long flags; | 
|  | 396 | int ret = 0; | 
|  | 397 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 398 | timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | BUG_ON(!timer->function); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 401 | base = lock_timer_base(timer, &flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 403 | if (timer_pending(timer)) { | 
|  | 404 | detach_timer(timer, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ret = 1; | 
|  | 406 | } | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 407 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 408 | new_base = __get_cpu_var(tvec_bases); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 409 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 410 | if (base != new_base) { | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 411 | /* | 
|  | 412 | * We are trying to schedule the timer on the local CPU. | 
|  | 413 | * However we can't change timer's base while it is running, | 
|  | 414 | * otherwise del_timer_sync() can't detect that the timer's | 
|  | 415 | * handler yet has not finished. This also guarantees that | 
|  | 416 | * the timer is serialized wrt itself. | 
|  | 417 | */ | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 418 | if (likely(base->running_timer != timer)) { | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 419 | /* See the comment in lock_timer_base() */ | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 420 | timer_set_base(timer, NULL); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 421 | spin_unlock(&base->lock); | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 422 | base = new_base; | 
|  | 423 | spin_lock(&base->lock); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 424 | timer_set_base(timer, base); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 425 | } | 
|  | 426 | } | 
|  | 427 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | timer->expires = expires; | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 429 | internal_add_timer(base, timer); | 
|  | 430 | spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 |  | 
|  | 432 | return ret; | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | EXPORT_SYMBOL(__mod_timer); | 
|  | 436 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 437 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | * add_timer_on - start a timer on a particular CPU | 
|  | 439 | * @timer: the timer to be added | 
|  | 440 | * @cpu: the CPU to start it on | 
|  | 441 | * | 
|  | 442 | * This is not very scalable on SMP. Double adds are not possible. | 
|  | 443 | */ | 
|  | 444 | void add_timer_on(struct timer_list *timer, int cpu) | 
|  | 445 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 446 | struct tvec_base *base = per_cpu(tvec_bases, cpu); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 447 | unsigned long flags; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 448 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 449 | timer_stats_timer_set_start_info(timer); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 450 | BUG_ON(timer_pending(timer) || !timer->function); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 451 | spin_lock_irqsave(&base->lock, flags); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 452 | timer_set_base(timer, base); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | internal_add_timer(base, timer); | 
| Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 454 | /* | 
|  | 455 | * Check whether the other CPU is idle and needs to be | 
|  | 456 | * triggered to reevaluate the timer wheel when nohz is | 
|  | 457 | * active. We are protected against the other CPU fiddling | 
|  | 458 | * with the timer by holding the timer base lock. This also | 
|  | 459 | * makes sure that a CPU on the way to idle can not evaluate | 
|  | 460 | * the timer wheel. | 
|  | 461 | */ | 
|  | 462 | wake_up_idle_cpu(cpu); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 463 | spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 466 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | * mod_timer - modify a timer's timeout | 
|  | 468 | * @timer: the timer to be modified | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 469 | * @expires: new timeout in jiffies | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 471 | * mod_timer() is a more efficient way to update the expire field of an | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | * active timer (if the timer is inactive it will be activated) | 
|  | 473 | * | 
|  | 474 | * mod_timer(timer, expires) is equivalent to: | 
|  | 475 | * | 
|  | 476 | *     del_timer(timer); timer->expires = expires; add_timer(timer); | 
|  | 477 | * | 
|  | 478 | * Note that if there are multiple unserialized concurrent users of the | 
|  | 479 | * same timer, then mod_timer() is the only safe way to modify the timeout, | 
|  | 480 | * since add_timer() cannot modify an already running timer. | 
|  | 481 | * | 
|  | 482 | * The function returns whether it has modified a pending timer or not. | 
|  | 483 | * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an | 
|  | 484 | * active timer returns 1.) | 
|  | 485 | */ | 
|  | 486 | int mod_timer(struct timer_list *timer, unsigned long expires) | 
|  | 487 | { | 
|  | 488 | BUG_ON(!timer->function); | 
|  | 489 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 490 | timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* | 
|  | 492 | * This is a common optimization triggered by the | 
|  | 493 | * networking code - if the timer is re-modified | 
|  | 494 | * to be the same thing then just return: | 
|  | 495 | */ | 
|  | 496 | if (timer->expires == expires && timer_pending(timer)) | 
|  | 497 | return 1; | 
|  | 498 |  | 
|  | 499 | return __mod_timer(timer, expires); | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | EXPORT_SYMBOL(mod_timer); | 
|  | 503 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 504 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | * del_timer - deactive a timer. | 
|  | 506 | * @timer: the timer to be deactivated | 
|  | 507 | * | 
|  | 508 | * del_timer() deactivates a timer - this works on both active and inactive | 
|  | 509 | * timers. | 
|  | 510 | * | 
|  | 511 | * The function returns whether it has deactivated a pending timer or not. | 
|  | 512 | * (ie. del_timer() of an inactive timer returns 0, del_timer() of an | 
|  | 513 | * active timer returns 1.) | 
|  | 514 | */ | 
|  | 515 | int del_timer(struct timer_list *timer) | 
|  | 516 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 517 | struct tvec_base *base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | unsigned long flags; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 519 | int ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 521 | timer_stats_timer_clear_start_info(timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 522 | if (timer_pending(timer)) { | 
|  | 523 | base = lock_timer_base(timer, &flags); | 
|  | 524 | if (timer_pending(timer)) { | 
|  | 525 | detach_timer(timer, 1); | 
|  | 526 | ret = 1; | 
|  | 527 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 531 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } | 
|  | 533 |  | 
|  | 534 | EXPORT_SYMBOL(del_timer); | 
|  | 535 |  | 
|  | 536 | #ifdef CONFIG_SMP | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 537 | /** | 
|  | 538 | * try_to_del_timer_sync - Try to deactivate a timer | 
|  | 539 | * @timer: timer do del | 
|  | 540 | * | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 541 | * This function tries to deactivate a timer. Upon successful (ret >= 0) | 
|  | 542 | * exit the timer is not queued and the handler is not running on any CPU. | 
|  | 543 | * | 
|  | 544 | * It must not be called from interrupt contexts. | 
|  | 545 | */ | 
|  | 546 | int try_to_del_timer_sync(struct timer_list *timer) | 
|  | 547 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 548 | struct tvec_base *base; | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 549 | unsigned long flags; | 
|  | 550 | int ret = -1; | 
|  | 551 |  | 
|  | 552 | base = lock_timer_base(timer, &flags); | 
|  | 553 |  | 
|  | 554 | if (base->running_timer == timer) | 
|  | 555 | goto out; | 
|  | 556 |  | 
|  | 557 | ret = 0; | 
|  | 558 | if (timer_pending(timer)) { | 
|  | 559 | detach_timer(timer, 1); | 
|  | 560 | ret = 1; | 
|  | 561 | } | 
|  | 562 | out: | 
|  | 563 | spin_unlock_irqrestore(&base->lock, flags); | 
|  | 564 |  | 
|  | 565 | return ret; | 
|  | 566 | } | 
|  | 567 |  | 
| David Howells | e19dff1 | 2007-04-26 15:46:56 -0700 | [diff] [blame] | 568 | EXPORT_SYMBOL(try_to_del_timer_sync); | 
|  | 569 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 570 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | * del_timer_sync - deactivate a timer and wait for the handler to finish. | 
|  | 572 | * @timer: the timer to be deactivated | 
|  | 573 | * | 
|  | 574 | * This function only differs from del_timer() on SMP: besides deactivating | 
|  | 575 | * the timer it also makes sure the handler has finished executing on other | 
|  | 576 | * CPUs. | 
|  | 577 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 578 | * Synchronization rules: Callers must prevent restarting of the timer, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | * otherwise this function is meaningless. It must not be called from | 
|  | 580 | * interrupt contexts. The caller must not hold locks which would prevent | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 581 | * completion of the timer's handler. The timer's handler must not call | 
|  | 582 | * add_timer_on(). Upon exit the timer is not queued and the handler is | 
|  | 583 | * not running on any CPU. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | * | 
|  | 585 | * The function returns whether it has deactivated a pending timer or not. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | */ | 
|  | 587 | int del_timer_sync(struct timer_list *timer) | 
|  | 588 | { | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 589 | for (;;) { | 
|  | 590 | int ret = try_to_del_timer_sync(timer); | 
|  | 591 | if (ret >= 0) | 
|  | 592 | return ret; | 
| Andrew Morton | a000965 | 2006-07-14 00:24:06 -0700 | [diff] [blame] | 593 | cpu_relax(); | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 594 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 596 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | EXPORT_SYMBOL(del_timer_sync); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | #endif | 
|  | 599 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 600 | static int cascade(struct tvec_base *base, struct tvec *tv, int index) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { | 
|  | 602 | /* cascade all the timers from tv up one level */ | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 603 | struct timer_list *timer, *tmp; | 
|  | 604 | struct list_head tv_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 |  | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 606 | list_replace_init(tv->vec + index, &tv_list); | 
|  | 607 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | /* | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 609 | * We are removing _all_ timers from the list, so we | 
|  | 610 | * don't have to detach them individually. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | */ | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 612 | list_for_each_entry_safe(timer, tmp, &tv_list, entry) { | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 613 | BUG_ON(tbase_get_base(timer->base) != base); | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 614 | internal_add_timer(base, timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 |  | 
|  | 617 | return index; | 
|  | 618 | } | 
|  | 619 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 620 | #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) | 
|  | 621 |  | 
|  | 622 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | * __run_timers - run all expired timers (if any) on this CPU. | 
|  | 624 | * @base: the timer vector to be processed. | 
|  | 625 | * | 
|  | 626 | * This function cascades all vectors and executes all expired timer | 
|  | 627 | * vectors. | 
|  | 628 | */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 629 | static inline void __run_timers(struct tvec_base *base) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { | 
|  | 631 | struct timer_list *timer; | 
|  | 632 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 633 | spin_lock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | while (time_after_eq(jiffies, base->timer_jiffies)) { | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 635 | struct list_head work_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | struct list_head *head = &work_list; | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 637 | int index = base->timer_jiffies & TVR_MASK; | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 638 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* | 
|  | 640 | * Cascade timers: | 
|  | 641 | */ | 
|  | 642 | if (!index && | 
|  | 643 | (!cascade(base, &base->tv2, INDEX(0))) && | 
|  | 644 | (!cascade(base, &base->tv3, INDEX(1))) && | 
|  | 645 | !cascade(base, &base->tv4, INDEX(2))) | 
|  | 646 | cascade(base, &base->tv5, INDEX(3)); | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 647 | ++base->timer_jiffies; | 
|  | 648 | list_replace_init(base->tv1.vec + index, &work_list); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 649 | while (!list_empty(head)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | void (*fn)(unsigned long); | 
|  | 651 | unsigned long data; | 
|  | 652 |  | 
| Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 653 | timer = list_first_entry(head, struct timer_list,entry); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 654 | fn = timer->function; | 
|  | 655 | data = timer->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 657 | timer_stats_account_timer(timer); | 
|  | 658 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | set_running_timer(base, timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 660 | detach_timer(timer, 1); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 661 | spin_unlock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { | 
| Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 663 | int preempt_count = preempt_count(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | fn(data); | 
|  | 665 | if (preempt_count != preempt_count()) { | 
| Pavel Machek | 4c9dc64 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 666 | printk(KERN_ERR "huh, entered %p " | 
| Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 667 | "with preempt_count %08x, exited" | 
|  | 668 | " with %08x?\n", | 
|  | 669 | fn, preempt_count, | 
|  | 670 | preempt_count()); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | BUG(); | 
|  | 672 | } | 
|  | 673 | } | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 674 | spin_lock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } | 
|  | 676 | } | 
|  | 677 | set_running_timer(base, NULL); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 678 | spin_unlock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } | 
|  | 680 |  | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 681 | #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | /* | 
|  | 683 | * Find out when the next timer event is due to happen. This | 
|  | 684 | * is used on S/390 to stop all activity when a cpus is idle. | 
|  | 685 | * This functions needs to be called disabled. | 
|  | 686 | */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 687 | static unsigned long __next_timer_interrupt(struct tvec_base *base) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 689 | unsigned long timer_jiffies = base->timer_jiffies; | 
| Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 690 | unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 691 | int index, slot, array, found = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | struct timer_list *nte; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 693 | struct tvec *varray[4]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 |  | 
|  | 695 | /* Look for timer events in tv1. */ | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 696 | index = slot = timer_jiffies & TVR_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | do { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 698 | list_for_each_entry(nte, base->tv1.vec + slot, entry) { | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 699 | if (tbase_get_deferrable(nte->base)) | 
|  | 700 | continue; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 701 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 702 | found = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | expires = nte->expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 704 | /* Look at the cascade bucket(s)? */ | 
|  | 705 | if (!index || slot < index) | 
|  | 706 | goto cascade; | 
|  | 707 | return expires; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 709 | slot = (slot + 1) & TVR_MASK; | 
|  | 710 | } while (slot != index); | 
|  | 711 |  | 
|  | 712 | cascade: | 
|  | 713 | /* Calculate the next cascade event */ | 
|  | 714 | if (index) | 
|  | 715 | timer_jiffies += TVR_SIZE - index; | 
|  | 716 | timer_jiffies >>= TVR_BITS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 |  | 
|  | 718 | /* Check tv2-tv5. */ | 
|  | 719 | varray[0] = &base->tv2; | 
|  | 720 | varray[1] = &base->tv3; | 
|  | 721 | varray[2] = &base->tv4; | 
|  | 722 | varray[3] = &base->tv5; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 723 |  | 
|  | 724 | for (array = 0; array < 4; array++) { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 725 | struct tvec *varp = varray[array]; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 726 |  | 
|  | 727 | index = slot = timer_jiffies & TVN_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | do { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 729 | list_for_each_entry(nte, varp->vec + slot, entry) { | 
|  | 730 | found = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (time_before(nte->expires, expires)) | 
|  | 732 | expires = nte->expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 733 | } | 
|  | 734 | /* | 
|  | 735 | * Do we still search for the first timer or are | 
|  | 736 | * we looking up the cascade buckets ? | 
|  | 737 | */ | 
|  | 738 | if (found) { | 
|  | 739 | /* Look at the cascade bucket(s)? */ | 
|  | 740 | if (!index || slot < index) | 
|  | 741 | break; | 
|  | 742 | return expires; | 
|  | 743 | } | 
|  | 744 | slot = (slot + 1) & TVN_MASK; | 
|  | 745 | } while (slot != index); | 
|  | 746 |  | 
|  | 747 | if (index) | 
|  | 748 | timer_jiffies += TVN_SIZE - index; | 
|  | 749 | timer_jiffies >>= TVN_BITS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 751 | return expires; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | /* | 
|  | 755 | * Check, if the next hrtimer event is before the next timer wheel | 
|  | 756 | * event: | 
|  | 757 | */ | 
|  | 758 | static unsigned long cmp_next_hrtimer_event(unsigned long now, | 
|  | 759 | unsigned long expires) | 
|  | 760 | { | 
|  | 761 | ktime_t hr_delta = hrtimer_get_next_event(); | 
|  | 762 | struct timespec tsdelta; | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 763 | unsigned long delta; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 764 |  | 
|  | 765 | if (hr_delta.tv64 == KTIME_MAX) | 
|  | 766 | return expires; | 
|  | 767 |  | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 768 | /* | 
|  | 769 | * Expired timer available, let it expire in the next tick | 
|  | 770 | */ | 
|  | 771 | if (hr_delta.tv64 <= 0) | 
|  | 772 | return now + 1; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 773 |  | 
|  | 774 | tsdelta = ktime_to_timespec(hr_delta); | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 775 | delta = timespec_to_jiffies(&tsdelta); | 
| Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 776 |  | 
|  | 777 | /* | 
|  | 778 | * Limit the delta to the max value, which is checked in | 
|  | 779 | * tick_nohz_stop_sched_tick(): | 
|  | 780 | */ | 
|  | 781 | if (delta > NEXT_TIMER_MAX_DELTA) | 
|  | 782 | delta = NEXT_TIMER_MAX_DELTA; | 
|  | 783 |  | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 784 | /* | 
|  | 785 | * Take rounding errors in to account and make sure, that it | 
|  | 786 | * expires in the next tick. Otherwise we go into an endless | 
|  | 787 | * ping pong due to tick_nohz_stop_sched_tick() retriggering | 
|  | 788 | * the timer softirq | 
|  | 789 | */ | 
|  | 790 | if (delta < 1) | 
|  | 791 | delta = 1; | 
|  | 792 | now += delta; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 793 | if (time_before(now, expires)) | 
|  | 794 | return now; | 
|  | 795 | return expires; | 
|  | 796 | } | 
|  | 797 |  | 
|  | 798 | /** | 
| Li Zefan | 8dce39c | 2007-11-05 14:51:10 -0800 | [diff] [blame] | 799 | * get_next_timer_interrupt - return the jiffy of the next pending timer | 
| Randy Dunlap | 05fb6bf | 2007-02-28 20:12:13 -0800 | [diff] [blame] | 800 | * @now: current time (in jiffies) | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 801 | */ | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 802 | unsigned long get_next_timer_interrupt(unsigned long now) | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 803 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 804 | struct tvec_base *base = __get_cpu_var(tvec_bases); | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 805 | unsigned long expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 806 |  | 
|  | 807 | spin_lock(&base->lock); | 
|  | 808 | expires = __next_timer_interrupt(base); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 809 | spin_unlock(&base->lock); | 
| Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 810 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 811 | if (time_before_eq(expires, now)) | 
|  | 812 | return now; | 
| Zachary Amsden | 0662b71 | 2006-05-20 15:00:24 -0700 | [diff] [blame] | 813 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 814 | return cmp_next_hrtimer_event(now, expires); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 816 |  | 
|  | 817 | #ifdef CONFIG_NO_IDLE_HZ | 
|  | 818 | unsigned long next_timer_interrupt(void) | 
|  | 819 | { | 
|  | 820 | return get_next_timer_interrupt(jiffies); | 
|  | 821 | } | 
|  | 822 | #endif | 
|  | 823 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | #endif | 
|  | 825 |  | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 826 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING | 
|  | 827 | void account_process_tick(struct task_struct *p, int user_tick) | 
|  | 828 | { | 
| Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 829 | cputime_t one_jiffy = jiffies_to_cputime(1); | 
|  | 830 |  | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 831 | if (user_tick) { | 
| Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 832 | account_user_time(p, one_jiffy); | 
|  | 833 | account_user_time_scaled(p, cputime_to_scaled(one_jiffy)); | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 834 | } else { | 
| Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 835 | account_system_time(p, HARDIRQ_OFFSET, one_jiffy); | 
|  | 836 | account_system_time_scaled(p, cputime_to_scaled(one_jiffy)); | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 837 | } | 
|  | 838 | } | 
|  | 839 | #endif | 
|  | 840 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | /* | 
| Daniel Walker | 5b4db0c | 2007-10-18 03:06:11 -0700 | [diff] [blame] | 842 | * 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] | 843 | * process.  user_tick is 1 if the tick is user time, 0 for system. | 
|  | 844 | */ | 
|  | 845 | void update_process_times(int user_tick) | 
|  | 846 | { | 
|  | 847 | struct task_struct *p = current; | 
|  | 848 | int cpu = smp_processor_id(); | 
|  | 849 |  | 
|  | 850 | /* Note: this timer irq context must be accounted for as well. */ | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 851 | account_process_tick(p, user_tick); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | run_local_timers(); | 
|  | 853 | if (rcu_pending(cpu)) | 
|  | 854 | rcu_check_callbacks(cpu, user_tick); | 
|  | 855 | scheduler_tick(); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 856 | run_posix_cpu_timers(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } | 
|  | 858 |  | 
|  | 859 | /* | 
|  | 860 | * Nr of active tasks - counted in fixed-point numbers | 
|  | 861 | */ | 
|  | 862 | static unsigned long count_active_tasks(void) | 
|  | 863 | { | 
| Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 864 | return nr_active() * FIXED_1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } | 
|  | 866 |  | 
|  | 867 | /* | 
|  | 868 | * Hmm.. Changed this, as the GNU make sources (load.c) seems to | 
|  | 869 | * imply that avenrun[] is the standard name for this kind of thing. | 
|  | 870 | * Nothing else seems to be standardized: the fractional size etc | 
|  | 871 | * all seem to differ on different machines. | 
|  | 872 | * | 
|  | 873 | * Requires xtime_lock to access. | 
|  | 874 | */ | 
|  | 875 | unsigned long avenrun[3]; | 
|  | 876 |  | 
|  | 877 | EXPORT_SYMBOL(avenrun); | 
|  | 878 |  | 
|  | 879 | /* | 
|  | 880 | * calc_load - given tick count, update the avenrun load estimates. | 
|  | 881 | * This is called while holding a write_lock on xtime_lock. | 
|  | 882 | */ | 
|  | 883 | static inline void calc_load(unsigned long ticks) | 
|  | 884 | { | 
|  | 885 | unsigned long active_tasks; /* fixed-point */ | 
|  | 886 | static int count = LOAD_FREQ; | 
|  | 887 |  | 
| Eric Dumazet | cd7175e | 2006-12-13 00:35:45 -0800 | [diff] [blame] | 888 | count -= ticks; | 
|  | 889 | if (unlikely(count < 0)) { | 
|  | 890 | active_tasks = count_active_tasks(); | 
|  | 891 | do { | 
|  | 892 | CALC_LOAD(avenrun[0], EXP_1, active_tasks); | 
|  | 893 | CALC_LOAD(avenrun[1], EXP_5, active_tasks); | 
|  | 894 | CALC_LOAD(avenrun[2], EXP_15, active_tasks); | 
|  | 895 | count += LOAD_FREQ; | 
|  | 896 | } while (count < 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } | 
|  | 898 | } | 
|  | 899 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | * This function runs timers and the timer-tq in bottom half context. | 
|  | 902 | */ | 
|  | 903 | static void run_timer_softirq(struct softirq_action *h) | 
|  | 904 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 905 | struct tvec_base *base = __get_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 |  | 
| Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 907 | hrtimer_run_pending(); | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 908 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | if (time_after_eq(jiffies, base->timer_jiffies)) | 
|  | 910 | __run_timers(base); | 
|  | 911 | } | 
|  | 912 |  | 
|  | 913 | /* | 
|  | 914 | * Called by the local, per-CPU timer interrupt on SMP. | 
|  | 915 | */ | 
|  | 916 | void run_local_timers(void) | 
|  | 917 | { | 
| Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 918 | hrtimer_run_queues(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | raise_softirq(TIMER_SOFTIRQ); | 
| Ingo Molnar | 6687a97 | 2006-03-24 03:18:41 -0800 | [diff] [blame] | 920 | softlockup_tick(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } | 
|  | 922 |  | 
|  | 923 | /* | 
|  | 924 | * Called by the timer interrupt. xtime_lock must already be taken | 
|  | 925 | * by the timer IRQ! | 
|  | 926 | */ | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 927 | static inline void update_times(unsigned long ticks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 929 | update_wall_time(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | calc_load(ticks); | 
|  | 931 | } | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 932 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | /* | 
|  | 934 | * The 64-bit jiffies value is not atomic - you MUST NOT read it | 
|  | 935 | * without sampling the sequence number in xtime_lock. | 
|  | 936 | * jiffies is defined in the linker script... | 
|  | 937 | */ | 
|  | 938 |  | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 939 | void do_timer(unsigned long ticks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 941 | jiffies_64 += ticks; | 
|  | 942 | update_times(ticks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | } | 
|  | 944 |  | 
|  | 945 | #ifdef __ARCH_WANT_SYS_ALARM | 
|  | 946 |  | 
|  | 947 | /* | 
|  | 948 | * For backwards compatibility?  This can be done in libc so Alpha | 
|  | 949 | * and all newer ports shouldn't need it. | 
|  | 950 | */ | 
|  | 951 | asmlinkage unsigned long sys_alarm(unsigned int seconds) | 
|  | 952 | { | 
| Thomas Gleixner | c08b8a4 | 2006-03-25 03:06:33 -0800 | [diff] [blame] | 953 | return alarm_setitimer(seconds); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } | 
|  | 955 |  | 
|  | 956 | #endif | 
|  | 957 |  | 
|  | 958 | #ifndef __alpha__ | 
|  | 959 |  | 
|  | 960 | /* | 
|  | 961 | * The Alpha uses getxpid, getxuid, and getxgid instead.  Maybe this | 
|  | 962 | * should be moved into arch/i386 instead? | 
|  | 963 | */ | 
|  | 964 |  | 
|  | 965 | /** | 
|  | 966 | * sys_getpid - return the thread group id of the current process | 
|  | 967 | * | 
|  | 968 | * Note, despite the name, this returns the tgid not the pid.  The tgid and | 
|  | 969 | * the pid are identical unless CLONE_THREAD was specified on clone() in | 
|  | 970 | * which case the tgid is the same in all threads of the same group. | 
|  | 971 | * | 
|  | 972 | * This is SMP safe as current->tgid does not change. | 
|  | 973 | */ | 
|  | 974 | asmlinkage long sys_getpid(void) | 
|  | 975 | { | 
| Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 976 | return task_tgid_vnr(current); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | } | 
|  | 978 |  | 
|  | 979 | /* | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 980 | * Accessing ->real_parent is not SMP-safe, it could | 
|  | 981 | * change from under us. However, we can use a stale | 
|  | 982 | * value of ->real_parent under rcu_read_lock(), see | 
|  | 983 | * release_task()->call_rcu(delayed_put_task_struct). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | */ | 
|  | 985 | asmlinkage long sys_getppid(void) | 
|  | 986 | { | 
|  | 987 | int pid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 |  | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 989 | rcu_read_lock(); | 
| Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 990 | pid = task_tgid_vnr(current->real_parent); | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 991 | rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | return pid; | 
|  | 994 | } | 
|  | 995 |  | 
|  | 996 | asmlinkage long sys_getuid(void) | 
|  | 997 | { | 
|  | 998 | /* Only we change this so SMP safe */ | 
|  | 999 | return current->uid; | 
|  | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | asmlinkage long sys_geteuid(void) | 
|  | 1003 | { | 
|  | 1004 | /* Only we change this so SMP safe */ | 
|  | 1005 | return current->euid; | 
|  | 1006 | } | 
|  | 1007 |  | 
|  | 1008 | asmlinkage long sys_getgid(void) | 
|  | 1009 | { | 
|  | 1010 | /* Only we change this so SMP safe */ | 
|  | 1011 | return current->gid; | 
|  | 1012 | } | 
|  | 1013 |  | 
|  | 1014 | asmlinkage long sys_getegid(void) | 
|  | 1015 | { | 
|  | 1016 | /* Only we change this so SMP safe */ | 
|  | 1017 | return  current->egid; | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | #endif | 
|  | 1021 |  | 
|  | 1022 | static void process_timeout(unsigned long __data) | 
|  | 1023 | { | 
| Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1024 | wake_up_process((struct task_struct *)__data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } | 
|  | 1026 |  | 
|  | 1027 | /** | 
|  | 1028 | * schedule_timeout - sleep until timeout | 
|  | 1029 | * @timeout: timeout value in jiffies | 
|  | 1030 | * | 
|  | 1031 | * Make the current task sleep until @timeout jiffies have | 
|  | 1032 | * elapsed. The routine will return immediately unless | 
|  | 1033 | * the current task state has been set (see set_current_state()). | 
|  | 1034 | * | 
|  | 1035 | * You can set the task state as follows - | 
|  | 1036 | * | 
|  | 1037 | * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to | 
|  | 1038 | * pass before the routine returns. The routine will return 0 | 
|  | 1039 | * | 
|  | 1040 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | 
|  | 1041 | * delivered to the current task. In this case the remaining time | 
|  | 1042 | * in jiffies will be returned, or 0 if the timer expired in time | 
|  | 1043 | * | 
|  | 1044 | * The current task state is guaranteed to be TASK_RUNNING when this | 
|  | 1045 | * routine returns. | 
|  | 1046 | * | 
|  | 1047 | * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule | 
|  | 1048 | * the CPU away without a bound on the timeout. In this case the return | 
|  | 1049 | * value will be %MAX_SCHEDULE_TIMEOUT. | 
|  | 1050 | * | 
|  | 1051 | * In all cases the return value is guaranteed to be non-negative. | 
|  | 1052 | */ | 
| Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1053 | signed long __sched schedule_timeout(signed long timeout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { | 
|  | 1055 | struct timer_list timer; | 
|  | 1056 | unsigned long expire; | 
|  | 1057 |  | 
|  | 1058 | switch (timeout) | 
|  | 1059 | { | 
|  | 1060 | case MAX_SCHEDULE_TIMEOUT: | 
|  | 1061 | /* | 
|  | 1062 | * These two special cases are useful to be comfortable | 
|  | 1063 | * in the caller. Nothing more. We could take | 
|  | 1064 | * MAX_SCHEDULE_TIMEOUT from one of the negative value | 
|  | 1065 | * but I' d like to return a valid offset (>=0) to allow | 
|  | 1066 | * the caller to do everything it want with the retval. | 
|  | 1067 | */ | 
|  | 1068 | schedule(); | 
|  | 1069 | goto out; | 
|  | 1070 | default: | 
|  | 1071 | /* | 
|  | 1072 | * Another bit of PARANOID. Note that the retval will be | 
|  | 1073 | * 0 since no piece of kernel is supposed to do a check | 
|  | 1074 | * for a negative retval of schedule_timeout() (since it | 
|  | 1075 | * should never happens anyway). You just have the printk() | 
|  | 1076 | * that will tell you if something is gone wrong and where. | 
|  | 1077 | */ | 
| Andrew Morton | 5b149bc | 2006-12-22 01:10:14 -0800 | [diff] [blame] | 1078 | if (timeout < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | printk(KERN_ERR "schedule_timeout: wrong timeout " | 
| Andrew Morton | 5b149bc | 2006-12-22 01:10:14 -0800 | [diff] [blame] | 1080 | "value %lx\n", timeout); | 
|  | 1081 | dump_stack(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | current->state = TASK_RUNNING; | 
|  | 1083 | goto out; | 
|  | 1084 | } | 
|  | 1085 | } | 
|  | 1086 |  | 
|  | 1087 | expire = timeout + jiffies; | 
|  | 1088 |  | 
| Oleg Nesterov | a8db2db | 2005-10-30 15:01:38 -0800 | [diff] [blame] | 1089 | setup_timer(&timer, process_timeout, (unsigned long)current); | 
|  | 1090 | __mod_timer(&timer, expire); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | schedule(); | 
|  | 1092 | del_singleshot_timer_sync(&timer); | 
|  | 1093 |  | 
|  | 1094 | timeout = expire - jiffies; | 
|  | 1095 |  | 
|  | 1096 | out: | 
|  | 1097 | return timeout < 0 ? 0 : timeout; | 
|  | 1098 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | EXPORT_SYMBOL(schedule_timeout); | 
|  | 1100 |  | 
| Andrew Morton | 8a1c175 | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 1101 | /* | 
|  | 1102 | * We can use __set_current_state() here because schedule_timeout() calls | 
|  | 1103 | * schedule() unconditionally. | 
|  | 1104 | */ | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1105 | signed long __sched schedule_timeout_interruptible(signed long timeout) | 
|  | 1106 | { | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1107 | __set_current_state(TASK_INTERRUPTIBLE); | 
|  | 1108 | return schedule_timeout(timeout); | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1109 | } | 
|  | 1110 | EXPORT_SYMBOL(schedule_timeout_interruptible); | 
|  | 1111 |  | 
| Matthew Wilcox | 294d5cc | 2007-12-06 11:59:46 -0500 | [diff] [blame] | 1112 | signed long __sched schedule_timeout_killable(signed long timeout) | 
|  | 1113 | { | 
|  | 1114 | __set_current_state(TASK_KILLABLE); | 
|  | 1115 | return schedule_timeout(timeout); | 
|  | 1116 | } | 
|  | 1117 | EXPORT_SYMBOL(schedule_timeout_killable); | 
|  | 1118 |  | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1119 | signed long __sched schedule_timeout_uninterruptible(signed long timeout) | 
|  | 1120 | { | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1121 | __set_current_state(TASK_UNINTERRUPTIBLE); | 
|  | 1122 | return schedule_timeout(timeout); | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1123 | } | 
|  | 1124 | EXPORT_SYMBOL(schedule_timeout_uninterruptible); | 
|  | 1125 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | /* Thread ID - the internal kernel "pid" */ | 
|  | 1127 | asmlinkage long sys_gettid(void) | 
|  | 1128 | { | 
| Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1129 | return task_pid_vnr(current); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } | 
|  | 1131 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1132 | /** | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1133 | * do_sysinfo - fill in sysinfo struct | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1134 | * @info: pointer to buffer to fill | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 1135 | */ | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1136 | int do_sysinfo(struct sysinfo *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | unsigned long mem_total, sav_total; | 
|  | 1139 | unsigned int mem_unit, bitcount; | 
|  | 1140 | unsigned long seq; | 
|  | 1141 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1142 | memset(info, 0, sizeof(struct sysinfo)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 |  | 
|  | 1144 | do { | 
|  | 1145 | struct timespec tp; | 
|  | 1146 | seq = read_seqbegin(&xtime_lock); | 
|  | 1147 |  | 
|  | 1148 | /* | 
|  | 1149 | * This is annoying.  The below is the same thing | 
|  | 1150 | * posix_get_clock_monotonic() does, but it wants to | 
|  | 1151 | * take the lock which we want to cover the loads stuff | 
|  | 1152 | * too. | 
|  | 1153 | */ | 
|  | 1154 |  | 
|  | 1155 | getnstimeofday(&tp); | 
|  | 1156 | tp.tv_sec += wall_to_monotonic.tv_sec; | 
|  | 1157 | tp.tv_nsec += wall_to_monotonic.tv_nsec; | 
| Tomas Janousek | d621414 | 2007-07-15 23:39:42 -0700 | [diff] [blame] | 1158 | monotonic_to_bootbased(&tp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | if (tp.tv_nsec - NSEC_PER_SEC >= 0) { | 
|  | 1160 | tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC; | 
|  | 1161 | tp.tv_sec++; | 
|  | 1162 | } | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1163 | info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1165 | info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT); | 
|  | 1166 | info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT); | 
|  | 1167 | info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1169 | info->procs = nr_threads; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } while (read_seqretry(&xtime_lock, seq)); | 
|  | 1171 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1172 | si_meminfo(info); | 
|  | 1173 | si_swapinfo(info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 |  | 
|  | 1175 | /* | 
|  | 1176 | * If the sum of all the available memory (i.e. ram + swap) | 
|  | 1177 | * is less than can be stored in a 32 bit unsigned long then | 
|  | 1178 | * we can be binary compatible with 2.2.x kernels.  If not, | 
|  | 1179 | * well, in that case 2.2.x was broken anyways... | 
|  | 1180 | * | 
|  | 1181 | *  -Erik Andersen <andersee@debian.org> | 
|  | 1182 | */ | 
|  | 1183 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1184 | mem_total = info->totalram + info->totalswap; | 
|  | 1185 | if (mem_total < info->totalram || mem_total < info->totalswap) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | goto out; | 
|  | 1187 | bitcount = 0; | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1188 | mem_unit = info->mem_unit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | while (mem_unit > 1) { | 
|  | 1190 | bitcount++; | 
|  | 1191 | mem_unit >>= 1; | 
|  | 1192 | sav_total = mem_total; | 
|  | 1193 | mem_total <<= 1; | 
|  | 1194 | if (mem_total < sav_total) | 
|  | 1195 | goto out; | 
|  | 1196 | } | 
|  | 1197 |  | 
|  | 1198 | /* | 
|  | 1199 | * If mem_total did not overflow, multiply all memory values by | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1200 | * info->mem_unit and set it to 1.  This leaves things compatible | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | * with 2.2.x, and also retains compatibility with earlier 2.4.x | 
|  | 1202 | * kernels... | 
|  | 1203 | */ | 
|  | 1204 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1205 | info->mem_unit = 1; | 
|  | 1206 | info->totalram <<= bitcount; | 
|  | 1207 | info->freeram <<= bitcount; | 
|  | 1208 | info->sharedram <<= bitcount; | 
|  | 1209 | info->bufferram <<= bitcount; | 
|  | 1210 | info->totalswap <<= bitcount; | 
|  | 1211 | info->freeswap <<= bitcount; | 
|  | 1212 | info->totalhigh <<= bitcount; | 
|  | 1213 | info->freehigh <<= bitcount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1215 | out: | 
|  | 1216 | return 0; | 
|  | 1217 | } | 
|  | 1218 |  | 
|  | 1219 | asmlinkage long sys_sysinfo(struct sysinfo __user *info) | 
|  | 1220 | { | 
|  | 1221 | struct sysinfo val; | 
|  | 1222 |  | 
|  | 1223 | do_sysinfo(&val); | 
|  | 1224 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | if (copy_to_user(info, &val, sizeof(struct sysinfo))) | 
|  | 1226 | return -EFAULT; | 
|  | 1227 |  | 
|  | 1228 | return 0; | 
|  | 1229 | } | 
|  | 1230 |  | 
| Adrian Bunk | b4be625 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1231 | static int __cpuinit init_timers_cpu(int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | { | 
|  | 1233 | int j; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1234 | struct tvec_base *base; | 
| Adrian Bunk | b4be625 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1235 | static char __cpuinitdata tvec_base_done[NR_CPUS]; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1236 |  | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1237 | if (!tvec_base_done[cpu]) { | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1238 | static char boot_done; | 
|  | 1239 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1240 | if (boot_done) { | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1241 | /* | 
|  | 1242 | * The APs use this path later in boot | 
|  | 1243 | */ | 
| Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 1244 | base = kmalloc_node(sizeof(*base), | 
|  | 1245 | GFP_KERNEL | __GFP_ZERO, | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1246 | cpu_to_node(cpu)); | 
|  | 1247 | if (!base) | 
|  | 1248 | return -ENOMEM; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 1249 |  | 
|  | 1250 | /* Make sure that tvec_base is 2 byte aligned */ | 
|  | 1251 | if (tbase_get_deferrable(base)) { | 
|  | 1252 | WARN_ON(1); | 
|  | 1253 | kfree(base); | 
|  | 1254 | return -ENOMEM; | 
|  | 1255 | } | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1256 | per_cpu(tvec_bases, cpu) = base; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1257 | } else { | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1258 | /* | 
|  | 1259 | * This is for the boot CPU - we use compile-time | 
|  | 1260 | * static initialisation because per-cpu memory isn't | 
|  | 1261 | * ready yet and because the memory allocators are not | 
|  | 1262 | * initialised either. | 
|  | 1263 | */ | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1264 | boot_done = 1; | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1265 | base = &boot_tvec_bases; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1266 | } | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1267 | tvec_base_done[cpu] = 1; | 
|  | 1268 | } else { | 
|  | 1269 | base = per_cpu(tvec_bases, cpu); | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1270 | } | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1271 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1272 | spin_lock_init(&base->lock); | 
| Ingo Molnar | d730e88 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 1273 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | for (j = 0; j < TVN_SIZE; j++) { | 
|  | 1275 | INIT_LIST_HEAD(base->tv5.vec + j); | 
|  | 1276 | INIT_LIST_HEAD(base->tv4.vec + j); | 
|  | 1277 | INIT_LIST_HEAD(base->tv3.vec + j); | 
|  | 1278 | INIT_LIST_HEAD(base->tv2.vec + j); | 
|  | 1279 | } | 
|  | 1280 | for (j = 0; j < TVR_SIZE; j++) | 
|  | 1281 | INIT_LIST_HEAD(base->tv1.vec + j); | 
|  | 1282 |  | 
|  | 1283 | base->timer_jiffies = jiffies; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1284 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } | 
|  | 1286 |  | 
|  | 1287 | #ifdef CONFIG_HOTPLUG_CPU | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1288 | 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] | 1289 | { | 
|  | 1290 | struct timer_list *timer; | 
|  | 1291 |  | 
|  | 1292 | while (!list_empty(head)) { | 
| Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 1293 | timer = list_first_entry(head, struct timer_list, entry); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1294 | detach_timer(timer, 0); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 1295 | timer_set_base(timer, new_base); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | internal_add_timer(new_base, timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } | 
|  | 1299 |  | 
| Randy Dunlap | 48ccf3d | 2008-01-21 17:18:25 -0800 | [diff] [blame] | 1300 | static void __cpuinit migrate_timers(int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1302 | struct tvec_base *old_base; | 
|  | 1303 | struct tvec_base *new_base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | int i; | 
|  | 1305 |  | 
|  | 1306 | BUG_ON(cpu_online(cpu)); | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1307 | old_base = per_cpu(tvec_bases, cpu); | 
|  | 1308 | new_base = get_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 |  | 
|  | 1310 | local_irq_disable(); | 
| Oleg Nesterov | 0d18040 | 2008-04-04 20:54:10 +0200 | [diff] [blame] | 1311 | spin_lock(&new_base->lock); | 
|  | 1312 | spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1314 | BUG_ON(old_base->running_timer); | 
|  | 1315 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | for (i = 0; i < TVR_SIZE; i++) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1317 | migrate_timer_list(new_base, old_base->tv1.vec + i); | 
|  | 1318 | for (i = 0; i < TVN_SIZE; i++) { | 
|  | 1319 | migrate_timer_list(new_base, old_base->tv2.vec + i); | 
|  | 1320 | migrate_timer_list(new_base, old_base->tv3.vec + i); | 
|  | 1321 | migrate_timer_list(new_base, old_base->tv4.vec + i); | 
|  | 1322 | migrate_timer_list(new_base, old_base->tv5.vec + i); | 
|  | 1323 | } | 
|  | 1324 |  | 
| Oleg Nesterov | 0d18040 | 2008-04-04 20:54:10 +0200 | [diff] [blame] | 1325 | spin_unlock(&old_base->lock); | 
|  | 1326 | spin_unlock(&new_base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | local_irq_enable(); | 
|  | 1328 | put_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } | 
|  | 1330 | #endif /* CONFIG_HOTPLUG_CPU */ | 
|  | 1331 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1332 | static int __cpuinit timer_cpu_notify(struct notifier_block *self, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | unsigned long action, void *hcpu) | 
|  | 1334 | { | 
|  | 1335 | long cpu = (long)hcpu; | 
|  | 1336 | switch(action) { | 
|  | 1337 | case CPU_UP_PREPARE: | 
| Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1338 | case CPU_UP_PREPARE_FROZEN: | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1339 | if (init_timers_cpu(cpu) < 0) | 
|  | 1340 | return NOTIFY_BAD; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | break; | 
|  | 1342 | #ifdef CONFIG_HOTPLUG_CPU | 
|  | 1343 | case CPU_DEAD: | 
| Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1344 | case CPU_DEAD_FROZEN: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | migrate_timers(cpu); | 
|  | 1346 | break; | 
|  | 1347 | #endif | 
|  | 1348 | default: | 
|  | 1349 | break; | 
|  | 1350 | } | 
|  | 1351 | return NOTIFY_OK; | 
|  | 1352 | } | 
|  | 1353 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1354 | static struct notifier_block __cpuinitdata timers_nb = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | .notifier_call	= timer_cpu_notify, | 
|  | 1356 | }; | 
|  | 1357 |  | 
|  | 1358 |  | 
|  | 1359 | void __init init_timers(void) | 
|  | 1360 | { | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1361 | int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | (void *)(long)smp_processor_id()); | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1363 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1364 | init_timer_stats(); | 
|  | 1365 |  | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1366 | BUG_ON(err == NOTIFY_BAD); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | register_cpu_notifier(&timers_nb); | 
|  | 1368 | open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL); | 
|  | 1369 | } | 
|  | 1370 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | /** | 
|  | 1372 | * msleep - sleep safely even with waitqueue interruptions | 
|  | 1373 | * @msecs: Time in milliseconds to sleep for | 
|  | 1374 | */ | 
|  | 1375 | void msleep(unsigned int msecs) | 
|  | 1376 | { | 
|  | 1377 | unsigned long timeout = msecs_to_jiffies(msecs) + 1; | 
|  | 1378 |  | 
| Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 1379 | while (timeout) | 
|  | 1380 | timeout = schedule_timeout_uninterruptible(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | EXPORT_SYMBOL(msleep); | 
|  | 1384 |  | 
|  | 1385 | /** | 
| Domen Puncer | 96ec3ef | 2005-06-25 14:58:43 -0700 | [diff] [blame] | 1386 | * msleep_interruptible - sleep waiting for signals | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | * @msecs: Time in milliseconds to sleep for | 
|  | 1388 | */ | 
|  | 1389 | unsigned long msleep_interruptible(unsigned int msecs) | 
|  | 1390 | { | 
|  | 1391 | unsigned long timeout = msecs_to_jiffies(msecs) + 1; | 
|  | 1392 |  | 
| Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 1393 | while (timeout && !signal_pending(current)) | 
|  | 1394 | timeout = schedule_timeout_interruptible(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | return jiffies_to_msecs(timeout); | 
|  | 1396 | } | 
|  | 1397 |  | 
|  | 1398 | EXPORT_SYMBOL(msleep_interruptible); |