| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/kernel/timer.c | 
 | 3 |  * | 
 | 4 |  *  Kernel internal timers, kernel timekeeping, basic process system calls | 
 | 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> | 
 | 29 | #include <linux/notifier.h> | 
 | 30 | #include <linux/thread_info.h> | 
 | 31 | #include <linux/time.h> | 
 | 32 | #include <linux/jiffies.h> | 
 | 33 | #include <linux/posix-timers.h> | 
 | 34 | #include <linux/cpu.h> | 
 | 35 | #include <linux/syscalls.h> | 
| Adrian Bunk | 97a41e2 | 2006-01-08 01:02:17 -0800 | [diff] [blame] | 36 | #include <linux/delay.h> | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 37 | #include <linux/tick.h> | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 38 | #include <linux/kallsyms.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
 | 40 | #include <asm/uaccess.h> | 
 | 41 | #include <asm/unistd.h> | 
 | 42 | #include <asm/div64.h> | 
 | 43 | #include <asm/timex.h> | 
 | 44 | #include <asm/io.h> | 
 | 45 |  | 
| Thomas Gleixner | ecea8d1 | 2005-10-30 15:03:00 -0800 | [diff] [blame] | 46 | u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; | 
 | 47 |  | 
 | 48 | EXPORT_SYMBOL(jiffies_64); | 
 | 49 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /* | 
 | 51 |  * per-CPU timer vector definitions: | 
 | 52 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6) | 
 | 54 | #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8) | 
 | 55 | #define TVN_SIZE (1 << TVN_BITS) | 
 | 56 | #define TVR_SIZE (1 << TVR_BITS) | 
 | 57 | #define TVN_MASK (TVN_SIZE - 1) | 
 | 58 | #define TVR_MASK (TVR_SIZE - 1) | 
 | 59 |  | 
 | 60 | typedef struct tvec_s { | 
 | 61 | 	struct list_head vec[TVN_SIZE]; | 
 | 62 | } tvec_t; | 
 | 63 |  | 
 | 64 | typedef struct tvec_root_s { | 
 | 65 | 	struct list_head vec[TVR_SIZE]; | 
 | 66 | } tvec_root_t; | 
 | 67 |  | 
 | 68 | struct tvec_t_base_s { | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 69 | 	spinlock_t lock; | 
 | 70 | 	struct timer_list *running_timer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | 	unsigned long timer_jiffies; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | 	tvec_root_t tv1; | 
 | 73 | 	tvec_t tv2; | 
 | 74 | 	tvec_t tv3; | 
 | 75 | 	tvec_t tv4; | 
 | 76 | 	tvec_t tv5; | 
 | 77 | } ____cacheline_aligned_in_smp; | 
 | 78 |  | 
 | 79 | typedef struct tvec_t_base_s tvec_base_t; | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 80 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 81 | tvec_base_t boot_tvec_bases; | 
 | 82 | EXPORT_SYMBOL(boot_tvec_bases); | 
| Josh Triplett | 51d8c5e | 2006-07-30 03:04:14 -0700 | [diff] [blame] | 83 | static DEFINE_PER_CPU(tvec_base_t *, tvec_bases) = &boot_tvec_bases; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
| Arjan van de Ven | 4c36a5d | 2006-12-10 02:21:24 -0800 | [diff] [blame] | 85 | /** | 
 | 86 |  * __round_jiffies - function to round jiffies to a full second | 
 | 87 |  * @j: the time in (absolute) jiffies that should be rounded | 
 | 88 |  * @cpu: the processor number on which the timeout will happen | 
 | 89 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 90 |  * __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] | 91 |  * up or down to (approximately) full seconds. This is useful for timers | 
 | 92 |  * for which the exact time they fire does not matter too much, as long as | 
 | 93 |  * they fire approximately every X seconds. | 
 | 94 |  * | 
 | 95 |  * By rounding these timers to whole seconds, all such timers will fire | 
 | 96 |  * at the same time, rather than at various times spread out. The goal | 
 | 97 |  * of this is to have the CPU wake up less, which saves power. | 
 | 98 |  * | 
 | 99 |  * The exact rounding is skewed for each processor to avoid all | 
 | 100 |  * processors firing at the exact same time, which could lead | 
 | 101 |  * to lock contention or spurious cache line bouncing. | 
 | 102 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 103 |  * 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] | 104 |  */ | 
 | 105 | unsigned long __round_jiffies(unsigned long j, int cpu) | 
 | 106 | { | 
 | 107 | 	int rem; | 
 | 108 | 	unsigned long original = j; | 
 | 109 |  | 
 | 110 | 	/* | 
 | 111 | 	 * We don't want all cpus firing their timers at once hitting the | 
 | 112 | 	 * same lock or cachelines, so we skew each extra cpu with an extra | 
 | 113 | 	 * 3 jiffies. This 3 jiffies came originally from the mm/ code which | 
 | 114 | 	 * already did this. | 
 | 115 | 	 * The skew is done by adding 3*cpunr, then round, then subtract this | 
 | 116 | 	 * extra offset again. | 
 | 117 | 	 */ | 
 | 118 | 	j += cpu * 3; | 
 | 119 |  | 
 | 120 | 	rem = j % HZ; | 
 | 121 |  | 
 | 122 | 	/* | 
 | 123 | 	 * If the target jiffie is just after a whole second (which can happen | 
 | 124 | 	 * due to delays of the timer irq, long irq off times etc etc) then | 
 | 125 | 	 * we should round down to the whole second, not up. Use 1/4th second | 
 | 126 | 	 * as cutoff for this rounding as an extreme upper bound for this. | 
 | 127 | 	 */ | 
 | 128 | 	if (rem < HZ/4) /* round down */ | 
 | 129 | 		j = j - rem; | 
 | 130 | 	else /* round up */ | 
 | 131 | 		j = j - rem + HZ; | 
 | 132 |  | 
 | 133 | 	/* now that we have rounded, subtract the extra skew again */ | 
 | 134 | 	j -= cpu * 3; | 
 | 135 |  | 
 | 136 | 	if (j <= jiffies) /* rounding ate our timeout entirely; */ | 
 | 137 | 		return original; | 
 | 138 | 	return j; | 
 | 139 | } | 
 | 140 | EXPORT_SYMBOL_GPL(__round_jiffies); | 
 | 141 |  | 
 | 142 | /** | 
 | 143 |  * __round_jiffies_relative - function to round jiffies to a full second | 
 | 144 |  * @j: the time in (relative) jiffies that should be rounded | 
 | 145 |  * @cpu: the processor number on which the timeout will happen | 
 | 146 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 147 |  * __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] | 148 |  * up or down to (approximately) full seconds. This is useful for timers | 
 | 149 |  * for which the exact time they fire does not matter too much, as long as | 
 | 150 |  * they fire approximately every X seconds. | 
 | 151 |  * | 
 | 152 |  * By rounding these timers to whole seconds, all such timers will fire | 
 | 153 |  * at the same time, rather than at various times spread out. The goal | 
 | 154 |  * of this is to have the CPU wake up less, which saves power. | 
 | 155 |  * | 
 | 156 |  * The exact rounding is skewed for each processor to avoid all | 
 | 157 |  * processors firing at the exact same time, which could lead | 
 | 158 |  * to lock contention or spurious cache line bouncing. | 
 | 159 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 160 |  * 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] | 161 |  */ | 
 | 162 | unsigned long __round_jiffies_relative(unsigned long j, int cpu) | 
 | 163 | { | 
 | 164 | 	/* | 
 | 165 | 	 * In theory the following code can skip a jiffy in case jiffies | 
 | 166 | 	 * increments right between the addition and the later subtraction. | 
 | 167 | 	 * However since the entire point of this function is to use approximate | 
 | 168 | 	 * timeouts, it's entirely ok to not handle that. | 
 | 169 | 	 */ | 
 | 170 | 	return  __round_jiffies(j + jiffies, cpu) - jiffies; | 
 | 171 | } | 
 | 172 | EXPORT_SYMBOL_GPL(__round_jiffies_relative); | 
 | 173 |  | 
 | 174 | /** | 
 | 175 |  * round_jiffies - function to round jiffies to a full second | 
 | 176 |  * @j: the time in (absolute) jiffies that should be rounded | 
 | 177 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 178 |  * round_jiffies() rounds an absolute time in the future (in jiffies) | 
| Arjan van de Ven | 4c36a5d | 2006-12-10 02:21:24 -0800 | [diff] [blame] | 179 |  * up or down to (approximately) full seconds. This is useful for timers | 
 | 180 |  * for which the exact time they fire does not matter too much, as long as | 
 | 181 |  * they fire approximately every X seconds. | 
 | 182 |  * | 
 | 183 |  * By rounding these timers to whole seconds, all such timers will fire | 
 | 184 |  * at the same time, rather than at various times spread out. The goal | 
 | 185 |  * of this is to have the CPU wake up less, which saves power. | 
 | 186 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 187 |  * 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] | 188 |  */ | 
 | 189 | unsigned long round_jiffies(unsigned long j) | 
 | 190 | { | 
 | 191 | 	return __round_jiffies(j, raw_smp_processor_id()); | 
 | 192 | } | 
 | 193 | EXPORT_SYMBOL_GPL(round_jiffies); | 
 | 194 |  | 
 | 195 | /** | 
 | 196 |  * round_jiffies_relative - function to round jiffies to a full second | 
 | 197 |  * @j: the time in (relative) jiffies that should be rounded | 
 | 198 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 199 |  * 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] | 200 |  * up or down to (approximately) full seconds. This is useful for timers | 
 | 201 |  * for which the exact time they fire does not matter too much, as long as | 
 | 202 |  * they fire approximately every X seconds. | 
 | 203 |  * | 
 | 204 |  * By rounding these timers to whole seconds, all such timers will fire | 
 | 205 |  * at the same time, rather than at various times spread out. The goal | 
 | 206 |  * of this is to have the CPU wake up less, which saves power. | 
 | 207 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 208 |  * 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] | 209 |  */ | 
 | 210 | unsigned long round_jiffies_relative(unsigned long j) | 
 | 211 | { | 
 | 212 | 	return __round_jiffies_relative(j, raw_smp_processor_id()); | 
 | 213 | } | 
 | 214 | EXPORT_SYMBOL_GPL(round_jiffies_relative); | 
 | 215 |  | 
 | 216 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | static inline void set_running_timer(tvec_base_t *base, | 
 | 218 | 					struct timer_list *timer) | 
 | 219 | { | 
 | 220 | #ifdef CONFIG_SMP | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 221 | 	base->running_timer = timer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #endif | 
 | 223 | } | 
 | 224 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | static void internal_add_timer(tvec_base_t *base, struct timer_list *timer) | 
 | 226 | { | 
 | 227 | 	unsigned long expires = timer->expires; | 
 | 228 | 	unsigned long idx = expires - base->timer_jiffies; | 
 | 229 | 	struct list_head *vec; | 
 | 230 |  | 
 | 231 | 	if (idx < TVR_SIZE) { | 
 | 232 | 		int i = expires & TVR_MASK; | 
 | 233 | 		vec = base->tv1.vec + i; | 
 | 234 | 	} else if (idx < 1 << (TVR_BITS + TVN_BITS)) { | 
 | 235 | 		int i = (expires >> TVR_BITS) & TVN_MASK; | 
 | 236 | 		vec = base->tv2.vec + i; | 
 | 237 | 	} else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) { | 
 | 238 | 		int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK; | 
 | 239 | 		vec = base->tv3.vec + i; | 
 | 240 | 	} else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) { | 
 | 241 | 		int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK; | 
 | 242 | 		vec = base->tv4.vec + i; | 
 | 243 | 	} else if ((signed long) idx < 0) { | 
 | 244 | 		/* | 
 | 245 | 		 * Can happen if you add a timer with expires == jiffies, | 
 | 246 | 		 * or you set a timer to go off in the past | 
 | 247 | 		 */ | 
 | 248 | 		vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK); | 
 | 249 | 	} else { | 
 | 250 | 		int i; | 
 | 251 | 		/* If the timeout is larger than 0xffffffff on 64-bit | 
 | 252 | 		 * architectures then we use the maximum timeout: | 
 | 253 | 		 */ | 
 | 254 | 		if (idx > 0xffffffffUL) { | 
 | 255 | 			idx = 0xffffffffUL; | 
 | 256 | 			expires = idx + base->timer_jiffies; | 
 | 257 | 		} | 
 | 258 | 		i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK; | 
 | 259 | 		vec = base->tv5.vec + i; | 
 | 260 | 	} | 
 | 261 | 	/* | 
 | 262 | 	 * Timers are FIFO: | 
 | 263 | 	 */ | 
 | 264 | 	list_add_tail(&timer->entry, vec); | 
 | 265 | } | 
 | 266 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 267 | #ifdef CONFIG_TIMER_STATS | 
 | 268 | void __timer_stats_timer_set_start_info(struct timer_list *timer, void *addr) | 
 | 269 | { | 
 | 270 | 	if (timer->start_site) | 
 | 271 | 		return; | 
 | 272 |  | 
 | 273 | 	timer->start_site = addr; | 
 | 274 | 	memcpy(timer->start_comm, current->comm, TASK_COMM_LEN); | 
 | 275 | 	timer->start_pid = current->pid; | 
 | 276 | } | 
 | 277 | #endif | 
 | 278 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 279 | /** | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 280 |  * init_timer - initialize a timer. | 
 | 281 |  * @timer: the timer to be initialized | 
 | 282 |  * | 
 | 283 |  * init_timer() must be done to a timer prior calling *any* of the | 
 | 284 |  * other timer functions. | 
 | 285 |  */ | 
 | 286 | void fastcall init_timer(struct timer_list *timer) | 
 | 287 | { | 
 | 288 | 	timer->entry.next = NULL; | 
| Paul Mackerras | bfe5d83 | 2006-06-25 05:47:14 -0700 | [diff] [blame] | 289 | 	timer->base = __raw_get_cpu_var(tvec_bases); | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 290 | #ifdef CONFIG_TIMER_STATS | 
 | 291 | 	timer->start_site = NULL; | 
 | 292 | 	timer->start_pid = -1; | 
 | 293 | 	memset(timer->start_comm, 0, TASK_COMM_LEN); | 
 | 294 | #endif | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 295 | } | 
 | 296 | EXPORT_SYMBOL(init_timer); | 
 | 297 |  | 
 | 298 | static inline void detach_timer(struct timer_list *timer, | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 299 | 				int clear_pending) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 300 | { | 
 | 301 | 	struct list_head *entry = &timer->entry; | 
 | 302 |  | 
 | 303 | 	__list_del(entry->prev, entry->next); | 
 | 304 | 	if (clear_pending) | 
 | 305 | 		entry->next = NULL; | 
 | 306 | 	entry->prev = LIST_POISON2; | 
 | 307 | } | 
 | 308 |  | 
 | 309 | /* | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 310 |  * We are using hashed locking: holding per_cpu(tvec_bases).lock | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 311 |  * means that all timers which are tied to this base via timer->base are | 
 | 312 |  * locked, and the base itself is locked too. | 
 | 313 |  * | 
 | 314 |  * So __run_timers/migrate_timers can safely modify all timers which could | 
 | 315 |  * be found on ->tvX lists. | 
 | 316 |  * | 
 | 317 |  * When the timer's base is locked, and the timer removed from list, it is | 
 | 318 |  * possible to set timer->base = NULL and drop the lock: the timer remains | 
 | 319 |  * locked. | 
 | 320 |  */ | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 321 | static tvec_base_t *lock_timer_base(struct timer_list *timer, | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 322 | 					unsigned long *flags) | 
| Josh Triplett | 89e7e374 | 2006-09-29 01:59:36 -0700 | [diff] [blame] | 323 | 	__acquires(timer->base->lock) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 324 | { | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 325 | 	tvec_base_t *base; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 326 |  | 
 | 327 | 	for (;;) { | 
 | 328 | 		base = timer->base; | 
 | 329 | 		if (likely(base != NULL)) { | 
 | 330 | 			spin_lock_irqsave(&base->lock, *flags); | 
 | 331 | 			if (likely(base == timer->base)) | 
 | 332 | 				return base; | 
 | 333 | 			/* The timer has migrated to another CPU */ | 
 | 334 | 			spin_unlock_irqrestore(&base->lock, *flags); | 
 | 335 | 		} | 
 | 336 | 		cpu_relax(); | 
 | 337 | 	} | 
 | 338 | } | 
 | 339 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | int __mod_timer(struct timer_list *timer, unsigned long expires) | 
 | 341 | { | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 342 | 	tvec_base_t *base, *new_base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | 	unsigned long flags; | 
 | 344 | 	int ret = 0; | 
 | 345 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 346 | 	timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | 	BUG_ON(!timer->function); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 349 | 	base = lock_timer_base(timer, &flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 351 | 	if (timer_pending(timer)) { | 
 | 352 | 		detach_timer(timer, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | 		ret = 1; | 
 | 354 | 	} | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 355 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 356 | 	new_base = __get_cpu_var(tvec_bases); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 357 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 358 | 	if (base != new_base) { | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 359 | 		/* | 
 | 360 | 		 * We are trying to schedule the timer on the local CPU. | 
 | 361 | 		 * However we can't change timer's base while it is running, | 
 | 362 | 		 * otherwise del_timer_sync() can't detect that the timer's | 
 | 363 | 		 * handler yet has not finished. This also guarantees that | 
 | 364 | 		 * the timer is serialized wrt itself. | 
 | 365 | 		 */ | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 366 | 		if (likely(base->running_timer != timer)) { | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 367 | 			/* See the comment in lock_timer_base() */ | 
 | 368 | 			timer->base = NULL; | 
 | 369 | 			spin_unlock(&base->lock); | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 370 | 			base = new_base; | 
 | 371 | 			spin_lock(&base->lock); | 
 | 372 | 			timer->base = base; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 373 | 		} | 
 | 374 | 	} | 
 | 375 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | 	timer->expires = expires; | 
| Oleg Nesterov | a2c348f | 2006-03-31 02:30:31 -0800 | [diff] [blame] | 377 | 	internal_add_timer(base, timer); | 
 | 378 | 	spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 |  | 
 | 380 | 	return ret; | 
 | 381 | } | 
 | 382 |  | 
 | 383 | EXPORT_SYMBOL(__mod_timer); | 
 | 384 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 385 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 |  * add_timer_on - start a timer on a particular CPU | 
 | 387 |  * @timer: the timer to be added | 
 | 388 |  * @cpu: the CPU to start it on | 
 | 389 |  * | 
 | 390 |  * This is not very scalable on SMP. Double adds are not possible. | 
 | 391 |  */ | 
 | 392 | void add_timer_on(struct timer_list *timer, int cpu) | 
 | 393 | { | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 394 | 	tvec_base_t *base = per_cpu(tvec_bases, cpu); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 |   	unsigned long flags; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 396 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 397 | 	timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 |   	BUG_ON(timer_pending(timer) || !timer->function); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 399 | 	spin_lock_irqsave(&base->lock, flags); | 
 | 400 | 	timer->base = base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | 	internal_add_timer(base, timer); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 402 | 	spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } | 
 | 404 |  | 
 | 405 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 406 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 |  * mod_timer - modify a timer's timeout | 
 | 408 |  * @timer: the timer to be modified | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 409 |  * @expires: new timeout in jiffies | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 411 |  * 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] | 412 |  * active timer (if the timer is inactive it will be activated) | 
 | 413 |  * | 
 | 414 |  * mod_timer(timer, expires) is equivalent to: | 
 | 415 |  * | 
 | 416 |  *     del_timer(timer); timer->expires = expires; add_timer(timer); | 
 | 417 |  * | 
 | 418 |  * Note that if there are multiple unserialized concurrent users of the | 
 | 419 |  * same timer, then mod_timer() is the only safe way to modify the timeout, | 
 | 420 |  * since add_timer() cannot modify an already running timer. | 
 | 421 |  * | 
 | 422 |  * The function returns whether it has modified a pending timer or not. | 
 | 423 |  * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an | 
 | 424 |  * active timer returns 1.) | 
 | 425 |  */ | 
 | 426 | int mod_timer(struct timer_list *timer, unsigned long expires) | 
 | 427 | { | 
 | 428 | 	BUG_ON(!timer->function); | 
 | 429 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 430 | 	timer_stats_timer_set_start_info(timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | 	/* | 
 | 432 | 	 * This is a common optimization triggered by the | 
 | 433 | 	 * networking code - if the timer is re-modified | 
 | 434 | 	 * to be the same thing then just return: | 
 | 435 | 	 */ | 
 | 436 | 	if (timer->expires == expires && timer_pending(timer)) | 
 | 437 | 		return 1; | 
 | 438 |  | 
 | 439 | 	return __mod_timer(timer, expires); | 
 | 440 | } | 
 | 441 |  | 
 | 442 | EXPORT_SYMBOL(mod_timer); | 
 | 443 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 444 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 |  * del_timer - deactive a timer. | 
 | 446 |  * @timer: the timer to be deactivated | 
 | 447 |  * | 
 | 448 |  * del_timer() deactivates a timer - this works on both active and inactive | 
 | 449 |  * timers. | 
 | 450 |  * | 
 | 451 |  * The function returns whether it has deactivated a pending timer or not. | 
 | 452 |  * (ie. del_timer() of an inactive timer returns 0, del_timer() of an | 
 | 453 |  * active timer returns 1.) | 
 | 454 |  */ | 
 | 455 | int del_timer(struct timer_list *timer) | 
 | 456 | { | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 457 | 	tvec_base_t *base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | 	unsigned long flags; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 459 | 	int ret = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 461 | 	timer_stats_timer_clear_start_info(timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 462 | 	if (timer_pending(timer)) { | 
 | 463 | 		base = lock_timer_base(timer, &flags); | 
 | 464 | 		if (timer_pending(timer)) { | 
 | 465 | 			detach_timer(timer, 1); | 
 | 466 | 			ret = 1; | 
 | 467 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | 		spin_unlock_irqrestore(&base->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 |  | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 471 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } | 
 | 473 |  | 
 | 474 | EXPORT_SYMBOL(del_timer); | 
 | 475 |  | 
 | 476 | #ifdef CONFIG_SMP | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 477 | /** | 
 | 478 |  * try_to_del_timer_sync - Try to deactivate a timer | 
 | 479 |  * @timer: timer do del | 
 | 480 |  * | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 481 |  * This function tries to deactivate a timer. Upon successful (ret >= 0) | 
 | 482 |  * exit the timer is not queued and the handler is not running on any CPU. | 
 | 483 |  * | 
 | 484 |  * It must not be called from interrupt contexts. | 
 | 485 |  */ | 
 | 486 | int try_to_del_timer_sync(struct timer_list *timer) | 
 | 487 | { | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 488 | 	tvec_base_t *base; | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 489 | 	unsigned long flags; | 
 | 490 | 	int ret = -1; | 
 | 491 |  | 
 | 492 | 	base = lock_timer_base(timer, &flags); | 
 | 493 |  | 
 | 494 | 	if (base->running_timer == timer) | 
 | 495 | 		goto out; | 
 | 496 |  | 
 | 497 | 	ret = 0; | 
 | 498 | 	if (timer_pending(timer)) { | 
 | 499 | 		detach_timer(timer, 1); | 
 | 500 | 		ret = 1; | 
 | 501 | 	} | 
 | 502 | out: | 
 | 503 | 	spin_unlock_irqrestore(&base->lock, flags); | 
 | 504 |  | 
 | 505 | 	return ret; | 
 | 506 | } | 
 | 507 |  | 
| David Howells | e19dff1 | 2007-04-26 15:46:56 -0700 | [diff] [blame] | 508 | EXPORT_SYMBOL(try_to_del_timer_sync); | 
 | 509 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 510 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 |  * del_timer_sync - deactivate a timer and wait for the handler to finish. | 
 | 512 |  * @timer: the timer to be deactivated | 
 | 513 |  * | 
 | 514 |  * This function only differs from del_timer() on SMP: besides deactivating | 
 | 515 |  * the timer it also makes sure the handler has finished executing on other | 
 | 516 |  * CPUs. | 
 | 517 |  * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 518 |  * Synchronization rules: Callers must prevent restarting of the timer, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 |  * otherwise this function is meaningless. It must not be called from | 
 | 520 |  * interrupt contexts. The caller must not hold locks which would prevent | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 521 |  * completion of the timer's handler. The timer's handler must not call | 
 | 522 |  * add_timer_on(). Upon exit the timer is not queued and the handler is | 
 | 523 |  * not running on any CPU. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 |  * | 
 | 525 |  * The function returns whether it has deactivated a pending timer or not. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 |  */ | 
 | 527 | int del_timer_sync(struct timer_list *timer) | 
 | 528 | { | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 529 | 	for (;;) { | 
 | 530 | 		int ret = try_to_del_timer_sync(timer); | 
 | 531 | 		if (ret >= 0) | 
 | 532 | 			return ret; | 
| Andrew Morton | a000965 | 2006-07-14 00:24:06 -0700 | [diff] [blame] | 533 | 		cpu_relax(); | 
| Oleg Nesterov | fd450b7 | 2005-06-23 00:08:59 -0700 | [diff] [blame] | 534 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 536 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | EXPORT_SYMBOL(del_timer_sync); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | #endif | 
 | 539 |  | 
 | 540 | static int cascade(tvec_base_t *base, tvec_t *tv, int index) | 
 | 541 | { | 
 | 542 | 	/* cascade all the timers from tv up one level */ | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 543 | 	struct timer_list *timer, *tmp; | 
 | 544 | 	struct list_head tv_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 |  | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 546 | 	list_replace_init(tv->vec + index, &tv_list); | 
 | 547 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | 	/* | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 549 | 	 * We are removing _all_ timers from the list, so we | 
 | 550 | 	 * don't have to detach them individually. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | 	 */ | 
| Porpoise | 3439dd8 | 2006-06-23 02:05:56 -0700 | [diff] [blame] | 552 | 	list_for_each_entry_safe(timer, tmp, &tv_list, entry) { | 
 | 553 | 		BUG_ON(timer->base != base); | 
 | 554 | 		internal_add_timer(base, timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 |  | 
 | 557 | 	return index; | 
 | 558 | } | 
 | 559 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 560 | #define INDEX(N) ((base->timer_jiffies >> (TVR_BITS + (N) * TVN_BITS)) & TVN_MASK) | 
 | 561 |  | 
 | 562 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 |  * __run_timers - run all expired timers (if any) on this CPU. | 
 | 564 |  * @base: the timer vector to be processed. | 
 | 565 |  * | 
 | 566 |  * This function cascades all vectors and executes all expired timer | 
 | 567 |  * vectors. | 
 | 568 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | static inline void __run_timers(tvec_base_t *base) | 
 | 570 | { | 
 | 571 | 	struct timer_list *timer; | 
 | 572 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 573 | 	spin_lock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | 	while (time_after_eq(jiffies, base->timer_jiffies)) { | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 575 | 		struct list_head work_list; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | 		struct list_head *head = &work_list; | 
 | 577 |  		int index = base->timer_jiffies & TVR_MASK; | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 578 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | 		/* | 
 | 580 | 		 * Cascade timers: | 
 | 581 | 		 */ | 
 | 582 | 		if (!index && | 
 | 583 | 			(!cascade(base, &base->tv2, INDEX(0))) && | 
 | 584 | 				(!cascade(base, &base->tv3, INDEX(1))) && | 
 | 585 | 					!cascade(base, &base->tv4, INDEX(2))) | 
 | 586 | 			cascade(base, &base->tv5, INDEX(3)); | 
| Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 587 | 		++base->timer_jiffies; | 
 | 588 | 		list_replace_init(base->tv1.vec + index, &work_list); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 589 | 		while (!list_empty(head)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | 			void (*fn)(unsigned long); | 
 | 591 | 			unsigned long data; | 
 | 592 |  | 
 | 593 | 			timer = list_entry(head->next,struct timer_list,entry); | 
 | 594 |  			fn = timer->function; | 
 | 595 |  			data = timer->data; | 
 | 596 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 597 | 			timer_stats_account_timer(timer); | 
 | 598 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | 			set_running_timer(base, timer); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 600 | 			detach_timer(timer, 1); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 601 | 			spin_unlock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | 			{ | 
| Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 603 | 				int preempt_count = preempt_count(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | 				fn(data); | 
 | 605 | 				if (preempt_count != preempt_count()) { | 
| Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 606 | 					printk(KERN_WARNING "huh, entered %p " | 
 | 607 | 					       "with preempt_count %08x, exited" | 
 | 608 | 					       " with %08x?\n", | 
 | 609 | 					       fn, preempt_count, | 
 | 610 | 					       preempt_count()); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | 					BUG(); | 
 | 612 | 				} | 
 | 613 | 			} | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 614 | 			spin_lock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | 		} | 
 | 616 | 	} | 
 | 617 | 	set_running_timer(base, NULL); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 618 | 	spin_unlock_irq(&base->lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } | 
 | 620 |  | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 621 | #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | /* | 
 | 623 |  * Find out when the next timer event is due to happen. This | 
 | 624 |  * is used on S/390 to stop all activity when a cpus is idle. | 
 | 625 |  * This functions needs to be called disabled. | 
 | 626 |  */ | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 627 | static unsigned long __next_timer_interrupt(tvec_base_t *base) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 629 | 	unsigned long timer_jiffies = base->timer_jiffies; | 
 | 630 | 	unsigned long expires = timer_jiffies + (LONG_MAX >> 1); | 
 | 631 | 	int index, slot, array, found = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | 	struct timer_list *nte; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | 	tvec_t *varray[4]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 |  | 
 | 635 | 	/* Look for timer events in tv1. */ | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 636 | 	index = slot = timer_jiffies & TVR_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | 	do { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 638 | 		list_for_each_entry(nte, base->tv1.vec + slot, entry) { | 
 | 639 | 			found = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | 			expires = nte->expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 641 | 			/* Look at the cascade bucket(s)? */ | 
 | 642 | 			if (!index || slot < index) | 
 | 643 | 				goto cascade; | 
 | 644 | 			return expires; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | 		} | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 646 | 		slot = (slot + 1) & TVR_MASK; | 
 | 647 | 	} while (slot != index); | 
 | 648 |  | 
 | 649 | cascade: | 
 | 650 | 	/* Calculate the next cascade event */ | 
 | 651 | 	if (index) | 
 | 652 | 		timer_jiffies += TVR_SIZE - index; | 
 | 653 | 	timer_jiffies >>= TVR_BITS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 |  | 
 | 655 | 	/* Check tv2-tv5. */ | 
 | 656 | 	varray[0] = &base->tv2; | 
 | 657 | 	varray[1] = &base->tv3; | 
 | 658 | 	varray[2] = &base->tv4; | 
 | 659 | 	varray[3] = &base->tv5; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 660 |  | 
 | 661 | 	for (array = 0; array < 4; array++) { | 
 | 662 | 		tvec_t *varp = varray[array]; | 
 | 663 |  | 
 | 664 | 		index = slot = timer_jiffies & TVN_MASK; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | 		do { | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 666 | 			list_for_each_entry(nte, varp->vec + slot, entry) { | 
 | 667 | 				found = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | 				if (time_before(nte->expires, expires)) | 
 | 669 | 					expires = nte->expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 670 | 			} | 
 | 671 | 			/* | 
 | 672 | 			 * Do we still search for the first timer or are | 
 | 673 | 			 * we looking up the cascade buckets ? | 
 | 674 | 			 */ | 
 | 675 | 			if (found) { | 
 | 676 | 				/* Look at the cascade bucket(s)? */ | 
 | 677 | 				if (!index || slot < index) | 
 | 678 | 					break; | 
 | 679 | 				return expires; | 
 | 680 | 			} | 
 | 681 | 			slot = (slot + 1) & TVN_MASK; | 
 | 682 | 		} while (slot != index); | 
 | 683 |  | 
 | 684 | 		if (index) | 
 | 685 | 			timer_jiffies += TVN_SIZE - index; | 
 | 686 | 		timer_jiffies >>= TVN_BITS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | 	} | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 688 | 	return expires; | 
 | 689 | } | 
 | 690 |  | 
 | 691 | /* | 
 | 692 |  * Check, if the next hrtimer event is before the next timer wheel | 
 | 693 |  * event: | 
 | 694 |  */ | 
 | 695 | static unsigned long cmp_next_hrtimer_event(unsigned long now, | 
 | 696 | 					    unsigned long expires) | 
 | 697 | { | 
 | 698 | 	ktime_t hr_delta = hrtimer_get_next_event(); | 
 | 699 | 	struct timespec tsdelta; | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 700 | 	unsigned long delta; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 701 |  | 
 | 702 | 	if (hr_delta.tv64 == KTIME_MAX) | 
 | 703 | 		return expires; | 
 | 704 |  | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 705 | 	/* | 
 | 706 | 	 * Expired timer available, let it expire in the next tick | 
 | 707 | 	 */ | 
 | 708 | 	if (hr_delta.tv64 <= 0) | 
 | 709 | 		return now + 1; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 710 |  | 
 | 711 | 	tsdelta = ktime_to_timespec(hr_delta); | 
| Thomas Gleixner | 9501b6c | 2007-03-25 14:31:17 +0200 | [diff] [blame] | 712 | 	delta = timespec_to_jiffies(&tsdelta); | 
 | 713 | 	/* | 
 | 714 | 	 * Take rounding errors in to account and make sure, that it | 
 | 715 | 	 * expires in the next tick. Otherwise we go into an endless | 
 | 716 | 	 * ping pong due to tick_nohz_stop_sched_tick() retriggering | 
 | 717 | 	 * the timer softirq | 
 | 718 | 	 */ | 
 | 719 | 	if (delta < 1) | 
 | 720 | 		delta = 1; | 
 | 721 | 	now += delta; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 722 | 	if (time_before(now, expires)) | 
 | 723 | 		return now; | 
 | 724 | 	return expires; | 
 | 725 | } | 
 | 726 |  | 
 | 727 | /** | 
 | 728 |  * next_timer_interrupt - return the jiffy of the next pending timer | 
| Randy Dunlap | 05fb6bf | 2007-02-28 20:12:13 -0800 | [diff] [blame] | 729 |  * @now: current time (in jiffies) | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 730 |  */ | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 731 | unsigned long get_next_timer_interrupt(unsigned long now) | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 732 | { | 
 | 733 | 	tvec_base_t *base = __get_cpu_var(tvec_bases); | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 734 | 	unsigned long expires; | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 735 |  | 
 | 736 | 	spin_lock(&base->lock); | 
 | 737 | 	expires = __next_timer_interrupt(base); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 738 | 	spin_unlock(&base->lock); | 
| Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 739 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 740 | 	if (time_before_eq(expires, now)) | 
 | 741 | 		return now; | 
| Zachary Amsden | 0662b71 | 2006-05-20 15:00:24 -0700 | [diff] [blame] | 742 |  | 
| Thomas Gleixner | 1cfd684 | 2007-02-16 01:27:46 -0800 | [diff] [blame] | 743 | 	return cmp_next_hrtimer_event(now, expires); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } | 
| Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 745 |  | 
 | 746 | #ifdef CONFIG_NO_IDLE_HZ | 
 | 747 | unsigned long next_timer_interrupt(void) | 
 | 748 | { | 
 | 749 | 	return get_next_timer_interrupt(jiffies); | 
 | 750 | } | 
 | 751 | #endif | 
 | 752 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | #endif | 
 | 754 |  | 
 | 755 | /******************************************************************/ | 
 | 756 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | /*  | 
 | 758 |  * The current time  | 
 | 759 |  * wall_to_monotonic is what we need to add to xtime (or xtime corrected  | 
 | 760 |  * for sub jiffie times) to get to monotonic time.  Monotonic is pegged | 
 | 761 |  * at zero at system boot time, so wall_to_monotonic will be negative, | 
 | 762 |  * however, we will ALWAYS keep the tv_nsec part positive so we can use | 
 | 763 |  * the usual normalization. | 
 | 764 |  */ | 
 | 765 | struct timespec xtime __attribute__ ((aligned (16))); | 
 | 766 | struct timespec wall_to_monotonic __attribute__ ((aligned (16))); | 
 | 767 |  | 
 | 768 | EXPORT_SYMBOL(xtime); | 
 | 769 |  | 
| Paul Mackerras | 726c14b | 2006-02-17 10:30:23 +1100 | [diff] [blame] | 770 |  | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 771 | /* XXX - all of this timekeeping code should be later moved to time.c */ | 
 | 772 | #include <linux/clocksource.h> | 
 | 773 | static struct clocksource *clock; /* pointer to current clocksource */ | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 774 |  | 
 | 775 | #ifdef CONFIG_GENERIC_TIME | 
 | 776 | /** | 
 | 777 |  * __get_nsec_offset - Returns nanoseconds since last call to periodic_hook | 
 | 778 |  * | 
 | 779 |  * private function, must hold xtime_lock lock when being | 
 | 780 |  * called. Returns the number of nanoseconds since the | 
 | 781 |  * last call to update_wall_time() (adjusted by NTP scaling) | 
 | 782 |  */ | 
 | 783 | static inline s64 __get_nsec_offset(void) | 
 | 784 | { | 
 | 785 | 	cycle_t cycle_now, cycle_delta; | 
 | 786 | 	s64 ns_offset; | 
 | 787 |  | 
 | 788 | 	/* read clocksource: */ | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 789 | 	cycle_now = clocksource_read(clock); | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 790 |  | 
 | 791 | 	/* calculate the delta since the last update_wall_time: */ | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 792 | 	cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 793 |  | 
 | 794 | 	/* convert to nanoseconds: */ | 
 | 795 | 	ns_offset = cyc2ns(clock, cycle_delta); | 
 | 796 |  | 
 | 797 | 	return ns_offset; | 
 | 798 | } | 
 | 799 |  | 
 | 800 | /** | 
 | 801 |  * __get_realtime_clock_ts - Returns the time of day in a timespec | 
 | 802 |  * @ts:		pointer to the timespec to be set | 
 | 803 |  * | 
 | 804 |  * Returns the time of day in a timespec. Used by | 
 | 805 |  * do_gettimeofday() and get_realtime_clock_ts(). | 
 | 806 |  */ | 
 | 807 | static inline void __get_realtime_clock_ts(struct timespec *ts) | 
 | 808 | { | 
 | 809 | 	unsigned long seq; | 
 | 810 | 	s64 nsecs; | 
 | 811 |  | 
 | 812 | 	do { | 
 | 813 | 		seq = read_seqbegin(&xtime_lock); | 
 | 814 |  | 
 | 815 | 		*ts = xtime; | 
 | 816 | 		nsecs = __get_nsec_offset(); | 
 | 817 |  | 
 | 818 | 	} while (read_seqretry(&xtime_lock, seq)); | 
 | 819 |  | 
 | 820 | 	timespec_add_ns(ts, nsecs); | 
 | 821 | } | 
 | 822 |  | 
 | 823 | /** | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 824 |  * getnstimeofday - Returns the time of day in a timespec | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 825 |  * @ts:		pointer to the timespec to be set | 
 | 826 |  * | 
 | 827 |  * Returns the time of day in a timespec. | 
 | 828 |  */ | 
 | 829 | void getnstimeofday(struct timespec *ts) | 
 | 830 | { | 
 | 831 | 	__get_realtime_clock_ts(ts); | 
 | 832 | } | 
 | 833 |  | 
 | 834 | EXPORT_SYMBOL(getnstimeofday); | 
 | 835 |  | 
 | 836 | /** | 
 | 837 |  * do_gettimeofday - Returns the time of day in a timeval | 
 | 838 |  * @tv:		pointer to the timeval to be set | 
 | 839 |  * | 
 | 840 |  * NOTE: Users should be converted to using get_realtime_clock_ts() | 
 | 841 |  */ | 
 | 842 | void do_gettimeofday(struct timeval *tv) | 
 | 843 | { | 
 | 844 | 	struct timespec now; | 
 | 845 |  | 
 | 846 | 	__get_realtime_clock_ts(&now); | 
 | 847 | 	tv->tv_sec = now.tv_sec; | 
 | 848 | 	tv->tv_usec = now.tv_nsec/1000; | 
 | 849 | } | 
 | 850 |  | 
 | 851 | EXPORT_SYMBOL(do_gettimeofday); | 
 | 852 | /** | 
 | 853 |  * do_settimeofday - Sets the time of day | 
 | 854 |  * @tv:		pointer to the timespec variable containing the new time | 
 | 855 |  * | 
 | 856 |  * Sets the time of day to the new time and update NTP and notify hrtimers | 
 | 857 |  */ | 
 | 858 | int do_settimeofday(struct timespec *tv) | 
 | 859 | { | 
 | 860 | 	unsigned long flags; | 
 | 861 | 	time_t wtm_sec, sec = tv->tv_sec; | 
 | 862 | 	long wtm_nsec, nsec = tv->tv_nsec; | 
 | 863 |  | 
 | 864 | 	if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC) | 
 | 865 | 		return -EINVAL; | 
 | 866 |  | 
 | 867 | 	write_seqlock_irqsave(&xtime_lock, flags); | 
 | 868 |  | 
 | 869 | 	nsec -= __get_nsec_offset(); | 
 | 870 |  | 
 | 871 | 	wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec); | 
 | 872 | 	wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec); | 
 | 873 |  | 
 | 874 | 	set_normalized_timespec(&xtime, sec, nsec); | 
 | 875 | 	set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec); | 
 | 876 |  | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 877 | 	clock->error = 0; | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 878 | 	ntp_clear(); | 
 | 879 |  | 
| Daniel Walker | 90675a2 | 2007-03-06 01:42:11 -0800 | [diff] [blame] | 880 | 	update_vsyscall(&xtime, clock); | 
 | 881 |  | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 882 | 	write_sequnlock_irqrestore(&xtime_lock, flags); | 
 | 883 |  | 
 | 884 | 	/* signal hrtimers about time change */ | 
 | 885 | 	clock_was_set(); | 
 | 886 |  | 
 | 887 | 	return 0; | 
 | 888 | } | 
 | 889 |  | 
 | 890 | EXPORT_SYMBOL(do_settimeofday); | 
 | 891 |  | 
 | 892 | /** | 
 | 893 |  * change_clocksource - Swaps clocksources if a new one is available | 
 | 894 |  * | 
 | 895 |  * Accumulates current time interval and initializes new clocksource | 
 | 896 |  */ | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 897 | static void change_clocksource(void) | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 898 | { | 
 | 899 | 	struct clocksource *new; | 
 | 900 | 	cycle_t now; | 
 | 901 | 	u64 nsec; | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 902 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 903 | 	new = clocksource_get_next(); | 
 | 904 |  | 
 | 905 | 	if (clock == new) | 
 | 906 | 		return; | 
 | 907 |  | 
 | 908 | 	now = clocksource_read(new); | 
 | 909 | 	nsec =  __get_nsec_offset(); | 
 | 910 | 	timespec_add_ns(&xtime, nsec); | 
 | 911 |  | 
 | 912 | 	clock = new; | 
 | 913 | 	clock->cycle_last = now; | 
 | 914 |  | 
 | 915 | 	clock->error = 0; | 
 | 916 | 	clock->xtime_nsec = 0; | 
 | 917 | 	clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); | 
 | 918 |  | 
| Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 919 | 	tick_clock_notify(); | 
 | 920 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 921 | 	printk(KERN_INFO "Time: %s clocksource has been installed.\n", | 
 | 922 | 	       clock->name); | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 923 | } | 
 | 924 | #else | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 925 | static inline void change_clocksource(void) { } | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 926 | #endif | 
 | 927 |  | 
 | 928 | /** | 
| Daniel Walker | 9d63463 | 2007-02-28 20:12:07 -0800 | [diff] [blame] | 929 |  * timekeeping_is_continuous - check to see if timekeeping is free running | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 930 |  */ | 
 | 931 | int timekeeping_is_continuous(void) | 
 | 932 | { | 
 | 933 | 	unsigned long seq; | 
 | 934 | 	int ret; | 
 | 935 |  | 
 | 936 | 	do { | 
 | 937 | 		seq = read_seqbegin(&xtime_lock); | 
 | 938 |  | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 939 | 		ret = clock->flags & CLOCK_SOURCE_VALID_FOR_HRES; | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 940 |  | 
 | 941 | 	} while (read_seqretry(&xtime_lock, seq)); | 
 | 942 |  | 
 | 943 | 	return ret; | 
 | 944 | } | 
 | 945 |  | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 946 | /** | 
 | 947 |  * read_persistent_clock -  Return time in seconds from the persistent clock. | 
 | 948 |  * | 
 | 949 |  * Weak dummy function for arches that do not yet support it. | 
 | 950 |  * Returns seconds from epoch using the battery backed persistent clock. | 
 | 951 |  * Returns zero if unsupported. | 
 | 952 |  * | 
 | 953 |  *  XXX - Do be sure to remove it once all arches implement it. | 
 | 954 |  */ | 
 | 955 | unsigned long __attribute__((weak)) read_persistent_clock(void) | 
 | 956 | { | 
 | 957 | 	return 0; | 
 | 958 | } | 
 | 959 |  | 
| Paul Mackerras | 726c14b | 2006-02-17 10:30:23 +1100 | [diff] [blame] | 960 | /* | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 961 |  * timekeeping_init - Initializes the clocksource and common timekeeping values | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 |  */ | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 963 | void __init timekeeping_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 965 | 	unsigned long flags; | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 966 | 	unsigned long sec = read_persistent_clock(); | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 967 |  | 
 | 968 | 	write_seqlock_irqsave(&xtime_lock, flags); | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 969 |  | 
 | 970 | 	ntp_clear(); | 
 | 971 |  | 
| john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 972 | 	clock = clocksource_get_next(); | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 973 | 	clocksource_calculate_interval(clock, NTP_INTERVAL_LENGTH); | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 974 | 	clock->cycle_last = clocksource_read(clock); | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 975 |  | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 976 | 	xtime.tv_sec = sec; | 
 | 977 | 	xtime.tv_nsec = 0; | 
 | 978 | 	set_normalized_timespec(&wall_to_monotonic, | 
 | 979 | 		-xtime.tv_sec, -xtime.tv_nsec); | 
 | 980 |  | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 981 | 	write_sequnlock_irqrestore(&xtime_lock, flags); | 
 | 982 | } | 
 | 983 |  | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 984 | /* flag for if timekeeping is suspended */ | 
| john stultz | 3e14347 | 2006-07-14 00:24:17 -0700 | [diff] [blame] | 985 | static int timekeeping_suspended; | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 986 | /* time in seconds when suspend began */ | 
 | 987 | static unsigned long timekeeping_suspend_time; | 
 | 988 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 989 | /** | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 990 |  * timekeeping_resume - Resumes the generic timekeeping subsystem. | 
 | 991 |  * @dev:	unused | 
 | 992 |  * | 
 | 993 |  * This is for the generic clocksource timekeeping. | 
| Atsushi Nemoto | 8ef3860 | 2006-09-30 23:28:31 -0700 | [diff] [blame] | 994 |  * xtime/wall_to_monotonic/jiffies/etc are | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 995 |  * still managed by arch specific suspend/resume code. | 
 | 996 |  */ | 
 | 997 | static int timekeeping_resume(struct sys_device *dev) | 
 | 998 | { | 
 | 999 | 	unsigned long flags; | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 1000 | 	unsigned long now = read_persistent_clock(); | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1001 |  | 
 | 1002 | 	write_seqlock_irqsave(&xtime_lock, flags); | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 1003 |  | 
 | 1004 | 	if (now && (now > timekeeping_suspend_time)) { | 
 | 1005 | 		unsigned long sleep_length = now - timekeeping_suspend_time; | 
 | 1006 |  | 
 | 1007 | 		xtime.tv_sec += sleep_length; | 
 | 1008 | 		wall_to_monotonic.tv_sec -= sleep_length; | 
 | 1009 | 	} | 
 | 1010 | 	/* re-base the last cycle value */ | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1011 | 	clock->cycle_last = clocksource_read(clock); | 
| john stultz | 3e14347 | 2006-07-14 00:24:17 -0700 | [diff] [blame] | 1012 | 	clock->error = 0; | 
 | 1013 | 	timekeeping_suspended = 0; | 
 | 1014 | 	write_sequnlock_irqrestore(&xtime_lock, flags); | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 1015 |  | 
 | 1016 | 	touch_softlockup_watchdog(); | 
| Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 1017 |  | 
 | 1018 | 	clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); | 
 | 1019 |  | 
| Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 1020 | 	/* Resume hrtimers */ | 
| Ingo Molnar | 995f054 | 2007-04-07 12:05:00 +0200 | [diff] [blame] | 1021 | 	hres_timers_resume(); | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 1022 |  | 
| john stultz | 3e14347 | 2006-07-14 00:24:17 -0700 | [diff] [blame] | 1023 | 	return 0; | 
 | 1024 | } | 
 | 1025 |  | 
 | 1026 | static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) | 
 | 1027 | { | 
 | 1028 | 	unsigned long flags; | 
 | 1029 |  | 
 | 1030 | 	write_seqlock_irqsave(&xtime_lock, flags); | 
 | 1031 | 	timekeeping_suspended = 1; | 
| John Stultz | 411187f | 2007-02-16 01:27:30 -0800 | [diff] [blame] | 1032 | 	timekeeping_suspend_time = read_persistent_clock(); | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1033 | 	write_sequnlock_irqrestore(&xtime_lock, flags); | 
| Thomas Gleixner | 6321dd6 | 2007-03-06 08:25:42 +0100 | [diff] [blame] | 1034 |  | 
 | 1035 | 	clockevents_notify(CLOCK_EVT_NOTIFY_SUSPEND, NULL); | 
 | 1036 |  | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1037 | 	return 0; | 
 | 1038 | } | 
 | 1039 |  | 
 | 1040 | /* sysfs resume/suspend bits for timekeeping */ | 
 | 1041 | static struct sysdev_class timekeeping_sysclass = { | 
 | 1042 | 	.resume		= timekeeping_resume, | 
| john stultz | 3e14347 | 2006-07-14 00:24:17 -0700 | [diff] [blame] | 1043 | 	.suspend	= timekeeping_suspend, | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1044 | 	set_kset_name("timekeeping"), | 
 | 1045 | }; | 
 | 1046 |  | 
 | 1047 | static struct sys_device device_timer = { | 
 | 1048 | 	.id		= 0, | 
 | 1049 | 	.cls		= &timekeeping_sysclass, | 
 | 1050 | }; | 
 | 1051 |  | 
 | 1052 | static int __init timekeeping_init_device(void) | 
 | 1053 | { | 
 | 1054 | 	int error = sysdev_class_register(&timekeeping_sysclass); | 
 | 1055 | 	if (!error) | 
 | 1056 | 		error = sysdev_register(&device_timer); | 
 | 1057 | 	return error; | 
 | 1058 | } | 
 | 1059 |  | 
 | 1060 | device_initcall(timekeeping_init_device); | 
 | 1061 |  | 
 | 1062 | /* | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1063 |  * If the error is already larger, we look ahead even further | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1064 |  * to compensate for late or lost adjustments. | 
 | 1065 |  */ | 
| Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 1066 | static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, | 
 | 1067 | 						 s64 *offset) | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1068 | { | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1069 | 	s64 tick_error, i; | 
 | 1070 | 	u32 look_ahead, adj; | 
 | 1071 | 	s32 error2, mult; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1072 |  | 
 | 1073 | 	/* | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1074 | 	 * Use the current error value to determine how much to look ahead. | 
 | 1075 | 	 * The larger the error the slower we adjust for it to avoid problems | 
 | 1076 | 	 * with losing too many ticks, otherwise we would overadjust and | 
 | 1077 | 	 * produce an even larger error.  The smaller the adjustment the | 
 | 1078 | 	 * faster we try to adjust for it, as lost ticks can do less harm | 
 | 1079 | 	 * here.  This is tuned so that an error of about 1 msec is adusted | 
 | 1080 | 	 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks). | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1081 | 	 */ | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1082 | 	error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ); | 
 | 1083 | 	error2 = abs(error2); | 
 | 1084 | 	for (look_ahead = 0; error2 > 0; look_ahead++) | 
 | 1085 | 		error2 >>= 2; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1086 |  | 
 | 1087 | 	/* | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1088 | 	 * Now calculate the error in (1 << look_ahead) ticks, but first | 
 | 1089 | 	 * remove the single look ahead already included in the error. | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1090 | 	 */ | 
| Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 1091 | 	tick_error = current_tick_length() >> | 
 | 1092 | 		(TICK_LENGTH_SHIFT - clock->shift + 1); | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1093 | 	tick_error -= clock->xtime_interval >> 1; | 
 | 1094 | 	error = ((error - tick_error) >> look_ahead) + tick_error; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1095 |  | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1096 | 	/* Finally calculate the adjustment shift value.  */ | 
 | 1097 | 	i = *interval; | 
 | 1098 | 	mult = 1; | 
 | 1099 | 	if (error < 0) { | 
 | 1100 | 		error = -error; | 
 | 1101 | 		*interval = -*interval; | 
 | 1102 | 		*offset = -*offset; | 
 | 1103 | 		mult = -1; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1104 | 	} | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1105 | 	for (adj = 0; error > i; adj++) | 
 | 1106 | 		error >>= 1; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1107 |  | 
 | 1108 | 	*interval <<= adj; | 
 | 1109 | 	*offset <<= adj; | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1110 | 	return mult << adj; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1111 | } | 
 | 1112 |  | 
 | 1113 | /* | 
 | 1114 |  * Adjust the multiplier to reduce the error value, | 
 | 1115 |  * this is optimized for the most common adjustments of -1,0,1, | 
 | 1116 |  * for other values we can do a bit more work. | 
 | 1117 |  */ | 
 | 1118 | static void clocksource_adjust(struct clocksource *clock, s64 offset) | 
 | 1119 | { | 
 | 1120 | 	s64 error, interval = clock->cycle_interval; | 
 | 1121 | 	int adj; | 
 | 1122 |  | 
 | 1123 | 	error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1); | 
 | 1124 | 	if (error > interval) { | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1125 | 		error >>= 2; | 
 | 1126 | 		if (likely(error <= interval)) | 
 | 1127 | 			adj = 1; | 
 | 1128 | 		else | 
 | 1129 | 			adj = clocksource_bigadjust(error, &interval, &offset); | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1130 | 	} else if (error < -interval) { | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1131 | 		error >>= 2; | 
 | 1132 | 		if (likely(error >= -interval)) { | 
 | 1133 | 			adj = -1; | 
 | 1134 | 			interval = -interval; | 
 | 1135 | 			offset = -offset; | 
 | 1136 | 		} else | 
 | 1137 | 			adj = clocksource_bigadjust(error, &interval, &offset); | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1138 | 	} else | 
 | 1139 | 		return; | 
 | 1140 |  | 
 | 1141 | 	clock->mult += adj; | 
 | 1142 | 	clock->xtime_interval += interval; | 
 | 1143 | 	clock->xtime_nsec -= offset; | 
| Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 1144 | 	clock->error -= (interval - offset) << | 
 | 1145 | 			(TICK_LENGTH_SHIFT - clock->shift); | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1146 | } | 
 | 1147 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1148 | /** | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1149 |  * update_wall_time - Uses the current clocksource to increment the wall time | 
 | 1150 |  * | 
 | 1151 |  * Called from the timer interrupt, must hold a write on xtime_lock. | 
 | 1152 |  */ | 
 | 1153 | static void update_wall_time(void) | 
 | 1154 | { | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1155 | 	cycle_t offset; | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1156 |  | 
| john stultz | 3e14347 | 2006-07-14 00:24:17 -0700 | [diff] [blame] | 1157 | 	/* Make sure we're fully resumed: */ | 
 | 1158 | 	if (unlikely(timekeeping_suspended)) | 
 | 1159 | 		return; | 
| john stultz | 5eb6d20 | 2006-06-26 00:25:07 -0700 | [diff] [blame] | 1160 |  | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1161 | #ifdef CONFIG_GENERIC_TIME | 
 | 1162 | 	offset = (clocksource_read(clock) - clock->cycle_last) & clock->mask; | 
 | 1163 | #else | 
 | 1164 | 	offset = clock->cycle_interval; | 
 | 1165 | #endif | 
| john stultz | 3e14347 | 2006-07-14 00:24:17 -0700 | [diff] [blame] | 1166 | 	clock->xtime_nsec += (s64)xtime.tv_nsec << clock->shift; | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1167 |  | 
 | 1168 | 	/* normally this loop will run just once, however in the | 
 | 1169 | 	 * case of lost or late ticks, it will accumulate correctly. | 
 | 1170 | 	 */ | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1171 | 	while (offset >= clock->cycle_interval) { | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1172 | 		/* accumulate one interval */ | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1173 | 		clock->xtime_nsec += clock->xtime_interval; | 
 | 1174 | 		clock->cycle_last += clock->cycle_interval; | 
 | 1175 | 		offset -= clock->cycle_interval; | 
 | 1176 |  | 
 | 1177 | 		if (clock->xtime_nsec >= (u64)NSEC_PER_SEC << clock->shift) { | 
 | 1178 | 			clock->xtime_nsec -= (u64)NSEC_PER_SEC << clock->shift; | 
 | 1179 | 			xtime.tv_sec++; | 
 | 1180 | 			second_overflow(); | 
 | 1181 | 		} | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1182 |  | 
| john stultz | 5eb6d20 | 2006-06-26 00:25:07 -0700 | [diff] [blame] | 1183 | 		/* interpolator bits */ | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1184 | 		time_interpolator_update(clock->xtime_interval | 
| john stultz | 5eb6d20 | 2006-06-26 00:25:07 -0700 | [diff] [blame] | 1185 | 						>> clock->shift); | 
| john stultz | 5eb6d20 | 2006-06-26 00:25:07 -0700 | [diff] [blame] | 1186 |  | 
 | 1187 | 		/* accumulate error between NTP and clock interval */ | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1188 | 		clock->error += current_tick_length(); | 
 | 1189 | 		clock->error -= clock->xtime_interval << (TICK_LENGTH_SHIFT - clock->shift); | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1190 | 	} | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1191 |  | 
 | 1192 | 	/* correct the clock when NTP error is too big */ | 
 | 1193 | 	clocksource_adjust(clock, offset); | 
 | 1194 |  | 
| john stultz | 5eb6d20 | 2006-06-26 00:25:07 -0700 | [diff] [blame] | 1195 | 	/* store full nanoseconds into xtime */ | 
| Roman Zippel | e154ff3 | 2006-07-10 04:44:32 -0700 | [diff] [blame] | 1196 | 	xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift; | 
| Roman Zippel | 19923c1 | 2006-06-26 00:25:18 -0700 | [diff] [blame] | 1197 | 	clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift; | 
| john stultz | cf3c769 | 2006-06-26 00:25:08 -0700 | [diff] [blame] | 1198 |  | 
 | 1199 | 	/* check to see if there is a new clocksource to use */ | 
| Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 1200 | 	change_clocksource(); | 
| john stultz | acc9a9d | 2007-02-16 01:28:17 -0800 | [diff] [blame] | 1201 | 	update_vsyscall(&xtime, clock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } | 
 | 1203 |  | 
 | 1204 | /* | 
 | 1205 |  * Called from the timer interrupt handler to charge one tick to the current  | 
 | 1206 |  * process.  user_tick is 1 if the tick is user time, 0 for system. | 
 | 1207 |  */ | 
 | 1208 | void update_process_times(int user_tick) | 
 | 1209 | { | 
 | 1210 | 	struct task_struct *p = current; | 
 | 1211 | 	int cpu = smp_processor_id(); | 
 | 1212 |  | 
 | 1213 | 	/* Note: this timer irq context must be accounted for as well. */ | 
 | 1214 | 	if (user_tick) | 
 | 1215 | 		account_user_time(p, jiffies_to_cputime(1)); | 
 | 1216 | 	else | 
 | 1217 | 		account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1)); | 
 | 1218 | 	run_local_timers(); | 
 | 1219 | 	if (rcu_pending(cpu)) | 
 | 1220 | 		rcu_check_callbacks(cpu, user_tick); | 
 | 1221 | 	scheduler_tick(); | 
 | 1222 |  	run_posix_cpu_timers(p); | 
 | 1223 | } | 
 | 1224 |  | 
 | 1225 | /* | 
 | 1226 |  * Nr of active tasks - counted in fixed-point numbers | 
 | 1227 |  */ | 
 | 1228 | static unsigned long count_active_tasks(void) | 
 | 1229 | { | 
| Jack Steiner | db1b1fe | 2006-03-31 02:31:21 -0800 | [diff] [blame] | 1230 | 	return nr_active() * FIXED_1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } | 
 | 1232 |  | 
 | 1233 | /* | 
 | 1234 |  * Hmm.. Changed this, as the GNU make sources (load.c) seems to | 
 | 1235 |  * imply that avenrun[] is the standard name for this kind of thing. | 
 | 1236 |  * Nothing else seems to be standardized: the fractional size etc | 
 | 1237 |  * all seem to differ on different machines. | 
 | 1238 |  * | 
 | 1239 |  * Requires xtime_lock to access. | 
 | 1240 |  */ | 
 | 1241 | unsigned long avenrun[3]; | 
 | 1242 |  | 
 | 1243 | EXPORT_SYMBOL(avenrun); | 
 | 1244 |  | 
 | 1245 | /* | 
 | 1246 |  * calc_load - given tick count, update the avenrun load estimates. | 
 | 1247 |  * This is called while holding a write_lock on xtime_lock. | 
 | 1248 |  */ | 
 | 1249 | static inline void calc_load(unsigned long ticks) | 
 | 1250 | { | 
 | 1251 | 	unsigned long active_tasks; /* fixed-point */ | 
 | 1252 | 	static int count = LOAD_FREQ; | 
 | 1253 |  | 
| Eric Dumazet | cd7175e | 2006-12-13 00:35:45 -0800 | [diff] [blame] | 1254 | 	count -= ticks; | 
 | 1255 | 	if (unlikely(count < 0)) { | 
 | 1256 | 		active_tasks = count_active_tasks(); | 
 | 1257 | 		do { | 
 | 1258 | 			CALC_LOAD(avenrun[0], EXP_1, active_tasks); | 
 | 1259 | 			CALC_LOAD(avenrun[1], EXP_5, active_tasks); | 
 | 1260 | 			CALC_LOAD(avenrun[2], EXP_15, active_tasks); | 
 | 1261 | 			count += LOAD_FREQ; | 
 | 1262 | 		} while (count < 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | 	} | 
 | 1264 | } | 
 | 1265 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | /* | 
 | 1267 |  * This read-write spinlock protects us from races in SMP while | 
 | 1268 |  * playing with xtime and avenrun. | 
 | 1269 |  */ | 
| Eric Dumazet | 5809f9d | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 1270 | __attribute__((weak)) __cacheline_aligned_in_smp DEFINE_SEQLOCK(xtime_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 |  | 
 | 1272 | EXPORT_SYMBOL(xtime_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 |  | 
 | 1274 | /* | 
 | 1275 |  * This function runs timers and the timer-tq in bottom half context. | 
 | 1276 |  */ | 
 | 1277 | static void run_timer_softirq(struct softirq_action *h) | 
 | 1278 | { | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1279 | 	tvec_base_t *base = __get_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1281 | 	hrtimer_run_queues(); | 
 | 1282 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | 	if (time_after_eq(jiffies, base->timer_jiffies)) | 
 | 1284 | 		__run_timers(base); | 
 | 1285 | } | 
 | 1286 |  | 
 | 1287 | /* | 
 | 1288 |  * Called by the local, per-CPU timer interrupt on SMP. | 
 | 1289 |  */ | 
 | 1290 | void run_local_timers(void) | 
 | 1291 | { | 
 | 1292 | 	raise_softirq(TIMER_SOFTIRQ); | 
| Ingo Molnar | 6687a97 | 2006-03-24 03:18:41 -0800 | [diff] [blame] | 1293 | 	softlockup_tick(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | } | 
 | 1295 |  | 
 | 1296 | /* | 
 | 1297 |  * Called by the timer interrupt. xtime_lock must already be taken | 
 | 1298 |  * by the timer IRQ! | 
 | 1299 |  */ | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 1300 | static inline void update_times(unsigned long ticks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | { | 
| john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 1302 | 	update_wall_time(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | 	calc_load(ticks); | 
 | 1304 | } | 
 | 1305 |    | 
 | 1306 | /* | 
 | 1307 |  * The 64-bit jiffies value is not atomic - you MUST NOT read it | 
 | 1308 |  * without sampling the sequence number in xtime_lock. | 
 | 1309 |  * jiffies is defined in the linker script... | 
 | 1310 |  */ | 
 | 1311 |  | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 1312 | void do_timer(unsigned long ticks) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | { | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 1314 | 	jiffies_64 += ticks; | 
 | 1315 | 	update_times(ticks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } | 
 | 1317 |  | 
 | 1318 | #ifdef __ARCH_WANT_SYS_ALARM | 
 | 1319 |  | 
 | 1320 | /* | 
 | 1321 |  * For backwards compatibility?  This can be done in libc so Alpha | 
 | 1322 |  * and all newer ports shouldn't need it. | 
 | 1323 |  */ | 
 | 1324 | asmlinkage unsigned long sys_alarm(unsigned int seconds) | 
 | 1325 | { | 
| Thomas Gleixner | c08b8a4 | 2006-03-25 03:06:33 -0800 | [diff] [blame] | 1326 | 	return alarm_setitimer(seconds); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | } | 
 | 1328 |  | 
 | 1329 | #endif | 
 | 1330 |  | 
 | 1331 | #ifndef __alpha__ | 
 | 1332 |  | 
 | 1333 | /* | 
 | 1334 |  * The Alpha uses getxpid, getxuid, and getxgid instead.  Maybe this | 
 | 1335 |  * should be moved into arch/i386 instead? | 
 | 1336 |  */ | 
 | 1337 |  | 
 | 1338 | /** | 
 | 1339 |  * sys_getpid - return the thread group id of the current process | 
 | 1340 |  * | 
 | 1341 |  * Note, despite the name, this returns the tgid not the pid.  The tgid and | 
 | 1342 |  * the pid are identical unless CLONE_THREAD was specified on clone() in | 
 | 1343 |  * which case the tgid is the same in all threads of the same group. | 
 | 1344 |  * | 
 | 1345 |  * This is SMP safe as current->tgid does not change. | 
 | 1346 |  */ | 
 | 1347 | asmlinkage long sys_getpid(void) | 
 | 1348 | { | 
 | 1349 | 	return current->tgid; | 
 | 1350 | } | 
 | 1351 |  | 
 | 1352 | /* | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 1353 |  * Accessing ->real_parent is not SMP-safe, it could | 
 | 1354 |  * change from under us. However, we can use a stale | 
 | 1355 |  * value of ->real_parent under rcu_read_lock(), see | 
 | 1356 |  * release_task()->call_rcu(delayed_put_task_struct). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 |  */ | 
 | 1358 | asmlinkage long sys_getppid(void) | 
 | 1359 | { | 
 | 1360 | 	int pid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 |  | 
| Kirill Korotaev | 6997a6f | 2006-08-13 23:24:23 -0700 | [diff] [blame] | 1362 | 	rcu_read_lock(); | 
 | 1363 | 	pid = rcu_dereference(current->real_parent)->tgid; | 
 | 1364 | 	rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | 	return pid; | 
 | 1367 | } | 
 | 1368 |  | 
 | 1369 | asmlinkage long sys_getuid(void) | 
 | 1370 | { | 
 | 1371 | 	/* Only we change this so SMP safe */ | 
 | 1372 | 	return current->uid; | 
 | 1373 | } | 
 | 1374 |  | 
 | 1375 | asmlinkage long sys_geteuid(void) | 
 | 1376 | { | 
 | 1377 | 	/* Only we change this so SMP safe */ | 
 | 1378 | 	return current->euid; | 
 | 1379 | } | 
 | 1380 |  | 
 | 1381 | asmlinkage long sys_getgid(void) | 
 | 1382 | { | 
 | 1383 | 	/* Only we change this so SMP safe */ | 
 | 1384 | 	return current->gid; | 
 | 1385 | } | 
 | 1386 |  | 
 | 1387 | asmlinkage long sys_getegid(void) | 
 | 1388 | { | 
 | 1389 | 	/* Only we change this so SMP safe */ | 
 | 1390 | 	return  current->egid; | 
 | 1391 | } | 
 | 1392 |  | 
 | 1393 | #endif | 
 | 1394 |  | 
 | 1395 | static void process_timeout(unsigned long __data) | 
 | 1396 | { | 
| Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1397 | 	wake_up_process((struct task_struct *)__data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } | 
 | 1399 |  | 
 | 1400 | /** | 
 | 1401 |  * schedule_timeout - sleep until timeout | 
 | 1402 |  * @timeout: timeout value in jiffies | 
 | 1403 |  * | 
 | 1404 |  * Make the current task sleep until @timeout jiffies have | 
 | 1405 |  * elapsed. The routine will return immediately unless | 
 | 1406 |  * the current task state has been set (see set_current_state()). | 
 | 1407 |  * | 
 | 1408 |  * You can set the task state as follows - | 
 | 1409 |  * | 
 | 1410 |  * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to | 
 | 1411 |  * pass before the routine returns. The routine will return 0 | 
 | 1412 |  * | 
 | 1413 |  * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | 
 | 1414 |  * delivered to the current task. In this case the remaining time | 
 | 1415 |  * in jiffies will be returned, or 0 if the timer expired in time | 
 | 1416 |  * | 
 | 1417 |  * The current task state is guaranteed to be TASK_RUNNING when this | 
 | 1418 |  * routine returns. | 
 | 1419 |  * | 
 | 1420 |  * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule | 
 | 1421 |  * the CPU away without a bound on the timeout. In this case the return | 
 | 1422 |  * value will be %MAX_SCHEDULE_TIMEOUT. | 
 | 1423 |  * | 
 | 1424 |  * In all cases the return value is guaranteed to be non-negative. | 
 | 1425 |  */ | 
 | 1426 | fastcall signed long __sched schedule_timeout(signed long timeout) | 
 | 1427 | { | 
 | 1428 | 	struct timer_list timer; | 
 | 1429 | 	unsigned long expire; | 
 | 1430 |  | 
 | 1431 | 	switch (timeout) | 
 | 1432 | 	{ | 
 | 1433 | 	case MAX_SCHEDULE_TIMEOUT: | 
 | 1434 | 		/* | 
 | 1435 | 		 * These two special cases are useful to be comfortable | 
 | 1436 | 		 * in the caller. Nothing more. We could take | 
 | 1437 | 		 * MAX_SCHEDULE_TIMEOUT from one of the negative value | 
 | 1438 | 		 * but I' d like to return a valid offset (>=0) to allow | 
 | 1439 | 		 * the caller to do everything it want with the retval. | 
 | 1440 | 		 */ | 
 | 1441 | 		schedule(); | 
 | 1442 | 		goto out; | 
 | 1443 | 	default: | 
 | 1444 | 		/* | 
 | 1445 | 		 * Another bit of PARANOID. Note that the retval will be | 
 | 1446 | 		 * 0 since no piece of kernel is supposed to do a check | 
 | 1447 | 		 * for a negative retval of schedule_timeout() (since it | 
 | 1448 | 		 * should never happens anyway). You just have the printk() | 
 | 1449 | 		 * that will tell you if something is gone wrong and where. | 
 | 1450 | 		 */ | 
| Andrew Morton | 5b149bc | 2006-12-22 01:10:14 -0800 | [diff] [blame] | 1451 | 		if (timeout < 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | 			printk(KERN_ERR "schedule_timeout: wrong timeout " | 
| Andrew Morton | 5b149bc | 2006-12-22 01:10:14 -0800 | [diff] [blame] | 1453 | 				"value %lx\n", timeout); | 
 | 1454 | 			dump_stack(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | 			current->state = TASK_RUNNING; | 
 | 1456 | 			goto out; | 
 | 1457 | 		} | 
 | 1458 | 	} | 
 | 1459 |  | 
 | 1460 | 	expire = timeout + jiffies; | 
 | 1461 |  | 
| Oleg Nesterov | a8db2db | 2005-10-30 15:01:38 -0800 | [diff] [blame] | 1462 | 	setup_timer(&timer, process_timeout, (unsigned long)current); | 
 | 1463 | 	__mod_timer(&timer, expire); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | 	schedule(); | 
 | 1465 | 	del_singleshot_timer_sync(&timer); | 
 | 1466 |  | 
 | 1467 | 	timeout = expire - jiffies; | 
 | 1468 |  | 
 | 1469 |  out: | 
 | 1470 | 	return timeout < 0 ? 0 : timeout; | 
 | 1471 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | EXPORT_SYMBOL(schedule_timeout); | 
 | 1473 |  | 
| Andrew Morton | 8a1c175 | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 1474 | /* | 
 | 1475 |  * We can use __set_current_state() here because schedule_timeout() calls | 
 | 1476 |  * schedule() unconditionally. | 
 | 1477 |  */ | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1478 | signed long __sched schedule_timeout_interruptible(signed long timeout) | 
 | 1479 | { | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1480 | 	__set_current_state(TASK_INTERRUPTIBLE); | 
 | 1481 | 	return schedule_timeout(timeout); | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1482 | } | 
 | 1483 | EXPORT_SYMBOL(schedule_timeout_interruptible); | 
 | 1484 |  | 
 | 1485 | signed long __sched schedule_timeout_uninterruptible(signed long timeout) | 
 | 1486 | { | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1487 | 	__set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 1488 | 	return schedule_timeout(timeout); | 
| Nishanth Aravamudan | 64ed93a | 2005-09-10 00:27:21 -0700 | [diff] [blame] | 1489 | } | 
 | 1490 | EXPORT_SYMBOL(schedule_timeout_uninterruptible); | 
 | 1491 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | /* Thread ID - the internal kernel "pid" */ | 
 | 1493 | asmlinkage long sys_gettid(void) | 
 | 1494 | { | 
 | 1495 | 	return current->pid; | 
 | 1496 | } | 
 | 1497 |  | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1498 | /** | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1499 |  * do_sysinfo - fill in sysinfo struct | 
| Rolf Eike Beer | 2aae4a1 | 2006-09-29 01:59:46 -0700 | [diff] [blame] | 1500 |  * @info: pointer to buffer to fill | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 |  */  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1502 | int do_sysinfo(struct sysinfo *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | 	unsigned long mem_total, sav_total; | 
 | 1505 | 	unsigned int mem_unit, bitcount; | 
 | 1506 | 	unsigned long seq; | 
 | 1507 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1508 | 	memset(info, 0, sizeof(struct sysinfo)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 |  | 
 | 1510 | 	do { | 
 | 1511 | 		struct timespec tp; | 
 | 1512 | 		seq = read_seqbegin(&xtime_lock); | 
 | 1513 |  | 
 | 1514 | 		/* | 
 | 1515 | 		 * This is annoying.  The below is the same thing | 
 | 1516 | 		 * posix_get_clock_monotonic() does, but it wants to | 
 | 1517 | 		 * take the lock which we want to cover the loads stuff | 
 | 1518 | 		 * too. | 
 | 1519 | 		 */ | 
 | 1520 |  | 
 | 1521 | 		getnstimeofday(&tp); | 
 | 1522 | 		tp.tv_sec += wall_to_monotonic.tv_sec; | 
 | 1523 | 		tp.tv_nsec += wall_to_monotonic.tv_nsec; | 
 | 1524 | 		if (tp.tv_nsec - NSEC_PER_SEC >= 0) { | 
 | 1525 | 			tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC; | 
 | 1526 | 			tp.tv_sec++; | 
 | 1527 | 		} | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1528 | 		info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1530 | 		info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT); | 
 | 1531 | 		info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT); | 
 | 1532 | 		info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1534 | 		info->procs = nr_threads; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | 	} while (read_seqretry(&xtime_lock, seq)); | 
 | 1536 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1537 | 	si_meminfo(info); | 
 | 1538 | 	si_swapinfo(info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 |  | 
 | 1540 | 	/* | 
 | 1541 | 	 * If the sum of all the available memory (i.e. ram + swap) | 
 | 1542 | 	 * is less than can be stored in a 32 bit unsigned long then | 
 | 1543 | 	 * we can be binary compatible with 2.2.x kernels.  If not, | 
 | 1544 | 	 * well, in that case 2.2.x was broken anyways... | 
 | 1545 | 	 * | 
 | 1546 | 	 *  -Erik Andersen <andersee@debian.org> | 
 | 1547 | 	 */ | 
 | 1548 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1549 | 	mem_total = info->totalram + info->totalswap; | 
 | 1550 | 	if (mem_total < info->totalram || mem_total < info->totalswap) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | 		goto out; | 
 | 1552 | 	bitcount = 0; | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1553 | 	mem_unit = info->mem_unit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | 	while (mem_unit > 1) { | 
 | 1555 | 		bitcount++; | 
 | 1556 | 		mem_unit >>= 1; | 
 | 1557 | 		sav_total = mem_total; | 
 | 1558 | 		mem_total <<= 1; | 
 | 1559 | 		if (mem_total < sav_total) | 
 | 1560 | 			goto out; | 
 | 1561 | 	} | 
 | 1562 |  | 
 | 1563 | 	/* | 
 | 1564 | 	 * If mem_total did not overflow, multiply all memory values by | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1565 | 	 * info->mem_unit and set it to 1.  This leaves things compatible | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | 	 * with 2.2.x, and also retains compatibility with earlier 2.4.x | 
 | 1567 | 	 * kernels... | 
 | 1568 | 	 */ | 
 | 1569 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1570 | 	info->mem_unit = 1; | 
 | 1571 | 	info->totalram <<= bitcount; | 
 | 1572 | 	info->freeram <<= bitcount; | 
 | 1573 | 	info->sharedram <<= bitcount; | 
 | 1574 | 	info->bufferram <<= bitcount; | 
 | 1575 | 	info->totalswap <<= bitcount; | 
 | 1576 | 	info->freeswap <<= bitcount; | 
 | 1577 | 	info->totalhigh <<= bitcount; | 
 | 1578 | 	info->freehigh <<= bitcount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 |  | 
| Kyle McMartin | d4d23ad | 2007-02-10 01:46:00 -0800 | [diff] [blame] | 1580 | out: | 
 | 1581 | 	return 0; | 
 | 1582 | } | 
 | 1583 |  | 
 | 1584 | asmlinkage long sys_sysinfo(struct sysinfo __user *info) | 
 | 1585 | { | 
 | 1586 | 	struct sysinfo val; | 
 | 1587 |  | 
 | 1588 | 	do_sysinfo(&val); | 
 | 1589 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | 	if (copy_to_user(info, &val, sizeof(struct sysinfo))) | 
 | 1591 | 		return -EFAULT; | 
 | 1592 |  | 
 | 1593 | 	return 0; | 
 | 1594 | } | 
 | 1595 |  | 
| Ingo Molnar | d730e88 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 1596 | /* | 
 | 1597 |  * lockdep: we want to track each per-CPU base as a separate lock-class, | 
 | 1598 |  * but timer-bases are kmalloc()-ed, so we need to attach separate | 
 | 1599 |  * keys to them: | 
 | 1600 |  */ | 
 | 1601 | static struct lock_class_key base_lock_keys[NR_CPUS]; | 
 | 1602 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1603 | static int __devinit init_timers_cpu(int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | { | 
 | 1605 | 	int j; | 
 | 1606 | 	tvec_base_t *base; | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1607 | 	static char __devinitdata tvec_base_done[NR_CPUS]; | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1608 |  | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1609 | 	if (!tvec_base_done[cpu]) { | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1610 | 		static char boot_done; | 
 | 1611 |  | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1612 | 		if (boot_done) { | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1613 | 			/* | 
 | 1614 | 			 * The APs use this path later in boot | 
 | 1615 | 			 */ | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1616 | 			base = kmalloc_node(sizeof(*base), GFP_KERNEL, | 
 | 1617 | 						cpu_to_node(cpu)); | 
 | 1618 | 			if (!base) | 
 | 1619 | 				return -ENOMEM; | 
 | 1620 | 			memset(base, 0, sizeof(*base)); | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1621 | 			per_cpu(tvec_bases, cpu) = base; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1622 | 		} else { | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1623 | 			/* | 
 | 1624 | 			 * This is for the boot CPU - we use compile-time | 
 | 1625 | 			 * static initialisation because per-cpu memory isn't | 
 | 1626 | 			 * ready yet and because the memory allocators are not | 
 | 1627 | 			 * initialised either. | 
 | 1628 | 			 */ | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1629 | 			boot_done = 1; | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1630 | 			base = &boot_tvec_bases; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1631 | 		} | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1632 | 		tvec_base_done[cpu] = 1; | 
 | 1633 | 	} else { | 
 | 1634 | 		base = per_cpu(tvec_bases, cpu); | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1635 | 	} | 
| Andrew Morton | ba6edfc | 2006-04-10 22:53:58 -0700 | [diff] [blame] | 1636 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1637 | 	spin_lock_init(&base->lock); | 
| Ingo Molnar | d730e88 | 2006-07-03 00:25:10 -0700 | [diff] [blame] | 1638 | 	lockdep_set_class(&base->lock, base_lock_keys + cpu); | 
 | 1639 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | 	for (j = 0; j < TVN_SIZE; j++) { | 
 | 1641 | 		INIT_LIST_HEAD(base->tv5.vec + j); | 
 | 1642 | 		INIT_LIST_HEAD(base->tv4.vec + j); | 
 | 1643 | 		INIT_LIST_HEAD(base->tv3.vec + j); | 
 | 1644 | 		INIT_LIST_HEAD(base->tv2.vec + j); | 
 | 1645 | 	} | 
 | 1646 | 	for (j = 0; j < TVR_SIZE; j++) | 
 | 1647 | 		INIT_LIST_HEAD(base->tv1.vec + j); | 
 | 1648 |  | 
 | 1649 | 	base->timer_jiffies = jiffies; | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1650 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } | 
 | 1652 |  | 
 | 1653 | #ifdef CONFIG_HOTPLUG_CPU | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1654 | static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | { | 
 | 1656 | 	struct timer_list *timer; | 
 | 1657 |  | 
 | 1658 | 	while (!list_empty(head)) { | 
 | 1659 | 		timer = list_entry(head->next, struct timer_list, entry); | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1660 | 		detach_timer(timer, 0); | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1661 | 		timer->base = new_base; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | 		internal_add_timer(new_base, timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | } | 
 | 1665 |  | 
 | 1666 | static void __devinit migrate_timers(int cpu) | 
 | 1667 | { | 
 | 1668 | 	tvec_base_t *old_base; | 
 | 1669 | 	tvec_base_t *new_base; | 
 | 1670 | 	int i; | 
 | 1671 |  | 
 | 1672 | 	BUG_ON(cpu_online(cpu)); | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1673 | 	old_base = per_cpu(tvec_bases, cpu); | 
 | 1674 | 	new_base = get_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 |  | 
 | 1676 | 	local_irq_disable(); | 
| Heiko Carstens | e81ce1f | 2007-03-05 00:30:51 -0800 | [diff] [blame] | 1677 | 	double_spin_lock(&new_base->lock, &old_base->lock, | 
 | 1678 | 			 smp_processor_id() < cpu); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 |  | 
| Oleg Nesterov | 3691c51 | 2006-03-31 02:30:30 -0800 | [diff] [blame] | 1680 | 	BUG_ON(old_base->running_timer); | 
 | 1681 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | 	for (i = 0; i < TVR_SIZE; i++) | 
| Oleg Nesterov | 55c888d | 2005-06-23 00:08:56 -0700 | [diff] [blame] | 1683 | 		migrate_timer_list(new_base, old_base->tv1.vec + i); | 
 | 1684 | 	for (i = 0; i < TVN_SIZE; i++) { | 
 | 1685 | 		migrate_timer_list(new_base, old_base->tv2.vec + i); | 
 | 1686 | 		migrate_timer_list(new_base, old_base->tv3.vec + i); | 
 | 1687 | 		migrate_timer_list(new_base, old_base->tv4.vec + i); | 
 | 1688 | 		migrate_timer_list(new_base, old_base->tv5.vec + i); | 
 | 1689 | 	} | 
 | 1690 |  | 
| Heiko Carstens | e81ce1f | 2007-03-05 00:30:51 -0800 | [diff] [blame] | 1691 | 	double_spin_unlock(&new_base->lock, &old_base->lock, | 
 | 1692 | 			   smp_processor_id() < cpu); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | 	local_irq_enable(); | 
 | 1694 | 	put_cpu_var(tvec_bases); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | } | 
 | 1696 | #endif /* CONFIG_HOTPLUG_CPU */ | 
 | 1697 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1698 | static int __cpuinit timer_cpu_notify(struct notifier_block *self, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | 				unsigned long action, void *hcpu) | 
 | 1700 | { | 
 | 1701 | 	long cpu = (long)hcpu; | 
 | 1702 | 	switch(action) { | 
 | 1703 | 	case CPU_UP_PREPARE: | 
| Jan Beulich | a4a6198 | 2006-03-24 03:15:54 -0800 | [diff] [blame] | 1704 | 		if (init_timers_cpu(cpu) < 0) | 
 | 1705 | 			return NOTIFY_BAD; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | 		break; | 
 | 1707 | #ifdef CONFIG_HOTPLUG_CPU | 
 | 1708 | 	case CPU_DEAD: | 
 | 1709 | 		migrate_timers(cpu); | 
 | 1710 | 		break; | 
 | 1711 | #endif | 
 | 1712 | 	default: | 
 | 1713 | 		break; | 
 | 1714 | 	} | 
 | 1715 | 	return NOTIFY_OK; | 
 | 1716 | } | 
 | 1717 |  | 
| Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1718 | static struct notifier_block __cpuinitdata timers_nb = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | 	.notifier_call	= timer_cpu_notify, | 
 | 1720 | }; | 
 | 1721 |  | 
 | 1722 |  | 
 | 1723 | void __init init_timers(void) | 
 | 1724 | { | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1725 | 	int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | 				(void *)(long)smp_processor_id()); | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1727 |  | 
| Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1728 | 	init_timer_stats(); | 
 | 1729 |  | 
| Akinobu Mita | 07dccf3 | 2006-09-29 02:00:22 -0700 | [diff] [blame] | 1730 | 	BUG_ON(err == NOTIFY_BAD); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | 	register_cpu_notifier(&timers_nb); | 
 | 1732 | 	open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL); | 
 | 1733 | } | 
 | 1734 |  | 
 | 1735 | #ifdef CONFIG_TIME_INTERPOLATION | 
 | 1736 |  | 
| Christoph Lameter | 67890d7 | 2006-03-16 23:04:00 -0800 | [diff] [blame] | 1737 | struct time_interpolator *time_interpolator __read_mostly; | 
 | 1738 | static struct time_interpolator *time_interpolator_list __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | static DEFINE_SPINLOCK(time_interpolator_lock); | 
 | 1740 |  | 
| Helge Deller | 3db5db4 | 2007-02-10 01:45:40 -0800 | [diff] [blame] | 1741 | static inline cycles_t time_interpolator_get_cycles(unsigned int src) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { | 
 | 1743 | 	unsigned long (*x)(void); | 
 | 1744 |  | 
 | 1745 | 	switch (src) | 
 | 1746 | 	{ | 
 | 1747 | 		case TIME_SOURCE_FUNCTION: | 
 | 1748 | 			x = time_interpolator->addr; | 
 | 1749 | 			return x(); | 
 | 1750 |  | 
 | 1751 | 		case TIME_SOURCE_MMIO64	: | 
| Christoph Lameter | 685db65 | 2006-03-02 02:54:35 -0800 | [diff] [blame] | 1752 | 			return readq_relaxed((void __iomem *)time_interpolator->addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 |  | 
 | 1754 | 		case TIME_SOURCE_MMIO32	: | 
| Christoph Lameter | 685db65 | 2006-03-02 02:54:35 -0800 | [diff] [blame] | 1755 | 			return readl_relaxed((void __iomem *)time_interpolator->addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 |  | 
 | 1757 | 		default: return get_cycles(); | 
 | 1758 | 	} | 
 | 1759 | } | 
 | 1760 |  | 
| Alex Williamson | 486d46a | 2005-09-06 15:17:04 -0700 | [diff] [blame] | 1761 | static inline u64 time_interpolator_get_counter(int writelock) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | { | 
 | 1763 | 	unsigned int src = time_interpolator->source; | 
 | 1764 |  | 
 | 1765 | 	if (time_interpolator->jitter) | 
 | 1766 | 	{ | 
| Helge Deller | 3db5db4 | 2007-02-10 01:45:40 -0800 | [diff] [blame] | 1767 | 		cycles_t lcycle; | 
 | 1768 | 		cycles_t now; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 |  | 
 | 1770 | 		do { | 
 | 1771 | 			lcycle = time_interpolator->last_cycle; | 
 | 1772 | 			now = time_interpolator_get_cycles(src); | 
 | 1773 | 			if (lcycle && time_after(lcycle, now)) | 
 | 1774 | 				return lcycle; | 
| Alex Williamson | 486d46a | 2005-09-06 15:17:04 -0700 | [diff] [blame] | 1775 |  | 
 | 1776 | 			/* When holding the xtime write lock, there's no need | 
 | 1777 | 			 * to add the overhead of the cmpxchg.  Readers are | 
 | 1778 | 			 * force to retry until the write lock is released. | 
 | 1779 | 			 */ | 
 | 1780 | 			if (writelock) { | 
 | 1781 | 				time_interpolator->last_cycle = now; | 
 | 1782 | 				return now; | 
 | 1783 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | 			/* Keep track of the last timer value returned. The use of cmpxchg here | 
 | 1785 | 			 * will cause contention in an SMP environment. | 
 | 1786 | 			 */ | 
 | 1787 | 		} while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle)); | 
 | 1788 | 		return now; | 
 | 1789 | 	} | 
 | 1790 | 	else | 
 | 1791 | 		return time_interpolator_get_cycles(src); | 
 | 1792 | } | 
 | 1793 |  | 
 | 1794 | void time_interpolator_reset(void) | 
 | 1795 | { | 
 | 1796 | 	time_interpolator->offset = 0; | 
| Alex Williamson | 486d46a | 2005-09-06 15:17:04 -0700 | [diff] [blame] | 1797 | 	time_interpolator->last_counter = time_interpolator_get_counter(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } | 
 | 1799 |  | 
 | 1800 | #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift) | 
 | 1801 |  | 
 | 1802 | unsigned long time_interpolator_get_offset(void) | 
 | 1803 | { | 
 | 1804 | 	/* If we do not have a time interpolator set up then just return zero */ | 
 | 1805 | 	if (!time_interpolator) | 
 | 1806 | 		return 0; | 
 | 1807 |  | 
 | 1808 | 	return time_interpolator->offset + | 
| Alex Williamson | 486d46a | 2005-09-06 15:17:04 -0700 | [diff] [blame] | 1809 | 		GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } | 
 | 1811 |  | 
 | 1812 | #define INTERPOLATOR_ADJUST 65536 | 
 | 1813 | #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST | 
 | 1814 |  | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 1815 | void time_interpolator_update(long delta_nsec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | { | 
 | 1817 | 	u64 counter; | 
 | 1818 | 	unsigned long offset; | 
 | 1819 |  | 
 | 1820 | 	/* If there is no time interpolator set up then do nothing */ | 
 | 1821 | 	if (!time_interpolator) | 
 | 1822 | 		return; | 
 | 1823 |  | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1824 | 	/* | 
 | 1825 | 	 * The interpolator compensates for late ticks by accumulating the late | 
 | 1826 | 	 * time in time_interpolator->offset. A tick earlier than expected will | 
 | 1827 | 	 * lead to a reset of the offset and a corresponding jump of the clock | 
 | 1828 | 	 * forward. Again this only works if the interpolator clock is running | 
 | 1829 | 	 * slightly slower than the regular clock and the tuning logic insures | 
 | 1830 | 	 * that. | 
 | 1831 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 |  | 
| Alex Williamson | 486d46a | 2005-09-06 15:17:04 -0700 | [diff] [blame] | 1833 | 	counter = time_interpolator_get_counter(1); | 
| Andrew Morton | a5a0d52 | 2005-10-30 15:01:42 -0800 | [diff] [blame] | 1834 | 	offset = time_interpolator->offset + | 
 | 1835 | 			GET_TI_NSECS(counter, time_interpolator); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 |  | 
 | 1837 | 	if (delta_nsec < 0 || (unsigned long) delta_nsec < offset) | 
 | 1838 | 		time_interpolator->offset = offset - delta_nsec; | 
 | 1839 | 	else { | 
 | 1840 | 		time_interpolator->skips++; | 
 | 1841 | 		time_interpolator->ns_skipped += delta_nsec - offset; | 
 | 1842 | 		time_interpolator->offset = 0; | 
 | 1843 | 	} | 
 | 1844 | 	time_interpolator->last_counter = counter; | 
 | 1845 |  | 
 | 1846 | 	/* Tuning logic for time interpolator invoked every minute or so. | 
 | 1847 | 	 * Decrease interpolator clock speed if no skips occurred and an offset is carried. | 
 | 1848 | 	 * Increase interpolator clock speed if we skip too much time. | 
 | 1849 | 	 */ | 
 | 1850 | 	if (jiffies % INTERPOLATOR_ADJUST == 0) | 
 | 1851 | 	{ | 
| Jordan Hargrave | b20367a | 2006-04-07 19:50:18 +0200 | [diff] [blame] | 1852 | 		if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | 			time_interpolator->nsec_per_cyc--; | 
 | 1854 | 		if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0) | 
 | 1855 | 			time_interpolator->nsec_per_cyc++; | 
 | 1856 | 		time_interpolator->skips = 0; | 
 | 1857 | 		time_interpolator->ns_skipped = 0; | 
 | 1858 | 	} | 
 | 1859 | } | 
 | 1860 |  | 
 | 1861 | static inline int | 
 | 1862 | is_better_time_interpolator(struct time_interpolator *new) | 
 | 1863 | { | 
 | 1864 | 	if (!time_interpolator) | 
 | 1865 | 		return 1; | 
 | 1866 | 	return new->frequency > 2*time_interpolator->frequency || | 
 | 1867 | 	    (unsigned long)new->drift < (unsigned long)time_interpolator->drift; | 
 | 1868 | } | 
 | 1869 |  | 
 | 1870 | void | 
 | 1871 | register_time_interpolator(struct time_interpolator *ti) | 
 | 1872 | { | 
 | 1873 | 	unsigned long flags; | 
 | 1874 |  | 
 | 1875 | 	/* Sanity check */ | 
| Eric Sesterhenn | 9f31252 | 2006-04-02 13:45:55 +0200 | [diff] [blame] | 1876 | 	BUG_ON(ti->frequency == 0 || ti->mask == 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 |  | 
 | 1878 | 	ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency; | 
 | 1879 | 	spin_lock(&time_interpolator_lock); | 
 | 1880 | 	write_seqlock_irqsave(&xtime_lock, flags); | 
 | 1881 | 	if (is_better_time_interpolator(ti)) { | 
 | 1882 | 		time_interpolator = ti; | 
 | 1883 | 		time_interpolator_reset(); | 
 | 1884 | 	} | 
 | 1885 | 	write_sequnlock_irqrestore(&xtime_lock, flags); | 
 | 1886 |  | 
 | 1887 | 	ti->next = time_interpolator_list; | 
 | 1888 | 	time_interpolator_list = ti; | 
 | 1889 | 	spin_unlock(&time_interpolator_lock); | 
 | 1890 | } | 
 | 1891 |  | 
 | 1892 | void | 
 | 1893 | unregister_time_interpolator(struct time_interpolator *ti) | 
 | 1894 | { | 
 | 1895 | 	struct time_interpolator *curr, **prev; | 
 | 1896 | 	unsigned long flags; | 
 | 1897 |  | 
 | 1898 | 	spin_lock(&time_interpolator_lock); | 
 | 1899 | 	prev = &time_interpolator_list; | 
 | 1900 | 	for (curr = *prev; curr; curr = curr->next) { | 
 | 1901 | 		if (curr == ti) { | 
 | 1902 | 			*prev = curr->next; | 
 | 1903 | 			break; | 
 | 1904 | 		} | 
 | 1905 | 		prev = &curr->next; | 
 | 1906 | 	} | 
 | 1907 |  | 
 | 1908 | 	write_seqlock_irqsave(&xtime_lock, flags); | 
 | 1909 | 	if (ti == time_interpolator) { | 
 | 1910 | 		/* we lost the best time-interpolator: */ | 
 | 1911 | 		time_interpolator = NULL; | 
 | 1912 | 		/* find the next-best interpolator */ | 
 | 1913 | 		for (curr = time_interpolator_list; curr; curr = curr->next) | 
 | 1914 | 			if (is_better_time_interpolator(curr)) | 
 | 1915 | 				time_interpolator = curr; | 
 | 1916 | 		time_interpolator_reset(); | 
 | 1917 | 	} | 
 | 1918 | 	write_sequnlock_irqrestore(&xtime_lock, flags); | 
 | 1919 | 	spin_unlock(&time_interpolator_lock); | 
 | 1920 | } | 
 | 1921 | #endif /* CONFIG_TIME_INTERPOLATION */ | 
 | 1922 |  | 
 | 1923 | /** | 
 | 1924 |  * msleep - sleep safely even with waitqueue interruptions | 
 | 1925 |  * @msecs: Time in milliseconds to sleep for | 
 | 1926 |  */ | 
 | 1927 | void msleep(unsigned int msecs) | 
 | 1928 | { | 
 | 1929 | 	unsigned long timeout = msecs_to_jiffies(msecs) + 1; | 
 | 1930 |  | 
| Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 1931 | 	while (timeout) | 
 | 1932 | 		timeout = schedule_timeout_uninterruptible(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | } | 
 | 1934 |  | 
 | 1935 | EXPORT_SYMBOL(msleep); | 
 | 1936 |  | 
 | 1937 | /** | 
| Domen Puncer | 96ec3ef | 2005-06-25 14:58:43 -0700 | [diff] [blame] | 1938 |  * msleep_interruptible - sleep waiting for signals | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 |  * @msecs: Time in milliseconds to sleep for | 
 | 1940 |  */ | 
 | 1941 | unsigned long msleep_interruptible(unsigned int msecs) | 
 | 1942 | { | 
 | 1943 | 	unsigned long timeout = msecs_to_jiffies(msecs) + 1; | 
 | 1944 |  | 
| Nishanth Aravamudan | 75bcc8c | 2005-09-10 00:27:24 -0700 | [diff] [blame] | 1945 | 	while (timeout && !signal_pending(current)) | 
 | 1946 | 		timeout = schedule_timeout_interruptible(timeout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | 	return jiffies_to_msecs(timeout); | 
 | 1948 | } | 
 | 1949 |  | 
 | 1950 | EXPORT_SYMBOL(msleep_interruptible); |