35 #include <linux/export.h>
37 #include <linux/hrtimer.h>
46 #include <linux/sched.h>
49 #include <asm/uaccess.h>
87 static const int hrtimer_clock_to_base_table[
MAX_CLOCKS] = {
93 static inline int hrtimer_clockid_to_base(
clockid_t clock_id)
95 return hrtimer_clock_to_base_table[clock_id];
110 xtim = timespec_to_ktime(xts);
111 mono = ktime_add(xtim, timespec_to_ktime(tom));
112 boot = ktime_add(mono, timespec_to_ktime(slp));
138 unsigned long *
flags)
159 static int hrtimer_get_target(
int this_cpu,
int pinned)
162 if (!pinned && get_sysctl_timer_migration() &&
idle_cpu(this_cpu))
163 return get_nohz_timer_target();
178 #ifdef CONFIG_HIGH_RES_TIMERS
181 if (!new_base->
cpu_base->hres_active)
184 expires = ktime_sub(hrtimer_get_expires(timer), new_base->
offset);
185 return expires.
tv64 <= new_base->
cpu_base->expires_next.tv64;
201 int cpu = hrtimer_get_target(this_cpu, pinned);
202 int basenum = base->
index;
205 new_cpu_base = &
per_cpu(hrtimer_bases, cpu);
206 new_base = &new_cpu_base->
clock_base[basenum];
208 if (base != new_base) {
218 if (
unlikely(hrtimer_callback_running(timer)))
226 if (cpu != this_cpu && hrtimer_check_target(timer, new_base)) {
233 timer->
base = new_base;
241 lock_hrtimer_base(
const struct hrtimer *timer,
unsigned long *flags)
250 # define switch_hrtimer_base(t, b, p) (b)
258 #if BITS_PER_LONG < 64
259 # ifndef CONFIG_KTIME_SCALAR
276 tmp = ktime_set((
long)nsec, rem);
279 return ktime_add(kt, tmp);
300 tmp = ktime_set((
long)nsec, rem);
303 return ktime_sub(kt, tmp);
317 dclc = ktime_to_ns(kt);
324 do_div(dclc, (
unsigned long) div);
349 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
353 static void *hrtimer_debug_hint(
void *
addr)
355 return ((
struct hrtimer *) addr)->function;
381 static int hrtimer_fixup_activate(
void *addr,
enum debug_obj_state state)
417 .debug_hint = hrtimer_debug_hint,
418 .fixup_init = hrtimer_fixup_init,
419 .fixup_activate = hrtimer_fixup_activate,
420 .fixup_free = hrtimer_fixup_free,
423 static inline void debug_hrtimer_init(
struct hrtimer *timer)
428 static inline void debug_hrtimer_activate(
struct hrtimer *timer)
433 static inline void debug_hrtimer_deactivate(
struct hrtimer *timer)
438 static inline void debug_hrtimer_free(
struct hrtimer *timer)
450 __hrtimer_init(timer, clock_id, mode);
454 void destroy_hrtimer_on_stack(
struct hrtimer *timer)
460 static inline void debug_hrtimer_init(
struct hrtimer *timer) { }
461 static inline void debug_hrtimer_activate(
struct hrtimer *timer) { }
462 static inline void debug_hrtimer_deactivate(
struct hrtimer *timer) { }
469 debug_hrtimer_init(timer);
470 trace_hrtimer_init(timer, clockid, mode);
473 static inline void debug_activate(
struct hrtimer *timer)
475 debug_hrtimer_activate(timer);
476 trace_hrtimer_start(timer);
479 static inline void debug_deactivate(
struct hrtimer *timer)
481 debug_hrtimer_deactivate(timer);
482 trace_hrtimer_cancel(timer);
486 #ifdef CONFIG_HIGH_RES_TIMERS
496 static int __init setup_hrtimer_hres(
char *
str)
499 hrtimer_hres_enabled = 0;
500 else if (!
strcmp(str,
"on"))
501 hrtimer_hres_enabled = 1;
507 __setup(
"highres=", setup_hrtimer_hres);
512 static inline int hrtimer_is_hres_enabled(
void)
514 return hrtimer_hres_enabled;
520 static inline int hrtimer_hres_active(
void)
543 next = timerqueue_getnext(&base->
active);
548 expires = ktime_sub(hrtimer_get_expires(timer), base->
offset);
554 if (expires.
tv64 < 0)
556 if (expires.
tv64 < expires_next.
tv64)
560 if (skip_equal && expires_next.
tv64 == cpu_base->expires_next.tv64)
563 cpu_base->expires_next.tv64 = expires_next.
tv64;
565 if (cpu_base->expires_next.tv64 !=
KTIME_MAX)
578 static int hrtimer_reprogram(
struct hrtimer *timer,
582 ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->
offset);
594 if (hrtimer_callback_running(timer))
603 if (expires.
tv64 < 0)
606 if (expires.
tv64 >= cpu_base->expires_next.tv64)
615 if (cpu_base->hang_detected)
623 cpu_base->expires_next = expires;
633 base->hres_active = 0;
642 static inline int hrtimer_enqueue_reprogram(
struct hrtimer *timer,
646 if (base->
cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
673 static void retrigger_next_event(
void *
arg)
677 if (!hrtimer_hres_active())
681 hrtimer_update_base(base);
682 hrtimer_force_reprogram(base, 0);
689 static int hrtimer_switch_to_hres(
void)
695 if (base->hres_active)
700 if (tick_init_highres()) {
703 "mode on CPU %d\n", cpu);
706 base->hres_active = 1;
708 base->
clock_base[i].resolution = KTIME_HIGH_RES;
710 tick_setup_sched_timer();
712 retrigger_next_event(
NULL);
722 void clock_was_set_delayed(
void)
732 static inline int hrtimer_hres_active(
void) {
return 0; }
733 static inline int hrtimer_is_hres_enabled(
void) {
return 0; }
734 static inline int hrtimer_switch_to_hres(
void) {
return 0; }
737 static inline int hrtimer_enqueue_reprogram(
struct hrtimer *timer,
744 static inline void retrigger_next_event(
void *arg) { }
761 #ifdef CONFIG_HIGH_RES_TIMERS
775 KERN_INFO "hrtimers_resume() called with IRQs enabled!");
777 retrigger_next_event(
NULL);
781 static inline void timer_stats_hrtimer_set_start_info(
struct hrtimer *timer)
783 #ifdef CONFIG_TIMER_STATS
784 if (timer->start_site)
786 timer->start_site = __builtin_return_address(0);
788 timer->start_pid =
current->pid;
792 static inline void timer_stats_hrtimer_clear_start_info(
struct hrtimer *timer)
794 #ifdef CONFIG_TIMER_STATS
795 timer->start_site =
NULL;
799 static inline void timer_stats_account_hrtimer(
struct hrtimer *timer)
801 #ifdef CONFIG_TIMER_STATS
805 timer->
function, timer->start_comm, 0);
813 void unlock_hrtimer_base(
const struct hrtimer *timer,
unsigned long *flags)
832 delta = ktime_sub(now, hrtimer_get_expires(timer));
837 if (interval.
tv64 < timer->
base->resolution.tv64)
838 interval.
tv64 = timer->
base->resolution.tv64;
841 s64 incr = ktime_to_ns(interval);
844 hrtimer_add_expires_ns(timer, incr * orun);
845 if (hrtimer_get_expires_tv64(timer) > now.
tv64)
853 hrtimer_add_expires(timer, interval);
867 static int enqueue_hrtimer(
struct hrtimer *timer,
870 debug_activate(timer);
894 static void __remove_hrtimer(
struct hrtimer *timer,
896 unsigned long newstate,
int reprogram)
902 next_timer = timerqueue_getnext(&base->
active);
904 if (&timer->
node == next_timer) {
905 #ifdef CONFIG_HIGH_RES_TIMERS
907 if (reprogram && hrtimer_hres_active()) {
910 expires = ktime_sub(hrtimer_get_expires(timer),
913 hrtimer_force_reprogram(base->
cpu_base, 1);
917 if (!timerqueue_getnext(&base->
active))
920 timer->
state = newstate;
929 if (hrtimer_is_queued(timer)) {
941 debug_deactivate(timer);
942 timer_stats_hrtimer_clear_start_info(timer);
950 __remove_hrtimer(timer, base, state, reprogram);
964 base = lock_hrtimer_base(timer, &flags);
967 ret = remove_hrtimer(timer, base);
981 #ifdef CONFIG_TIME_LOW_RES
986 hrtimer_set_expires_range_ns(timer, tim, delta_ns);
988 timer_stats_hrtimer_set_start_info(timer);
990 leftmost = enqueue_hrtimer(timer, new_base);
999 hrtimer_enqueue_reprogram(timer, new_base, wakeup);
1001 unlock_hrtimer_base(timer, &flags);
1055 unsigned long flags;
1058 base = lock_hrtimer_base(timer, &flags);
1060 if (!hrtimer_callback_running(timer))
1061 ret = remove_hrtimer(timer, base);
1063 unlock_hrtimer_base(timer, &flags);
1096 unsigned long flags;
1099 lock_hrtimer_base(timer, &flags);
1100 rem = hrtimer_expires_remaining(timer);
1101 unlock_hrtimer_base(timer, &flags);
1119 unsigned long flags;
1124 if (!hrtimer_hres_active()) {
1129 next = timerqueue_getnext(&base->
active);
1134 delta.
tv64 = hrtimer_get_expires_tv64(timer);
1135 delta = ktime_sub(delta, base->
get_time());
1143 if (mindelta.
tv64 < 0)
1162 base = hrtimer_clockid_to_base(clock_id);
1164 timerqueue_init(&timer->
node);
1166 #ifdef CONFIG_TIMER_STATS
1167 timer->start_site =
NULL;
1168 timer->start_pid = -1;
1182 debug_init(timer, clock_id, mode);
1183 __hrtimer_init(timer, clock_id, mode);
1198 int base = hrtimer_clockid_to_base(which_clock);
1201 *tp = ktime_to_timespec(cpu_base->
clock_base[base].resolution);
1216 debug_deactivate(timer);
1218 timer_stats_account_hrtimer(timer);
1227 trace_hrtimer_expire_entry(timer, now);
1228 restart =
fn(timer);
1229 trace_hrtimer_expire_exit(timer);
1239 enqueue_hrtimer(timer, base);
1247 #ifdef CONFIG_HIGH_RES_TIMERS
1253 void hrtimer_interrupt(
struct clock_event_device *
dev)
1259 BUG_ON(!cpu_base->hres_active);
1260 cpu_base->nr_events++;
1264 entry_time = now = hrtimer_update_base(cpu_base);
1274 cpu_base->expires_next.tv64 =
KTIME_MAX;
1285 basenow = ktime_add(now, base->
offset);
1287 while ((node = timerqueue_getnext(&base->
active))) {
1305 if (basenow.
tv64 < hrtimer_get_softexpires_tv64(timer)) {
1308 expires = ktime_sub(hrtimer_get_expires(timer),
1310 if (expires.
tv64 < expires_next.
tv64)
1311 expires_next = expires;
1315 __run_hrtimer(timer, &basenow);
1323 cpu_base->expires_next = expires_next;
1329 cpu_base->hang_detected = 0;
1347 now = hrtimer_update_base(cpu_base);
1348 cpu_base->nr_retries++;
1357 cpu_base->nr_hangs++;
1358 cpu_base->hang_detected = 1;
1360 delta = ktime_sub(now, entry_time);
1361 if (delta.
tv64 > cpu_base->max_hang_time.tv64)
1362 cpu_base->max_hang_time =
delta;
1370 expires_next = ktime_add(now, delta);
1373 ktime_to_ns(delta));
1380 static void __hrtimer_peek_ahead_timers(
void)
1382 struct tick_device *
td;
1384 if (!hrtimer_hres_active())
1388 if (td && td->evtdev)
1389 hrtimer_interrupt(td->evtdev);
1401 void hrtimer_peek_ahead_timers(
void)
1403 unsigned long flags;
1406 __hrtimer_peek_ahead_timers();
1419 hrtimer_peek_ahead_timers();
1424 static inline void __hrtimer_peek_ahead_timers(
void) { }
1437 if (hrtimer_hres_active())
1449 hrtimer_switch_to_hres();
1460 int index, gettime = 1;
1462 if (hrtimer_hres_active())
1467 if (!timerqueue_getnext(&base->
active))
1471 hrtimer_get_softirq_time(cpu_base);
1477 while ((node = timerqueue_getnext(&base->
active))) {
1482 hrtimer_get_expires_tv64(timer))
1509 sl->
timer.function = hrtimer_wakeup;
1520 hrtimer_start_expires(&t->
timer, mode);
1521 if (!hrtimer_active(&t->
timer))
1537 static int update_rmtp(
struct hrtimer *timer,
struct timespec __user *rmtp)
1542 rem = hrtimer_expires_remaining(timer);
1545 rmt = ktime_to_timespec(rem);
1568 ret = update_rmtp(&t.
timer, rmtp);
1576 destroy_hrtimer_on_stack(&t.
timer);
1586 unsigned long slack;
1588 slack =
current->timer_slack_ns;
1592 hrtimer_init_on_stack(&t.
timer, clockid, mode);
1593 hrtimer_set_expires_range_ns(&t.
timer, timespec_to_ktime(*rqtp), slack);
1594 if (do_nanosleep(&t, mode))
1604 ret = update_rmtp(&t.
timer, rmtp);
1617 destroy_hrtimer_on_stack(&t.
timer);
1629 if (!timespec_valid(&tu))
1638 static void __cpuinit init_hrtimers_cpu(
int cpu)
1647 timerqueue_init_head(&cpu_base->
clock_base[i].active);
1650 hrtimer_init_hres(cpu_base);
1653 #ifdef CONFIG_HOTPLUG_CPU
1661 while ((node = timerqueue_getnext(&old_base->
active))) {
1663 BUG_ON(hrtimer_callback_running(timer));
1664 debug_deactivate(timer);
1672 timer->
base = new_base;
1681 enqueue_hrtimer(timer, new_base);
1688 static void migrate_hrtimers(
int scpu)
1694 tick_cancel_sched_timer(scpu);
1697 old_base = &
per_cpu(hrtimer_bases, scpu);
1707 migrate_hrtimer_list(&old_base->
clock_base[i],
1715 __hrtimer_peek_ahead_timers();
1722 unsigned long action,
void *hcpu)
1724 int scpu = (
long)hcpu;
1730 init_hrtimers_cpu(scpu);
1733 #ifdef CONFIG_HOTPLUG_CPU
1742 migrate_hrtimers(scpu);
1755 .notifier_call = hrtimer_cpu_notify,
1762 register_cpu_notifier(&hrtimers_nb);
1763 #ifdef CONFIG_HIGH_RES_TIMERS
1785 if (expires && !expires->
tv64) {
1799 hrtimer_init_on_stack(&t.
timer, clock, mode);
1800 hrtimer_set_expires_range_ns(&t.
timer, *expires, delta);
1804 hrtimer_start_expires(&t.
timer, mode);
1805 if (!hrtimer_active(&t.
timer))
1812 destroy_hrtimer_on_stack(&t.
timer);