| 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 |  | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 323 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS | 
 | 324 |  | 
 | 325 | static struct debug_obj_descr timer_debug_descr; | 
 | 326 |  | 
 | 327 | /* | 
 | 328 |  * fixup_init is called when: | 
 | 329 |  * - an active object is initialized | 
 | 330 |  */ | 
 | 331 | static int timer_fixup_init(void *addr, enum debug_obj_state state) | 
 | 332 | { | 
 | 333 | 	struct timer_list *timer = addr; | 
 | 334 |  | 
 | 335 | 	switch (state) { | 
 | 336 | 	case ODEBUG_STATE_ACTIVE: | 
 | 337 | 		del_timer_sync(timer); | 
 | 338 | 		debug_object_init(timer, &timer_debug_descr); | 
 | 339 | 		return 1; | 
 | 340 | 	default: | 
 | 341 | 		return 0; | 
 | 342 | 	} | 
 | 343 | } | 
 | 344 |  | 
 | 345 | /* | 
 | 346 |  * fixup_activate is called when: | 
 | 347 |  * - an active object is activated | 
 | 348 |  * - an unknown object is activated (might be a statically initialized object) | 
 | 349 |  */ | 
 | 350 | static int timer_fixup_activate(void *addr, enum debug_obj_state state) | 
 | 351 | { | 
 | 352 | 	struct timer_list *timer = addr; | 
 | 353 |  | 
 | 354 | 	switch (state) { | 
 | 355 |  | 
 | 356 | 	case ODEBUG_STATE_NOTAVAILABLE: | 
 | 357 | 		/* | 
 | 358 | 		 * This is not really a fixup. The timer was | 
 | 359 | 		 * statically initialized. We just make sure that it | 
 | 360 | 		 * is tracked in the object tracker. | 
 | 361 | 		 */ | 
 | 362 | 		if (timer->entry.next == NULL && | 
 | 363 | 		    timer->entry.prev == TIMER_ENTRY_STATIC) { | 
 | 364 | 			debug_object_init(timer, &timer_debug_descr); | 
 | 365 | 			debug_object_activate(timer, &timer_debug_descr); | 
 | 366 | 			return 0; | 
 | 367 | 		} else { | 
 | 368 | 			WARN_ON_ONCE(1); | 
 | 369 | 		} | 
 | 370 | 		return 0; | 
 | 371 |  | 
 | 372 | 	case ODEBUG_STATE_ACTIVE: | 
 | 373 | 		WARN_ON(1); | 
 | 374 |  | 
 | 375 | 	default: | 
 | 376 | 		return 0; | 
 | 377 | 	} | 
 | 378 | } | 
 | 379 |  | 
 | 380 | /* | 
 | 381 |  * fixup_free is called when: | 
 | 382 |  * - an active object is freed | 
 | 383 |  */ | 
 | 384 | static int timer_fixup_free(void *addr, enum debug_obj_state state) | 
 | 385 | { | 
 | 386 | 	struct timer_list *timer = addr; | 
 | 387 |  | 
 | 388 | 	switch (state) { | 
 | 389 | 	case ODEBUG_STATE_ACTIVE: | 
 | 390 | 		del_timer_sync(timer); | 
 | 391 | 		debug_object_free(timer, &timer_debug_descr); | 
 | 392 | 		return 1; | 
 | 393 | 	default: | 
 | 394 | 		return 0; | 
 | 395 | 	} | 
 | 396 | } | 
 | 397 |  | 
 | 398 | static struct debug_obj_descr timer_debug_descr = { | 
 | 399 | 	.name		= "timer_list", | 
 | 400 | 	.fixup_init	= timer_fixup_init, | 
 | 401 | 	.fixup_activate	= timer_fixup_activate, | 
 | 402 | 	.fixup_free	= timer_fixup_free, | 
 | 403 | }; | 
 | 404 |  | 
 | 405 | static inline void debug_timer_init(struct timer_list *timer) | 
 | 406 | { | 
 | 407 | 	debug_object_init(timer, &timer_debug_descr); | 
 | 408 | } | 
 | 409 |  | 
 | 410 | static inline void debug_timer_activate(struct timer_list *timer) | 
 | 411 | { | 
 | 412 | 	debug_object_activate(timer, &timer_debug_descr); | 
 | 413 | } | 
 | 414 |  | 
 | 415 | static inline void debug_timer_deactivate(struct timer_list *timer) | 
 | 416 | { | 
 | 417 | 	debug_object_deactivate(timer, &timer_debug_descr); | 
 | 418 | } | 
 | 419 |  | 
 | 420 | static inline void debug_timer_free(struct timer_list *timer) | 
 | 421 | { | 
 | 422 | 	debug_object_free(timer, &timer_debug_descr); | 
 | 423 | } | 
 | 424 |  | 
 | 425 | static void __init_timer(struct timer_list *timer); | 
 | 426 |  | 
 | 427 | void init_timer_on_stack(struct timer_list *timer) | 
 | 428 | { | 
 | 429 | 	debug_object_init_on_stack(timer, &timer_debug_descr); | 
 | 430 | 	__init_timer(timer); | 
 | 431 | } | 
 | 432 | EXPORT_SYMBOL_GPL(init_timer_on_stack); | 
 | 433 |  | 
 | 434 | void destroy_timer_on_stack(struct timer_list *timer) | 
 | 435 | { | 
 | 436 | 	debug_object_free(timer, &timer_debug_descr); | 
 | 437 | } | 
 | 438 | EXPORT_SYMBOL_GPL(destroy_timer_on_stack); | 
 | 439 |  | 
 | 440 | #else | 
 | 441 | static inline void debug_timer_init(struct timer_list *timer) { } | 
 | 442 | static inline void debug_timer_activate(struct timer_list *timer) { } | 
 | 443 | static inline void debug_timer_deactivate(struct timer_list *timer) { } | 
 | 444 | #endif | 
 | 445 |  | 
 | 446 | static void __init_timer(struct timer_list *timer) | 
 | 447 | { | 
 | 448 | 	timer->entry.next = NULL; | 
 | 449 | 	timer->base = __raw_get_cpu_var(tvec_bases); | 
 | 450 | #ifdef CONFIG_TIMER_STATS | 
 | 451 | 	timer->start_site = NULL; | 
 | 452 | 	timer->start_pid = -1; | 
 | 453 | 	memset(timer->start_comm, 0, TASK_COMM_LEN); | 
 | 454 | #endif | 
 | 455 | } | 
 | 456 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 457 | /** | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 458 |  * init_timer - initialize a timer. | 
 | 459 |  * @timer: the timer to be initialized | 
 | 460 |  * | 
 | 461 |  * init_timer() must be done to a timer prior calling *any* of the | 
 | 462 |  * other timer functions. | 
 | 463 |  */ | 
| Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 464 | void init_timer(struct timer_list *timer) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 465 | { | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 466 | 	debug_timer_init(timer); | 
 | 467 | 	__init_timer(timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 468 | } | 
 | 469 | EXPORT_SYMBOL(init_timer); | 
 | 470 |  | 
| Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 471 | void init_timer_deferrable(struct timer_list *timer) | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 472 | { | 
 | 473 | 	init_timer(timer); | 
 | 474 | 	timer_set_deferrable(timer); | 
 | 475 | } | 
 | 476 | EXPORT_SYMBOL(init_timer_deferrable); | 
 | 477 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 478 | static inline void detach_timer(struct timer_list *timer, | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 479 | 				int clear_pending) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 480 | { | 
 | 481 | 	struct list_head *entry = &timer->entry; | 
 | 482 |  | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 483 | 	debug_timer_deactivate(timer); | 
 | 484 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 485 | 	__list_del(entry->prev, entry->next); | 
 | 486 | 	if (clear_pending) | 
 | 487 | 		entry->next = NULL; | 
 | 488 | 	entry->prev = LIST_POISON2; | 
 | 489 | } | 
 | 490 |  | 
 | 491 | /* | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 492 |  * We are using hashed locking: holding per_cpu(tvec_bases).lock | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 493 |  * means that all timers which are tied to this base via timer->base are | 
 | 494 |  * locked, and the base itself is locked too. | 
 | 495 |  * | 
 | 496 |  * So __run_timers/migrate_timers can safely modify all timers which could | 
 | 497 |  * be found on ->tvX lists. | 
 | 498 |  * | 
 | 499 |  * When the timer's base is locked, and the timer removed from list, it is | 
 | 500 |  * possible to set timer->base = NULL and drop the lock: the timer remains | 
 | 501 |  * locked. | 
 | 502 |  */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 503 | static struct tvec_base *lock_timer_base(struct timer_list *timer, | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 504 | 					unsigned long *flags) | 
| Josh Triplett | 89e7e374 | 2006-09-29 01:59:36 -0700 | [diff] [blame] | 505 | 	__acquires(timer->base->lock) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 506 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 507 | 	struct tvec_base *base; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 508 |  | 
 | 509 | 	for (;;) { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 510 | 		struct tvec_base *prelock_base = timer->base; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 511 | 		base = tbase_get_base(prelock_base); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 512 | 		if (likely(base != NULL)) { | 
 | 513 | 			spin_lock_irqsave(&base->lock, *flags); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 514 | 			if (likely(prelock_base == timer->base)) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 515 | 				return base; | 
 | 516 | 			/* The timer has migrated to another CPU */ | 
 | 517 | 			spin_unlock_irqrestore(&base->lock, *flags); | 
 | 518 | 		} | 
 | 519 | 		cpu_relax(); | 
 | 520 | 	} | 
 | 521 | } | 
 | 522 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | int __mod_timer(struct timer_list *timer, unsigned long expires) | 
 | 524 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 525 | 	struct tvec_base *base, *new_base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | 	unsigned long flags; | 
 | 527 | 	int ret = 0; | 
 | 528 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 529 | 	timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | 	BUG_ON(!timer->function); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 532 | 	base = lock_timer_base(timer, &flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 534 | 	if (timer_pending(timer)) { | 
 | 535 | 		detach_timer(timer, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | 		ret = 1; | 
 | 537 | 	} | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 538 |  | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 539 | 	debug_timer_activate(timer); | 
 | 540 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 541 | 	new_base = __get_cpu_var(tvec_bases); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 542 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 543 | 	if (base != new_base) { | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 544 | 		/* | 
 | 545 | 		 * We are trying to schedule the timer on the local CPU. | 
 | 546 | 		 * However we can't change timer's base while it is running, | 
 | 547 | 		 * otherwise del_timer_sync() can't detect that the timer's | 
 | 548 | 		 * handler yet has not finished. This also guarantees that | 
 | 549 | 		 * the timer is serialized wrt itself. | 
 | 550 | 		 */ | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 551 | 		if (likely(base->running_timer != timer)) { | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 552 | 			/* See the comment in lock_timer_base() */ | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 553 | 			timer_set_base(timer, NULL); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 554 | 			spin_unlock(&base->lock); | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 555 | 			base = new_base; | 
 | 556 | 			spin_lock(&base->lock); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 557 | 			timer_set_base(timer, base); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 558 | 		} | 
 | 559 | 	} | 
 | 560 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | 	timer->expires = expires; | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 562 | 	internal_add_timer(base, timer); | 
 | 563 | 	spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 |  | 
 | 565 | 	return ret; | 
 | 566 | } | 
 | 567 |  | 
 | 568 | EXPORT_SYMBOL(__mod_timer); | 
 | 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 |  * add_timer_on - start a timer on a particular CPU | 
 | 572 |  * @timer: the timer to be added | 
 | 573 |  * @cpu: the CPU to start it on | 
 | 574 |  * | 
 | 575 |  * This is not very scalable on SMP. Double adds are not possible. | 
 | 576 |  */ | 
 | 577 | void add_timer_on(struct timer_list *timer, int cpu) | 
 | 578 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 579 | 	struct tvec_base *base = per_cpu(tvec_bases, cpu); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 580 | 	unsigned long flags; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 581 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 582 | 	timer_stats_timer_set_start_info(timer); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 583 | 	BUG_ON(timer_pending(timer) || !timer->function); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 584 | 	spin_lock_irqsave(&base->lock, flags); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 585 | 	timer_set_base(timer, base); | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 586 | 	debug_timer_activate(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | 	internal_add_timer(base, timer); | 
| Thomas Gleixner | 06d8308 | 2008-03-22 09:20:24 +0100 | [diff] [blame] | 588 | 	/* | 
 | 589 | 	 * Check whether the other CPU is idle and needs to be | 
 | 590 | 	 * triggered to reevaluate the timer wheel when nohz is | 
 | 591 | 	 * active. We are protected against the other CPU fiddling | 
 | 592 | 	 * with the timer by holding the timer base lock. This also | 
 | 593 | 	 * makes sure that a CPU on the way to idle can not evaluate | 
 | 594 | 	 * the timer wheel. | 
 | 595 | 	 */ | 
 | 596 | 	wake_up_idle_cpu(cpu); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 597 | 	spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } | 
 | 599 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 600 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 |  * mod_timer - modify a timer's timeout | 
 | 602 |  * @timer: the timer to be modified | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 603 |  * @expires: new timeout in jiffies | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 605 |  * 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] | 606 |  * active timer (if the timer is inactive it will be activated) | 
 | 607 |  * | 
 | 608 |  * mod_timer(timer, expires) is equivalent to: | 
 | 609 |  * | 
 | 610 |  *     del_timer(timer); timer->expires = expires; add_timer(timer); | 
 | 611 |  * | 
 | 612 |  * Note that if there are multiple unserialized concurrent users of the | 
 | 613 |  * same timer, then mod_timer() is the only safe way to modify the timeout, | 
 | 614 |  * since add_timer() cannot modify an already running timer. | 
 | 615 |  * | 
 | 616 |  * The function returns whether it has modified a pending timer or not. | 
 | 617 |  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an | 
 | 618 |  * active timer returns 1.) | 
 | 619 |  */ | 
 | 620 | int mod_timer(struct timer_list *timer, unsigned long expires) | 
 | 621 | { | 
 | 622 | 	BUG_ON(!timer->function); | 
 | 623 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 624 | 	timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | 	/* | 
 | 626 | 	 * This is a common optimization triggered by the | 
 | 627 | 	 * networking code - if the timer is re-modified | 
 | 628 | 	 * to be the same thing then just return: | 
 | 629 | 	 */ | 
 | 630 | 	if (timer->expires == expires && timer_pending(timer)) | 
 | 631 | 		return 1; | 
 | 632 |  | 
 | 633 | 	return __mod_timer(timer, expires); | 
 | 634 | } | 
 | 635 |  | 
 | 636 | EXPORT_SYMBOL(mod_timer); | 
 | 637 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 638 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 |  * del_timer - deactive a timer. | 
 | 640 |  * @timer: the timer to be deactivated | 
 | 641 |  * | 
 | 642 |  * del_timer() deactivates a timer - this works on both active and inactive | 
 | 643 |  * timers. | 
 | 644 |  * | 
 | 645 |  * The function returns whether it has deactivated a pending timer or not. | 
 | 646 |  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an | 
 | 647 |  * active timer returns 1.) | 
 | 648 |  */ | 
 | 649 | int del_timer(struct timer_list *timer) | 
 | 650 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 651 | 	struct tvec_base *base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | 	unsigned long flags; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 653 | 	int ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 655 | 	timer_stats_timer_clear_start_info(timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 656 | 	if (timer_pending(timer)) { | 
 | 657 | 		base = lock_timer_base(timer, &flags); | 
 | 658 | 		if (timer_pending(timer)) { | 
 | 659 | 			detach_timer(timer, 1); | 
 | 660 | 			ret = 1; | 
 | 661 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | 		spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 665 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } | 
 | 667 |  | 
 | 668 | EXPORT_SYMBOL(del_timer); | 
 | 669 |  | 
 | 670 | #ifdef CONFIG_SMP | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 671 | /** | 
 | 672 |  * try_to_del_timer_sync - Try to deactivate a timer | 
 | 673 |  * @timer: timer do del | 
 | 674 |  * | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 675 |  * This function tries to deactivate a timer. Upon successful (ret >= 0) | 
 | 676 |  * exit the timer is not queued and the handler is not running on any CPU. | 
 | 677 |  * | 
 | 678 |  * It must not be called from interrupt contexts. | 
 | 679 |  */ | 
 | 680 | int try_to_del_timer_sync(struct timer_list *timer) | 
 | 681 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 682 | 	struct tvec_base *base; | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 683 | 	unsigned long flags; | 
 | 684 | 	int ret = -1; | 
 | 685 |  | 
 | 686 | 	base = lock_timer_base(timer, &flags); | 
 | 687 |  | 
 | 688 | 	if (base->running_timer == timer) | 
 | 689 | 		goto out; | 
 | 690 |  | 
 | 691 | 	ret = 0; | 
 | 692 | 	if (timer_pending(timer)) { | 
 | 693 | 		detach_timer(timer, 1); | 
 | 694 | 		ret = 1; | 
 | 695 | 	} | 
 | 696 | out: | 
 | 697 | 	spin_unlock_irqrestore(&base->lock, flags); | 
 | 698 |  | 
 | 699 | 	return ret; | 
 | 700 | } | 
 | 701 |  | 
| David Howells | e19dff1 | 2007-04-26 15:46:56 -0700 | [diff] [blame] | 702 | EXPORT_SYMBOL(try_to_del_timer_sync); | 
 | 703 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 704 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 |  * del_timer_sync - deactivate a timer and wait for the handler to finish. | 
 | 706 |  * @timer: the timer to be deactivated | 
 | 707 |  * | 
 | 708 |  * This function only differs from del_timer() on SMP: besides deactivating | 
 | 709 |  * the timer it also makes sure the handler has finished executing on other | 
 | 710 |  * CPUs. | 
 | 711 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 712 |  * Synchronization rules: Callers must prevent restarting of the timer, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 |  * otherwise this function is meaningless. It must not be called from | 
 | 714 |  * interrupt contexts. The caller must not hold locks which would prevent | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 715 |  * completion of the timer's handler. The timer's handler must not call | 
 | 716 |  * add_timer_on(). Upon exit the timer is not queued and the handler is | 
 | 717 |  * not running on any CPU. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 |  * | 
 | 719 |  * The function returns whether it has deactivated a pending timer or not. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 |  */ | 
 | 721 | int del_timer_sync(struct timer_list *timer) | 
 | 722 | { | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 723 | 	for (;;) { | 
 | 724 | 		int ret = try_to_del_timer_sync(timer); | 
 | 725 | 		if (ret >= 0) | 
 | 726 | 			return ret; | 
| Andrew Morton | a000965 | 2006-07-14 00:24:06 -0700 | [diff] [blame] | 727 | 		cpu_relax(); | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 728 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 730 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | EXPORT_SYMBOL(del_timer_sync); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | #endif | 
 | 733 |  | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 734 | static int cascade(struct tvec_base *base, struct tvec *tv, int index) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { | 
 | 736 | 	/* cascade all the timers from tv up one level */ | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 737 | 	struct timer_list *timer, *tmp; | 
 | 738 | 	struct list_head tv_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 |  | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 740 | 	list_replace_init(tv->vec + index, &tv_list); | 
 | 741 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | 	/* | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 743 | 	 * We are removing _all_ timers from the list, so we | 
 | 744 | 	 * don't have to detach them individually. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | 	 */ | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 746 | 	list_for_each_entry_safe(timer, tmp, &tv_list, entry) { | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 747 | 		BUG_ON(tbase_get_base(timer->base) != base); | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 748 | 		internal_add_timer(base, timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 |  | 
 | 751 | 	return index; | 
 | 752 | } | 
 | 753 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 754 | #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) | 
 | 755 |  | 
 | 756 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 |  * __run_timers - run all expired timers (if any) on this CPU. | 
 | 758 |  * @base: the timer vector to be processed. | 
 | 759 |  * | 
 | 760 |  * This function cascades all vectors and executes all expired timer | 
 | 761 |  * vectors. | 
 | 762 |  */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 763 | static inline void __run_timers(struct tvec_base *base) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { | 
 | 765 | 	struct timer_list *timer; | 
 | 766 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 767 | 	spin_lock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | 	while (time_after_eq(jiffies, base->timer_jiffies)) { | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 769 | 		struct list_head work_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | 		struct list_head *head = &work_list; | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 771 | 		int index = base->timer_jiffies & TVR_MASK; | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 772 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | 		/* | 
 | 774 | 		 * Cascade timers: | 
 | 775 | 		 */ | 
 | 776 | 		if (!index && | 
 | 777 | 			(!cascade(base, &base->tv2, INDEX(0))) && | 
 | 778 | 				(!cascade(base, &base->tv3, INDEX(1))) && | 
 | 779 | 					!cascade(base, &base->tv4, INDEX(2))) | 
 | 780 | 			cascade(base, &base->tv5, INDEX(3)); | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 781 | 		++base->timer_jiffies; | 
 | 782 | 		list_replace_init(base->tv1.vec + index, &work_list); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 783 | 		while (!list_empty(head)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | 			void (*fn)(unsigned long); | 
 | 785 | 			unsigned long data; | 
 | 786 |  | 
| Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 787 | 			timer = list_first_entry(head, struct timer_list,entry); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 788 | 			fn = timer->function; | 
 | 789 | 			data = timer->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 791 | 			timer_stats_account_timer(timer); | 
 | 792 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | 			set_running_timer(base, timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 794 | 			detach_timer(timer, 1); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 795 | 			spin_unlock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | 			{ | 
| Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 797 | 				int preempt_count = preempt_count(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | 				fn(data); | 
 | 799 | 				if (preempt_count != preempt_count()) { | 
| Pavel Machek | 4c9dc64 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 800 | 					printk(KERN_ERR "huh, entered %p " | 
| Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 801 | 					       "with preempt_count %08x, exited" | 
 | 802 | 					       " with %08x?\n", | 
 | 803 | 					       fn, preempt_count, | 
 | 804 | 					       preempt_count()); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | 					BUG(); | 
 | 806 | 				} | 
 | 807 | 			} | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 808 | 			spin_lock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | 		} | 
 | 810 | 	} | 
 | 811 | 	set_running_timer(base, NULL); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 812 | 	spin_unlock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } | 
 | 814 |  | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 815 | #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | /* | 
 | 817 |  * Find out when the next timer event is due to happen. This | 
 | 818 |  * is used on S/390 to stop all activity when a cpus is idle. | 
 | 819 |  * This functions needs to be called disabled. | 
 | 820 |  */ | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 821 | static unsigned long __next_timer_interrupt(struct tvec_base *base) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 823 | 	unsigned long timer_jiffies = base->timer_jiffies; | 
| Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 824 | 	unsigned long expires = timer_jiffies + NEXT_TIMER_MAX_DELTA; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 825 | 	int index, slot, array, found = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | 	struct timer_list *nte; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 827 | 	struct tvec *varray[4]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 |  | 
 | 829 | 	/* Look for timer events in tv1. */ | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 830 | 	index = slot = timer_jiffies & TVR_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | 	do { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 832 | 		list_for_each_entry(nte, base->tv1.vec + slot, entry) { | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 833 | 			if (tbase_get_deferrable(nte->base)) | 
 | 834 | 				continue; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 835 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 836 | 			found = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | 			expires = nte->expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 838 | 			/* Look at the cascade bucket(s)? */ | 
 | 839 | 			if (!index || slot < index) | 
 | 840 | 				goto cascade; | 
 | 841 | 			return expires; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | 		} | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 843 | 		slot = (slot + 1) & TVR_MASK; | 
 | 844 | 	} while (slot != index); | 
 | 845 |  | 
 | 846 | cascade: | 
 | 847 | 	/* Calculate the next cascade event */ | 
 | 848 | 	if (index) | 
 | 849 | 		timer_jiffies += TVR_SIZE - index; | 
 | 850 | 	timer_jiffies >>= TVR_BITS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 |  | 
 | 852 | 	/* Check tv2-tv5. */ | 
 | 853 | 	varray[0] = &base->tv2; | 
 | 854 | 	varray[1] = &base->tv3; | 
 | 855 | 	varray[2] = &base->tv4; | 
 | 856 | 	varray[3] = &base->tv5; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 857 |  | 
 | 858 | 	for (array = 0; array < 4; array++) { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 859 | 		struct tvec *varp = varray[array]; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 860 |  | 
 | 861 | 		index = slot = timer_jiffies & TVN_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | 		do { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 863 | 			list_for_each_entry(nte, varp->vec + slot, entry) { | 
 | 864 | 				found = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | 				if (time_before(nte->expires, expires)) | 
 | 866 | 					expires = nte->expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 867 | 			} | 
 | 868 | 			/* | 
 | 869 | 			 * Do we still search for the first timer or are | 
 | 870 | 			 * we looking up the cascade buckets ? | 
 | 871 | 			 */ | 
 | 872 | 			if (found) { | 
 | 873 | 				/* Look at the cascade bucket(s)? */ | 
 | 874 | 				if (!index || slot < index) | 
 | 875 | 					break; | 
 | 876 | 				return expires; | 
 | 877 | 			} | 
 | 878 | 			slot = (slot + 1) & TVN_MASK; | 
 | 879 | 		} while (slot != index); | 
 | 880 |  | 
 | 881 | 		if (index) | 
 | 882 | 			timer_jiffies += TVN_SIZE - index; | 
 | 883 | 		timer_jiffies >>= TVN_BITS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | 	} | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 885 | 	return expires; | 
 | 886 | } | 
 | 887 |  | 
 | 888 | /* | 
 | 889 |  * Check, if the next hrtimer event is before the next timer wheel | 
 | 890 |  * event: | 
 | 891 |  */ | 
 | 892 | static unsigned long cmp_next_hrtimer_event(unsigned long now, | 
 | 893 | 					    unsigned long expires) | 
 | 894 | { | 
 | 895 | 	ktime_t hr_delta = hrtimer_get_next_event(); | 
 | 896 | 	struct timespec tsdelta; | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 897 | 	unsigned long delta; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 898 |  | 
 | 899 | 	if (hr_delta.tv64 == KTIME_MAX) | 
 | 900 | 		return expires; | 
 | 901 |  | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 902 | 	/* | 
 | 903 | 	 * Expired timer available, let it expire in the next tick | 
 | 904 | 	 */ | 
 | 905 | 	if (hr_delta.tv64 <= 0) | 
 | 906 | 		return now + 1; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 907 |  | 
 | 908 | 	tsdelta = ktime_to_timespec(hr_delta); | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 909 | 	delta = timespec_to_jiffies(&tsdelta); | 
| Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 910 |  | 
 | 911 | 	/* | 
 | 912 | 	 * Limit the delta to the max value, which is checked in | 
 | 913 | 	 * tick_nohz_stop_sched_tick(): | 
 | 914 | 	 */ | 
 | 915 | 	if (delta > NEXT_TIMER_MAX_DELTA) | 
 | 916 | 		delta = NEXT_TIMER_MAX_DELTA; | 
 | 917 |  | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 918 | 	/* | 
 | 919 | 	 * Take rounding errors in to account and make sure, that it | 
 | 920 | 	 * expires in the next tick. Otherwise we go into an endless | 
 | 921 | 	 * ping pong due to tick_nohz_stop_sched_tick() retriggering | 
 | 922 | 	 * the timer softirq | 
 | 923 | 	 */ | 
 | 924 | 	if (delta < 1) | 
 | 925 | 		delta = 1; | 
 | 926 | 	now += delta; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 927 | 	if (time_before(now, expires)) | 
 | 928 | 		return now; | 
 | 929 | 	return expires; | 
 | 930 | } | 
 | 931 |  | 
 | 932 | /** | 
| Li Zefan | 8dce39c | 2007-11-05 14:51:10 -0800 | [diff] [blame] | 933 |  * get_next_timer_interrupt - return the jiffy of the next pending timer | 
| Randy Dunlap | 05fb6bf | 2007-02-28 20:12:13 -0800 | [diff] [blame] | 934 |  * @now: current time (in jiffies) | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 935 |  */ | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 936 | unsigned long get_next_timer_interrupt(unsigned long now) | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 937 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 938 | 	struct tvec_base *base = __get_cpu_var(tvec_bases); | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 939 | 	unsigned long expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 940 |  | 
 | 941 | 	spin_lock(&base->lock); | 
 | 942 | 	expires = __next_timer_interrupt(base); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 943 | 	spin_unlock(&base->lock); | 
| Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 944 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 945 | 	if (time_before_eq(expires, now)) | 
 | 946 | 		return now; | 
| Zachary Amsden | 0662b71 | 2006-05-20 15:00:24 -0700 | [diff] [blame] | 947 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 948 | 	return cmp_next_hrtimer_event(now, expires); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | } | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 950 |  | 
 | 951 | #ifdef CONFIG_NO_IDLE_HZ | 
 | 952 | unsigned long next_timer_interrupt(void) | 
 | 953 | { | 
 | 954 | 	return get_next_timer_interrupt(jiffies); | 
 | 955 | } | 
 | 956 | #endif | 
 | 957 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | #endif | 
 | 959 |  | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 960 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING | 
 | 961 | void account_process_tick(struct task_struct *p, int user_tick) | 
 | 962 | { | 
| Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 963 | 	cputime_t one_jiffy = jiffies_to_cputime(1); | 
 | 964 |  | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 965 | 	if (user_tick) { | 
| Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 966 | 		account_user_time(p, one_jiffy); | 
 | 967 | 		account_user_time_scaled(p, cputime_to_scaled(one_jiffy)); | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 968 | 	} else { | 
| Michael Neuling | 06b8e87 | 2008-02-06 01:36:12 -0800 | [diff] [blame] | 969 | 		account_system_time(p, HARDIRQ_OFFSET, one_jiffy); | 
 | 970 | 		account_system_time_scaled(p, cputime_to_scaled(one_jiffy)); | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 971 | 	} | 
 | 972 | } | 
 | 973 | #endif | 
 | 974 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* | 
| Daniel Walker | 5b4db0c | 2007-10-18 03:06:11 -0700 | [diff] [blame] | 976 |  * 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] | 977 |  * process.  user_tick is 1 if the tick is user time, 0 for system. | 
 | 978 |  */ | 
 | 979 | void update_process_times(int user_tick) | 
 | 980 | { | 
 | 981 | 	struct task_struct *p = current; | 
 | 982 | 	int cpu = smp_processor_id(); | 
 | 983 |  | 
 | 984 | 	/* Note: this timer irq context must be accounted for as well. */ | 
| Paul Mackerras | fa13a5a | 2007-11-09 22:39:38 +0100 | [diff] [blame] | 985 | 	account_process_tick(p, user_tick); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | 	run_local_timers(); | 
 | 987 | 	if (rcu_pending(cpu)) | 
 | 988 | 		rcu_check_callbacks(cpu, user_tick); | 
 | 989 | 	scheduler_tick(); | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 990 | 	run_posix_cpu_timers(p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } | 
 | 992 |  | 
 | 993 | /* | 
 | 994 |  * Nr of active tasks - counted in fixed-point numbers | 
 | 995 |  */ | 
 | 996 | static unsigned long count_active_tasks(void) | 
 | 997 | { | 
| Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 998 | 	return nr_active() * FIXED_1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } | 
 | 1000 |  | 
 | 1001 | /* | 
 | 1002 |  * Hmm.. Changed this, as the GNU make sources (load.c) seems to | 
 | 1003 |  * imply that avenrun[] is the standard name for this kind of thing. | 
 | 1004 |  * Nothing else seems to be standardized: the fractional size etc | 
 | 1005 |  * all seem to differ on different machines. | 
 | 1006 |  * | 
 | 1007 |  * Requires xtime_lock to access. | 
 | 1008 |  */ | 
 | 1009 | unsigned long avenrun[3]; | 
 | 1010 |  | 
 | 1011 | EXPORT_SYMBOL(avenrun); | 
 | 1012 |  | 
 | 1013 | /* | 
 | 1014 |  * calc_load - given tick count, update the avenrun load estimates. | 
 | 1015 |  * This is called while holding a write_lock on xtime_lock. | 
 | 1016 |  */ | 
 | 1017 | static inline void calc_load(unsigned long ticks) | 
 | 1018 | { | 
 | 1019 | 	unsigned long active_tasks; /* fixed-point */ | 
 | 1020 | 	static int count = LOAD_FREQ; | 
 | 1021 |  | 
| Eric Dumazet | cd7175e | 2006-12-13 00:35:45 -0800 | [diff] [blame] | 1022 | 	count -= ticks; | 
 | 1023 | 	if (unlikely(count < 0)) { | 
 | 1024 | 		active_tasks = count_active_tasks(); | 
 | 1025 | 		do { | 
 | 1026 | 			CALC_LOAD(avenrun[0], EXP_1, active_tasks); | 
 | 1027 | 			CALC_LOAD(avenrun[1], EXP_5, active_tasks); | 
 | 1028 | 			CALC_LOAD(avenrun[2], EXP_15, active_tasks); | 
 | 1029 | 			count += LOAD_FREQ; | 
 | 1030 | 		} while (count < 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | 	} | 
 | 1032 | } | 
 | 1033 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 |  * This function runs timers and the timer-tq in bottom half context. | 
 | 1036 |  */ | 
 | 1037 | static void run_timer_softirq(struct softirq_action *h) | 
 | 1038 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1039 | 	struct tvec_base *base = __get_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 |  | 
| Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1041 | 	hrtimer_run_pending(); | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1042 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | 	if (time_after_eq(jiffies, base->timer_jiffies)) | 
 | 1044 | 		__run_timers(base); | 
 | 1045 | } | 
 | 1046 |  | 
 | 1047 | /* | 
 | 1048 |  * Called by the local, per-CPU timer interrupt on SMP. | 
 | 1049 |  */ | 
 | 1050 | void run_local_timers(void) | 
 | 1051 | { | 
| Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1052 | 	hrtimer_run_queues(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | 	raise_softirq(TIMER_SOFTIRQ); | 
| Ingo Molnar | 6687a97 | 2006-03-24 03:18:41 -0800 | [diff] [blame] | 1054 | 	softlockup_tick(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } | 
 | 1056 |  | 
 | 1057 | /* | 
 | 1058 |  * Called by the timer interrupt. xtime_lock must already be taken | 
 | 1059 |  * by the timer IRQ! | 
 | 1060 |  */ | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 1061 | static inline void update_times(unsigned long ticks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1063 | 	update_wall_time(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | 	calc_load(ticks); | 
 | 1065 | } | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 1066 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | /* | 
 | 1068 |  * The 64-bit jiffies value is not atomic - you MUST NOT read it | 
 | 1069 |  * without sampling the sequence number in xtime_lock. | 
 | 1070 |  * jiffies is defined in the linker script... | 
 | 1071 |  */ | 
 | 1072 |  | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 1073 | void do_timer(unsigned long ticks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | { | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 1075 | 	jiffies_64 += ticks; | 
 | 1076 | 	update_times(ticks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } | 
 | 1078 |  | 
 | 1079 | #ifdef __ARCH_WANT_SYS_ALARM | 
 | 1080 |  | 
 | 1081 | /* | 
 | 1082 |  * For backwards compatibility?  This can be done in libc so Alpha | 
 | 1083 |  * and all newer ports shouldn't need it. | 
 | 1084 |  */ | 
 | 1085 | asmlinkage unsigned long sys_alarm(unsigned int seconds) | 
 | 1086 | { | 
| Thomas Gleixner | c08b8a4 | 2006-03-25 03:06:33 -0800 | [diff] [blame] | 1087 | 	return alarm_setitimer(seconds); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } | 
 | 1089 |  | 
 | 1090 | #endif | 
 | 1091 |  | 
 | 1092 | #ifndef __alpha__ | 
 | 1093 |  | 
 | 1094 | /* | 
 | 1095 |  * The Alpha uses getxpid, getxuid, and getxgid instead.  Maybe this | 
 | 1096 |  * should be moved into arch/i386 instead? | 
 | 1097 |  */ | 
 | 1098 |  | 
 | 1099 | /** | 
 | 1100 |  * sys_getpid - return the thread group id of the current process | 
 | 1101 |  * | 
 | 1102 |  * Note, despite the name, this returns the tgid not the pid.  The tgid and | 
 | 1103 |  * the pid are identical unless CLONE_THREAD was specified on clone() in | 
 | 1104 |  * which case the tgid is the same in all threads of the same group. | 
 | 1105 |  * | 
 | 1106 |  * This is SMP safe as current->tgid does not change. | 
 | 1107 |  */ | 
 | 1108 | asmlinkage long sys_getpid(void) | 
 | 1109 | { | 
| Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1110 | 	return task_tgid_vnr(current); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | } | 
 | 1112 |  | 
 | 1113 | /* | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 1114 |  * Accessing ->real_parent is not SMP-safe, it could | 
 | 1115 |  * change from under us. However, we can use a stale | 
 | 1116 |  * value of ->real_parent under rcu_read_lock(), see | 
 | 1117 |  * release_task()->call_rcu(delayed_put_task_struct). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 |  */ | 
 | 1119 | asmlinkage long sys_getppid(void) | 
 | 1120 | { | 
 | 1121 | 	int pid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 |  | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 1123 | 	rcu_read_lock(); | 
| Pavel Emelyanov | 6c5f3e7 | 2008-02-08 04:19:20 -0800 | [diff] [blame] | 1124 | 	pid = task_tgid_vnr(current->real_parent); | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 1125 | 	rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | 	return pid; | 
 | 1128 | } | 
 | 1129 |  | 
 | 1130 | asmlinkage long sys_getuid(void) | 
 | 1131 | { | 
 | 1132 | 	/* Only we change this so SMP safe */ | 
 | 1133 | 	return current->uid; | 
 | 1134 | } | 
 | 1135 |  | 
 | 1136 | asmlinkage long sys_geteuid(void) | 
 | 1137 | { | 
 | 1138 | 	/* Only we change this so SMP safe */ | 
 | 1139 | 	return current->euid; | 
 | 1140 | } | 
 | 1141 |  | 
 | 1142 | asmlinkage long sys_getgid(void) | 
 | 1143 | { | 
 | 1144 | 	/* Only we change this so SMP safe */ | 
 | 1145 | 	return current->gid; | 
 | 1146 | } | 
 | 1147 |  | 
 | 1148 | asmlinkage long sys_getegid(void) | 
 | 1149 | { | 
 | 1150 | 	/* Only we change this so SMP safe */ | 
 | 1151 | 	return  current->egid; | 
 | 1152 | } | 
 | 1153 |  | 
 | 1154 | #endif | 
 | 1155 |  | 
 | 1156 | static void process_timeout(unsigned long __data) | 
 | 1157 | { | 
| Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1158 | 	wake_up_process((struct task_struct *)__data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | } | 
 | 1160 |  | 
 | 1161 | /** | 
 | 1162 |  * schedule_timeout - sleep until timeout | 
 | 1163 |  * @timeout: timeout value in jiffies | 
 | 1164 |  * | 
 | 1165 |  * Make the current task sleep until @timeout jiffies have | 
 | 1166 |  * elapsed. The routine will return immediately unless | 
 | 1167 |  * the current task state has been set (see set_current_state()). | 
 | 1168 |  * | 
 | 1169 |  * You can set the task state as follows - | 
 | 1170 |  * | 
 | 1171 |  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to | 
 | 1172 |  * pass before the routine returns. The routine will return 0 | 
 | 1173 |  * | 
 | 1174 |  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | 
 | 1175 |  * delivered to the current task. In this case the remaining time | 
 | 1176 |  * in jiffies will be returned, or 0 if the timer expired in time | 
 | 1177 |  * | 
 | 1178 |  * The current task state is guaranteed to be TASK_RUNNING when this | 
 | 1179 |  * routine returns. | 
 | 1180 |  * | 
 | 1181 |  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule | 
 | 1182 |  * the CPU away without a bound on the timeout. In this case the return | 
 | 1183 |  * value will be %MAX_SCHEDULE_TIMEOUT. | 
 | 1184 |  * | 
 | 1185 |  * In all cases the return value is guaranteed to be non-negative. | 
 | 1186 |  */ | 
| Harvey Harrison | 7ad5b3a | 2008-02-08 04:19:53 -0800 | [diff] [blame] | 1187 | signed long __sched schedule_timeout(signed long timeout) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | { | 
 | 1189 | 	struct timer_list timer; | 
 | 1190 | 	unsigned long expire; | 
 | 1191 |  | 
 | 1192 | 	switch (timeout) | 
 | 1193 | 	{ | 
 | 1194 | 	case MAX_SCHEDULE_TIMEOUT: | 
 | 1195 | 		/* | 
 | 1196 | 		 * These two special cases are useful to be comfortable | 
 | 1197 | 		 * in the caller. Nothing more. We could take | 
 | 1198 | 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value | 
 | 1199 | 		 * but I' d like to return a valid offset (>=0) to allow | 
 | 1200 | 		 * the caller to do everything it want with the retval. | 
 | 1201 | 		 */ | 
 | 1202 | 		schedule(); | 
 | 1203 | 		goto out; | 
 | 1204 | 	default: | 
 | 1205 | 		/* | 
 | 1206 | 		 * Another bit of PARANOID. Note that the retval will be | 
 | 1207 | 		 * 0 since no piece of kernel is supposed to do a check | 
 | 1208 | 		 * for a negative retval of schedule_timeout() (since it | 
 | 1209 | 		 * should never happens anyway). You just have the printk() | 
 | 1210 | 		 * that will tell you if something is gone wrong and where. | 
 | 1211 | 		 */ | 
| Andrew Morton | 5b149bc | 2006-12-22 01:10:14 -0800 | [diff] [blame] | 1212 | 		if (timeout < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | 			printk(KERN_ERR "schedule_timeout: wrong timeout " | 
| Andrew Morton | 5b149bc | 2006-12-22 01:10:14 -0800 | [diff] [blame] | 1214 | 				"value %lx\n", timeout); | 
 | 1215 | 			dump_stack(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | 			current->state = TASK_RUNNING; | 
 | 1217 | 			goto out; | 
 | 1218 | 		} | 
 | 1219 | 	} | 
 | 1220 |  | 
 | 1221 | 	expire = timeout + jiffies; | 
 | 1222 |  | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 1223 | 	setup_timer_on_stack(&timer, process_timeout, (unsigned long)current); | 
| Oleg Nesterov | a8db2db | 2005-10-30 15:01:38 -0800 | [diff] [blame] | 1224 | 	__mod_timer(&timer, expire); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | 	schedule(); | 
 | 1226 | 	del_singleshot_timer_sync(&timer); | 
 | 1227 |  | 
| Thomas Gleixner | c6f3a97 | 2008-04-30 00:55:03 -0700 | [diff] [blame] | 1228 | 	/* Remove the timer from the object tracker */ | 
 | 1229 | 	destroy_timer_on_stack(&timer); | 
 | 1230 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | 	timeout = expire - jiffies; | 
 | 1232 |  | 
 | 1233 |  out: | 
 | 1234 | 	return timeout < 0 ? 0 : timeout; | 
 | 1235 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | EXPORT_SYMBOL(schedule_timeout); | 
 | 1237 |  | 
| Andrew Morton | 8a1c175 | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 1238 | /* | 
 | 1239 |  * We can use __set_current_state() here because schedule_timeout() calls | 
 | 1240 |  * schedule() unconditionally. | 
 | 1241 |  */ | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1242 | signed long __sched schedule_timeout_interruptible(signed long timeout) | 
 | 1243 | { | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1244 | 	__set_current_state(TASK_INTERRUPTIBLE); | 
 | 1245 | 	return schedule_timeout(timeout); | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1246 | } | 
 | 1247 | EXPORT_SYMBOL(schedule_timeout_interruptible); | 
 | 1248 |  | 
| Matthew Wilcox | 294d5cc | 2007-12-06 11:59:46 -0500 | [diff] [blame] | 1249 | signed long __sched schedule_timeout_killable(signed long timeout) | 
 | 1250 | { | 
 | 1251 | 	__set_current_state(TASK_KILLABLE); | 
 | 1252 | 	return schedule_timeout(timeout); | 
 | 1253 | } | 
 | 1254 | EXPORT_SYMBOL(schedule_timeout_killable); | 
 | 1255 |  | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1256 | signed long __sched schedule_timeout_uninterruptible(signed long timeout) | 
 | 1257 | { | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1258 | 	__set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 1259 | 	return schedule_timeout(timeout); | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1260 | } | 
 | 1261 | EXPORT_SYMBOL(schedule_timeout_uninterruptible); | 
 | 1262 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | /* Thread ID - the internal kernel "pid" */ | 
 | 1264 | asmlinkage long sys_gettid(void) | 
 | 1265 | { | 
| Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1266 | 	return task_pid_vnr(current); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | } | 
 | 1268 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1269 | /** | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1270 |  * do_sysinfo - fill in sysinfo struct | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1271 |  * @info: pointer to buffer to fill | 
| Thomas Gleixner | 6819457 | 2007-07-19 01:49:16 -0700 | [diff] [blame] | 1272 |  */ | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1273 | int do_sysinfo(struct sysinfo *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | 	unsigned long mem_total, sav_total; | 
 | 1276 | 	unsigned int mem_unit, bitcount; | 
 | 1277 | 	unsigned long seq; | 
 | 1278 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1279 | 	memset(info, 0, sizeof(struct sysinfo)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 |  | 
 | 1281 | 	do { | 
 | 1282 | 		struct timespec tp; | 
 | 1283 | 		seq = read_seqbegin(&xtime_lock); | 
 | 1284 |  | 
 | 1285 | 		/* | 
 | 1286 | 		 * This is annoying.  The below is the same thing | 
 | 1287 | 		 * posix_get_clock_monotonic() does, but it wants to | 
 | 1288 | 		 * take the lock which we want to cover the loads stuff | 
 | 1289 | 		 * too. | 
 | 1290 | 		 */ | 
 | 1291 |  | 
 | 1292 | 		getnstimeofday(&tp); | 
 | 1293 | 		tp.tv_sec += wall_to_monotonic.tv_sec; | 
 | 1294 | 		tp.tv_nsec += wall_to_monotonic.tv_nsec; | 
| Tomas Janousek | d621414 | 2007-07-15 23:39:42 -0700 | [diff] [blame] | 1295 | 		monotonic_to_bootbased(&tp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | 		if (tp.tv_nsec - NSEC_PER_SEC >= 0) { | 
 | 1297 | 			tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC; | 
 | 1298 | 			tp.tv_sec++; | 
 | 1299 | 		} | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1300 | 		info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1302 | 		info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT); | 
 | 1303 | 		info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT); | 
 | 1304 | 		info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1306 | 		info->procs = nr_threads; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | 	} while (read_seqretry(&xtime_lock, seq)); | 
 | 1308 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1309 | 	si_meminfo(info); | 
 | 1310 | 	si_swapinfo(info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 |  | 
 | 1312 | 	/* | 
 | 1313 | 	 * If the sum of all the available memory (i.e. ram + swap) | 
 | 1314 | 	 * is less than can be stored in a 32 bit unsigned long then | 
 | 1315 | 	 * we can be binary compatible with 2.2.x kernels.  If not, | 
 | 1316 | 	 * well, in that case 2.2.x was broken anyways... | 
 | 1317 | 	 * | 
 | 1318 | 	 *  -Erik Andersen <andersee@debian.org> | 
 | 1319 | 	 */ | 
 | 1320 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1321 | 	mem_total = info->totalram + info->totalswap; | 
 | 1322 | 	if (mem_total < info->totalram || mem_total < info->totalswap) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | 		goto out; | 
 | 1324 | 	bitcount = 0; | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1325 | 	mem_unit = info->mem_unit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | 	while (mem_unit > 1) { | 
 | 1327 | 		bitcount++; | 
 | 1328 | 		mem_unit >>= 1; | 
 | 1329 | 		sav_total = mem_total; | 
 | 1330 | 		mem_total <<= 1; | 
 | 1331 | 		if (mem_total < sav_total) | 
 | 1332 | 			goto out; | 
 | 1333 | 	} | 
 | 1334 |  | 
 | 1335 | 	/* | 
 | 1336 | 	 * If mem_total did not overflow, multiply all memory values by | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1337 | 	 * info->mem_unit and set it to 1.  This leaves things compatible | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | 	 * with 2.2.x, and also retains compatibility with earlier 2.4.x | 
 | 1339 | 	 * kernels... | 
 | 1340 | 	 */ | 
 | 1341 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1342 | 	info->mem_unit = 1; | 
 | 1343 | 	info->totalram <<= bitcount; | 
 | 1344 | 	info->freeram <<= bitcount; | 
 | 1345 | 	info->sharedram <<= bitcount; | 
 | 1346 | 	info->bufferram <<= bitcount; | 
 | 1347 | 	info->totalswap <<= bitcount; | 
 | 1348 | 	info->freeswap <<= bitcount; | 
 | 1349 | 	info->totalhigh <<= bitcount; | 
 | 1350 | 	info->freehigh <<= bitcount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1352 | out: | 
 | 1353 | 	return 0; | 
 | 1354 | } | 
 | 1355 |  | 
 | 1356 | asmlinkage long sys_sysinfo(struct sysinfo __user *info) | 
 | 1357 | { | 
 | 1358 | 	struct sysinfo val; | 
 | 1359 |  | 
 | 1360 | 	do_sysinfo(&val); | 
 | 1361 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | 	if (copy_to_user(info, &val, sizeof(struct sysinfo))) | 
 | 1363 | 		return -EFAULT; | 
 | 1364 |  | 
 | 1365 | 	return 0; | 
 | 1366 | } | 
 | 1367 |  | 
| Adrian Bunk | b4be625 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1368 | static int __cpuinit init_timers_cpu(int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | { | 
 | 1370 | 	int j; | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1371 | 	struct tvec_base *base; | 
| Adrian Bunk | b4be625 | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 1372 | 	static char __cpuinitdata tvec_base_done[NR_CPUS]; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1373 |  | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1374 | 	if (!tvec_base_done[cpu]) { | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1375 | 		static char boot_done; | 
 | 1376 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1377 | 		if (boot_done) { | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1378 | 			/* | 
 | 1379 | 			 * The APs use this path later in boot | 
 | 1380 | 			 */ | 
| Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 1381 | 			base = kmalloc_node(sizeof(*base), | 
 | 1382 | 						GFP_KERNEL | __GFP_ZERO, | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1383 | 						cpu_to_node(cpu)); | 
 | 1384 | 			if (!base) | 
 | 1385 | 				return -ENOMEM; | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 1386 |  | 
 | 1387 | 			/* Make sure that tvec_base is 2 byte aligned */ | 
 | 1388 | 			if (tbase_get_deferrable(base)) { | 
 | 1389 | 				WARN_ON(1); | 
 | 1390 | 				kfree(base); | 
 | 1391 | 				return -ENOMEM; | 
 | 1392 | 			} | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1393 | 			per_cpu(tvec_bases, cpu) = base; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1394 | 		} else { | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1395 | 			/* | 
 | 1396 | 			 * This is for the boot CPU - we use compile-time | 
 | 1397 | 			 * static initialisation because per-cpu memory isn't | 
 | 1398 | 			 * ready yet and because the memory allocators are not | 
 | 1399 | 			 * initialised either. | 
 | 1400 | 			 */ | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1401 | 			boot_done = 1; | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1402 | 			base = &boot_tvec_bases; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1403 | 		} | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1404 | 		tvec_base_done[cpu] = 1; | 
 | 1405 | 	} else { | 
 | 1406 | 		base = per_cpu(tvec_bases, cpu); | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1407 | 	} | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1408 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1409 | 	spin_lock_init(&base->lock); | 
| Ingo Molnar | d730e88 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 1410 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | 	for (j = 0; j < TVN_SIZE; j++) { | 
 | 1412 | 		INIT_LIST_HEAD(base->tv5.vec + j); | 
 | 1413 | 		INIT_LIST_HEAD(base->tv4.vec + j); | 
 | 1414 | 		INIT_LIST_HEAD(base->tv3.vec + j); | 
 | 1415 | 		INIT_LIST_HEAD(base->tv2.vec + j); | 
 | 1416 | 	} | 
 | 1417 | 	for (j = 0; j < TVR_SIZE; j++) | 
 | 1418 | 		INIT_LIST_HEAD(base->tv1.vec + j); | 
 | 1419 |  | 
 | 1420 | 	base->timer_jiffies = jiffies; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1421 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | } | 
 | 1423 |  | 
 | 1424 | #ifdef CONFIG_HOTPLUG_CPU | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1425 | 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] | 1426 | { | 
 | 1427 | 	struct timer_list *timer; | 
 | 1428 |  | 
 | 1429 | 	while (!list_empty(head)) { | 
| Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 1430 | 		timer = list_first_entry(head, struct timer_list, entry); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1431 | 		detach_timer(timer, 0); | 
| Venki Pallipadi | 6e453a6 | 2007-05-08 00:27:44 -0700 | [diff] [blame] | 1432 | 		timer_set_base(timer, new_base); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | 		internal_add_timer(new_base, timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | } | 
 | 1436 |  | 
| Randy Dunlap | 48ccf3d | 2008-01-21 17:18:25 -0800 | [diff] [blame] | 1437 | static void __cpuinit migrate_timers(int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | { | 
| Pavel Machek | a6fa8e5 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 1439 | 	struct tvec_base *old_base; | 
 | 1440 | 	struct tvec_base *new_base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | 	int i; | 
 | 1442 |  | 
 | 1443 | 	BUG_ON(cpu_online(cpu)); | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1444 | 	old_base = per_cpu(tvec_bases, cpu); | 
 | 1445 | 	new_base = get_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 |  | 
 | 1447 | 	local_irq_disable(); | 
| Oleg Nesterov | 0d18040 | 2008-04-04 20:54:10 +0200 | [diff] [blame] | 1448 | 	spin_lock(&new_base->lock); | 
 | 1449 | 	spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1451 | 	BUG_ON(old_base->running_timer); | 
 | 1452 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | 	for (i = 0; i < TVR_SIZE; i++) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1454 | 		migrate_timer_list(new_base, old_base->tv1.vec + i); | 
 | 1455 | 	for (i = 0; i < TVN_SIZE; i++) { | 
 | 1456 | 		migrate_timer_list(new_base, old_base->tv2.vec + i); | 
 | 1457 | 		migrate_timer_list(new_base, old_base->tv3.vec + i); | 
 | 1458 | 		migrate_timer_list(new_base, old_base->tv4.vec + i); | 
 | 1459 | 		migrate_timer_list(new_base, old_base->tv5.vec + i); | 
 | 1460 | 	} | 
 | 1461 |  | 
| Oleg Nesterov | 0d18040 | 2008-04-04 20:54:10 +0200 | [diff] [blame] | 1462 | 	spin_unlock(&old_base->lock); | 
 | 1463 | 	spin_unlock(&new_base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | 	local_irq_enable(); | 
 | 1465 | 	put_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } | 
 | 1467 | #endif /* CONFIG_HOTPLUG_CPU */ | 
 | 1468 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1469 | static int __cpuinit timer_cpu_notify(struct notifier_block *self, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | 				unsigned long action, void *hcpu) | 
 | 1471 | { | 
 | 1472 | 	long cpu = (long)hcpu; | 
 | 1473 | 	switch(action) { | 
 | 1474 | 	case CPU_UP_PREPARE: | 
| Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1475 | 	case CPU_UP_PREPARE_FROZEN: | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1476 | 		if (init_timers_cpu(cpu) < 0) | 
 | 1477 | 			return NOTIFY_BAD; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | 		break; | 
 | 1479 | #ifdef CONFIG_HOTPLUG_CPU | 
 | 1480 | 	case CPU_DEAD: | 
| Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1481 | 	case CPU_DEAD_FROZEN: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | 		migrate_timers(cpu); | 
 | 1483 | 		break; | 
 | 1484 | #endif | 
 | 1485 | 	default: | 
 | 1486 | 		break; | 
 | 1487 | 	} | 
 | 1488 | 	return NOTIFY_OK; | 
 | 1489 | } | 
 | 1490 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1491 | static struct notifier_block __cpuinitdata timers_nb = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | 	.notifier_call	= timer_cpu_notify, | 
 | 1493 | }; | 
 | 1494 |  | 
 | 1495 |  | 
 | 1496 | void __init init_timers(void) | 
 | 1497 | { | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1498 | 	int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | 				(void *)(long)smp_processor_id()); | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1500 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1501 | 	init_timer_stats(); | 
 | 1502 |  | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1503 | 	BUG_ON(err == NOTIFY_BAD); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | 	register_cpu_notifier(&timers_nb); | 
 | 1505 | 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL); | 
 | 1506 | } | 
 | 1507 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | /** | 
 | 1509 |  * msleep - sleep safely even with waitqueue interruptions | 
 | 1510 |  * @msecs: Time in milliseconds to sleep for | 
 | 1511 |  */ | 
 | 1512 | void msleep(unsigned int msecs) | 
 | 1513 | { | 
 | 1514 | 	unsigned long timeout = msecs_to_jiffies(msecs) + 1; | 
 | 1515 |  | 
| Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 1516 | 	while (timeout) | 
 | 1517 | 		timeout = schedule_timeout_uninterruptible(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } | 
 | 1519 |  | 
 | 1520 | EXPORT_SYMBOL(msleep); | 
 | 1521 |  | 
 | 1522 | /** | 
| Domen Puncer | 96ec3ef | 2005-06-25 14:58:43 -0700 | [diff] [blame] | 1523 |  * msleep_interruptible - sleep waiting for signals | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 |  * @msecs: Time in milliseconds to sleep for | 
 | 1525 |  */ | 
 | 1526 | unsigned long msleep_interruptible(unsigned int msecs) | 
 | 1527 | { | 
 | 1528 | 	unsigned long timeout = msecs_to_jiffies(msecs) + 1; | 
 | 1529 |  | 
| Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 1530 | 	while (timeout && !signal_pending(current)) | 
 | 1531 | 		timeout = schedule_timeout_interruptible(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | 	return jiffies_to_msecs(timeout); | 
 | 1533 | } | 
 | 1534 |  | 
 | 1535 | EXPORT_SYMBOL(msleep_interruptible); |