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