aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/hrtimer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/hrtimer.c')
-rw-r--r--kernel/hrtimer.c228
1 files changed, 124 insertions, 104 deletions
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index 0c8d7c048615..a9205e32a059 100644
--- a/kernel/hrtimer.c
+++ b/kernel/hrtimer.c
@@ -53,11 +53,10 @@
53/* 53/*
54 * The timer bases: 54 * The timer bases:
55 * 55 *
56 * Note: If we want to add new timer bases, we have to skip the two 56 * There are more clockids then hrtimer bases. Thus, we index
57 * clock ids captured by the cpu-timers. We do this by holding empty 57 * into the timer bases by the hrtimer_base_type enum. When trying
58 * entries rather than doing math adjustment of the clock ids. 58 * to reach a base using a clockid, hrtimer_clockid_to_base()
59 * This ensures that we capture erroneous accesses to these clock ids 59 * is used to convert from clockid to the proper hrtimer_base_type.
60 * rather than moving them into the range of valid clock id's.
61 */ 60 */
62DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = 61DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
63{ 62{
@@ -65,39 +64,55 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
65 .clock_base = 64 .clock_base =
66 { 65 {
67 { 66 {
68 .index = CLOCK_REALTIME, 67 .index = HRTIMER_BASE_MONOTONIC,
68 .clockid = CLOCK_MONOTONIC,
69 .get_time = &ktime_get,
70 .resolution = KTIME_LOW_RES,
71 },
72 {
73 .index = HRTIMER_BASE_REALTIME,
74 .clockid = CLOCK_REALTIME,
69 .get_time = &ktime_get_real, 75 .get_time = &ktime_get_real,
70 .resolution = KTIME_LOW_RES, 76 .resolution = KTIME_LOW_RES,
71 }, 77 },
72 { 78 {
73 .index = CLOCK_MONOTONIC, 79 .index = HRTIMER_BASE_BOOTTIME,
74 .get_time = &ktime_get, 80 .clockid = CLOCK_BOOTTIME,
81 .get_time = &ktime_get_boottime,
75 .resolution = KTIME_LOW_RES, 82 .resolution = KTIME_LOW_RES,
76 }, 83 },
77 } 84 }
78}; 85};
79 86
87static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
88 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
89 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
90 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
91};
92
93static inline int hrtimer_clockid_to_base(clockid_t clock_id)
94{
95 return hrtimer_clock_to_base_table[clock_id];
96}
97
98
80/* 99/*
81 * Get the coarse grained time at the softirq based on xtime and 100 * Get the coarse grained time at the softirq based on xtime and
82 * wall_to_monotonic. 101 * wall_to_monotonic.
83 */ 102 */
84static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base) 103static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
85{ 104{
86 ktime_t xtim, tomono; 105 ktime_t xtim, mono, boot;
87 struct timespec xts, tom; 106 struct timespec xts, tom, slp;
88 unsigned long seq;
89 107
90 do { 108 get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
91 seq = read_seqbegin(&xtime_lock);
92 xts = __current_kernel_time();
93 tom = __get_wall_to_monotonic();
94 } while (read_seqretry(&xtime_lock, seq));
95 109
96 xtim = timespec_to_ktime(xts); 110 xtim = timespec_to_ktime(xts);
97 tomono = timespec_to_ktime(tom); 111 mono = ktime_add(xtim, timespec_to_ktime(tom));
98 base->clock_base[CLOCK_REALTIME].softirq_time = xtim; 112 boot = ktime_add(mono, timespec_to_ktime(slp));
99 base->clock_base[CLOCK_MONOTONIC].softirq_time = 113 base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
100 ktime_add(xtim, tomono); 114 base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
115 base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
101} 116}
102 117
103/* 118/*
@@ -184,10 +199,11 @@ switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base,
184 struct hrtimer_cpu_base *new_cpu_base; 199 struct hrtimer_cpu_base *new_cpu_base;
185 int this_cpu = smp_processor_id(); 200 int this_cpu = smp_processor_id();
186 int cpu = hrtimer_get_target(this_cpu, pinned); 201 int cpu = hrtimer_get_target(this_cpu, pinned);
202 int basenum = base->index;
187 203
188again: 204again:
189 new_cpu_base = &per_cpu(hrtimer_bases, cpu); 205 new_cpu_base = &per_cpu(hrtimer_bases, cpu);
190 new_base = &new_cpu_base->clock_base[base->index]; 206 new_base = &new_cpu_base->clock_base[basenum];
191 207
192 if (base != new_base) { 208 if (base != new_base) {
193 /* 209 /*
@@ -334,6 +350,11 @@ EXPORT_SYMBOL_GPL(ktime_add_safe);
334 350
335static struct debug_obj_descr hrtimer_debug_descr; 351static struct debug_obj_descr hrtimer_debug_descr;
336 352
353static void *hrtimer_debug_hint(void *addr)
354{
355 return ((struct hrtimer *) addr)->function;
356}
357
337/* 358/*
338 * fixup_init is called when: 359 * fixup_init is called when:
339 * - an active object is initialized 360 * - an active object is initialized
@@ -393,6 +414,7 @@ static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
393 414
394static struct debug_obj_descr hrtimer_debug_descr = { 415static struct debug_obj_descr hrtimer_debug_descr = {
395 .name = "hrtimer", 416 .name = "hrtimer",
417 .debug_hint = hrtimer_debug_hint,
396 .fixup_init = hrtimer_fixup_init, 418 .fixup_init = hrtimer_fixup_init,
397 .fixup_activate = hrtimer_fixup_activate, 419 .fixup_activate = hrtimer_fixup_activate,
398 .fixup_free = hrtimer_fixup_free, 420 .fixup_free = hrtimer_fixup_free,
@@ -602,67 +624,6 @@ static int hrtimer_reprogram(struct hrtimer *timer,
602 return res; 624 return res;
603} 625}
604 626
605
606/*
607 * Retrigger next event is called after clock was set
608 *
609 * Called with interrupts disabled via on_each_cpu()
610 */
611static void retrigger_next_event(void *arg)
612{
613 struct hrtimer_cpu_base *base;
614 struct timespec realtime_offset, wtm;
615 unsigned long seq;
616
617 if (!hrtimer_hres_active())
618 return;
619
620 do {
621 seq = read_seqbegin(&xtime_lock);
622 wtm = __get_wall_to_monotonic();
623 } while (read_seqretry(&xtime_lock, seq));
624 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
625
626 base = &__get_cpu_var(hrtimer_bases);
627
628 /* Adjust CLOCK_REALTIME offset */
629 raw_spin_lock(&base->lock);
630 base->clock_base[CLOCK_REALTIME].offset =
631 timespec_to_ktime(realtime_offset);
632
633 hrtimer_force_reprogram(base, 0);
634 raw_spin_unlock(&base->lock);
635}
636
637/*
638 * Clock realtime was set
639 *
640 * Change the offset of the realtime clock vs. the monotonic
641 * clock.
642 *
643 * We might have to reprogram the high resolution timer interrupt. On
644 * SMP we call the architecture specific code to retrigger _all_ high
645 * resolution timer interrupts. On UP we just disable interrupts and
646 * call the high resolution interrupt code.
647 */
648void clock_was_set(void)
649{
650 /* Retrigger the CPU local events everywhere */
651 on_each_cpu(retrigger_next_event, NULL, 1);
652}
653
654/*
655 * During resume we might have to reprogram the high resolution timer
656 * interrupt (on the local CPU):
657 */
658void hres_timers_resume(void)
659{
660 WARN_ONCE(!irqs_disabled(),
661 KERN_INFO "hres_timers_resume() called with IRQs enabled!");
662
663 retrigger_next_event(NULL);
664}
665
666/* 627/*
667 * Initialize the high resolution related parts of cpu_base 628 * Initialize the high resolution related parts of cpu_base
668 */ 629 */
@@ -673,14 +634,6 @@ static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
673} 634}
674 635
675/* 636/*
676 * Initialize the high resolution related parts of a hrtimer
677 */
678static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
679{
680}
681
682
683/*
684 * When High resolution timers are active, try to reprogram. Note, that in case 637 * When High resolution timers are active, try to reprogram. Note, that in case
685 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry 638 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
686 * check happens. The timer gets enqueued into the rbtree. The reprogramming 639 * check happens. The timer gets enqueued into the rbtree. The reprogramming
@@ -705,11 +658,39 @@ static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
705} 658}
706 659
707/* 660/*
661 * Retrigger next event is called after clock was set
662 *
663 * Called with interrupts disabled via on_each_cpu()
664 */
665static void retrigger_next_event(void *arg)
666{
667 struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
668 struct timespec realtime_offset, xtim, wtm, sleep;
669
670 if (!hrtimer_hres_active())
671 return;
672
673 /* Optimized out for !HIGH_RES */
674 get_xtime_and_monotonic_and_sleep_offset(&xtim, &wtm, &sleep);
675 set_normalized_timespec(&realtime_offset, -wtm.tv_sec, -wtm.tv_nsec);
676
677 /* Adjust CLOCK_REALTIME offset */
678 raw_spin_lock(&base->lock);
679 base->clock_base[HRTIMER_BASE_REALTIME].offset =
680 timespec_to_ktime(realtime_offset);
681 base->clock_base[HRTIMER_BASE_BOOTTIME].offset =
682 timespec_to_ktime(sleep);
683
684 hrtimer_force_reprogram(base, 0);
685 raw_spin_unlock(&base->lock);
686}
687
688/*
708 * Switch to high resolution mode 689 * Switch to high resolution mode
709 */ 690 */
710static int hrtimer_switch_to_hres(void) 691static int hrtimer_switch_to_hres(void)
711{ 692{
712 int cpu = smp_processor_id(); 693 int i, cpu = smp_processor_id();
713 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu); 694 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
714 unsigned long flags; 695 unsigned long flags;
715 696
@@ -725,8 +706,8 @@ static int hrtimer_switch_to_hres(void)
725 return 0; 706 return 0;
726 } 707 }
727 base->hres_active = 1; 708 base->hres_active = 1;
728 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES; 709 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
729 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES; 710 base->clock_base[i].resolution = KTIME_HIGH_RES;
730 711
731 tick_setup_sched_timer(); 712 tick_setup_sched_timer();
732 713
@@ -750,10 +731,43 @@ static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
750 return 0; 731 return 0;
751} 732}
752static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } 733static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
753static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { } 734static inline void retrigger_next_event(void *arg) { }
754 735
755#endif /* CONFIG_HIGH_RES_TIMERS */ 736#endif /* CONFIG_HIGH_RES_TIMERS */
756 737
738/*
739 * Clock realtime was set
740 *
741 * Change the offset of the realtime clock vs. the monotonic
742 * clock.
743 *
744 * We might have to reprogram the high resolution timer interrupt. On
745 * SMP we call the architecture specific code to retrigger _all_ high
746 * resolution timer interrupts. On UP we just disable interrupts and
747 * call the high resolution interrupt code.
748 */
749void clock_was_set(void)
750{
751#ifdef CONFIG_HIGH_RES_TIMERS
752 /* Retrigger the CPU local events everywhere */
753 on_each_cpu(retrigger_next_event, NULL, 1);
754#endif
755 timerfd_clock_was_set();
756}
757
758/*
759 * During resume we might have to reprogram the high resolution timer
760 * interrupt (on the local CPU):
761 */
762void hrtimers_resume(void)
763{
764 WARN_ONCE(!irqs_disabled(),
765 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
766
767 retrigger_next_event(NULL);
768 timerfd_clock_was_set();
769}
770
757static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer) 771static inline void timer_stats_hrtimer_set_start_info(struct hrtimer *timer)
758{ 772{
759#ifdef CONFIG_TIMER_STATS 773#ifdef CONFIG_TIMER_STATS
@@ -846,6 +860,7 @@ static int enqueue_hrtimer(struct hrtimer *timer,
846 debug_activate(timer); 860 debug_activate(timer);
847 861
848 timerqueue_add(&base->active, &timer->node); 862 timerqueue_add(&base->active, &timer->node);
863 base->cpu_base->active_bases |= 1 << base->index;
849 864
850 /* 865 /*
851 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the 866 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
@@ -887,6 +902,8 @@ static void __remove_hrtimer(struct hrtimer *timer,
887#endif 902#endif
888 } 903 }
889 timerqueue_del(&base->active, &timer->node); 904 timerqueue_del(&base->active, &timer->node);
905 if (!timerqueue_getnext(&base->active))
906 base->cpu_base->active_bases &= ~(1 << base->index);
890out: 907out:
891 timer->state = newstate; 908 timer->state = newstate;
892} 909}
@@ -1121,6 +1138,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1121 enum hrtimer_mode mode) 1138 enum hrtimer_mode mode)
1122{ 1139{
1123 struct hrtimer_cpu_base *cpu_base; 1140 struct hrtimer_cpu_base *cpu_base;
1141 int base;
1124 1142
1125 memset(timer, 0, sizeof(struct hrtimer)); 1143 memset(timer, 0, sizeof(struct hrtimer));
1126 1144
@@ -1129,8 +1147,8 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1129 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) 1147 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1130 clock_id = CLOCK_MONOTONIC; 1148 clock_id = CLOCK_MONOTONIC;
1131 1149
1132 timer->base = &cpu_base->clock_base[clock_id]; 1150 base = hrtimer_clockid_to_base(clock_id);
1133 hrtimer_init_timer_hres(timer); 1151 timer->base = &cpu_base->clock_base[base];
1134 timerqueue_init(&timer->node); 1152 timerqueue_init(&timer->node);
1135 1153
1136#ifdef CONFIG_TIMER_STATS 1154#ifdef CONFIG_TIMER_STATS
@@ -1165,9 +1183,10 @@ EXPORT_SYMBOL_GPL(hrtimer_init);
1165int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp) 1183int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1166{ 1184{
1167 struct hrtimer_cpu_base *cpu_base; 1185 struct hrtimer_cpu_base *cpu_base;
1186 int base = hrtimer_clockid_to_base(which_clock);
1168 1187
1169 cpu_base = &__raw_get_cpu_var(hrtimer_bases); 1188 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1170 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution); 1189 *tp = ktime_to_timespec(cpu_base->clock_base[base].resolution);
1171 1190
1172 return 0; 1191 return 0;
1173} 1192}
@@ -1222,7 +1241,6 @@ static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1222void hrtimer_interrupt(struct clock_event_device *dev) 1241void hrtimer_interrupt(struct clock_event_device *dev)
1223{ 1242{
1224 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); 1243 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1225 struct hrtimer_clock_base *base;
1226 ktime_t expires_next, now, entry_time, delta; 1244 ktime_t expires_next, now, entry_time, delta;
1227 int i, retries = 0; 1245 int i, retries = 0;
1228 1246
@@ -1244,12 +1262,15 @@ retry:
1244 */ 1262 */
1245 cpu_base->expires_next.tv64 = KTIME_MAX; 1263 cpu_base->expires_next.tv64 = KTIME_MAX;
1246 1264
1247 base = cpu_base->clock_base;
1248
1249 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { 1265 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1250 ktime_t basenow; 1266 struct hrtimer_clock_base *base;
1251 struct timerqueue_node *node; 1267 struct timerqueue_node *node;
1268 ktime_t basenow;
1269
1270 if (!(cpu_base->active_bases & (1 << i)))
1271 continue;
1252 1272
1273 base = cpu_base->clock_base + i;
1253 basenow = ktime_add(now, base->offset); 1274 basenow = ktime_add(now, base->offset);
1254 1275
1255 while ((node = timerqueue_getnext(&base->active))) { 1276 while ((node = timerqueue_getnext(&base->active))) {
@@ -1282,7 +1303,6 @@ retry:
1282 1303
1283 __run_hrtimer(timer, &basenow); 1304 __run_hrtimer(timer, &basenow);
1284 } 1305 }
1285 base++;
1286 } 1306 }
1287 1307
1288 /* 1308 /*
@@ -1513,7 +1533,7 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1513 struct timespec __user *rmtp; 1533 struct timespec __user *rmtp;
1514 int ret = 0; 1534 int ret = 0;
1515 1535
1516 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index, 1536 hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
1517 HRTIMER_MODE_ABS); 1537 HRTIMER_MODE_ABS);
1518 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); 1538 hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
1519 1539
@@ -1565,7 +1585,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1565 1585
1566 restart = &current_thread_info()->restart_block; 1586 restart = &current_thread_info()->restart_block;
1567 restart->fn = hrtimer_nanosleep_restart; 1587 restart->fn = hrtimer_nanosleep_restart;
1568 restart->nanosleep.index = t.timer.base->index; 1588 restart->nanosleep.clockid = t.timer.base->clockid;
1569 restart->nanosleep.rmtp = rmtp; 1589 restart->nanosleep.rmtp = rmtp;
1570 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); 1590 restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1571 1591