aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/hrtimer.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/hrtimer.c')
-rw-r--r--kernel/hrtimer.c148
1 files changed, 66 insertions, 82 deletions
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index 05071bf6a37b..6d7020490f94 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
@@ -764,7 +736,8 @@ static int hrtimer_switch_to_hres(void)
764static inline int hrtimer_hres_active(void) { return 0; } 736static inline int hrtimer_hres_active(void) { return 0; }
765static inline int hrtimer_is_hres_enabled(void) { return 0; } 737static inline int hrtimer_is_hres_enabled(void) { return 0; }
766static inline int hrtimer_switch_to_hres(void) { return 0; } 738static inline int hrtimer_switch_to_hres(void) { return 0; }
767static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { } 739static inline void
740hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
768static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, 741static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
769 struct hrtimer_clock_base *base, 742 struct hrtimer_clock_base *base,
770 int wakeup) 743 int wakeup)
@@ -854,7 +827,7 @@ static int enqueue_hrtimer(struct hrtimer *timer,
854 struct hrtimer *entry; 827 struct hrtimer *entry;
855 int leftmost = 1; 828 int leftmost = 1;
856 829
857 debug_hrtimer_activate(timer); 830 debug_activate(timer);
858 831
859 /* 832 /*
860 * Find the right place in the rbtree: 833 * Find the right place in the rbtree:
@@ -907,19 +880,29 @@ static void __remove_hrtimer(struct hrtimer *timer,
907 struct hrtimer_clock_base *base, 880 struct hrtimer_clock_base *base,
908 unsigned long newstate, int reprogram) 881 unsigned long newstate, int reprogram)
909{ 882{
910 if (timer->state & HRTIMER_STATE_ENQUEUED) { 883 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
911 /* 884 goto out;
912 * Remove the timer from the rbtree and replace the 885
913 * first entry pointer if necessary. 886 /*
914 */ 887 * Remove the timer from the rbtree and replace the first
915 if (base->first == &timer->node) { 888 * entry pointer if necessary.
916 base->first = rb_next(&timer->node); 889 */
917 /* Reprogram the clock event device. if enabled */ 890 if (base->first == &timer->node) {
918 if (reprogram && hrtimer_hres_active()) 891 base->first = rb_next(&timer->node);
919 hrtimer_force_reprogram(base->cpu_base); 892#ifdef CONFIG_HIGH_RES_TIMERS
893 /* Reprogram the clock event device. if enabled */
894 if (reprogram && hrtimer_hres_active()) {
895 ktime_t expires;
896
897 expires = ktime_sub(hrtimer_get_expires(timer),
898 base->offset);
899 if (base->cpu_base->expires_next.tv64 == expires.tv64)
900 hrtimer_force_reprogram(base->cpu_base, 1);
920 } 901 }
921 rb_erase(&timer->node, &base->active); 902#endif
922 } 903 }
904 rb_erase(&timer->node, &base->active);
905out:
923 timer->state = newstate; 906 timer->state = newstate;
924} 907}
925 908
@@ -940,7 +923,7 @@ remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
940 * reprogramming happens in the interrupt handler. This is a 923 * reprogramming happens in the interrupt handler. This is a
941 * rare case and less expensive than a smp call. 924 * rare case and less expensive than a smp call.
942 */ 925 */
943 debug_hrtimer_deactivate(timer); 926 debug_deactivate(timer);
944 timer_stats_hrtimer_clear_start_info(timer); 927 timer_stats_hrtimer_clear_start_info(timer);
945 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); 928 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
946 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 929 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
@@ -1155,7 +1138,6 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1155 clock_id = CLOCK_MONOTONIC; 1138 clock_id = CLOCK_MONOTONIC;
1156 1139
1157 timer->base = &cpu_base->clock_base[clock_id]; 1140 timer->base = &cpu_base->clock_base[clock_id];
1158 INIT_LIST_HEAD(&timer->cb_entry);
1159 hrtimer_init_timer_hres(timer); 1141 hrtimer_init_timer_hres(timer);
1160 1142
1161#ifdef CONFIG_TIMER_STATS 1143#ifdef CONFIG_TIMER_STATS
@@ -1174,7 +1156,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1174void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 1156void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1175 enum hrtimer_mode mode) 1157 enum hrtimer_mode mode)
1176{ 1158{
1177 debug_hrtimer_init(timer); 1159 debug_init(timer, clock_id, mode);
1178 __hrtimer_init(timer, clock_id, mode); 1160 __hrtimer_init(timer, clock_id, mode);
1179} 1161}
1180EXPORT_SYMBOL_GPL(hrtimer_init); 1162EXPORT_SYMBOL_GPL(hrtimer_init);
@@ -1198,7 +1180,7 @@ int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1198} 1180}
1199EXPORT_SYMBOL_GPL(hrtimer_get_res); 1181EXPORT_SYMBOL_GPL(hrtimer_get_res);
1200 1182
1201static void __run_hrtimer(struct hrtimer *timer) 1183static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1202{ 1184{
1203 struct hrtimer_clock_base *base = timer->base; 1185 struct hrtimer_clock_base *base = timer->base;
1204 struct hrtimer_cpu_base *cpu_base = base->cpu_base; 1186 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
@@ -1207,7 +1189,7 @@ static void __run_hrtimer(struct hrtimer *timer)
1207 1189
1208 WARN_ON(!irqs_disabled()); 1190 WARN_ON(!irqs_disabled());
1209 1191
1210 debug_hrtimer_deactivate(timer); 1192 debug_deactivate(timer);
1211 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); 1193 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1212 timer_stats_account_hrtimer(timer); 1194 timer_stats_account_hrtimer(timer);
1213 fn = timer->function; 1195 fn = timer->function;
@@ -1218,7 +1200,9 @@ static void __run_hrtimer(struct hrtimer *timer)
1218 * the timer base. 1200 * the timer base.
1219 */ 1201 */
1220 spin_unlock(&cpu_base->lock); 1202 spin_unlock(&cpu_base->lock);
1203 trace_hrtimer_expire_entry(timer, now);
1221 restart = fn(timer); 1204 restart = fn(timer);
1205 trace_hrtimer_expire_exit(timer);
1222 spin_lock(&cpu_base->lock); 1206 spin_lock(&cpu_base->lock);
1223 1207
1224 /* 1208 /*
@@ -1329,7 +1313,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
1329 break; 1313 break;
1330 } 1314 }
1331 1315
1332 __run_hrtimer(timer); 1316 __run_hrtimer(timer, &basenow);
1333 } 1317 }
1334 base++; 1318 base++;
1335 } 1319 }
@@ -1451,7 +1435,7 @@ void hrtimer_run_queues(void)
1451 hrtimer_get_expires_tv64(timer)) 1435 hrtimer_get_expires_tv64(timer))
1452 break; 1436 break;
1453 1437
1454 __run_hrtimer(timer); 1438 __run_hrtimer(timer, &base->softirq_time);
1455 } 1439 }
1456 spin_unlock(&cpu_base->lock); 1440 spin_unlock(&cpu_base->lock);
1457 } 1441 }
@@ -1628,7 +1612,7 @@ static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1628 while ((node = rb_first(&old_base->active))) { 1612 while ((node = rb_first(&old_base->active))) {
1629 timer = rb_entry(node, struct hrtimer, node); 1613 timer = rb_entry(node, struct hrtimer, node);
1630 BUG_ON(hrtimer_callback_running(timer)); 1614 BUG_ON(hrtimer_callback_running(timer));
1631 debug_hrtimer_deactivate(timer); 1615 debug_deactivate(timer);
1632 1616
1633 /* 1617 /*
1634 * Mark it as STATE_MIGRATE not INACTIVE otherwise the 1618 * Mark it as STATE_MIGRATE not INACTIVE otherwise the