at v2.6.31-rc1 1814 lines 46 kB view raw
1/* 2 * linux/kernel/hrtimer.c 3 * 4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> 5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar 6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner 7 * 8 * High-resolution kernel timers 9 * 10 * In contrast to the low-resolution timeout API implemented in 11 * kernel/timer.c, hrtimers provide finer resolution and accuracy 12 * depending on system configuration and capabilities. 13 * 14 * These timers are currently used for: 15 * - itimers 16 * - POSIX timers 17 * - nanosleep 18 * - precise in-kernel timing 19 * 20 * Started by: Thomas Gleixner and Ingo Molnar 21 * 22 * Credits: 23 * based on kernel/timer.c 24 * 25 * Help, testing, suggestions, bugfixes, improvements were 26 * provided by: 27 * 28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel 29 * et. al. 30 * 31 * For licencing details see kernel-base/COPYING 32 */ 33 34#include <linux/cpu.h> 35#include <linux/module.h> 36#include <linux/percpu.h> 37#include <linux/hrtimer.h> 38#include <linux/notifier.h> 39#include <linux/syscalls.h> 40#include <linux/kallsyms.h> 41#include <linux/interrupt.h> 42#include <linux/tick.h> 43#include <linux/seq_file.h> 44#include <linux/err.h> 45#include <linux/debugobjects.h> 46#include <linux/sched.h> 47#include <linux/timer.h> 48 49#include <asm/uaccess.h> 50 51/** 52 * ktime_get - get the monotonic time in ktime_t format 53 * 54 * returns the time in ktime_t format 55 */ 56ktime_t ktime_get(void) 57{ 58 struct timespec now; 59 60 ktime_get_ts(&now); 61 62 return timespec_to_ktime(now); 63} 64EXPORT_SYMBOL_GPL(ktime_get); 65 66/** 67 * ktime_get_real - get the real (wall-) time in ktime_t format 68 * 69 * returns the time in ktime_t format 70 */ 71ktime_t ktime_get_real(void) 72{ 73 struct timespec now; 74 75 getnstimeofday(&now); 76 77 return timespec_to_ktime(now); 78} 79 80EXPORT_SYMBOL_GPL(ktime_get_real); 81 82/* 83 * The timer bases: 84 * 85 * Note: If we want to add new timer bases, we have to skip the two 86 * clock ids captured by the cpu-timers. We do this by holding empty 87 * entries rather than doing math adjustment of the clock ids. 88 * This ensures that we capture erroneous accesses to these clock ids 89 * rather than moving them into the range of valid clock id's. 90 */ 91DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = 92{ 93 94 .clock_base = 95 { 96 { 97 .index = CLOCK_REALTIME, 98 .get_time = &ktime_get_real, 99 .resolution = KTIME_LOW_RES, 100 }, 101 { 102 .index = CLOCK_MONOTONIC, 103 .get_time = &ktime_get, 104 .resolution = KTIME_LOW_RES, 105 }, 106 } 107}; 108 109/** 110 * ktime_get_ts - get the monotonic clock in timespec format 111 * @ts: pointer to timespec variable 112 * 113 * The function calculates the monotonic clock from the realtime 114 * clock and the wall_to_monotonic offset and stores the result 115 * in normalized timespec format in the variable pointed to by @ts. 116 */ 117void ktime_get_ts(struct timespec *ts) 118{ 119 struct timespec tomono; 120 unsigned long seq; 121 122 do { 123 seq = read_seqbegin(&xtime_lock); 124 getnstimeofday(ts); 125 tomono = wall_to_monotonic; 126 127 } while (read_seqretry(&xtime_lock, seq)); 128 129 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, 130 ts->tv_nsec + tomono.tv_nsec); 131} 132EXPORT_SYMBOL_GPL(ktime_get_ts); 133 134/* 135 * Get the coarse grained time at the softirq based on xtime and 136 * wall_to_monotonic. 137 */ 138static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base) 139{ 140 ktime_t xtim, tomono; 141 struct timespec xts, tom; 142 unsigned long seq; 143 144 do { 145 seq = read_seqbegin(&xtime_lock); 146 xts = current_kernel_time(); 147 tom = wall_to_monotonic; 148 } while (read_seqretry(&xtime_lock, seq)); 149 150 xtim = timespec_to_ktime(xts); 151 tomono = timespec_to_ktime(tom); 152 base->clock_base[CLOCK_REALTIME].softirq_time = xtim; 153 base->clock_base[CLOCK_MONOTONIC].softirq_time = 154 ktime_add(xtim, tomono); 155} 156 157/* 158 * Functions and macros which are different for UP/SMP systems are kept in a 159 * single place 160 */ 161#ifdef CONFIG_SMP 162 163/* 164 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock 165 * means that all timers which are tied to this base via timer->base are 166 * locked, and the base itself is locked too. 167 * 168 * So __run_timers/migrate_timers can safely modify all timers which could 169 * be found on the lists/queues. 170 * 171 * When the timer's base is locked, and the timer removed from list, it is 172 * possible to set timer->base = NULL and drop the lock: the timer remains 173 * locked. 174 */ 175static 176struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, 177 unsigned long *flags) 178{ 179 struct hrtimer_clock_base *base; 180 181 for (;;) { 182 base = timer->base; 183 if (likely(base != NULL)) { 184 spin_lock_irqsave(&base->cpu_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->cpu_base->lock, *flags); 189 } 190 cpu_relax(); 191 } 192} 193 194/* 195 * Switch the timer base to the current CPU when possible. 196 */ 197static inline struct hrtimer_clock_base * 198switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, 199 int pinned) 200{ 201 struct hrtimer_clock_base *new_base; 202 struct hrtimer_cpu_base *new_cpu_base; 203 int cpu, preferred_cpu = -1; 204 205 cpu = smp_processor_id(); 206#if defined(CONFIG_NO_HZ) && defined(CONFIG_SMP) 207 if (!pinned && get_sysctl_timer_migration() && idle_cpu(cpu)) { 208 preferred_cpu = get_nohz_load_balancer(); 209 if (preferred_cpu >= 0) 210 cpu = preferred_cpu; 211 } 212#endif 213 214again: 215 new_cpu_base = &per_cpu(hrtimer_bases, cpu); 216 new_base = &new_cpu_base->clock_base[base->index]; 217 218 if (base != new_base) { 219 /* 220 * We are trying to schedule the timer on the local CPU. 221 * However we can't change timer's base while it is running, 222 * so we keep it on the same CPU. No hassle vs. reprogramming 223 * the event source in the high resolution case. The softirq 224 * code will take care of this when the timer function has 225 * completed. There is no conflict as we hold the lock until 226 * the timer is enqueued. 227 */ 228 if (unlikely(hrtimer_callback_running(timer))) 229 return base; 230 231 /* See the comment in lock_timer_base() */ 232 timer->base = NULL; 233 spin_unlock(&base->cpu_base->lock); 234 spin_lock(&new_base->cpu_base->lock); 235 236 /* Optimized away for NOHZ=n SMP=n */ 237 if (cpu == preferred_cpu) { 238 /* Calculate clock monotonic expiry time */ 239#ifdef CONFIG_HIGH_RES_TIMERS 240 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), 241 new_base->offset); 242#else 243 ktime_t expires = hrtimer_get_expires(timer); 244#endif 245 246 /* 247 * Get the next event on target cpu from the 248 * clock events layer. 249 * This covers the highres=off nohz=on case as well. 250 */ 251 ktime_t next = clockevents_get_next_event(cpu); 252 253 ktime_t delta = ktime_sub(expires, next); 254 255 /* 256 * We do not migrate the timer when it is expiring 257 * before the next event on the target cpu because 258 * we cannot reprogram the target cpu hardware and 259 * we would cause it to fire late. 260 */ 261 if (delta.tv64 < 0) { 262 cpu = smp_processor_id(); 263 spin_unlock(&new_base->cpu_base->lock); 264 spin_lock(&base->cpu_base->lock); 265 timer->base = base; 266 goto again; 267 } 268 } 269 timer->base = new_base; 270 } 271 return new_base; 272} 273 274#else /* CONFIG_SMP */ 275 276static inline struct hrtimer_clock_base * 277lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) 278{ 279 struct hrtimer_clock_base *base = timer->base; 280 281 spin_lock_irqsave(&base->cpu_base->lock, *flags); 282 283 return base; 284} 285 286# define switch_hrtimer_base(t, b, p) (b) 287 288#endif /* !CONFIG_SMP */ 289 290/* 291 * Functions for the union type storage format of ktime_t which are 292 * too large for inlining: 293 */ 294#if BITS_PER_LONG < 64 295# ifndef CONFIG_KTIME_SCALAR 296/** 297 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable 298 * @kt: addend 299 * @nsec: the scalar nsec value to add 300 * 301 * Returns the sum of kt and nsec in ktime_t format 302 */ 303ktime_t ktime_add_ns(const ktime_t kt, u64 nsec) 304{ 305 ktime_t tmp; 306 307 if (likely(nsec < NSEC_PER_SEC)) { 308 tmp.tv64 = nsec; 309 } else { 310 unsigned long rem = do_div(nsec, NSEC_PER_SEC); 311 312 tmp = ktime_set((long)nsec, rem); 313 } 314 315 return ktime_add(kt, tmp); 316} 317 318EXPORT_SYMBOL_GPL(ktime_add_ns); 319 320/** 321 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable 322 * @kt: minuend 323 * @nsec: the scalar nsec value to subtract 324 * 325 * Returns the subtraction of @nsec from @kt in ktime_t format 326 */ 327ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec) 328{ 329 ktime_t tmp; 330 331 if (likely(nsec < NSEC_PER_SEC)) { 332 tmp.tv64 = nsec; 333 } else { 334 unsigned long rem = do_div(nsec, NSEC_PER_SEC); 335 336 tmp = ktime_set((long)nsec, rem); 337 } 338 339 return ktime_sub(kt, tmp); 340} 341 342EXPORT_SYMBOL_GPL(ktime_sub_ns); 343# endif /* !CONFIG_KTIME_SCALAR */ 344 345/* 346 * Divide a ktime value by a nanosecond value 347 */ 348u64 ktime_divns(const ktime_t kt, s64 div) 349{ 350 u64 dclc; 351 int sft = 0; 352 353 dclc = ktime_to_ns(kt); 354 /* Make sure the divisor is less than 2^32: */ 355 while (div >> 32) { 356 sft++; 357 div >>= 1; 358 } 359 dclc >>= sft; 360 do_div(dclc, (unsigned long) div); 361 362 return dclc; 363} 364#endif /* BITS_PER_LONG >= 64 */ 365 366/* 367 * Add two ktime values and do a safety check for overflow: 368 */ 369ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs) 370{ 371 ktime_t res = ktime_add(lhs, rhs); 372 373 /* 374 * We use KTIME_SEC_MAX here, the maximum timeout which we can 375 * return to user space in a timespec: 376 */ 377 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64) 378 res = ktime_set(KTIME_SEC_MAX, 0); 379 380 return res; 381} 382 383EXPORT_SYMBOL_GPL(ktime_add_safe); 384 385#ifdef CONFIG_DEBUG_OBJECTS_TIMERS 386 387static struct debug_obj_descr hrtimer_debug_descr; 388 389/* 390 * fixup_init is called when: 391 * - an active object is initialized 392 */ 393static int hrtimer_fixup_init(void *addr, enum debug_obj_state state) 394{ 395 struct hrtimer *timer = addr; 396 397 switch (state) { 398 case ODEBUG_STATE_ACTIVE: 399 hrtimer_cancel(timer); 400 debug_object_init(timer, &hrtimer_debug_descr); 401 return 1; 402 default: 403 return 0; 404 } 405} 406 407/* 408 * fixup_activate is called when: 409 * - an active object is activated 410 * - an unknown object is activated (might be a statically initialized object) 411 */ 412static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state) 413{ 414 switch (state) { 415 416 case ODEBUG_STATE_NOTAVAILABLE: 417 WARN_ON_ONCE(1); 418 return 0; 419 420 case ODEBUG_STATE_ACTIVE: 421 WARN_ON(1); 422 423 default: 424 return 0; 425 } 426} 427 428/* 429 * fixup_free is called when: 430 * - an active object is freed 431 */ 432static int hrtimer_fixup_free(void *addr, enum debug_obj_state state) 433{ 434 struct hrtimer *timer = addr; 435 436 switch (state) { 437 case ODEBUG_STATE_ACTIVE: 438 hrtimer_cancel(timer); 439 debug_object_free(timer, &hrtimer_debug_descr); 440 return 1; 441 default: 442 return 0; 443 } 444} 445 446static struct debug_obj_descr hrtimer_debug_descr = { 447 .name = "hrtimer", 448 .fixup_init = hrtimer_fixup_init, 449 .fixup_activate = hrtimer_fixup_activate, 450 .fixup_free = hrtimer_fixup_free, 451}; 452 453static inline void debug_hrtimer_init(struct hrtimer *timer) 454{ 455 debug_object_init(timer, &hrtimer_debug_descr); 456} 457 458static inline void debug_hrtimer_activate(struct hrtimer *timer) 459{ 460 debug_object_activate(timer, &hrtimer_debug_descr); 461} 462 463static inline void debug_hrtimer_deactivate(struct hrtimer *timer) 464{ 465 debug_object_deactivate(timer, &hrtimer_debug_descr); 466} 467 468static inline void debug_hrtimer_free(struct hrtimer *timer) 469{ 470 debug_object_free(timer, &hrtimer_debug_descr); 471} 472 473static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 474 enum hrtimer_mode mode); 475 476void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id, 477 enum hrtimer_mode mode) 478{ 479 debug_object_init_on_stack(timer, &hrtimer_debug_descr); 480 __hrtimer_init(timer, clock_id, mode); 481} 482 483void destroy_hrtimer_on_stack(struct hrtimer *timer) 484{ 485 debug_object_free(timer, &hrtimer_debug_descr); 486} 487 488#else 489static inline void debug_hrtimer_init(struct hrtimer *timer) { } 490static inline void debug_hrtimer_activate(struct hrtimer *timer) { } 491static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } 492#endif 493 494/* High resolution timer related functions */ 495#ifdef CONFIG_HIGH_RES_TIMERS 496 497/* 498 * High resolution timer enabled ? 499 */ 500static int hrtimer_hres_enabled __read_mostly = 1; 501 502/* 503 * Enable / Disable high resolution mode 504 */ 505static int __init setup_hrtimer_hres(char *str) 506{ 507 if (!strcmp(str, "off")) 508 hrtimer_hres_enabled = 0; 509 else if (!strcmp(str, "on")) 510 hrtimer_hres_enabled = 1; 511 else 512 return 0; 513 return 1; 514} 515 516__setup("highres=", setup_hrtimer_hres); 517 518/* 519 * hrtimer_high_res_enabled - query, if the highres mode is enabled 520 */ 521static inline int hrtimer_is_hres_enabled(void) 522{ 523 return hrtimer_hres_enabled; 524} 525 526/* 527 * Is the high resolution mode active ? 528 */ 529static inline int hrtimer_hres_active(void) 530{ 531 return __get_cpu_var(hrtimer_bases).hres_active; 532} 533 534/* 535 * Reprogram the event source with checking both queues for the 536 * next event 537 * Called with interrupts disabled and base->lock held 538 */ 539static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base) 540{ 541 int i; 542 struct hrtimer_clock_base *base = cpu_base->clock_base; 543 ktime_t expires; 544 545 cpu_base->expires_next.tv64 = KTIME_MAX; 546 547 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { 548 struct hrtimer *timer; 549 550 if (!base->first) 551 continue; 552 timer = rb_entry(base->first, struct hrtimer, node); 553 expires = ktime_sub(hrtimer_get_expires(timer), base->offset); 554 /* 555 * clock_was_set() has changed base->offset so the 556 * result might be negative. Fix it up to prevent a 557 * false positive in clockevents_program_event() 558 */ 559 if (expires.tv64 < 0) 560 expires.tv64 = 0; 561 if (expires.tv64 < cpu_base->expires_next.tv64) 562 cpu_base->expires_next = expires; 563 } 564 565 if (cpu_base->expires_next.tv64 != KTIME_MAX) 566 tick_program_event(cpu_base->expires_next, 1); 567} 568 569/* 570 * Shared reprogramming for clock_realtime and clock_monotonic 571 * 572 * When a timer is enqueued and expires earlier than the already enqueued 573 * timers, we have to check, whether it expires earlier than the timer for 574 * which the clock event device was armed. 575 * 576 * Called with interrupts disabled and base->cpu_base.lock held 577 */ 578static int hrtimer_reprogram(struct hrtimer *timer, 579 struct hrtimer_clock_base *base) 580{ 581 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next; 582 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset); 583 int res; 584 585 WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0); 586 587 /* 588 * When the callback is running, we do not reprogram the clock event 589 * device. The timer callback is either running on a different CPU or 590 * the callback is executed in the hrtimer_interrupt context. The 591 * reprogramming is handled either by the softirq, which called the 592 * callback or at the end of the hrtimer_interrupt. 593 */ 594 if (hrtimer_callback_running(timer)) 595 return 0; 596 597 /* 598 * CLOCK_REALTIME timer might be requested with an absolute 599 * expiry time which is less than base->offset. Nothing wrong 600 * about that, just avoid to call into the tick code, which 601 * has now objections against negative expiry values. 602 */ 603 if (expires.tv64 < 0) 604 return -ETIME; 605 606 if (expires.tv64 >= expires_next->tv64) 607 return 0; 608 609 /* 610 * Clockevents returns -ETIME, when the event was in the past. 611 */ 612 res = tick_program_event(expires, 0); 613 if (!IS_ERR_VALUE(res)) 614 *expires_next = expires; 615 return res; 616} 617 618 619/* 620 * Retrigger next event is called after clock was set 621 * 622 * Called with interrupts disabled via on_each_cpu() 623 */ 624static void retrigger_next_event(void *arg) 625{ 626 struct hrtimer_cpu_base *base; 627 struct timespec realtime_offset; 628 unsigned long seq; 629 630 if (!hrtimer_hres_active()) 631 return; 632 633 do { 634 seq = read_seqbegin(&xtime_lock); 635 set_normalized_timespec(&realtime_offset, 636 -wall_to_monotonic.tv_sec, 637 -wall_to_monotonic.tv_nsec); 638 } while (read_seqretry(&xtime_lock, seq)); 639 640 base = &__get_cpu_var(hrtimer_bases); 641 642 /* Adjust CLOCK_REALTIME offset */ 643 spin_lock(&base->lock); 644 base->clock_base[CLOCK_REALTIME].offset = 645 timespec_to_ktime(realtime_offset); 646 647 hrtimer_force_reprogram(base); 648 spin_unlock(&base->lock); 649} 650 651/* 652 * Clock realtime was set 653 * 654 * Change the offset of the realtime clock vs. the monotonic 655 * clock. 656 * 657 * We might have to reprogram the high resolution timer interrupt. On 658 * SMP we call the architecture specific code to retrigger _all_ high 659 * resolution timer interrupts. On UP we just disable interrupts and 660 * call the high resolution interrupt code. 661 */ 662void clock_was_set(void) 663{ 664 /* Retrigger the CPU local events everywhere */ 665 on_each_cpu(retrigger_next_event, NULL, 1); 666} 667 668/* 669 * During resume we might have to reprogram the high resolution timer 670 * interrupt (on the local CPU): 671 */ 672void hres_timers_resume(void) 673{ 674 WARN_ONCE(!irqs_disabled(), 675 KERN_INFO "hres_timers_resume() called with IRQs enabled!"); 676 677 retrigger_next_event(NULL); 678} 679 680/* 681 * Initialize the high resolution related parts of cpu_base 682 */ 683static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) 684{ 685 base->expires_next.tv64 = KTIME_MAX; 686 base->hres_active = 0; 687} 688 689/* 690 * Initialize the high resolution related parts of a hrtimer 691 */ 692static inline void hrtimer_init_timer_hres(struct hrtimer *timer) 693{ 694} 695 696 697/* 698 * When High resolution timers are active, try to reprogram. Note, that in case 699 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry 700 * check happens. The timer gets enqueued into the rbtree. The reprogramming 701 * and expiry check is done in the hrtimer_interrupt or in the softirq. 702 */ 703static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, 704 struct hrtimer_clock_base *base, 705 int wakeup) 706{ 707 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) { 708 if (wakeup) { 709 spin_unlock(&base->cpu_base->lock); 710 raise_softirq_irqoff(HRTIMER_SOFTIRQ); 711 spin_lock(&base->cpu_base->lock); 712 } else 713 __raise_softirq_irqoff(HRTIMER_SOFTIRQ); 714 715 return 1; 716 } 717 718 return 0; 719} 720 721/* 722 * Switch to high resolution mode 723 */ 724static int hrtimer_switch_to_hres(void) 725{ 726 int cpu = smp_processor_id(); 727 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu); 728 unsigned long flags; 729 730 if (base->hres_active) 731 return 1; 732 733 local_irq_save(flags); 734 735 if (tick_init_highres()) { 736 local_irq_restore(flags); 737 printk(KERN_WARNING "Could not switch to high resolution " 738 "mode on CPU %d\n", cpu); 739 return 0; 740 } 741 base->hres_active = 1; 742 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES; 743 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES; 744 745 tick_setup_sched_timer(); 746 747 /* "Retrigger" the interrupt to get things going */ 748 retrigger_next_event(NULL); 749 local_irq_restore(flags); 750 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n", 751 smp_processor_id()); 752 return 1; 753} 754 755#else 756 757static inline int hrtimer_hres_active(void) { return 0; } 758static inline int hrtimer_is_hres_enabled(void) { return 0; } 759static inline int hrtimer_switch_to_hres(void) { return 0; } 760static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { } 761static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, 762 struct hrtimer_clock_base *base, 763 int wakeup) 764{ 765 return 0; 766} 767static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } 768static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { } 769 770#endif /* CONFIG_HIGH_RES_TIMERS */ 771 772#ifdef CONFIG_TIMER_STATS 773void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr) 774{ 775 if (timer->start_site) 776 return; 777 778 timer->start_site = addr; 779 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN); 780 timer->start_pid = current->pid; 781} 782#endif 783 784/* 785 * Counterpart to lock_hrtimer_base above: 786 */ 787static inline 788void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) 789{ 790 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); 791} 792 793/** 794 * hrtimer_forward - forward the timer expiry 795 * @timer: hrtimer to forward 796 * @now: forward past this time 797 * @interval: the interval to forward 798 * 799 * Forward the timer expiry so it will expire in the future. 800 * Returns the number of overruns. 801 */ 802u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) 803{ 804 u64 orun = 1; 805 ktime_t delta; 806 807 delta = ktime_sub(now, hrtimer_get_expires(timer)); 808 809 if (delta.tv64 < 0) 810 return 0; 811 812 if (interval.tv64 < timer->base->resolution.tv64) 813 interval.tv64 = timer->base->resolution.tv64; 814 815 if (unlikely(delta.tv64 >= interval.tv64)) { 816 s64 incr = ktime_to_ns(interval); 817 818 orun = ktime_divns(delta, incr); 819 hrtimer_add_expires_ns(timer, incr * orun); 820 if (hrtimer_get_expires_tv64(timer) > now.tv64) 821 return orun; 822 /* 823 * This (and the ktime_add() below) is the 824 * correction for exact: 825 */ 826 orun++; 827 } 828 hrtimer_add_expires(timer, interval); 829 830 return orun; 831} 832EXPORT_SYMBOL_GPL(hrtimer_forward); 833 834/* 835 * enqueue_hrtimer - internal function to (re)start a timer 836 * 837 * The timer is inserted in expiry order. Insertion into the 838 * red black tree is O(log(n)). Must hold the base lock. 839 * 840 * Returns 1 when the new timer is the leftmost timer in the tree. 841 */ 842static int enqueue_hrtimer(struct hrtimer *timer, 843 struct hrtimer_clock_base *base) 844{ 845 struct rb_node **link = &base->active.rb_node; 846 struct rb_node *parent = NULL; 847 struct hrtimer *entry; 848 int leftmost = 1; 849 850 debug_hrtimer_activate(timer); 851 852 /* 853 * Find the right place in the rbtree: 854 */ 855 while (*link) { 856 parent = *link; 857 entry = rb_entry(parent, struct hrtimer, node); 858 /* 859 * We dont care about collisions. Nodes with 860 * the same expiry time stay together. 861 */ 862 if (hrtimer_get_expires_tv64(timer) < 863 hrtimer_get_expires_tv64(entry)) { 864 link = &(*link)->rb_left; 865 } else { 866 link = &(*link)->rb_right; 867 leftmost = 0; 868 } 869 } 870 871 /* 872 * Insert the timer to the rbtree and check whether it 873 * replaces the first pending timer 874 */ 875 if (leftmost) 876 base->first = &timer->node; 877 878 rb_link_node(&timer->node, parent, link); 879 rb_insert_color(&timer->node, &base->active); 880 /* 881 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the 882 * state of a possibly running callback. 883 */ 884 timer->state |= HRTIMER_STATE_ENQUEUED; 885 886 return leftmost; 887} 888 889/* 890 * __remove_hrtimer - internal function to remove a timer 891 * 892 * Caller must hold the base lock. 893 * 894 * High resolution timer mode reprograms the clock event device when the 895 * timer is the one which expires next. The caller can disable this by setting 896 * reprogram to zero. This is useful, when the context does a reprogramming 897 * anyway (e.g. timer interrupt) 898 */ 899static void __remove_hrtimer(struct hrtimer *timer, 900 struct hrtimer_clock_base *base, 901 unsigned long newstate, int reprogram) 902{ 903 if (timer->state & HRTIMER_STATE_ENQUEUED) { 904 /* 905 * Remove the timer from the rbtree and replace the 906 * first entry pointer if necessary. 907 */ 908 if (base->first == &timer->node) { 909 base->first = rb_next(&timer->node); 910 /* Reprogram the clock event device. if enabled */ 911 if (reprogram && hrtimer_hres_active()) 912 hrtimer_force_reprogram(base->cpu_base); 913 } 914 rb_erase(&timer->node, &base->active); 915 } 916 timer->state = newstate; 917} 918 919/* 920 * remove hrtimer, called with base lock held 921 */ 922static inline int 923remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base) 924{ 925 if (hrtimer_is_queued(timer)) { 926 int reprogram; 927 928 /* 929 * Remove the timer and force reprogramming when high 930 * resolution mode is active and the timer is on the current 931 * CPU. If we remove a timer on another CPU, reprogramming is 932 * skipped. The interrupt event on this CPU is fired and 933 * reprogramming happens in the interrupt handler. This is a 934 * rare case and less expensive than a smp call. 935 */ 936 debug_hrtimer_deactivate(timer); 937 timer_stats_hrtimer_clear_start_info(timer); 938 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); 939 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 940 reprogram); 941 return 1; 942 } 943 return 0; 944} 945 946int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 947 unsigned long delta_ns, const enum hrtimer_mode mode, 948 int wakeup) 949{ 950 struct hrtimer_clock_base *base, *new_base; 951 unsigned long flags; 952 int ret, leftmost; 953 954 base = lock_hrtimer_base(timer, &flags); 955 956 /* Remove an active timer from the queue: */ 957 ret = remove_hrtimer(timer, base); 958 959 /* Switch the timer base, if necessary: */ 960 new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED); 961 962 if (mode & HRTIMER_MODE_REL) { 963 tim = ktime_add_safe(tim, new_base->get_time()); 964 /* 965 * CONFIG_TIME_LOW_RES is a temporary way for architectures 966 * to signal that they simply return xtime in 967 * do_gettimeoffset(). In this case we want to round up by 968 * resolution when starting a relative timer, to avoid short 969 * timeouts. This will go away with the GTOD framework. 970 */ 971#ifdef CONFIG_TIME_LOW_RES 972 tim = ktime_add_safe(tim, base->resolution); 973#endif 974 } 975 976 hrtimer_set_expires_range_ns(timer, tim, delta_ns); 977 978 timer_stats_hrtimer_set_start_info(timer); 979 980 leftmost = enqueue_hrtimer(timer, new_base); 981 982 /* 983 * Only allow reprogramming if the new base is on this CPU. 984 * (it might still be on another CPU if the timer was pending) 985 * 986 * XXX send_remote_softirq() ? 987 */ 988 if (leftmost && new_base->cpu_base == &__get_cpu_var(hrtimer_bases)) 989 hrtimer_enqueue_reprogram(timer, new_base, wakeup); 990 991 unlock_hrtimer_base(timer, &flags); 992 993 return ret; 994} 995 996/** 997 * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU 998 * @timer: the timer to be added 999 * @tim: expiry time 1000 * @delta_ns: "slack" range for the timer 1001 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) 1002 * 1003 * Returns: 1004 * 0 on success 1005 * 1 when the timer was active 1006 */ 1007int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 1008 unsigned long delta_ns, const enum hrtimer_mode mode) 1009{ 1010 return __hrtimer_start_range_ns(timer, tim, delta_ns, mode, 1); 1011} 1012EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); 1013 1014/** 1015 * hrtimer_start - (re)start an hrtimer on the current CPU 1016 * @timer: the timer to be added 1017 * @tim: expiry time 1018 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) 1019 * 1020 * Returns: 1021 * 0 on success 1022 * 1 when the timer was active 1023 */ 1024int 1025hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) 1026{ 1027 return __hrtimer_start_range_ns(timer, tim, 0, mode, 1); 1028} 1029EXPORT_SYMBOL_GPL(hrtimer_start); 1030 1031 1032/** 1033 * hrtimer_try_to_cancel - try to deactivate a timer 1034 * @timer: hrtimer to stop 1035 * 1036 * Returns: 1037 * 0 when the timer was not active 1038 * 1 when the timer was active 1039 * -1 when the timer is currently excuting the callback function and 1040 * cannot be stopped 1041 */ 1042int hrtimer_try_to_cancel(struct hrtimer *timer) 1043{ 1044 struct hrtimer_clock_base *base; 1045 unsigned long flags; 1046 int ret = -1; 1047 1048 base = lock_hrtimer_base(timer, &flags); 1049 1050 if (!hrtimer_callback_running(timer)) 1051 ret = remove_hrtimer(timer, base); 1052 1053 unlock_hrtimer_base(timer, &flags); 1054 1055 return ret; 1056 1057} 1058EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); 1059 1060/** 1061 * hrtimer_cancel - cancel a timer and wait for the handler to finish. 1062 * @timer: the timer to be cancelled 1063 * 1064 * Returns: 1065 * 0 when the timer was not active 1066 * 1 when the timer was active 1067 */ 1068int hrtimer_cancel(struct hrtimer *timer) 1069{ 1070 for (;;) { 1071 int ret = hrtimer_try_to_cancel(timer); 1072 1073 if (ret >= 0) 1074 return ret; 1075 cpu_relax(); 1076 } 1077} 1078EXPORT_SYMBOL_GPL(hrtimer_cancel); 1079 1080/** 1081 * hrtimer_get_remaining - get remaining time for the timer 1082 * @timer: the timer to read 1083 */ 1084ktime_t hrtimer_get_remaining(const struct hrtimer *timer) 1085{ 1086 struct hrtimer_clock_base *base; 1087 unsigned long flags; 1088 ktime_t rem; 1089 1090 base = lock_hrtimer_base(timer, &flags); 1091 rem = hrtimer_expires_remaining(timer); 1092 unlock_hrtimer_base(timer, &flags); 1093 1094 return rem; 1095} 1096EXPORT_SYMBOL_GPL(hrtimer_get_remaining); 1097 1098#ifdef CONFIG_NO_HZ 1099/** 1100 * hrtimer_get_next_event - get the time until next expiry event 1101 * 1102 * Returns the delta to the next expiry event or KTIME_MAX if no timer 1103 * is pending. 1104 */ 1105ktime_t hrtimer_get_next_event(void) 1106{ 1107 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1108 struct hrtimer_clock_base *base = cpu_base->clock_base; 1109 ktime_t delta, mindelta = { .tv64 = KTIME_MAX }; 1110 unsigned long flags; 1111 int i; 1112 1113 spin_lock_irqsave(&cpu_base->lock, flags); 1114 1115 if (!hrtimer_hres_active()) { 1116 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { 1117 struct hrtimer *timer; 1118 1119 if (!base->first) 1120 continue; 1121 1122 timer = rb_entry(base->first, struct hrtimer, node); 1123 delta.tv64 = hrtimer_get_expires_tv64(timer); 1124 delta = ktime_sub(delta, base->get_time()); 1125 if (delta.tv64 < mindelta.tv64) 1126 mindelta.tv64 = delta.tv64; 1127 } 1128 } 1129 1130 spin_unlock_irqrestore(&cpu_base->lock, flags); 1131 1132 if (mindelta.tv64 < 0) 1133 mindelta.tv64 = 0; 1134 return mindelta; 1135} 1136#endif 1137 1138static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 1139 enum hrtimer_mode mode) 1140{ 1141 struct hrtimer_cpu_base *cpu_base; 1142 1143 memset(timer, 0, sizeof(struct hrtimer)); 1144 1145 cpu_base = &__raw_get_cpu_var(hrtimer_bases); 1146 1147 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) 1148 clock_id = CLOCK_MONOTONIC; 1149 1150 timer->base = &cpu_base->clock_base[clock_id]; 1151 INIT_LIST_HEAD(&timer->cb_entry); 1152 hrtimer_init_timer_hres(timer); 1153 1154#ifdef CONFIG_TIMER_STATS 1155 timer->start_site = NULL; 1156 timer->start_pid = -1; 1157 memset(timer->start_comm, 0, TASK_COMM_LEN); 1158#endif 1159} 1160 1161/** 1162 * hrtimer_init - initialize a timer to the given clock 1163 * @timer: the timer to be initialized 1164 * @clock_id: the clock to be used 1165 * @mode: timer mode abs/rel 1166 */ 1167void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 1168 enum hrtimer_mode mode) 1169{ 1170 debug_hrtimer_init(timer); 1171 __hrtimer_init(timer, clock_id, mode); 1172} 1173EXPORT_SYMBOL_GPL(hrtimer_init); 1174 1175/** 1176 * hrtimer_get_res - get the timer resolution for a clock 1177 * @which_clock: which clock to query 1178 * @tp: pointer to timespec variable to store the resolution 1179 * 1180 * Store the resolution of the clock selected by @which_clock in the 1181 * variable pointed to by @tp. 1182 */ 1183int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp) 1184{ 1185 struct hrtimer_cpu_base *cpu_base; 1186 1187 cpu_base = &__raw_get_cpu_var(hrtimer_bases); 1188 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution); 1189 1190 return 0; 1191} 1192EXPORT_SYMBOL_GPL(hrtimer_get_res); 1193 1194static void __run_hrtimer(struct hrtimer *timer) 1195{ 1196 struct hrtimer_clock_base *base = timer->base; 1197 struct hrtimer_cpu_base *cpu_base = base->cpu_base; 1198 enum hrtimer_restart (*fn)(struct hrtimer *); 1199 int restart; 1200 1201 WARN_ON(!irqs_disabled()); 1202 1203 debug_hrtimer_deactivate(timer); 1204 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); 1205 timer_stats_account_hrtimer(timer); 1206 fn = timer->function; 1207 1208 /* 1209 * Because we run timers from hardirq context, there is no chance 1210 * they get migrated to another cpu, therefore its safe to unlock 1211 * the timer base. 1212 */ 1213 spin_unlock(&cpu_base->lock); 1214 restart = fn(timer); 1215 spin_lock(&cpu_base->lock); 1216 1217 /* 1218 * Note: We clear the CALLBACK bit after enqueue_hrtimer and 1219 * we do not reprogramm the event hardware. Happens either in 1220 * hrtimer_start_range_ns() or in hrtimer_interrupt() 1221 */ 1222 if (restart != HRTIMER_NORESTART) { 1223 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); 1224 enqueue_hrtimer(timer, base); 1225 } 1226 timer->state &= ~HRTIMER_STATE_CALLBACK; 1227} 1228 1229#ifdef CONFIG_HIGH_RES_TIMERS 1230 1231static int force_clock_reprogram; 1232 1233/* 1234 * After 5 iteration's attempts, we consider that hrtimer_interrupt() 1235 * is hanging, which could happen with something that slows the interrupt 1236 * such as the tracing. Then we force the clock reprogramming for each future 1237 * hrtimer interrupts to avoid infinite loops and use the min_delta_ns 1238 * threshold that we will overwrite. 1239 * The next tick event will be scheduled to 3 times we currently spend on 1240 * hrtimer_interrupt(). This gives a good compromise, the cpus will spend 1241 * 1/4 of their time to process the hrtimer interrupts. This is enough to 1242 * let it running without serious starvation. 1243 */ 1244 1245static inline void 1246hrtimer_interrupt_hanging(struct clock_event_device *dev, 1247 ktime_t try_time) 1248{ 1249 force_clock_reprogram = 1; 1250 dev->min_delta_ns = (unsigned long)try_time.tv64 * 3; 1251 printk(KERN_WARNING "hrtimer: interrupt too slow, " 1252 "forcing clock min delta to %lu ns\n", dev->min_delta_ns); 1253} 1254/* 1255 * High resolution timer interrupt 1256 * Called with interrupts disabled 1257 */ 1258void hrtimer_interrupt(struct clock_event_device *dev) 1259{ 1260 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1261 struct hrtimer_clock_base *base; 1262 ktime_t expires_next, now; 1263 int nr_retries = 0; 1264 int i; 1265 1266 BUG_ON(!cpu_base->hres_active); 1267 cpu_base->nr_events++; 1268 dev->next_event.tv64 = KTIME_MAX; 1269 1270 retry: 1271 /* 5 retries is enough to notice a hang */ 1272 if (!(++nr_retries % 5)) 1273 hrtimer_interrupt_hanging(dev, ktime_sub(ktime_get(), now)); 1274 1275 now = ktime_get(); 1276 1277 expires_next.tv64 = KTIME_MAX; 1278 1279 base = cpu_base->clock_base; 1280 1281 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { 1282 ktime_t basenow; 1283 struct rb_node *node; 1284 1285 spin_lock(&cpu_base->lock); 1286 1287 basenow = ktime_add(now, base->offset); 1288 1289 while ((node = base->first)) { 1290 struct hrtimer *timer; 1291 1292 timer = rb_entry(node, struct hrtimer, node); 1293 1294 /* 1295 * The immediate goal for using the softexpires is 1296 * minimizing wakeups, not running timers at the 1297 * earliest interrupt after their soft expiration. 1298 * This allows us to avoid using a Priority Search 1299 * Tree, which can answer a stabbing querry for 1300 * overlapping intervals and instead use the simple 1301 * BST we already have. 1302 * We don't add extra wakeups by delaying timers that 1303 * are right-of a not yet expired timer, because that 1304 * timer will have to trigger a wakeup anyway. 1305 */ 1306 1307 if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) { 1308 ktime_t expires; 1309 1310 expires = ktime_sub(hrtimer_get_expires(timer), 1311 base->offset); 1312 if (expires.tv64 < expires_next.tv64) 1313 expires_next = expires; 1314 break; 1315 } 1316 1317 __run_hrtimer(timer); 1318 } 1319 spin_unlock(&cpu_base->lock); 1320 base++; 1321 } 1322 1323 cpu_base->expires_next = expires_next; 1324 1325 /* Reprogramming necessary ? */ 1326 if (expires_next.tv64 != KTIME_MAX) { 1327 if (tick_program_event(expires_next, force_clock_reprogram)) 1328 goto retry; 1329 } 1330} 1331 1332/* 1333 * local version of hrtimer_peek_ahead_timers() called with interrupts 1334 * disabled. 1335 */ 1336static void __hrtimer_peek_ahead_timers(void) 1337{ 1338 struct tick_device *td; 1339 1340 if (!hrtimer_hres_active()) 1341 return; 1342 1343 td = &__get_cpu_var(tick_cpu_device); 1344 if (td && td->evtdev) 1345 hrtimer_interrupt(td->evtdev); 1346} 1347 1348/** 1349 * hrtimer_peek_ahead_timers -- run soft-expired timers now 1350 * 1351 * hrtimer_peek_ahead_timers will peek at the timer queue of 1352 * the current cpu and check if there are any timers for which 1353 * the soft expires time has passed. If any such timers exist, 1354 * they are run immediately and then removed from the timer queue. 1355 * 1356 */ 1357void hrtimer_peek_ahead_timers(void) 1358{ 1359 unsigned long flags; 1360 1361 local_irq_save(flags); 1362 __hrtimer_peek_ahead_timers(); 1363 local_irq_restore(flags); 1364} 1365 1366static void run_hrtimer_softirq(struct softirq_action *h) 1367{ 1368 hrtimer_peek_ahead_timers(); 1369} 1370 1371#else /* CONFIG_HIGH_RES_TIMERS */ 1372 1373static inline void __hrtimer_peek_ahead_timers(void) { } 1374 1375#endif /* !CONFIG_HIGH_RES_TIMERS */ 1376 1377/* 1378 * Called from timer softirq every jiffy, expire hrtimers: 1379 * 1380 * For HRT its the fall back code to run the softirq in the timer 1381 * softirq context in case the hrtimer initialization failed or has 1382 * not been done yet. 1383 */ 1384void hrtimer_run_pending(void) 1385{ 1386 if (hrtimer_hres_active()) 1387 return; 1388 1389 /* 1390 * This _is_ ugly: We have to check in the softirq context, 1391 * whether we can switch to highres and / or nohz mode. The 1392 * clocksource switch happens in the timer interrupt with 1393 * xtime_lock held. Notification from there only sets the 1394 * check bit in the tick_oneshot code, otherwise we might 1395 * deadlock vs. xtime_lock. 1396 */ 1397 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) 1398 hrtimer_switch_to_hres(); 1399} 1400 1401/* 1402 * Called from hardirq context every jiffy 1403 */ 1404void hrtimer_run_queues(void) 1405{ 1406 struct rb_node *node; 1407 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1408 struct hrtimer_clock_base *base; 1409 int index, gettime = 1; 1410 1411 if (hrtimer_hres_active()) 1412 return; 1413 1414 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) { 1415 base = &cpu_base->clock_base[index]; 1416 1417 if (!base->first) 1418 continue; 1419 1420 if (gettime) { 1421 hrtimer_get_softirq_time(cpu_base); 1422 gettime = 0; 1423 } 1424 1425 spin_lock(&cpu_base->lock); 1426 1427 while ((node = base->first)) { 1428 struct hrtimer *timer; 1429 1430 timer = rb_entry(node, struct hrtimer, node); 1431 if (base->softirq_time.tv64 <= 1432 hrtimer_get_expires_tv64(timer)) 1433 break; 1434 1435 __run_hrtimer(timer); 1436 } 1437 spin_unlock(&cpu_base->lock); 1438 } 1439} 1440 1441/* 1442 * Sleep related functions: 1443 */ 1444static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) 1445{ 1446 struct hrtimer_sleeper *t = 1447 container_of(timer, struct hrtimer_sleeper, timer); 1448 struct task_struct *task = t->task; 1449 1450 t->task = NULL; 1451 if (task) 1452 wake_up_process(task); 1453 1454 return HRTIMER_NORESTART; 1455} 1456 1457void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) 1458{ 1459 sl->timer.function = hrtimer_wakeup; 1460 sl->task = task; 1461} 1462 1463static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) 1464{ 1465 hrtimer_init_sleeper(t, current); 1466 1467 do { 1468 set_current_state(TASK_INTERRUPTIBLE); 1469 hrtimer_start_expires(&t->timer, mode); 1470 if (!hrtimer_active(&t->timer)) 1471 t->task = NULL; 1472 1473 if (likely(t->task)) 1474 schedule(); 1475 1476 hrtimer_cancel(&t->timer); 1477 mode = HRTIMER_MODE_ABS; 1478 1479 } while (t->task && !signal_pending(current)); 1480 1481 __set_current_state(TASK_RUNNING); 1482 1483 return t->task == NULL; 1484} 1485 1486static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp) 1487{ 1488 struct timespec rmt; 1489 ktime_t rem; 1490 1491 rem = hrtimer_expires_remaining(timer); 1492 if (rem.tv64 <= 0) 1493 return 0; 1494 rmt = ktime_to_timespec(rem); 1495 1496 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) 1497 return -EFAULT; 1498 1499 return 1; 1500} 1501 1502long __sched hrtimer_nanosleep_restart(struct restart_block *restart) 1503{ 1504 struct hrtimer_sleeper t; 1505 struct timespec __user *rmtp; 1506 int ret = 0; 1507 1508 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index, 1509 HRTIMER_MODE_ABS); 1510 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); 1511 1512 if (do_nanosleep(&t, HRTIMER_MODE_ABS)) 1513 goto out; 1514 1515 rmtp = restart->nanosleep.rmtp; 1516 if (rmtp) { 1517 ret = update_rmtp(&t.timer, rmtp); 1518 if (ret <= 0) 1519 goto out; 1520 } 1521 1522 /* The other values in restart are already filled in */ 1523 ret = -ERESTART_RESTARTBLOCK; 1524out: 1525 destroy_hrtimer_on_stack(&t.timer); 1526 return ret; 1527} 1528 1529long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, 1530 const enum hrtimer_mode mode, const clockid_t clockid) 1531{ 1532 struct restart_block *restart; 1533 struct hrtimer_sleeper t; 1534 int ret = 0; 1535 unsigned long slack; 1536 1537 slack = current->timer_slack_ns; 1538 if (rt_task(current)) 1539 slack = 0; 1540 1541 hrtimer_init_on_stack(&t.timer, clockid, mode); 1542 hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack); 1543 if (do_nanosleep(&t, mode)) 1544 goto out; 1545 1546 /* Absolute timers do not update the rmtp value and restart: */ 1547 if (mode == HRTIMER_MODE_ABS) { 1548 ret = -ERESTARTNOHAND; 1549 goto out; 1550 } 1551 1552 if (rmtp) { 1553 ret = update_rmtp(&t.timer, rmtp); 1554 if (ret <= 0) 1555 goto out; 1556 } 1557 1558 restart = &current_thread_info()->restart_block; 1559 restart->fn = hrtimer_nanosleep_restart; 1560 restart->nanosleep.index = t.timer.base->index; 1561 restart->nanosleep.rmtp = rmtp; 1562 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); 1563 1564 ret = -ERESTART_RESTARTBLOCK; 1565out: 1566 destroy_hrtimer_on_stack(&t.timer); 1567 return ret; 1568} 1569 1570SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp, 1571 struct timespec __user *, rmtp) 1572{ 1573 struct timespec tu; 1574 1575 if (copy_from_user(&tu, rqtp, sizeof(tu))) 1576 return -EFAULT; 1577 1578 if (!timespec_valid(&tu)) 1579 return -EINVAL; 1580 1581 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC); 1582} 1583 1584/* 1585 * Functions related to boot-time initialization: 1586 */ 1587static void __cpuinit init_hrtimers_cpu(int cpu) 1588{ 1589 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); 1590 int i; 1591 1592 spin_lock_init(&cpu_base->lock); 1593 1594 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) 1595 cpu_base->clock_base[i].cpu_base = cpu_base; 1596 1597 hrtimer_init_hres(cpu_base); 1598} 1599 1600#ifdef CONFIG_HOTPLUG_CPU 1601 1602static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, 1603 struct hrtimer_clock_base *new_base) 1604{ 1605 struct hrtimer *timer; 1606 struct rb_node *node; 1607 1608 while ((node = rb_first(&old_base->active))) { 1609 timer = rb_entry(node, struct hrtimer, node); 1610 BUG_ON(hrtimer_callback_running(timer)); 1611 debug_hrtimer_deactivate(timer); 1612 1613 /* 1614 * Mark it as STATE_MIGRATE not INACTIVE otherwise the 1615 * timer could be seen as !active and just vanish away 1616 * under us on another CPU 1617 */ 1618 __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0); 1619 timer->base = new_base; 1620 /* 1621 * Enqueue the timers on the new cpu. This does not 1622 * reprogram the event device in case the timer 1623 * expires before the earliest on this CPU, but we run 1624 * hrtimer_interrupt after we migrated everything to 1625 * sort out already expired timers and reprogram the 1626 * event device. 1627 */ 1628 enqueue_hrtimer(timer, new_base); 1629 1630 /* Clear the migration state bit */ 1631 timer->state &= ~HRTIMER_STATE_MIGRATE; 1632 } 1633} 1634 1635static void migrate_hrtimers(int scpu) 1636{ 1637 struct hrtimer_cpu_base *old_base, *new_base; 1638 int i; 1639 1640 BUG_ON(cpu_online(scpu)); 1641 tick_cancel_sched_timer(scpu); 1642 1643 local_irq_disable(); 1644 old_base = &per_cpu(hrtimer_bases, scpu); 1645 new_base = &__get_cpu_var(hrtimer_bases); 1646 /* 1647 * The caller is globally serialized and nobody else 1648 * takes two locks at once, deadlock is not possible. 1649 */ 1650 spin_lock(&new_base->lock); 1651 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); 1652 1653 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { 1654 migrate_hrtimer_list(&old_base->clock_base[i], 1655 &new_base->clock_base[i]); 1656 } 1657 1658 spin_unlock(&old_base->lock); 1659 spin_unlock(&new_base->lock); 1660 1661 /* Check, if we got expired work to do */ 1662 __hrtimer_peek_ahead_timers(); 1663 local_irq_enable(); 1664} 1665 1666#endif /* CONFIG_HOTPLUG_CPU */ 1667 1668static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self, 1669 unsigned long action, void *hcpu) 1670{ 1671 int scpu = (long)hcpu; 1672 1673 switch (action) { 1674 1675 case CPU_UP_PREPARE: 1676 case CPU_UP_PREPARE_FROZEN: 1677 init_hrtimers_cpu(scpu); 1678 break; 1679 1680#ifdef CONFIG_HOTPLUG_CPU 1681 case CPU_DYING: 1682 case CPU_DYING_FROZEN: 1683 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DYING, &scpu); 1684 break; 1685 case CPU_DEAD: 1686 case CPU_DEAD_FROZEN: 1687 { 1688 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &scpu); 1689 migrate_hrtimers(scpu); 1690 break; 1691 } 1692#endif 1693 1694 default: 1695 break; 1696 } 1697 1698 return NOTIFY_OK; 1699} 1700 1701static struct notifier_block __cpuinitdata hrtimers_nb = { 1702 .notifier_call = hrtimer_cpu_notify, 1703}; 1704 1705void __init hrtimers_init(void) 1706{ 1707 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE, 1708 (void *)(long)smp_processor_id()); 1709 register_cpu_notifier(&hrtimers_nb); 1710#ifdef CONFIG_HIGH_RES_TIMERS 1711 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq); 1712#endif 1713} 1714 1715/** 1716 * schedule_hrtimeout_range - sleep until timeout 1717 * @expires: timeout value (ktime_t) 1718 * @delta: slack in expires timeout (ktime_t) 1719 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL 1720 * 1721 * Make the current task sleep until the given expiry time has 1722 * elapsed. The routine will return immediately unless 1723 * the current task state has been set (see set_current_state()). 1724 * 1725 * The @delta argument gives the kernel the freedom to schedule the 1726 * actual wakeup to a time that is both power and performance friendly. 1727 * The kernel give the normal best effort behavior for "@expires+@delta", 1728 * but may decide to fire the timer earlier, but no earlier than @expires. 1729 * 1730 * You can set the task state as follows - 1731 * 1732 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to 1733 * pass before the routine returns. 1734 * 1735 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is 1736 * delivered to the current task. 1737 * 1738 * The current task state is guaranteed to be TASK_RUNNING when this 1739 * routine returns. 1740 * 1741 * Returns 0 when the timer has expired otherwise -EINTR 1742 */ 1743int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta, 1744 const enum hrtimer_mode mode) 1745{ 1746 struct hrtimer_sleeper t; 1747 1748 /* 1749 * Optimize when a zero timeout value is given. It does not 1750 * matter whether this is an absolute or a relative time. 1751 */ 1752 if (expires && !expires->tv64) { 1753 __set_current_state(TASK_RUNNING); 1754 return 0; 1755 } 1756 1757 /* 1758 * A NULL parameter means "inifinte" 1759 */ 1760 if (!expires) { 1761 schedule(); 1762 __set_current_state(TASK_RUNNING); 1763 return -EINTR; 1764 } 1765 1766 hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode); 1767 hrtimer_set_expires_range_ns(&t.timer, *expires, delta); 1768 1769 hrtimer_init_sleeper(&t, current); 1770 1771 hrtimer_start_expires(&t.timer, mode); 1772 if (!hrtimer_active(&t.timer)) 1773 t.task = NULL; 1774 1775 if (likely(t.task)) 1776 schedule(); 1777 1778 hrtimer_cancel(&t.timer); 1779 destroy_hrtimer_on_stack(&t.timer); 1780 1781 __set_current_state(TASK_RUNNING); 1782 1783 return !t.task ? 0 : -EINTR; 1784} 1785EXPORT_SYMBOL_GPL(schedule_hrtimeout_range); 1786 1787/** 1788 * schedule_hrtimeout - sleep until timeout 1789 * @expires: timeout value (ktime_t) 1790 * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL 1791 * 1792 * Make the current task sleep until the given expiry time has 1793 * elapsed. The routine will return immediately unless 1794 * the current task state has been set (see set_current_state()). 1795 * 1796 * You can set the task state as follows - 1797 * 1798 * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to 1799 * pass before the routine returns. 1800 * 1801 * %TASK_INTERRUPTIBLE - the routine may return early if a signal is 1802 * delivered to the current task. 1803 * 1804 * The current task state is guaranteed to be TASK_RUNNING when this 1805 * routine returns. 1806 * 1807 * Returns 0 when the timer has expired otherwise -EINTR 1808 */ 1809int __sched schedule_hrtimeout(ktime_t *expires, 1810 const enum hrtimer_mode mode) 1811{ 1812 return schedule_hrtimeout_range(expires, 0, mode); 1813} 1814EXPORT_SYMBOL_GPL(schedule_hrtimeout);