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, 68 insertions, 82 deletions
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index 49da79ab8486..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.
@@ -485,6 +431,7 @@ void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
485 debug_object_init_on_stack(timer, &hrtimer_debug_descr); 431 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
486 __hrtimer_init(timer, clock_id, mode); 432 __hrtimer_init(timer, clock_id, mode);
487} 433}
434EXPORT_SYMBOL_GPL(hrtimer_init_on_stack);
488 435
489void destroy_hrtimer_on_stack(struct hrtimer *timer) 436void destroy_hrtimer_on_stack(struct hrtimer *timer)
490{ 437{
@@ -497,6 +444,26 @@ static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
497static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } 444static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
498#endif 445#endif
499 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
500/* High resolution timer related functions */ 467/* High resolution timer related functions */
501#ifdef CONFIG_HIGH_RES_TIMERS 468#ifdef CONFIG_HIGH_RES_TIMERS
502 469
@@ -542,13 +509,14 @@ static inline int hrtimer_hres_active(void)
542 * next event 509 * next event
543 * Called with interrupts disabled and base->lock held 510 * Called with interrupts disabled and base->lock held
544 */ 511 */
545static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base) 512static void
513hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)
546{ 514{
547 int i; 515 int i;
548 struct hrtimer_clock_base *base = cpu_base->clock_base; 516 struct hrtimer_clock_base *base = cpu_base->clock_base;
549 ktime_t expires; 517 ktime_t expires, expires_next;
550 518
551 cpu_base->expires_next.tv64 = KTIME_MAX; 519 expires_next.tv64 = KTIME_MAX;
552 520
553 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { 521 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
554 struct hrtimer *timer; 522 struct hrtimer *timer;
@@ -564,10 +532,15 @@ static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
564 */ 532 */
565 if (expires.tv64 < 0) 533 if (expires.tv64 < 0)
566 expires.tv64 = 0; 534 expires.tv64 = 0;
567 if (expires.tv64 < cpu_base->expires_next.tv64) 535 if (expires.tv64 < expires_next.tv64)
568 cpu_base->expires_next = expires; 536 expires_next = expires;
569 } 537 }
570 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
571 if (cpu_base->expires_next.tv64 != KTIME_MAX) 544 if (cpu_base->expires_next.tv64 != KTIME_MAX)
572 tick_program_event(cpu_base->expires_next, 1); 545 tick_program_event(cpu_base->expires_next, 1);
573} 546}
@@ -650,7 +623,7 @@ static void retrigger_next_event(void *arg)
650 base->clock_base[CLOCK_REALTIME].offset = 623 base->clock_base[CLOCK_REALTIME].offset =
651 timespec_to_ktime(realtime_offset); 624 timespec_to_ktime(realtime_offset);
652 625
653 hrtimer_force_reprogram(base); 626 hrtimer_force_reprogram(base, 0);
654 spin_unlock(&base->lock); 627 spin_unlock(&base->lock);
655} 628}
656 629
@@ -763,7 +736,8 @@ static int hrtimer_switch_to_hres(void)
763static inline int hrtimer_hres_active(void) { return 0; } 736static inline int hrtimer_hres_active(void) { return 0; }
764static inline int hrtimer_is_hres_enabled(void) { return 0; } 737static inline int hrtimer_is_hres_enabled(void) { return 0; }
765static inline int hrtimer_switch_to_hres(void) { return 0; } 738static inline int hrtimer_switch_to_hres(void) { return 0; }
766static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { } 739static inline void
740hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { }
767static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, 741static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
768 struct hrtimer_clock_base *base, 742 struct hrtimer_clock_base *base,
769 int wakeup) 743 int wakeup)
@@ -853,7 +827,7 @@ static int enqueue_hrtimer(struct hrtimer *timer,
853 struct hrtimer *entry; 827 struct hrtimer *entry;
854 int leftmost = 1; 828 int leftmost = 1;
855 829
856 debug_hrtimer_activate(timer); 830 debug_activate(timer);
857 831
858 /* 832 /*
859 * Find the right place in the rbtree: 833 * Find the right place in the rbtree:
@@ -906,19 +880,29 @@ static void __remove_hrtimer(struct hrtimer *timer,
906 struct hrtimer_clock_base *base, 880 struct hrtimer_clock_base *base,
907 unsigned long newstate, int reprogram) 881 unsigned long newstate, int reprogram)
908{ 882{
909 if (timer->state & HRTIMER_STATE_ENQUEUED) { 883 if (!(timer->state & HRTIMER_STATE_ENQUEUED))
910 /* 884 goto out;
911 * Remove the timer from the rbtree and replace the 885
912 * first entry pointer if necessary. 886 /*
913 */ 887 * Remove the timer from the rbtree and replace the first
914 if (base->first == &timer->node) { 888 * entry pointer if necessary.
915 base->first = rb_next(&timer->node); 889 */
916 /* Reprogram the clock event device. if enabled */ 890 if (base->first == &timer->node) {
917 if (reprogram && hrtimer_hres_active()) 891 base->first = rb_next(&timer->node);
918 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);
919 } 901 }
920 rb_erase(&timer->node, &base->active); 902#endif
921 } 903 }
904 rb_erase(&timer->node, &base->active);
905out:
922 timer->state = newstate; 906 timer->state = newstate;
923} 907}
924 908
@@ -939,7 +923,7 @@ remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
939 * reprogramming happens in the interrupt handler. This is a 923 * reprogramming happens in the interrupt handler. This is a
940 * rare case and less expensive than a smp call. 924 * rare case and less expensive than a smp call.
941 */ 925 */
942 debug_hrtimer_deactivate(timer); 926 debug_deactivate(timer);
943 timer_stats_hrtimer_clear_start_info(timer); 927 timer_stats_hrtimer_clear_start_info(timer);
944 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); 928 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
945 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 929 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
@@ -1154,7 +1138,6 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1154 clock_id = CLOCK_MONOTONIC; 1138 clock_id = CLOCK_MONOTONIC;
1155 1139
1156 timer->base = &cpu_base->clock_base[clock_id]; 1140 timer->base = &cpu_base->clock_base[clock_id];
1157 INIT_LIST_HEAD(&timer->cb_entry);
1158 hrtimer_init_timer_hres(timer); 1141 hrtimer_init_timer_hres(timer);
1159 1142
1160#ifdef CONFIG_TIMER_STATS 1143#ifdef CONFIG_TIMER_STATS
@@ -1173,7 +1156,7 @@ static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1173void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 1156void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1174 enum hrtimer_mode mode) 1157 enum hrtimer_mode mode)
1175{ 1158{
1176 debug_hrtimer_init(timer); 1159 debug_init(timer, clock_id, mode);
1177 __hrtimer_init(timer, clock_id, mode); 1160 __hrtimer_init(timer, clock_id, mode);
1178} 1161}
1179EXPORT_SYMBOL_GPL(hrtimer_init); 1162EXPORT_SYMBOL_GPL(hrtimer_init);
@@ -1197,7 +1180,7 @@ int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1197} 1180}
1198EXPORT_SYMBOL_GPL(hrtimer_get_res); 1181EXPORT_SYMBOL_GPL(hrtimer_get_res);
1199 1182
1200static void __run_hrtimer(struct hrtimer *timer) 1183static void __run_hrtimer(struct hrtimer *timer, ktime_t *now)
1201{ 1184{
1202 struct hrtimer_clock_base *base = timer->base; 1185 struct hrtimer_clock_base *base = timer->base;
1203 struct hrtimer_cpu_base *cpu_base = base->cpu_base; 1186 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
@@ -1206,7 +1189,7 @@ static void __run_hrtimer(struct hrtimer *timer)
1206 1189
1207 WARN_ON(!irqs_disabled()); 1190 WARN_ON(!irqs_disabled());
1208 1191
1209 debug_hrtimer_deactivate(timer); 1192 debug_deactivate(timer);
1210 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); 1193 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1211 timer_stats_account_hrtimer(timer); 1194 timer_stats_account_hrtimer(timer);
1212 fn = timer->function; 1195 fn = timer->function;
@@ -1217,7 +1200,9 @@ static void __run_hrtimer(struct hrtimer *timer)
1217 * the timer base. 1200 * the timer base.
1218 */ 1201 */
1219 spin_unlock(&cpu_base->lock); 1202 spin_unlock(&cpu_base->lock);
1203 trace_hrtimer_expire_entry(timer, now);
1220 restart = fn(timer); 1204 restart = fn(timer);
1205 trace_hrtimer_expire_exit(timer);
1221 spin_lock(&cpu_base->lock); 1206 spin_lock(&cpu_base->lock);
1222 1207
1223 /* 1208 /*
@@ -1328,7 +1313,7 @@ void hrtimer_interrupt(struct clock_event_device *dev)
1328 break; 1313 break;
1329 } 1314 }
1330 1315
1331 __run_hrtimer(timer); 1316 __run_hrtimer(timer, &basenow);
1332 } 1317 }
1333 base++; 1318 base++;
1334 } 1319 }
@@ -1450,7 +1435,7 @@ void hrtimer_run_queues(void)
1450 hrtimer_get_expires_tv64(timer)) 1435 hrtimer_get_expires_tv64(timer))
1451 break; 1436 break;
1452 1437
1453 __run_hrtimer(timer); 1438 __run_hrtimer(timer, &base->softirq_time);
1454 } 1439 }
1455 spin_unlock(&cpu_base->lock); 1440 spin_unlock(&cpu_base->lock);
1456 } 1441 }
@@ -1477,6 +1462,7 @@ void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1477 sl->timer.function = hrtimer_wakeup; 1462 sl->timer.function = hrtimer_wakeup;
1478 sl->task = task; 1463 sl->task = task;
1479} 1464}
1465EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
1480 1466
1481static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) 1467static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1482{ 1468{
@@ -1626,7 +1612,7 @@ static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1626 while ((node = rb_first(&old_base->active))) { 1612 while ((node = rb_first(&old_base->active))) {
1627 timer = rb_entry(node, struct hrtimer, node); 1613 timer = rb_entry(node, struct hrtimer, node);
1628 BUG_ON(hrtimer_callback_running(timer)); 1614 BUG_ON(hrtimer_callback_running(timer));
1629 debug_hrtimer_deactivate(timer); 1615 debug_deactivate(timer);
1630 1616
1631 /* 1617 /*
1632 * Mark it as STATE_MIGRATE not INACTIVE otherwise the 1618 * Mark it as STATE_MIGRATE not INACTIVE otherwise the