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