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