aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/hrtimer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/hrtimer.c')
-rw-r--r--kernel/hrtimer.c150
1 files changed, 66 insertions, 84 deletions
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index 05071bf6a37b..3e1c36e7998f 100644
--- a/kernel/hrtimer.c
+++ b/kernel/hrtimer.c
@@ -48,36 +48,7 @@
48 48
49#include <asm/uaccess.h> 49#include <asm/uaccess.h>
50 50
51/** 51#include <trace/events/timer.h>
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 52
82/* 53/*
83 * The timer bases: 54 * The timer bases:
@@ -106,31 +77,6 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
106 } 77 }
107}; 78};
108 79
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/* 80/*
135 * Get the coarse grained time at the softirq based on xtime and 81 * Get the coarse grained time at the softirq based on xtime and
136 * wall_to_monotonic. 82 * wall_to_monotonic.
@@ -498,6 +444,26 @@ static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
498static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } 444static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
499#endif 445#endif
500 446
447static inline void
448debug_init(struct hrtimer *timer, clockid_t clockid,
449 enum hrtimer_mode mode)
450{
451 debug_hrtimer_init(timer);
452 trace_hrtimer_init(timer, clockid, mode);
453}
454
455static inline void debug_activate(struct hrtimer *timer)
456{
457 debug_hrtimer_activate(timer);
458 trace_hrtimer_start(timer);
459}
460
461static inline void debug_deactivate(struct hrtimer *timer)
462{
463 debug_hrtimer_deactivate(timer);
464 trace_hrtimer_cancel(timer);
465}
466
501/* High resolution timer related functions */ 467/* High resolution timer related functions */
502#ifdef CONFIG_HIGH_RES_TIMERS 468#ifdef CONFIG_HIGH_RES_TIMERS
503 469
@@ -543,13 +509,14 @@ static inline int hrtimer_hres_active(void)
543 * next event 509 * next event
544 * Called with interrupts disabled and base->lock held 510 * Called with interrupts disabled and base->lock held
545 */ 511 */
546static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base) 512static void
513hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
547{ 514{
548 int i; 515 int i;
549 struct hrtimer_clock_base *base = cpu_base->clock_base; 516 struct hrtimer_clock_base *base = cpu_base->clock_base;
550 ktime_t expires; 517 ktime_t expires, expires_next;
551 518
552 cpu_base->expires_next.tv64 = KTIME_MAX; 519 expires_next.tv64 = KTIME_MAX;
553 520
554 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { 521 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
555 struct hrtimer *timer; 522 struct hrtimer *timer;
@@ -565,10 +532,15 @@ static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
565 */ 532 */
566 if (expires.tv64 < 0) 533 if (expires.tv64 < 0)
567 expires.tv64 = 0; 534 expires.tv64 = 0;
568 if (expires.tv64 < cpu_base->expires_next.tv64) 535 if (expires.tv64 < expires_next.tv64)
569 cpu_base->expires_next = expires; 536 expires_next = expires;
570 } 537 }
571 538
539 if (skip_equal && expires_next.tv64 == cpu_base->expires_next.tv64)
540 return;
541
542 cpu_base->expires_next.tv64 = expires_next.tv64;
543
572 if (cpu_base->expires_next.tv64 != KTIME_MAX) 544 if (cpu_base->expires_next.tv64 != KTIME_MAX)
573 tick_program_event(cpu_base->expires_next, 1); 545 tick_program_event(cpu_base->expires_next, 1);
574} 546}
@@ -651,7 +623,7 @@ static void retrigger_next_event(void *arg)
651 base->clock_base[CLOCK_REALTIME].offset = 623 base->clock_base[CLOCK_REALTIME].offset =
652 timespec_to_ktime(realtime_offset); 624 timespec_to_ktime(realtime_offset);
653 625
654 hrtimer_force_reprogram(base); 626 hrtimer_force_reprogram(base, 0);
655 spin_unlock(&base->lock); 627 spin_unlock(&base->lock);
656} 628}
657 629
@@ -754,8 +726,6 @@ static int hrtimer_switch_to_hres(void)
754 /* "Retrigger" the interrupt to get things going */ 726 /* "Retrigger" the interrupt to get things going */
755 retrigger_next_event(NULL); 727 retrigger_next_event(NULL);
756 local_irq_restore(flags); 728 local_irq_restore(flags);
757 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
758 smp_processor_id());
759 return 1; 729 return 1;
760} 730}
761 731
@@ -764,7 +734,8 @@ static int hrtimer_switch_to_hres(void)
764static inline int hrtimer_hres_active(void) { return 0; } 734static inline int hrtimer_hres_active(void) { return 0; }
765static inline int hrtimer_is_hres_enabled(void) { return 0; } 735static inline int hrtimer_is_hres_enabled(void) { return 0; }
766static inline int hrtimer_switch_to_hres(void) { return 0; } 736static inline int hrtimer_switch_to_hres(void) { return 0; }
767static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { } 737static inline void
738hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
768static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, 739static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
769 struct hrtimer_clock_base *base, 740 struct hrtimer_clock_base *base,
770 int wakeup) 741 int wakeup)
@@ -854,7 +825,7 @@ static int enqueue_hrtimer(struct hrtimer *timer,
854 struct hrtimer *entry; 825 struct hrtimer *entry;
855 int leftmost = 1; 826 int leftmost = 1;
856 827
857 debug_hrtimer_activate(timer); 828 debug_activate(timer);
858 829
859 /* 830 /*
860 * Find the right place in the rbtree: 831 * Find the right place in the rbtree:
@@ -907,19 +878,29 @@ static void __remove_hrtimer(struct hrtimer *timer,
907 struct hrtimer_clock_base *base, 878 struct hrtimer_clock_base *base,
908 unsigned long newstate, int reprogram) 879 unsigned long newstate, int reprogram)
909{ 880{
910 if (timer->state & HRTIMER_STATE_ENQUEUED) { 881 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
911 /* 882 goto out;
912 * Remove the timer from the rbtree and replace the 883
913 * first entry pointer if necessary. 884 /*
914 */ 885 * Remove the timer from the rbtree and replace the first
915 if (base->first == &timer->node) { 886 * entry pointer if necessary.
916 base->first = rb_next(&timer->node); 887 */
917 /* Reprogram the clock event device. if enabled */ 888 if (base->first == &timer->node) {
918 if (reprogram && hrtimer_hres_active()) 889 base->first = rb_next(&timer->node);
919 hrtimer_force_reprogram(base->cpu_base); 890#ifdef CONFIG_HIGH_RES_TIMERS
891 /* Reprogram the clock event device. if enabled */
892 if (reprogram && hrtimer_hres_active()) {
893 ktime_t expires;
894
895 expires = ktime_sub(hrtimer_get_expires(timer),
896 base->offset);
897 if (base->cpu_base->expires_next.tv64 == expires.tv64)
898 hrtimer_force_reprogram(base->cpu_base, 1);
920 } 899 }
921 rb_erase(&timer->node, &base->active); 900#endif
922 } 901 }
902 rb_erase(&timer->node, &base->active);
903out:
923 timer->state = newstate; 904 timer->state = newstate;
924} 905}
925 906
@@ -940,7 +921,7 @@ remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
940 * reprogramming happens in the interrupt handler. This is a 921 * reprogramming happens in the interrupt handler. This is a
941 * rare case and less expensive than a smp call. 922 * rare case and less expensive than a smp call.
942 */ 923 */
943 debug_hrtimer_deactivate(timer); 924 debug_deactivate(timer);
944 timer_stats_hrtimer_clear_start_info(timer); 925 timer_stats_hrtimer_clear_start_info(timer);
945 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); 926 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
946 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 927 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
@@ -1155,7 +1136,6 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1155 clock_id = CLOCK_MONOTONIC; 1136 clock_id = CLOCK_MONOTONIC;
1156 1137
1157 timer->base = &cpu_base->clock_base[clock_id]; 1138 timer->base = &cpu_base->clock_base[clock_id];
1158 INIT_LIST_HEAD(&timer->cb_entry);
1159 hrtimer_init_timer_hres(timer); 1139 hrtimer_init_timer_hres(timer);
1160 1140
1161#ifdef CONFIG_TIMER_STATS 1141#ifdef CONFIG_TIMER_STATS
@@ -1174,7 +1154,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1174void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 1154void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1175 enum hrtimer_mode mode) 1155 enum hrtimer_mode mode)
1176{ 1156{
1177 debug_hrtimer_init(timer); 1157 debug_init(timer, clock_id, mode);
1178 __hrtimer_init(timer, clock_id, mode); 1158 __hrtimer_init(timer, clock_id, mode);
1179} 1159}
1180EXPORT_SYMBOL_GPL(hrtimer_init); 1160EXPORT_SYMBOL_GPL(hrtimer_init);
@@ -1198,7 +1178,7 @@ int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1198} 1178}
1199EXPORT_SYMBOL_GPL(hrtimer_get_res); 1179EXPORT_SYMBOL_GPL(hrtimer_get_res);
1200 1180
1201static void __run_hrtimer(struct hrtimer *timer) 1181static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1202{ 1182{
1203 struct hrtimer_clock_base *base = timer->base; 1183 struct hrtimer_clock_base *base = timer->base;
1204 struct hrtimer_cpu_base *cpu_base = base->cpu_base; 1184 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
@@ -1207,7 +1187,7 @@ static void __run_hrtimer(struct hrtimer *timer)
1207 1187
1208 WARN_ON(!irqs_disabled()); 1188 WARN_ON(!irqs_disabled());
1209 1189
1210 debug_hrtimer_deactivate(timer); 1190 debug_deactivate(timer);
1211 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); 1191 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1212 timer_stats_account_hrtimer(timer); 1192 timer_stats_account_hrtimer(timer);
1213 fn = timer->function; 1193 fn = timer->function;
@@ -1218,7 +1198,9 @@ static void __run_hrtimer(struct hrtimer *timer)
1218 * the timer base. 1198 * the timer base.
1219 */ 1199 */
1220 spin_unlock(&cpu_base->lock); 1200 spin_unlock(&cpu_base->lock);
1201 trace_hrtimer_expire_entry(timer, now);
1221 restart = fn(timer); 1202 restart = fn(timer);
1203 trace_hrtimer_expire_exit(timer);
1222 spin_lock(&cpu_base->lock); 1204 spin_lock(&cpu_base->lock);
1223 1205
1224 /* 1206 /*
@@ -1329,7 +1311,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
1329 break; 1311 break;
1330 } 1312 }
1331 1313
1332 __run_hrtimer(timer); 1314 __run_hrtimer(timer, &basenow);
1333 } 1315 }
1334 base++; 1316 base++;
1335 } 1317 }
@@ -1451,7 +1433,7 @@ void hrtimer_run_queues(void)
1451 hrtimer_get_expires_tv64(timer)) 1433 hrtimer_get_expires_tv64(timer))
1452 break; 1434 break;
1453 1435
1454 __run_hrtimer(timer); 1436 __run_hrtimer(timer, &base->softirq_time);
1455 } 1437 }
1456 spin_unlock(&cpu_base->lock); 1438 spin_unlock(&cpu_base->lock);
1457 } 1439 }
@@ -1628,7 +1610,7 @@ static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1628 while ((node = rb_first(&old_base->active))) { 1610 while ((node = rb_first(&old_base->active))) {
1629 timer = rb_entry(node, struct hrtimer, node); 1611 timer = rb_entry(node, struct hrtimer, node);
1630 BUG_ON(hrtimer_callback_running(timer)); 1612 BUG_ON(hrtimer_callback_running(timer));
1631 debug_hrtimer_deactivate(timer); 1613 debug_deactivate(timer);
1632 1614
1633 /* 1615 /*
1634 * Mark it as STATE_MIGRATE not INACTIVE otherwise the 1616 * Mark it as STATE_MIGRATE not INACTIVE otherwise the