15cee9645SThomas Gleixner /* 25cee9645SThomas Gleixner * linux/kernel/hrtimer.c 35cee9645SThomas Gleixner * 45cee9645SThomas Gleixner * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> 55cee9645SThomas Gleixner * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar 65cee9645SThomas Gleixner * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner 75cee9645SThomas Gleixner * 85cee9645SThomas Gleixner * High-resolution kernel timers 95cee9645SThomas Gleixner * 105cee9645SThomas Gleixner * In contrast to the low-resolution timeout API implemented in 115cee9645SThomas Gleixner * kernel/timer.c, hrtimers provide finer resolution and accuracy 125cee9645SThomas Gleixner * depending on system configuration and capabilities. 135cee9645SThomas Gleixner * 145cee9645SThomas Gleixner * These timers are currently used for: 155cee9645SThomas Gleixner * - itimers 165cee9645SThomas Gleixner * - POSIX timers 175cee9645SThomas Gleixner * - nanosleep 185cee9645SThomas Gleixner * - precise in-kernel timing 195cee9645SThomas Gleixner * 205cee9645SThomas Gleixner * Started by: Thomas Gleixner and Ingo Molnar 215cee9645SThomas Gleixner * 225cee9645SThomas Gleixner * Credits: 235cee9645SThomas Gleixner * based on kernel/timer.c 245cee9645SThomas Gleixner * 255cee9645SThomas Gleixner * Help, testing, suggestions, bugfixes, improvements were 265cee9645SThomas Gleixner * provided by: 275cee9645SThomas Gleixner * 285cee9645SThomas Gleixner * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel 295cee9645SThomas Gleixner * et. al. 305cee9645SThomas Gleixner * 315cee9645SThomas Gleixner * For licencing details see kernel-base/COPYING 325cee9645SThomas Gleixner */ 335cee9645SThomas Gleixner 345cee9645SThomas Gleixner #include <linux/cpu.h> 355cee9645SThomas Gleixner #include <linux/export.h> 365cee9645SThomas Gleixner #include <linux/percpu.h> 375cee9645SThomas Gleixner #include <linux/hrtimer.h> 385cee9645SThomas Gleixner #include <linux/notifier.h> 395cee9645SThomas Gleixner #include <linux/syscalls.h> 405cee9645SThomas Gleixner #include <linux/kallsyms.h> 415cee9645SThomas Gleixner #include <linux/interrupt.h> 425cee9645SThomas Gleixner #include <linux/tick.h> 435cee9645SThomas Gleixner #include <linux/seq_file.h> 445cee9645SThomas Gleixner #include <linux/err.h> 455cee9645SThomas Gleixner #include <linux/debugobjects.h> 46174cd4b1SIngo Molnar #include <linux/sched/signal.h> 475cee9645SThomas Gleixner #include <linux/sched/sysctl.h> 485cee9645SThomas Gleixner #include <linux/sched/rt.h> 495cee9645SThomas Gleixner #include <linux/sched/deadline.h> 50370c9135SIngo Molnar #include <linux/sched/nohz.h> 51b17b0153SIngo Molnar #include <linux/sched/debug.h> 525cee9645SThomas Gleixner #include <linux/timer.h> 535cee9645SThomas Gleixner #include <linux/freezer.h> 54edbeda46SAl Viro #include <linux/compat.h> 555cee9645SThomas Gleixner 567c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 575cee9645SThomas Gleixner 585cee9645SThomas Gleixner #include <trace/events/timer.h> 595cee9645SThomas Gleixner 60c1797bafSThomas Gleixner #include "tick-internal.h" 618b094cd0SThomas Gleixner 625cee9645SThomas Gleixner /* 63*c458b1d1SAnna-Maria Gleixner * Masks for selecting the soft and hard context timers from 64*c458b1d1SAnna-Maria Gleixner * cpu_base->active 65*c458b1d1SAnna-Maria Gleixner */ 66*c458b1d1SAnna-Maria Gleixner #define MASK_SHIFT (HRTIMER_BASE_MONOTONIC_SOFT) 67*c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_HARD ((1U << MASK_SHIFT) - 1) 68*c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_SOFT (HRTIMER_ACTIVE_HARD << MASK_SHIFT) 69*c458b1d1SAnna-Maria Gleixner #define HRTIMER_ACTIVE_ALL (HRTIMER_ACTIVE_SOFT | HRTIMER_ACTIVE_HARD) 70*c458b1d1SAnna-Maria Gleixner 71*c458b1d1SAnna-Maria Gleixner /* 725cee9645SThomas Gleixner * The timer bases: 735cee9645SThomas Gleixner * 74571af55aSZhen Lei * There are more clockids than hrtimer bases. Thus, we index 755cee9645SThomas Gleixner * into the timer bases by the hrtimer_base_type enum. When trying 765cee9645SThomas Gleixner * to reach a base using a clockid, hrtimer_clockid_to_base() 775cee9645SThomas Gleixner * is used to convert from clockid to the proper hrtimer_base_type. 785cee9645SThomas Gleixner */ 795cee9645SThomas Gleixner DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = 805cee9645SThomas Gleixner { 815cee9645SThomas Gleixner .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock), 825cee9645SThomas Gleixner .clock_base = 835cee9645SThomas Gleixner { 845cee9645SThomas Gleixner { 855cee9645SThomas Gleixner .index = HRTIMER_BASE_MONOTONIC, 865cee9645SThomas Gleixner .clockid = CLOCK_MONOTONIC, 875cee9645SThomas Gleixner .get_time = &ktime_get, 885cee9645SThomas Gleixner }, 895cee9645SThomas Gleixner { 905cee9645SThomas Gleixner .index = HRTIMER_BASE_REALTIME, 915cee9645SThomas Gleixner .clockid = CLOCK_REALTIME, 925cee9645SThomas Gleixner .get_time = &ktime_get_real, 935cee9645SThomas Gleixner }, 945cee9645SThomas Gleixner { 955cee9645SThomas Gleixner .index = HRTIMER_BASE_BOOTTIME, 965cee9645SThomas Gleixner .clockid = CLOCK_BOOTTIME, 975cee9645SThomas Gleixner .get_time = &ktime_get_boottime, 985cee9645SThomas Gleixner }, 995cee9645SThomas Gleixner { 1005cee9645SThomas Gleixner .index = HRTIMER_BASE_TAI, 1015cee9645SThomas Gleixner .clockid = CLOCK_TAI, 1025cee9645SThomas Gleixner .get_time = &ktime_get_clocktai, 1035cee9645SThomas Gleixner }, 10498ecadd4SAnna-Maria Gleixner { 10598ecadd4SAnna-Maria Gleixner .index = HRTIMER_BASE_MONOTONIC_SOFT, 10698ecadd4SAnna-Maria Gleixner .clockid = CLOCK_MONOTONIC, 10798ecadd4SAnna-Maria Gleixner .get_time = &ktime_get, 10898ecadd4SAnna-Maria Gleixner }, 10998ecadd4SAnna-Maria Gleixner { 11098ecadd4SAnna-Maria Gleixner .index = HRTIMER_BASE_REALTIME_SOFT, 11198ecadd4SAnna-Maria Gleixner .clockid = CLOCK_REALTIME, 11298ecadd4SAnna-Maria Gleixner .get_time = &ktime_get_real, 11398ecadd4SAnna-Maria Gleixner }, 11498ecadd4SAnna-Maria Gleixner { 11598ecadd4SAnna-Maria Gleixner .index = HRTIMER_BASE_BOOTTIME_SOFT, 11698ecadd4SAnna-Maria Gleixner .clockid = CLOCK_BOOTTIME, 11798ecadd4SAnna-Maria Gleixner .get_time = &ktime_get_boottime, 11898ecadd4SAnna-Maria Gleixner }, 11998ecadd4SAnna-Maria Gleixner { 12098ecadd4SAnna-Maria Gleixner .index = HRTIMER_BASE_TAI_SOFT, 12198ecadd4SAnna-Maria Gleixner .clockid = CLOCK_TAI, 12298ecadd4SAnna-Maria Gleixner .get_time = &ktime_get_clocktai, 12398ecadd4SAnna-Maria Gleixner }, 1245cee9645SThomas Gleixner } 1255cee9645SThomas Gleixner }; 1265cee9645SThomas Gleixner 1275cee9645SThomas Gleixner static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = { 128336a9cdeSMarc Zyngier /* Make sure we catch unsupported clockids */ 129336a9cdeSMarc Zyngier [0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES, 130336a9cdeSMarc Zyngier 1315cee9645SThomas Gleixner [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME, 1325cee9645SThomas Gleixner [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC, 1335cee9645SThomas Gleixner [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME, 1345cee9645SThomas Gleixner [CLOCK_TAI] = HRTIMER_BASE_TAI, 1355cee9645SThomas Gleixner }; 1365cee9645SThomas Gleixner 1375cee9645SThomas Gleixner /* 1385cee9645SThomas Gleixner * Functions and macros which are different for UP/SMP systems are kept in a 1395cee9645SThomas Gleixner * single place 1405cee9645SThomas Gleixner */ 1415cee9645SThomas Gleixner #ifdef CONFIG_SMP 1425cee9645SThomas Gleixner 1435cee9645SThomas Gleixner /* 144887d9dc9SPeter Zijlstra * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base() 145887d9dc9SPeter Zijlstra * such that hrtimer_callback_running() can unconditionally dereference 146887d9dc9SPeter Zijlstra * timer->base->cpu_base 147887d9dc9SPeter Zijlstra */ 148887d9dc9SPeter Zijlstra static struct hrtimer_cpu_base migration_cpu_base = { 149887d9dc9SPeter Zijlstra .clock_base = { { .cpu_base = &migration_cpu_base, }, }, 150887d9dc9SPeter Zijlstra }; 151887d9dc9SPeter Zijlstra 152887d9dc9SPeter Zijlstra #define migration_base migration_cpu_base.clock_base[0] 153887d9dc9SPeter Zijlstra 154887d9dc9SPeter Zijlstra /* 1555cee9645SThomas Gleixner * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock 1565cee9645SThomas Gleixner * means that all timers which are tied to this base via timer->base are 1575cee9645SThomas Gleixner * locked, and the base itself is locked too. 1585cee9645SThomas Gleixner * 1595cee9645SThomas Gleixner * So __run_timers/migrate_timers can safely modify all timers which could 1605cee9645SThomas Gleixner * be found on the lists/queues. 1615cee9645SThomas Gleixner * 1625cee9645SThomas Gleixner * When the timer's base is locked, and the timer removed from list, it is 163887d9dc9SPeter Zijlstra * possible to set timer->base = &migration_base and drop the lock: the timer 164887d9dc9SPeter Zijlstra * remains locked. 1655cee9645SThomas Gleixner */ 1665cee9645SThomas Gleixner static 1675cee9645SThomas Gleixner struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, 1685cee9645SThomas Gleixner unsigned long *flags) 1695cee9645SThomas Gleixner { 1705cee9645SThomas Gleixner struct hrtimer_clock_base *base; 1715cee9645SThomas Gleixner 1725cee9645SThomas Gleixner for (;;) { 1735cee9645SThomas Gleixner base = timer->base; 174887d9dc9SPeter Zijlstra if (likely(base != &migration_base)) { 1755cee9645SThomas Gleixner raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); 1765cee9645SThomas Gleixner if (likely(base == timer->base)) 1775cee9645SThomas Gleixner return base; 1785cee9645SThomas Gleixner /* The timer has migrated to another CPU: */ 1795cee9645SThomas Gleixner raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags); 1805cee9645SThomas Gleixner } 1815cee9645SThomas Gleixner cpu_relax(); 1825cee9645SThomas Gleixner } 1835cee9645SThomas Gleixner } 1845cee9645SThomas Gleixner 1855cee9645SThomas Gleixner /* 18607a9a7eaSAnna-Maria Gleixner * We do not migrate the timer when it is expiring before the next 18707a9a7eaSAnna-Maria Gleixner * event on the target cpu. When high resolution is enabled, we cannot 18807a9a7eaSAnna-Maria Gleixner * reprogram the target cpu hardware and we would cause it to fire 18907a9a7eaSAnna-Maria Gleixner * late. To keep it simple, we handle the high resolution enabled and 19007a9a7eaSAnna-Maria Gleixner * disabled case similar. 1915cee9645SThomas Gleixner * 1925cee9645SThomas Gleixner * Called with cpu_base->lock of target cpu held. 1935cee9645SThomas Gleixner */ 1945cee9645SThomas Gleixner static int 1955cee9645SThomas Gleixner hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base) 1965cee9645SThomas Gleixner { 1975cee9645SThomas Gleixner ktime_t expires; 1985cee9645SThomas Gleixner 1995cee9645SThomas Gleixner expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset); 2002ac2dcccSAnna-Maria Gleixner return expires < new_base->cpu_base->expires_next; 2015cee9645SThomas Gleixner } 2025cee9645SThomas Gleixner 203bc7a34b8SThomas Gleixner static inline 204bc7a34b8SThomas Gleixner struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base, 205bc7a34b8SThomas Gleixner int pinned) 206bc7a34b8SThomas Gleixner { 207ae67badaSThomas Gleixner #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) 208ae67badaSThomas Gleixner if (static_branch_likely(&timers_migration_enabled) && !pinned) 209bc7a34b8SThomas Gleixner return &per_cpu(hrtimer_bases, get_nohz_timer_target()); 210ae67badaSThomas Gleixner #endif 211662b3e19SFrederic Weisbecker return base; 212bc7a34b8SThomas Gleixner } 213bc7a34b8SThomas Gleixner 2145cee9645SThomas Gleixner /* 215b48362d8SFrederic Weisbecker * We switch the timer base to a power-optimized selected CPU target, 216b48362d8SFrederic Weisbecker * if: 217b48362d8SFrederic Weisbecker * - NO_HZ_COMMON is enabled 218b48362d8SFrederic Weisbecker * - timer migration is enabled 219b48362d8SFrederic Weisbecker * - the timer callback is not running 220b48362d8SFrederic Weisbecker * - the timer is not the first expiring timer on the new target 221b48362d8SFrederic Weisbecker * 222b48362d8SFrederic Weisbecker * If one of the above requirements is not fulfilled we move the timer 223b48362d8SFrederic Weisbecker * to the current CPU or leave it on the previously assigned CPU if 224b48362d8SFrederic Weisbecker * the timer callback is currently running. 2255cee9645SThomas Gleixner */ 2265cee9645SThomas Gleixner static inline struct hrtimer_clock_base * 2275cee9645SThomas Gleixner switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, 2285cee9645SThomas Gleixner int pinned) 2295cee9645SThomas Gleixner { 230b48362d8SFrederic Weisbecker struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base; 2315cee9645SThomas Gleixner struct hrtimer_clock_base *new_base; 2325cee9645SThomas Gleixner int basenum = base->index; 2335cee9645SThomas Gleixner 234b48362d8SFrederic Weisbecker this_cpu_base = this_cpu_ptr(&hrtimer_bases); 235b48362d8SFrederic Weisbecker new_cpu_base = get_target_base(this_cpu_base, pinned); 2365cee9645SThomas Gleixner again: 2375cee9645SThomas Gleixner new_base = &new_cpu_base->clock_base[basenum]; 2385cee9645SThomas Gleixner 2395cee9645SThomas Gleixner if (base != new_base) { 2405cee9645SThomas Gleixner /* 2415cee9645SThomas Gleixner * We are trying to move timer to new_base. 2425cee9645SThomas Gleixner * However we can't change timer's base while it is running, 2435cee9645SThomas Gleixner * so we keep it on the same CPU. No hassle vs. reprogramming 2445cee9645SThomas Gleixner * the event source in the high resolution case. The softirq 2455cee9645SThomas Gleixner * code will take care of this when the timer function has 2465cee9645SThomas Gleixner * completed. There is no conflict as we hold the lock until 2475cee9645SThomas Gleixner * the timer is enqueued. 2485cee9645SThomas Gleixner */ 2495cee9645SThomas Gleixner if (unlikely(hrtimer_callback_running(timer))) 2505cee9645SThomas Gleixner return base; 2515cee9645SThomas Gleixner 252887d9dc9SPeter Zijlstra /* See the comment in lock_hrtimer_base() */ 253887d9dc9SPeter Zijlstra timer->base = &migration_base; 2545cee9645SThomas Gleixner raw_spin_unlock(&base->cpu_base->lock); 2555cee9645SThomas Gleixner raw_spin_lock(&new_base->cpu_base->lock); 2565cee9645SThomas Gleixner 257b48362d8SFrederic Weisbecker if (new_cpu_base != this_cpu_base && 258bc7a34b8SThomas Gleixner hrtimer_check_target(timer, new_base)) { 2595cee9645SThomas Gleixner raw_spin_unlock(&new_base->cpu_base->lock); 2605cee9645SThomas Gleixner raw_spin_lock(&base->cpu_base->lock); 261b48362d8SFrederic Weisbecker new_cpu_base = this_cpu_base; 2625cee9645SThomas Gleixner timer->base = base; 2635cee9645SThomas Gleixner goto again; 2645cee9645SThomas Gleixner } 2655cee9645SThomas Gleixner timer->base = new_base; 2665cee9645SThomas Gleixner } else { 267b48362d8SFrederic Weisbecker if (new_cpu_base != this_cpu_base && 268bc7a34b8SThomas Gleixner hrtimer_check_target(timer, new_base)) { 269b48362d8SFrederic Weisbecker new_cpu_base = this_cpu_base; 2705cee9645SThomas Gleixner goto again; 2715cee9645SThomas Gleixner } 2725cee9645SThomas Gleixner } 2735cee9645SThomas Gleixner return new_base; 2745cee9645SThomas Gleixner } 2755cee9645SThomas Gleixner 2765cee9645SThomas Gleixner #else /* CONFIG_SMP */ 2775cee9645SThomas Gleixner 2785cee9645SThomas Gleixner static inline struct hrtimer_clock_base * 2795cee9645SThomas Gleixner lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) 2805cee9645SThomas Gleixner { 2815cee9645SThomas Gleixner struct hrtimer_clock_base *base = timer->base; 2825cee9645SThomas Gleixner 2835cee9645SThomas Gleixner raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); 2845cee9645SThomas Gleixner 2855cee9645SThomas Gleixner return base; 2865cee9645SThomas Gleixner } 2875cee9645SThomas Gleixner 2885cee9645SThomas Gleixner # define switch_hrtimer_base(t, b, p) (b) 2895cee9645SThomas Gleixner 2905cee9645SThomas Gleixner #endif /* !CONFIG_SMP */ 2915cee9645SThomas Gleixner 2925cee9645SThomas Gleixner /* 2935cee9645SThomas Gleixner * Functions for the union type storage format of ktime_t which are 2945cee9645SThomas Gleixner * too large for inlining: 2955cee9645SThomas Gleixner */ 2965cee9645SThomas Gleixner #if BITS_PER_LONG < 64 2975cee9645SThomas Gleixner /* 2985cee9645SThomas Gleixner * Divide a ktime value by a nanosecond value 2995cee9645SThomas Gleixner */ 300f7bcb70eSJohn Stultz s64 __ktime_divns(const ktime_t kt, s64 div) 3015cee9645SThomas Gleixner { 3025cee9645SThomas Gleixner int sft = 0; 303f7bcb70eSJohn Stultz s64 dclc; 304f7bcb70eSJohn Stultz u64 tmp; 3055cee9645SThomas Gleixner 3065cee9645SThomas Gleixner dclc = ktime_to_ns(kt); 307f7bcb70eSJohn Stultz tmp = dclc < 0 ? -dclc : dclc; 308f7bcb70eSJohn Stultz 3095cee9645SThomas Gleixner /* Make sure the divisor is less than 2^32: */ 3105cee9645SThomas Gleixner while (div >> 32) { 3115cee9645SThomas Gleixner sft++; 3125cee9645SThomas Gleixner div >>= 1; 3135cee9645SThomas Gleixner } 314f7bcb70eSJohn Stultz tmp >>= sft; 315f7bcb70eSJohn Stultz do_div(tmp, (unsigned long) div); 316f7bcb70eSJohn Stultz return dclc < 0 ? -tmp : tmp; 3175cee9645SThomas Gleixner } 3188b618628SNicolas Pitre EXPORT_SYMBOL_GPL(__ktime_divns); 3195cee9645SThomas Gleixner #endif /* BITS_PER_LONG >= 64 */ 3205cee9645SThomas Gleixner 3215cee9645SThomas Gleixner /* 3225cee9645SThomas Gleixner * Add two ktime values and do a safety check for overflow: 3235cee9645SThomas Gleixner */ 3245cee9645SThomas Gleixner ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs) 3255cee9645SThomas Gleixner { 326979515c5SVegard Nossum ktime_t res = ktime_add_unsafe(lhs, rhs); 3275cee9645SThomas Gleixner 3285cee9645SThomas Gleixner /* 3295cee9645SThomas Gleixner * We use KTIME_SEC_MAX here, the maximum timeout which we can 3305cee9645SThomas Gleixner * return to user space in a timespec: 3315cee9645SThomas Gleixner */ 3322456e855SThomas Gleixner if (res < 0 || res < lhs || res < rhs) 3335cee9645SThomas Gleixner res = ktime_set(KTIME_SEC_MAX, 0); 3345cee9645SThomas Gleixner 3355cee9645SThomas Gleixner return res; 3365cee9645SThomas Gleixner } 3375cee9645SThomas Gleixner 3385cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(ktime_add_safe); 3395cee9645SThomas Gleixner 3405cee9645SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_TIMERS 3415cee9645SThomas Gleixner 3425cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr; 3435cee9645SThomas Gleixner 3445cee9645SThomas Gleixner static void *hrtimer_debug_hint(void *addr) 3455cee9645SThomas Gleixner { 3465cee9645SThomas Gleixner return ((struct hrtimer *) addr)->function; 3475cee9645SThomas Gleixner } 3485cee9645SThomas Gleixner 3495cee9645SThomas Gleixner /* 3505cee9645SThomas Gleixner * fixup_init is called when: 3515cee9645SThomas Gleixner * - an active object is initialized 3525cee9645SThomas Gleixner */ 353e3252464SDu, Changbin static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state) 3545cee9645SThomas Gleixner { 3555cee9645SThomas Gleixner struct hrtimer *timer = addr; 3565cee9645SThomas Gleixner 3575cee9645SThomas Gleixner switch (state) { 3585cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE: 3595cee9645SThomas Gleixner hrtimer_cancel(timer); 3605cee9645SThomas Gleixner debug_object_init(timer, &hrtimer_debug_descr); 361e3252464SDu, Changbin return true; 3625cee9645SThomas Gleixner default: 363e3252464SDu, Changbin return false; 3645cee9645SThomas Gleixner } 3655cee9645SThomas Gleixner } 3665cee9645SThomas Gleixner 3675cee9645SThomas Gleixner /* 3685cee9645SThomas Gleixner * fixup_activate is called when: 3695cee9645SThomas Gleixner * - an active object is activated 370b9fdac7fSDu, Changbin * - an unknown non-static object is activated 3715cee9645SThomas Gleixner */ 372e3252464SDu, Changbin static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state) 3735cee9645SThomas Gleixner { 3745cee9645SThomas Gleixner switch (state) { 3755cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE: 3765cee9645SThomas Gleixner WARN_ON(1); 3775cee9645SThomas Gleixner 3785cee9645SThomas Gleixner default: 379e3252464SDu, Changbin return false; 3805cee9645SThomas Gleixner } 3815cee9645SThomas Gleixner } 3825cee9645SThomas Gleixner 3835cee9645SThomas Gleixner /* 3845cee9645SThomas Gleixner * fixup_free is called when: 3855cee9645SThomas Gleixner * - an active object is freed 3865cee9645SThomas Gleixner */ 387e3252464SDu, Changbin static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state) 3885cee9645SThomas Gleixner { 3895cee9645SThomas Gleixner struct hrtimer *timer = addr; 3905cee9645SThomas Gleixner 3915cee9645SThomas Gleixner switch (state) { 3925cee9645SThomas Gleixner case ODEBUG_STATE_ACTIVE: 3935cee9645SThomas Gleixner hrtimer_cancel(timer); 3945cee9645SThomas Gleixner debug_object_free(timer, &hrtimer_debug_descr); 395e3252464SDu, Changbin return true; 3965cee9645SThomas Gleixner default: 397e3252464SDu, Changbin return false; 3985cee9645SThomas Gleixner } 3995cee9645SThomas Gleixner } 4005cee9645SThomas Gleixner 4015cee9645SThomas Gleixner static struct debug_obj_descr hrtimer_debug_descr = { 4025cee9645SThomas Gleixner .name = "hrtimer", 4035cee9645SThomas Gleixner .debug_hint = hrtimer_debug_hint, 4045cee9645SThomas Gleixner .fixup_init = hrtimer_fixup_init, 4055cee9645SThomas Gleixner .fixup_activate = hrtimer_fixup_activate, 4065cee9645SThomas Gleixner .fixup_free = hrtimer_fixup_free, 4075cee9645SThomas Gleixner }; 4085cee9645SThomas Gleixner 4095cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) 4105cee9645SThomas Gleixner { 4115cee9645SThomas Gleixner debug_object_init(timer, &hrtimer_debug_descr); 4125cee9645SThomas Gleixner } 4135cee9645SThomas Gleixner 4145cee9645SThomas Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer) 4155cee9645SThomas Gleixner { 4165cee9645SThomas Gleixner debug_object_activate(timer, &hrtimer_debug_descr); 4175cee9645SThomas Gleixner } 4185cee9645SThomas Gleixner 4195cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) 4205cee9645SThomas Gleixner { 4215cee9645SThomas Gleixner debug_object_deactivate(timer, &hrtimer_debug_descr); 4225cee9645SThomas Gleixner } 4235cee9645SThomas Gleixner 4245cee9645SThomas Gleixner static inline void debug_hrtimer_free(struct hrtimer *timer) 4255cee9645SThomas Gleixner { 4265cee9645SThomas Gleixner debug_object_free(timer, &hrtimer_debug_descr); 4275cee9645SThomas Gleixner } 4285cee9645SThomas Gleixner 4295cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 4305cee9645SThomas Gleixner enum hrtimer_mode mode); 4315cee9645SThomas Gleixner 4325cee9645SThomas Gleixner void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id, 4335cee9645SThomas Gleixner enum hrtimer_mode mode) 4345cee9645SThomas Gleixner { 4355cee9645SThomas Gleixner debug_object_init_on_stack(timer, &hrtimer_debug_descr); 4365cee9645SThomas Gleixner __hrtimer_init(timer, clock_id, mode); 4375cee9645SThomas Gleixner } 4385cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_on_stack); 4395cee9645SThomas Gleixner 4405cee9645SThomas Gleixner void destroy_hrtimer_on_stack(struct hrtimer *timer) 4415cee9645SThomas Gleixner { 4425cee9645SThomas Gleixner debug_object_free(timer, &hrtimer_debug_descr); 4435cee9645SThomas Gleixner } 444c08376acSGuenter Roeck EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack); 4455cee9645SThomas Gleixner 4465cee9645SThomas Gleixner #else 4475cee9645SThomas Gleixner static inline void debug_hrtimer_init(struct hrtimer *timer) { } 4485cee9645SThomas Gleixner static inline void debug_hrtimer_activate(struct hrtimer *timer) { } 4495cee9645SThomas Gleixner static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } 4505cee9645SThomas Gleixner #endif 4515cee9645SThomas Gleixner 4525cee9645SThomas Gleixner static inline void 4535cee9645SThomas Gleixner debug_init(struct hrtimer *timer, clockid_t clockid, 4545cee9645SThomas Gleixner enum hrtimer_mode mode) 4555cee9645SThomas Gleixner { 4565cee9645SThomas Gleixner debug_hrtimer_init(timer); 4575cee9645SThomas Gleixner trace_hrtimer_init(timer, clockid, mode); 4585cee9645SThomas Gleixner } 4595cee9645SThomas Gleixner 46063e2ed36SAnna-Maria Gleixner static inline void debug_activate(struct hrtimer *timer, 46163e2ed36SAnna-Maria Gleixner enum hrtimer_mode mode) 4625cee9645SThomas Gleixner { 4635cee9645SThomas Gleixner debug_hrtimer_activate(timer); 46463e2ed36SAnna-Maria Gleixner trace_hrtimer_start(timer, mode); 4655cee9645SThomas Gleixner } 4665cee9645SThomas Gleixner 4675cee9645SThomas Gleixner static inline void debug_deactivate(struct hrtimer *timer) 4685cee9645SThomas Gleixner { 4695cee9645SThomas Gleixner debug_hrtimer_deactivate(timer); 4705cee9645SThomas Gleixner trace_hrtimer_cancel(timer); 4715cee9645SThomas Gleixner } 4725cee9645SThomas Gleixner 473c272ca58SAnna-Maria Gleixner static struct hrtimer_clock_base * 474c272ca58SAnna-Maria Gleixner __next_base(struct hrtimer_cpu_base *cpu_base, unsigned int *active) 475c272ca58SAnna-Maria Gleixner { 476c272ca58SAnna-Maria Gleixner unsigned int idx; 477c272ca58SAnna-Maria Gleixner 478c272ca58SAnna-Maria Gleixner if (!*active) 479c272ca58SAnna-Maria Gleixner return NULL; 480c272ca58SAnna-Maria Gleixner 481c272ca58SAnna-Maria Gleixner idx = __ffs(*active); 482c272ca58SAnna-Maria Gleixner *active &= ~(1U << idx); 483c272ca58SAnna-Maria Gleixner 484c272ca58SAnna-Maria Gleixner return &cpu_base->clock_base[idx]; 485c272ca58SAnna-Maria Gleixner } 486c272ca58SAnna-Maria Gleixner 487c272ca58SAnna-Maria Gleixner #define for_each_active_base(base, cpu_base, active) \ 488c272ca58SAnna-Maria Gleixner while ((base = __next_base((cpu_base), &(active)))) 489c272ca58SAnna-Maria Gleixner 490ad38f596SAnna-Maria Gleixner static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base, 491ad38f596SAnna-Maria Gleixner unsigned int active, 492ad38f596SAnna-Maria Gleixner ktime_t expires_next) 4939bc74919SThomas Gleixner { 494c272ca58SAnna-Maria Gleixner struct hrtimer_clock_base *base; 495ad38f596SAnna-Maria Gleixner ktime_t expires; 4969bc74919SThomas Gleixner 497c272ca58SAnna-Maria Gleixner for_each_active_base(base, cpu_base, active) { 4989bc74919SThomas Gleixner struct timerqueue_node *next; 4999bc74919SThomas Gleixner struct hrtimer *timer; 5009bc74919SThomas Gleixner 50134aee88aSThomas Gleixner next = timerqueue_getnext(&base->active); 5029bc74919SThomas Gleixner timer = container_of(next, struct hrtimer, node); 5039bc74919SThomas Gleixner expires = ktime_sub(hrtimer_get_expires(timer), base->offset); 5042456e855SThomas Gleixner if (expires < expires_next) { 5059bc74919SThomas Gleixner expires_next = expires; 506eb27926bSAnna-Maria Gleixner cpu_base->next_timer = timer; 507895bdfa7SThomas Gleixner } 5089bc74919SThomas Gleixner } 5099bc74919SThomas Gleixner /* 5109bc74919SThomas Gleixner * clock_was_set() might have changed base->offset of any of 5119bc74919SThomas Gleixner * the clock bases so the result might be negative. Fix it up 5129bc74919SThomas Gleixner * to prevent a false positive in clockevents_program_event(). 5139bc74919SThomas Gleixner */ 5142456e855SThomas Gleixner if (expires_next < 0) 5152456e855SThomas Gleixner expires_next = 0; 5169bc74919SThomas Gleixner return expires_next; 5179bc74919SThomas Gleixner } 5189bc74919SThomas Gleixner 519*c458b1d1SAnna-Maria Gleixner /* 520*c458b1d1SAnna-Maria Gleixner * Recomputes cpu_base::*next_timer and returns the earliest expires_next but 521*c458b1d1SAnna-Maria Gleixner * does not set cpu_base::*expires_next, that is done by hrtimer_reprogram. 522*c458b1d1SAnna-Maria Gleixner * 523*c458b1d1SAnna-Maria Gleixner * @active_mask must be one of: 524*c458b1d1SAnna-Maria Gleixner * - HRTIMER_ACTIVE, 525*c458b1d1SAnna-Maria Gleixner * - HRTIMER_ACTIVE_SOFT, or 526*c458b1d1SAnna-Maria Gleixner * - HRTIMER_ACTIVE_HARD. 527*c458b1d1SAnna-Maria Gleixner */ 528*c458b1d1SAnna-Maria Gleixner static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, 529*c458b1d1SAnna-Maria Gleixner unsigned int active_mask) 530ad38f596SAnna-Maria Gleixner { 531*c458b1d1SAnna-Maria Gleixner unsigned int active; 532ad38f596SAnna-Maria Gleixner ktime_t expires_next = KTIME_MAX; 533ad38f596SAnna-Maria Gleixner 534ad38f596SAnna-Maria Gleixner cpu_base->next_timer = NULL; 535ad38f596SAnna-Maria Gleixner 536*c458b1d1SAnna-Maria Gleixner active = cpu_base->active_bases & active_mask; 537ad38f596SAnna-Maria Gleixner expires_next = __hrtimer_next_event_base(cpu_base, active, expires_next); 538ad38f596SAnna-Maria Gleixner 539ad38f596SAnna-Maria Gleixner return expires_next; 540ad38f596SAnna-Maria Gleixner } 541ad38f596SAnna-Maria Gleixner 54221d6d52aSThomas Gleixner static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base) 54321d6d52aSThomas Gleixner { 54421d6d52aSThomas Gleixner ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset; 54521d6d52aSThomas Gleixner ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset; 54621d6d52aSThomas Gleixner ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset; 54721d6d52aSThomas Gleixner 548868a3e91SThomas Gleixner return ktime_get_update_offsets_now(&base->clock_was_set_seq, 549868a3e91SThomas Gleixner offs_real, offs_boot, offs_tai); 55021d6d52aSThomas Gleixner } 55121d6d52aSThomas Gleixner 55228bfd18bSAnna-Maria Gleixner /* 55328bfd18bSAnna-Maria Gleixner * Is the high resolution mode active ? 55428bfd18bSAnna-Maria Gleixner */ 55528bfd18bSAnna-Maria Gleixner static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base) 55628bfd18bSAnna-Maria Gleixner { 55728bfd18bSAnna-Maria Gleixner return IS_ENABLED(CONFIG_HIGH_RES_TIMERS) ? 55828bfd18bSAnna-Maria Gleixner cpu_base->hres_active : 0; 55928bfd18bSAnna-Maria Gleixner } 56028bfd18bSAnna-Maria Gleixner 56128bfd18bSAnna-Maria Gleixner static inline int hrtimer_hres_active(void) 56228bfd18bSAnna-Maria Gleixner { 56328bfd18bSAnna-Maria Gleixner return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases)); 56428bfd18bSAnna-Maria Gleixner } 56528bfd18bSAnna-Maria Gleixner 5665cee9645SThomas Gleixner /* 5675cee9645SThomas Gleixner * Reprogram the event source with checking both queues for the 5685cee9645SThomas Gleixner * next event 5695cee9645SThomas Gleixner * Called with interrupts disabled and base->lock held 5705cee9645SThomas Gleixner */ 5715cee9645SThomas Gleixner static void 5725cee9645SThomas Gleixner hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal) 5735cee9645SThomas Gleixner { 57421d6d52aSThomas Gleixner ktime_t expires_next; 57521d6d52aSThomas Gleixner 576*c458b1d1SAnna-Maria Gleixner expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD); 5775cee9645SThomas Gleixner 5782456e855SThomas Gleixner if (skip_equal && expires_next == cpu_base->expires_next) 5795cee9645SThomas Gleixner return; 5805cee9645SThomas Gleixner 5812456e855SThomas Gleixner cpu_base->expires_next = expires_next; 5825cee9645SThomas Gleixner 5835cee9645SThomas Gleixner /* 58461bb4bcbSAnna-Maria Gleixner * If hres is not active, hardware does not have to be 58561bb4bcbSAnna-Maria Gleixner * reprogrammed yet. 58661bb4bcbSAnna-Maria Gleixner * 5875cee9645SThomas Gleixner * If a hang was detected in the last timer interrupt then we 5885cee9645SThomas Gleixner * leave the hang delay active in the hardware. We want the 5895cee9645SThomas Gleixner * system to make progress. That also prevents the following 5905cee9645SThomas Gleixner * scenario: 5915cee9645SThomas Gleixner * T1 expires 50ms from now 5925cee9645SThomas Gleixner * T2 expires 5s from now 5935cee9645SThomas Gleixner * 5945cee9645SThomas Gleixner * T1 is removed, so this code is called and would reprogram 5955cee9645SThomas Gleixner * the hardware to 5s from now. Any hrtimer_start after that 5965cee9645SThomas Gleixner * will not reprogram the hardware due to hang_detected being 5975cee9645SThomas Gleixner * set. So we'd effectivly block all timers until the T2 event 5985cee9645SThomas Gleixner * fires. 5995cee9645SThomas Gleixner */ 60061bb4bcbSAnna-Maria Gleixner if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected) 6015cee9645SThomas Gleixner return; 6025cee9645SThomas Gleixner 6035cee9645SThomas Gleixner tick_program_event(cpu_base->expires_next, 1); 6045cee9645SThomas Gleixner } 6055cee9645SThomas Gleixner 606ebba2c72SAnna-Maria Gleixner /* High resolution timer related functions */ 607ebba2c72SAnna-Maria Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 608ebba2c72SAnna-Maria Gleixner 609ebba2c72SAnna-Maria Gleixner /* 610ebba2c72SAnna-Maria Gleixner * High resolution timer enabled ? 611ebba2c72SAnna-Maria Gleixner */ 612ebba2c72SAnna-Maria Gleixner static bool hrtimer_hres_enabled __read_mostly = true; 613ebba2c72SAnna-Maria Gleixner unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC; 614ebba2c72SAnna-Maria Gleixner EXPORT_SYMBOL_GPL(hrtimer_resolution); 615ebba2c72SAnna-Maria Gleixner 616ebba2c72SAnna-Maria Gleixner /* 617ebba2c72SAnna-Maria Gleixner * Enable / Disable high resolution mode 618ebba2c72SAnna-Maria Gleixner */ 619ebba2c72SAnna-Maria Gleixner static int __init setup_hrtimer_hres(char *str) 620ebba2c72SAnna-Maria Gleixner { 621ebba2c72SAnna-Maria Gleixner return (kstrtobool(str, &hrtimer_hres_enabled) == 0); 622ebba2c72SAnna-Maria Gleixner } 623ebba2c72SAnna-Maria Gleixner 624ebba2c72SAnna-Maria Gleixner __setup("highres=", setup_hrtimer_hres); 625ebba2c72SAnna-Maria Gleixner 626ebba2c72SAnna-Maria Gleixner /* 627ebba2c72SAnna-Maria Gleixner * hrtimer_high_res_enabled - query, if the highres mode is enabled 628ebba2c72SAnna-Maria Gleixner */ 629ebba2c72SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void) 630ebba2c72SAnna-Maria Gleixner { 631ebba2c72SAnna-Maria Gleixner return hrtimer_hres_enabled; 632ebba2c72SAnna-Maria Gleixner } 633ebba2c72SAnna-Maria Gleixner 6345cee9645SThomas Gleixner /* 63511a9fe06SAnna-Maria Gleixner * Retrigger next event is called after clock was set 63611a9fe06SAnna-Maria Gleixner * 63711a9fe06SAnna-Maria Gleixner * Called with interrupts disabled via on_each_cpu() 63811a9fe06SAnna-Maria Gleixner */ 63911a9fe06SAnna-Maria Gleixner static void retrigger_next_event(void *arg) 64011a9fe06SAnna-Maria Gleixner { 64111a9fe06SAnna-Maria Gleixner struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases); 64211a9fe06SAnna-Maria Gleixner 64311a9fe06SAnna-Maria Gleixner if (!__hrtimer_hres_active(base)) 64411a9fe06SAnna-Maria Gleixner return; 64511a9fe06SAnna-Maria Gleixner 64611a9fe06SAnna-Maria Gleixner raw_spin_lock(&base->lock); 64711a9fe06SAnna-Maria Gleixner hrtimer_update_base(base); 64811a9fe06SAnna-Maria Gleixner hrtimer_force_reprogram(base, 0); 64911a9fe06SAnna-Maria Gleixner raw_spin_unlock(&base->lock); 65011a9fe06SAnna-Maria Gleixner } 65111a9fe06SAnna-Maria Gleixner 65211a9fe06SAnna-Maria Gleixner /* 65311a9fe06SAnna-Maria Gleixner * Switch to high resolution mode 65411a9fe06SAnna-Maria Gleixner */ 65511a9fe06SAnna-Maria Gleixner static void hrtimer_switch_to_hres(void) 65611a9fe06SAnna-Maria Gleixner { 65711a9fe06SAnna-Maria Gleixner struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases); 65811a9fe06SAnna-Maria Gleixner 65911a9fe06SAnna-Maria Gleixner if (tick_init_highres()) { 66011a9fe06SAnna-Maria Gleixner printk(KERN_WARNING "Could not switch to high resolution " 66111a9fe06SAnna-Maria Gleixner "mode on CPU %d\n", base->cpu); 66211a9fe06SAnna-Maria Gleixner return; 66311a9fe06SAnna-Maria Gleixner } 66411a9fe06SAnna-Maria Gleixner base->hres_active = 1; 66511a9fe06SAnna-Maria Gleixner hrtimer_resolution = HIGH_RES_NSEC; 66611a9fe06SAnna-Maria Gleixner 66711a9fe06SAnna-Maria Gleixner tick_setup_sched_timer(); 66811a9fe06SAnna-Maria Gleixner /* "Retrigger" the interrupt to get things going */ 66911a9fe06SAnna-Maria Gleixner retrigger_next_event(NULL); 67011a9fe06SAnna-Maria Gleixner } 67111a9fe06SAnna-Maria Gleixner 67211a9fe06SAnna-Maria Gleixner static void clock_was_set_work(struct work_struct *work) 67311a9fe06SAnna-Maria Gleixner { 67411a9fe06SAnna-Maria Gleixner clock_was_set(); 67511a9fe06SAnna-Maria Gleixner } 67611a9fe06SAnna-Maria Gleixner 67711a9fe06SAnna-Maria Gleixner static DECLARE_WORK(hrtimer_work, clock_was_set_work); 67811a9fe06SAnna-Maria Gleixner 67911a9fe06SAnna-Maria Gleixner /* 68011a9fe06SAnna-Maria Gleixner * Called from timekeeping and resume code to reprogram the hrtimer 68111a9fe06SAnna-Maria Gleixner * interrupt device on all cpus. 68211a9fe06SAnna-Maria Gleixner */ 68311a9fe06SAnna-Maria Gleixner void clock_was_set_delayed(void) 68411a9fe06SAnna-Maria Gleixner { 68511a9fe06SAnna-Maria Gleixner schedule_work(&hrtimer_work); 68611a9fe06SAnna-Maria Gleixner } 68711a9fe06SAnna-Maria Gleixner 68811a9fe06SAnna-Maria Gleixner #else 68911a9fe06SAnna-Maria Gleixner 69011a9fe06SAnna-Maria Gleixner static inline int hrtimer_is_hres_enabled(void) { return 0; } 69111a9fe06SAnna-Maria Gleixner static inline void hrtimer_switch_to_hres(void) { } 69211a9fe06SAnna-Maria Gleixner static inline void retrigger_next_event(void *arg) { } 69311a9fe06SAnna-Maria Gleixner 69411a9fe06SAnna-Maria Gleixner #endif /* CONFIG_HIGH_RES_TIMERS */ 69511a9fe06SAnna-Maria Gleixner 69611a9fe06SAnna-Maria Gleixner /* 6975cee9645SThomas Gleixner * When a timer is enqueued and expires earlier than the already enqueued 6985cee9645SThomas Gleixner * timers, we have to check, whether it expires earlier than the timer for 6995cee9645SThomas Gleixner * which the clock event device was armed. 7005cee9645SThomas Gleixner * 7015cee9645SThomas Gleixner * Called with interrupts disabled and base->cpu_base.lock held 7025cee9645SThomas Gleixner */ 7033ec7a3eeSAnna-Maria Gleixner static void hrtimer_reprogram(struct hrtimer *timer) 7045cee9645SThomas Gleixner { 705dc5df73bSChristoph Lameter struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); 7063ec7a3eeSAnna-Maria Gleixner struct hrtimer_clock_base *base = timer->base; 7075cee9645SThomas Gleixner ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset); 7085cee9645SThomas Gleixner 7095cee9645SThomas Gleixner WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0); 7105cee9645SThomas Gleixner 7115cee9645SThomas Gleixner /* 712c6eb3f70SThomas Gleixner * If the timer is not on the current cpu, we cannot reprogram 713c6eb3f70SThomas Gleixner * the other cpus clock event device. 7145cee9645SThomas Gleixner */ 715c6eb3f70SThomas Gleixner if (base->cpu_base != cpu_base) 716c6eb3f70SThomas Gleixner return; 717c6eb3f70SThomas Gleixner 718c6eb3f70SThomas Gleixner /* 719c6eb3f70SThomas Gleixner * If the hrtimer interrupt is running, then it will 720c6eb3f70SThomas Gleixner * reevaluate the clock bases and reprogram the clock event 721c6eb3f70SThomas Gleixner * device. The callbacks are always executed in hard interrupt 722c6eb3f70SThomas Gleixner * context so we don't need an extra check for a running 723c6eb3f70SThomas Gleixner * callback. 724c6eb3f70SThomas Gleixner */ 725c6eb3f70SThomas Gleixner if (cpu_base->in_hrtirq) 726c6eb3f70SThomas Gleixner return; 7275cee9645SThomas Gleixner 7285cee9645SThomas Gleixner /* 7295cee9645SThomas Gleixner * CLOCK_REALTIME timer might be requested with an absolute 730c6eb3f70SThomas Gleixner * expiry time which is less than base->offset. Set it to 0. 7315cee9645SThomas Gleixner */ 7322456e855SThomas Gleixner if (expires < 0) 7332456e855SThomas Gleixner expires = 0; 7345cee9645SThomas Gleixner 7352456e855SThomas Gleixner if (expires >= cpu_base->expires_next) 736c6eb3f70SThomas Gleixner return; 7375cee9645SThomas Gleixner 738c6eb3f70SThomas Gleixner /* Update the pointer to the next expiring timer */ 739895bdfa7SThomas Gleixner cpu_base->next_timer = timer; 74014c80341SAnna-Maria Gleixner cpu_base->expires_next = expires; 7419bc74919SThomas Gleixner 7429bc74919SThomas Gleixner /* 74314c80341SAnna-Maria Gleixner * If hres is not active, hardware does not have to be 74414c80341SAnna-Maria Gleixner * programmed yet. 74514c80341SAnna-Maria Gleixner * 7465cee9645SThomas Gleixner * If a hang was detected in the last timer interrupt then we 7475cee9645SThomas Gleixner * do not schedule a timer which is earlier than the expiry 7485cee9645SThomas Gleixner * which we enforced in the hang detection. We want the system 7495cee9645SThomas Gleixner * to make progress. 7505cee9645SThomas Gleixner */ 75114c80341SAnna-Maria Gleixner if (!__hrtimer_hres_active(cpu_base) || cpu_base->hang_detected) 752c6eb3f70SThomas Gleixner return; 7535cee9645SThomas Gleixner 7545cee9645SThomas Gleixner /* 755c6eb3f70SThomas Gleixner * Program the timer hardware. We enforce the expiry for 756c6eb3f70SThomas Gleixner * events which are already in the past. 7575cee9645SThomas Gleixner */ 758c6eb3f70SThomas Gleixner tick_program_event(expires, 1); 7595cee9645SThomas Gleixner } 7605cee9645SThomas Gleixner 7615cee9645SThomas Gleixner /* 7625cee9645SThomas Gleixner * Clock realtime was set 7635cee9645SThomas Gleixner * 7645cee9645SThomas Gleixner * Change the offset of the realtime clock vs. the monotonic 7655cee9645SThomas Gleixner * clock. 7665cee9645SThomas Gleixner * 7675cee9645SThomas Gleixner * We might have to reprogram the high resolution timer interrupt. On 7685cee9645SThomas Gleixner * SMP we call the architecture specific code to retrigger _all_ high 7695cee9645SThomas Gleixner * resolution timer interrupts. On UP we just disable interrupts and 7705cee9645SThomas Gleixner * call the high resolution interrupt code. 7715cee9645SThomas Gleixner */ 7725cee9645SThomas Gleixner void clock_was_set(void) 7735cee9645SThomas Gleixner { 7745cee9645SThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 7755cee9645SThomas Gleixner /* Retrigger the CPU local events everywhere */ 7765cee9645SThomas Gleixner on_each_cpu(retrigger_next_event, NULL, 1); 7775cee9645SThomas Gleixner #endif 7785cee9645SThomas Gleixner timerfd_clock_was_set(); 7795cee9645SThomas Gleixner } 7805cee9645SThomas Gleixner 7815cee9645SThomas Gleixner /* 7825cee9645SThomas Gleixner * During resume we might have to reprogram the high resolution timer 7835cee9645SThomas Gleixner * interrupt on all online CPUs. However, all other CPUs will be 7845cee9645SThomas Gleixner * stopped with IRQs interrupts disabled so the clock_was_set() call 7855cee9645SThomas Gleixner * must be deferred. 7865cee9645SThomas Gleixner */ 7875cee9645SThomas Gleixner void hrtimers_resume(void) 7885cee9645SThomas Gleixner { 78953bef3fdSFrederic Weisbecker lockdep_assert_irqs_disabled(); 7905cee9645SThomas Gleixner /* Retrigger on the local CPU */ 7915cee9645SThomas Gleixner retrigger_next_event(NULL); 7925cee9645SThomas Gleixner /* And schedule a retrigger for all others */ 7935cee9645SThomas Gleixner clock_was_set_delayed(); 7945cee9645SThomas Gleixner } 7955cee9645SThomas Gleixner 7965cee9645SThomas Gleixner /* 7975cee9645SThomas Gleixner * Counterpart to lock_hrtimer_base above: 7985cee9645SThomas Gleixner */ 7995cee9645SThomas Gleixner static inline 8005cee9645SThomas Gleixner void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) 8015cee9645SThomas Gleixner { 8025cee9645SThomas Gleixner raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); 8035cee9645SThomas Gleixner } 8045cee9645SThomas Gleixner 8055cee9645SThomas Gleixner /** 8065cee9645SThomas Gleixner * hrtimer_forward - forward the timer expiry 8075cee9645SThomas Gleixner * @timer: hrtimer to forward 8085cee9645SThomas Gleixner * @now: forward past this time 8095cee9645SThomas Gleixner * @interval: the interval to forward 8105cee9645SThomas Gleixner * 8115cee9645SThomas Gleixner * Forward the timer expiry so it will expire in the future. 8125cee9645SThomas Gleixner * Returns the number of overruns. 81391e5a217SThomas Gleixner * 81491e5a217SThomas Gleixner * Can be safely called from the callback function of @timer. If 81591e5a217SThomas Gleixner * called from other contexts @timer must neither be enqueued nor 81691e5a217SThomas Gleixner * running the callback and the caller needs to take care of 81791e5a217SThomas Gleixner * serialization. 81891e5a217SThomas Gleixner * 81991e5a217SThomas Gleixner * Note: This only updates the timer expiry value and does not requeue 82091e5a217SThomas Gleixner * the timer. 8215cee9645SThomas Gleixner */ 8225cee9645SThomas Gleixner u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) 8235cee9645SThomas Gleixner { 8245cee9645SThomas Gleixner u64 orun = 1; 8255cee9645SThomas Gleixner ktime_t delta; 8265cee9645SThomas Gleixner 8275cee9645SThomas Gleixner delta = ktime_sub(now, hrtimer_get_expires(timer)); 8285cee9645SThomas Gleixner 8292456e855SThomas Gleixner if (delta < 0) 8305cee9645SThomas Gleixner return 0; 8315cee9645SThomas Gleixner 8325de2755cSPeter Zijlstra if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED)) 8335de2755cSPeter Zijlstra return 0; 8345de2755cSPeter Zijlstra 8352456e855SThomas Gleixner if (interval < hrtimer_resolution) 8362456e855SThomas Gleixner interval = hrtimer_resolution; 8375cee9645SThomas Gleixner 8382456e855SThomas Gleixner if (unlikely(delta >= interval)) { 8395cee9645SThomas Gleixner s64 incr = ktime_to_ns(interval); 8405cee9645SThomas Gleixner 8415cee9645SThomas Gleixner orun = ktime_divns(delta, incr); 8425cee9645SThomas Gleixner hrtimer_add_expires_ns(timer, incr * orun); 8432456e855SThomas Gleixner if (hrtimer_get_expires_tv64(timer) > now) 8445cee9645SThomas Gleixner return orun; 8455cee9645SThomas Gleixner /* 8465cee9645SThomas Gleixner * This (and the ktime_add() below) is the 8475cee9645SThomas Gleixner * correction for exact: 8485cee9645SThomas Gleixner */ 8495cee9645SThomas Gleixner orun++; 8505cee9645SThomas Gleixner } 8515cee9645SThomas Gleixner hrtimer_add_expires(timer, interval); 8525cee9645SThomas Gleixner 8535cee9645SThomas Gleixner return orun; 8545cee9645SThomas Gleixner } 8555cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_forward); 8565cee9645SThomas Gleixner 8575cee9645SThomas Gleixner /* 8585cee9645SThomas Gleixner * enqueue_hrtimer - internal function to (re)start a timer 8595cee9645SThomas Gleixner * 8605cee9645SThomas Gleixner * The timer is inserted in expiry order. Insertion into the 8615cee9645SThomas Gleixner * red black tree is O(log(n)). Must hold the base lock. 8625cee9645SThomas Gleixner * 8635cee9645SThomas Gleixner * Returns 1 when the new timer is the leftmost timer in the tree. 8645cee9645SThomas Gleixner */ 8655cee9645SThomas Gleixner static int enqueue_hrtimer(struct hrtimer *timer, 86663e2ed36SAnna-Maria Gleixner struct hrtimer_clock_base *base, 86763e2ed36SAnna-Maria Gleixner enum hrtimer_mode mode) 8685cee9645SThomas Gleixner { 86963e2ed36SAnna-Maria Gleixner debug_activate(timer, mode); 8705cee9645SThomas Gleixner 8715cee9645SThomas Gleixner base->cpu_base->active_bases |= 1 << base->index; 8725cee9645SThomas Gleixner 873887d9dc9SPeter Zijlstra timer->state = HRTIMER_STATE_ENQUEUED; 8745cee9645SThomas Gleixner 875b97f44c9SThomas Gleixner return timerqueue_add(&base->active, &timer->node); 8765cee9645SThomas Gleixner } 8775cee9645SThomas Gleixner 8785cee9645SThomas Gleixner /* 8795cee9645SThomas Gleixner * __remove_hrtimer - internal function to remove a timer 8805cee9645SThomas Gleixner * 8815cee9645SThomas Gleixner * Caller must hold the base lock. 8825cee9645SThomas Gleixner * 8835cee9645SThomas Gleixner * High resolution timer mode reprograms the clock event device when the 8845cee9645SThomas Gleixner * timer is the one which expires next. The caller can disable this by setting 8855cee9645SThomas Gleixner * reprogram to zero. This is useful, when the context does a reprogramming 8865cee9645SThomas Gleixner * anyway (e.g. timer interrupt) 8875cee9645SThomas Gleixner */ 8885cee9645SThomas Gleixner static void __remove_hrtimer(struct hrtimer *timer, 8895cee9645SThomas Gleixner struct hrtimer_clock_base *base, 890203cbf77SThomas Gleixner u8 newstate, int reprogram) 8915cee9645SThomas Gleixner { 892e19ffe8bSThomas Gleixner struct hrtimer_cpu_base *cpu_base = base->cpu_base; 893203cbf77SThomas Gleixner u8 state = timer->state; 8945cee9645SThomas Gleixner 8955cee9645SThomas Gleixner timer->state = newstate; 896895bdfa7SThomas Gleixner if (!(state & HRTIMER_STATE_ENQUEUED)) 897895bdfa7SThomas Gleixner return; 8985cee9645SThomas Gleixner 899b97f44c9SThomas Gleixner if (!timerqueue_del(&base->active, &timer->node)) 900e19ffe8bSThomas Gleixner cpu_base->active_bases &= ~(1 << base->index); 901d9f0acdeSViresh Kumar 902895bdfa7SThomas Gleixner /* 903895bdfa7SThomas Gleixner * Note: If reprogram is false we do not update 904895bdfa7SThomas Gleixner * cpu_base->next_timer. This happens when we remove the first 905895bdfa7SThomas Gleixner * timer on a remote cpu. No harm as we never dereference 906895bdfa7SThomas Gleixner * cpu_base->next_timer. So the worst thing what can happen is 907895bdfa7SThomas Gleixner * an superflous call to hrtimer_force_reprogram() on the 908895bdfa7SThomas Gleixner * remote cpu later on if the same timer gets enqueued again. 909895bdfa7SThomas Gleixner */ 910895bdfa7SThomas Gleixner if (reprogram && timer == cpu_base->next_timer) 911e19ffe8bSThomas Gleixner hrtimer_force_reprogram(cpu_base, 1); 9125cee9645SThomas Gleixner } 9135cee9645SThomas Gleixner 9145cee9645SThomas Gleixner /* 9155cee9645SThomas Gleixner * remove hrtimer, called with base lock held 9165cee9645SThomas Gleixner */ 9175cee9645SThomas Gleixner static inline int 9188edfb036SPeter Zijlstra remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart) 9195cee9645SThomas Gleixner { 9205cee9645SThomas Gleixner if (hrtimer_is_queued(timer)) { 921203cbf77SThomas Gleixner u8 state = timer->state; 9225cee9645SThomas Gleixner int reprogram; 9235cee9645SThomas Gleixner 9245cee9645SThomas Gleixner /* 9255cee9645SThomas Gleixner * Remove the timer and force reprogramming when high 9265cee9645SThomas Gleixner * resolution mode is active and the timer is on the current 9275cee9645SThomas Gleixner * CPU. If we remove a timer on another CPU, reprogramming is 9285cee9645SThomas Gleixner * skipped. The interrupt event on this CPU is fired and 9295cee9645SThomas Gleixner * reprogramming happens in the interrupt handler. This is a 9305cee9645SThomas Gleixner * rare case and less expensive than a smp call. 9315cee9645SThomas Gleixner */ 9325cee9645SThomas Gleixner debug_deactivate(timer); 933dc5df73bSChristoph Lameter reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases); 9348edfb036SPeter Zijlstra 935887d9dc9SPeter Zijlstra if (!restart) 936887d9dc9SPeter Zijlstra state = HRTIMER_STATE_INACTIVE; 937887d9dc9SPeter Zijlstra 9385cee9645SThomas Gleixner __remove_hrtimer(timer, base, state, reprogram); 9395cee9645SThomas Gleixner return 1; 9405cee9645SThomas Gleixner } 9415cee9645SThomas Gleixner return 0; 9425cee9645SThomas Gleixner } 9435cee9645SThomas Gleixner 944203cbf77SThomas Gleixner static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim, 945203cbf77SThomas Gleixner const enum hrtimer_mode mode) 946203cbf77SThomas Gleixner { 947203cbf77SThomas Gleixner #ifdef CONFIG_TIME_LOW_RES 948203cbf77SThomas Gleixner /* 949203cbf77SThomas Gleixner * CONFIG_TIME_LOW_RES indicates that the system has no way to return 950203cbf77SThomas Gleixner * granular time values. For relative timers we add hrtimer_resolution 951203cbf77SThomas Gleixner * (i.e. one jiffie) to prevent short timeouts. 952203cbf77SThomas Gleixner */ 953203cbf77SThomas Gleixner timer->is_rel = mode & HRTIMER_MODE_REL; 954203cbf77SThomas Gleixner if (timer->is_rel) 9558b0e1953SThomas Gleixner tim = ktime_add_safe(tim, hrtimer_resolution); 956203cbf77SThomas Gleixner #endif 957203cbf77SThomas Gleixner return tim; 958203cbf77SThomas Gleixner } 959203cbf77SThomas Gleixner 960138a6b7aSAnna-Maria Gleixner static int __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 961138a6b7aSAnna-Maria Gleixner u64 delta_ns, const enum hrtimer_mode mode, 962138a6b7aSAnna-Maria Gleixner struct hrtimer_clock_base *base) 9635cee9645SThomas Gleixner { 964138a6b7aSAnna-Maria Gleixner struct hrtimer_clock_base *new_base; 9655cee9645SThomas Gleixner 9665cee9645SThomas Gleixner /* Remove an active timer from the queue: */ 9678edfb036SPeter Zijlstra remove_hrtimer(timer, base, true); 9685cee9645SThomas Gleixner 969203cbf77SThomas Gleixner if (mode & HRTIMER_MODE_REL) 9705cee9645SThomas Gleixner tim = ktime_add_safe(tim, base->get_time()); 971203cbf77SThomas Gleixner 972203cbf77SThomas Gleixner tim = hrtimer_update_lowres(timer, tim, mode); 9735cee9645SThomas Gleixner 9745cee9645SThomas Gleixner hrtimer_set_expires_range_ns(timer, tim, delta_ns); 9755cee9645SThomas Gleixner 9765cee9645SThomas Gleixner /* Switch the timer base, if necessary: */ 9775cee9645SThomas Gleixner new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED); 9785cee9645SThomas Gleixner 979138a6b7aSAnna-Maria Gleixner return enqueue_hrtimer(timer, new_base, mode); 980138a6b7aSAnna-Maria Gleixner } 981138a6b7aSAnna-Maria Gleixner /** 982138a6b7aSAnna-Maria Gleixner * hrtimer_start_range_ns - (re)start an hrtimer 983138a6b7aSAnna-Maria Gleixner * @timer: the timer to be added 984138a6b7aSAnna-Maria Gleixner * @tim: expiry time 985138a6b7aSAnna-Maria Gleixner * @delta_ns: "slack" range for the timer 986138a6b7aSAnna-Maria Gleixner * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or 987138a6b7aSAnna-Maria Gleixner * relative (HRTIMER_MODE_REL), and pinned (HRTIMER_MODE_PINNED) 988138a6b7aSAnna-Maria Gleixner */ 989138a6b7aSAnna-Maria Gleixner void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, 990138a6b7aSAnna-Maria Gleixner u64 delta_ns, const enum hrtimer_mode mode) 991138a6b7aSAnna-Maria Gleixner { 992138a6b7aSAnna-Maria Gleixner struct hrtimer_clock_base *base; 993138a6b7aSAnna-Maria Gleixner unsigned long flags; 99449a2a075SViresh Kumar 995138a6b7aSAnna-Maria Gleixner base = lock_hrtimer_base(timer, &flags); 996138a6b7aSAnna-Maria Gleixner 997138a6b7aSAnna-Maria Gleixner if (__hrtimer_start_range_ns(timer, tim, delta_ns, mode, base)) 9983ec7a3eeSAnna-Maria Gleixner hrtimer_reprogram(timer); 999138a6b7aSAnna-Maria Gleixner 10005cee9645SThomas Gleixner unlock_hrtimer_base(timer, &flags); 10015cee9645SThomas Gleixner } 10025cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); 10035cee9645SThomas Gleixner 10045cee9645SThomas Gleixner /** 10055cee9645SThomas Gleixner * hrtimer_try_to_cancel - try to deactivate a timer 10065cee9645SThomas Gleixner * @timer: hrtimer to stop 10075cee9645SThomas Gleixner * 10085cee9645SThomas Gleixner * Returns: 10095cee9645SThomas Gleixner * 0 when the timer was not active 10105cee9645SThomas Gleixner * 1 when the timer was active 10110ba42a59SMasanari Iida * -1 when the timer is currently executing the callback function and 10125cee9645SThomas Gleixner * cannot be stopped 10135cee9645SThomas Gleixner */ 10145cee9645SThomas Gleixner int hrtimer_try_to_cancel(struct hrtimer *timer) 10155cee9645SThomas Gleixner { 10165cee9645SThomas Gleixner struct hrtimer_clock_base *base; 10175cee9645SThomas Gleixner unsigned long flags; 10185cee9645SThomas Gleixner int ret = -1; 10195cee9645SThomas Gleixner 102019d9f422SThomas Gleixner /* 102119d9f422SThomas Gleixner * Check lockless first. If the timer is not active (neither 102219d9f422SThomas Gleixner * enqueued nor running the callback, nothing to do here. The 102319d9f422SThomas Gleixner * base lock does not serialize against a concurrent enqueue, 102419d9f422SThomas Gleixner * so we can avoid taking it. 102519d9f422SThomas Gleixner */ 102619d9f422SThomas Gleixner if (!hrtimer_active(timer)) 102719d9f422SThomas Gleixner return 0; 102819d9f422SThomas Gleixner 10295cee9645SThomas Gleixner base = lock_hrtimer_base(timer, &flags); 10305cee9645SThomas Gleixner 10315cee9645SThomas Gleixner if (!hrtimer_callback_running(timer)) 10328edfb036SPeter Zijlstra ret = remove_hrtimer(timer, base, false); 10335cee9645SThomas Gleixner 10345cee9645SThomas Gleixner unlock_hrtimer_base(timer, &flags); 10355cee9645SThomas Gleixner 10365cee9645SThomas Gleixner return ret; 10375cee9645SThomas Gleixner 10385cee9645SThomas Gleixner } 10395cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); 10405cee9645SThomas Gleixner 10415cee9645SThomas Gleixner /** 10425cee9645SThomas Gleixner * hrtimer_cancel - cancel a timer and wait for the handler to finish. 10435cee9645SThomas Gleixner * @timer: the timer to be cancelled 10445cee9645SThomas Gleixner * 10455cee9645SThomas Gleixner * Returns: 10465cee9645SThomas Gleixner * 0 when the timer was not active 10475cee9645SThomas Gleixner * 1 when the timer was active 10485cee9645SThomas Gleixner */ 10495cee9645SThomas Gleixner int hrtimer_cancel(struct hrtimer *timer) 10505cee9645SThomas Gleixner { 10515cee9645SThomas Gleixner for (;;) { 10525cee9645SThomas Gleixner int ret = hrtimer_try_to_cancel(timer); 10535cee9645SThomas Gleixner 10545cee9645SThomas Gleixner if (ret >= 0) 10555cee9645SThomas Gleixner return ret; 10565cee9645SThomas Gleixner cpu_relax(); 10575cee9645SThomas Gleixner } 10585cee9645SThomas Gleixner } 10595cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_cancel); 10605cee9645SThomas Gleixner 10615cee9645SThomas Gleixner /** 10625cee9645SThomas Gleixner * hrtimer_get_remaining - get remaining time for the timer 10635cee9645SThomas Gleixner * @timer: the timer to read 1064203cbf77SThomas Gleixner * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y 10655cee9645SThomas Gleixner */ 1066203cbf77SThomas Gleixner ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust) 10675cee9645SThomas Gleixner { 10685cee9645SThomas Gleixner unsigned long flags; 10695cee9645SThomas Gleixner ktime_t rem; 10705cee9645SThomas Gleixner 10715cee9645SThomas Gleixner lock_hrtimer_base(timer, &flags); 1072203cbf77SThomas Gleixner if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust) 1073203cbf77SThomas Gleixner rem = hrtimer_expires_remaining_adjusted(timer); 1074203cbf77SThomas Gleixner else 10755cee9645SThomas Gleixner rem = hrtimer_expires_remaining(timer); 10765cee9645SThomas Gleixner unlock_hrtimer_base(timer, &flags); 10775cee9645SThomas Gleixner 10785cee9645SThomas Gleixner return rem; 10795cee9645SThomas Gleixner } 1080203cbf77SThomas Gleixner EXPORT_SYMBOL_GPL(__hrtimer_get_remaining); 10815cee9645SThomas Gleixner 10825cee9645SThomas Gleixner #ifdef CONFIG_NO_HZ_COMMON 10835cee9645SThomas Gleixner /** 10845cee9645SThomas Gleixner * hrtimer_get_next_event - get the time until next expiry event 10855cee9645SThomas Gleixner * 1086c1ad348bSThomas Gleixner * Returns the next expiry time or KTIME_MAX if no timer is pending. 10875cee9645SThomas Gleixner */ 1088c1ad348bSThomas Gleixner u64 hrtimer_get_next_event(void) 10895cee9645SThomas Gleixner { 1090dc5df73bSChristoph Lameter struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); 1091c1ad348bSThomas Gleixner u64 expires = KTIME_MAX; 10925cee9645SThomas Gleixner unsigned long flags; 10935cee9645SThomas Gleixner 10945cee9645SThomas Gleixner raw_spin_lock_irqsave(&cpu_base->lock, flags); 10955cee9645SThomas Gleixner 1096e19ffe8bSThomas Gleixner if (!__hrtimer_hres_active(cpu_base)) 1097*c458b1d1SAnna-Maria Gleixner expires = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD); 10985cee9645SThomas Gleixner 10995cee9645SThomas Gleixner raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 11005cee9645SThomas Gleixner 1101c1ad348bSThomas Gleixner return expires; 11025cee9645SThomas Gleixner } 11035cee9645SThomas Gleixner #endif 11045cee9645SThomas Gleixner 1105336a9cdeSMarc Zyngier static inline int hrtimer_clockid_to_base(clockid_t clock_id) 1106336a9cdeSMarc Zyngier { 1107336a9cdeSMarc Zyngier if (likely(clock_id < MAX_CLOCKS)) { 1108336a9cdeSMarc Zyngier int base = hrtimer_clock_to_base_table[clock_id]; 1109336a9cdeSMarc Zyngier 1110336a9cdeSMarc Zyngier if (likely(base != HRTIMER_MAX_CLOCK_BASES)) 1111336a9cdeSMarc Zyngier return base; 1112336a9cdeSMarc Zyngier } 1113336a9cdeSMarc Zyngier WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id); 1114336a9cdeSMarc Zyngier return HRTIMER_BASE_MONOTONIC; 1115336a9cdeSMarc Zyngier } 1116336a9cdeSMarc Zyngier 11175cee9645SThomas Gleixner static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 11185cee9645SThomas Gleixner enum hrtimer_mode mode) 11195cee9645SThomas Gleixner { 11205cee9645SThomas Gleixner struct hrtimer_cpu_base *cpu_base; 11215cee9645SThomas Gleixner int base; 11225cee9645SThomas Gleixner 11235cee9645SThomas Gleixner memset(timer, 0, sizeof(struct hrtimer)); 11245cee9645SThomas Gleixner 112522127e93SChristoph Lameter cpu_base = raw_cpu_ptr(&hrtimer_bases); 11265cee9645SThomas Gleixner 112748d0c9beSAnna-Maria Gleixner /* 112848d0c9beSAnna-Maria Gleixner * POSIX magic: Relative CLOCK_REALTIME timers are not affected by 112948d0c9beSAnna-Maria Gleixner * clock modifications, so they needs to become CLOCK_MONOTONIC to 113048d0c9beSAnna-Maria Gleixner * ensure POSIX compliance. 113148d0c9beSAnna-Maria Gleixner */ 113248d0c9beSAnna-Maria Gleixner if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL) 11335cee9645SThomas Gleixner clock_id = CLOCK_MONOTONIC; 11345cee9645SThomas Gleixner 11355cee9645SThomas Gleixner base = hrtimer_clockid_to_base(clock_id); 11365cee9645SThomas Gleixner timer->base = &cpu_base->clock_base[base]; 11375cee9645SThomas Gleixner timerqueue_init(&timer->node); 11385cee9645SThomas Gleixner } 11395cee9645SThomas Gleixner 11405cee9645SThomas Gleixner /** 11415cee9645SThomas Gleixner * hrtimer_init - initialize a timer to the given clock 11425cee9645SThomas Gleixner * @timer: the timer to be initialized 11435cee9645SThomas Gleixner * @clock_id: the clock to be used 11446de6250cSAnna-Maria Gleixner * @mode: timer mode: absolute (HRTIMER_MODE_ABS) or 11456de6250cSAnna-Maria Gleixner * relative (HRTIMER_MODE_REL); pinned is not considered here! 11465cee9645SThomas Gleixner */ 11475cee9645SThomas Gleixner void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 11485cee9645SThomas Gleixner enum hrtimer_mode mode) 11495cee9645SThomas Gleixner { 11505cee9645SThomas Gleixner debug_init(timer, clock_id, mode); 11515cee9645SThomas Gleixner __hrtimer_init(timer, clock_id, mode); 11525cee9645SThomas Gleixner } 11535cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init); 11545cee9645SThomas Gleixner 1155887d9dc9SPeter Zijlstra /* 1156887d9dc9SPeter Zijlstra * A timer is active, when it is enqueued into the rbtree or the 1157887d9dc9SPeter Zijlstra * callback function is running or it's in the state of being migrated 1158887d9dc9SPeter Zijlstra * to another cpu. 11595cee9645SThomas Gleixner * 1160887d9dc9SPeter Zijlstra * It is important for this function to not return a false negative. 11615cee9645SThomas Gleixner */ 1162887d9dc9SPeter Zijlstra bool hrtimer_active(const struct hrtimer *timer) 11635cee9645SThomas Gleixner { 11643f0b9e8eSAnna-Maria Gleixner struct hrtimer_clock_base *base; 1165887d9dc9SPeter Zijlstra unsigned int seq; 11665cee9645SThomas Gleixner 1167887d9dc9SPeter Zijlstra do { 11683f0b9e8eSAnna-Maria Gleixner base = READ_ONCE(timer->base); 11693f0b9e8eSAnna-Maria Gleixner seq = raw_read_seqcount_begin(&base->seq); 11705cee9645SThomas Gleixner 1171887d9dc9SPeter Zijlstra if (timer->state != HRTIMER_STATE_INACTIVE || 11723f0b9e8eSAnna-Maria Gleixner base->running == timer) 1173887d9dc9SPeter Zijlstra return true; 1174887d9dc9SPeter Zijlstra 11753f0b9e8eSAnna-Maria Gleixner } while (read_seqcount_retry(&base->seq, seq) || 11763f0b9e8eSAnna-Maria Gleixner base != READ_ONCE(timer->base)); 1177887d9dc9SPeter Zijlstra 1178887d9dc9SPeter Zijlstra return false; 11795cee9645SThomas Gleixner } 1180887d9dc9SPeter Zijlstra EXPORT_SYMBOL_GPL(hrtimer_active); 11815cee9645SThomas Gleixner 1182887d9dc9SPeter Zijlstra /* 1183887d9dc9SPeter Zijlstra * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3 1184887d9dc9SPeter Zijlstra * distinct sections: 1185887d9dc9SPeter Zijlstra * 1186887d9dc9SPeter Zijlstra * - queued: the timer is queued 1187887d9dc9SPeter Zijlstra * - callback: the timer is being ran 1188887d9dc9SPeter Zijlstra * - post: the timer is inactive or (re)queued 1189887d9dc9SPeter Zijlstra * 1190887d9dc9SPeter Zijlstra * On the read side we ensure we observe timer->state and cpu_base->running 1191887d9dc9SPeter Zijlstra * from the same section, if anything changed while we looked at it, we retry. 1192887d9dc9SPeter Zijlstra * This includes timer->base changing because sequence numbers alone are 1193887d9dc9SPeter Zijlstra * insufficient for that. 1194887d9dc9SPeter Zijlstra * 1195887d9dc9SPeter Zijlstra * The sequence numbers are required because otherwise we could still observe 1196887d9dc9SPeter Zijlstra * a false negative if the read side got smeared over multiple consequtive 1197887d9dc9SPeter Zijlstra * __run_hrtimer() invocations. 1198887d9dc9SPeter Zijlstra */ 1199887d9dc9SPeter Zijlstra 120021d6d52aSThomas Gleixner static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, 120121d6d52aSThomas Gleixner struct hrtimer_clock_base *base, 1202dd934aa8SAnna-Maria Gleixner struct hrtimer *timer, ktime_t *now, 1203dd934aa8SAnna-Maria Gleixner unsigned long flags) 12045cee9645SThomas Gleixner { 12055cee9645SThomas Gleixner enum hrtimer_restart (*fn)(struct hrtimer *); 12065cee9645SThomas Gleixner int restart; 12075cee9645SThomas Gleixner 1208887d9dc9SPeter Zijlstra lockdep_assert_held(&cpu_base->lock); 12095cee9645SThomas Gleixner 12105cee9645SThomas Gleixner debug_deactivate(timer); 12113f0b9e8eSAnna-Maria Gleixner base->running = timer; 1212887d9dc9SPeter Zijlstra 1213887d9dc9SPeter Zijlstra /* 1214887d9dc9SPeter Zijlstra * Separate the ->running assignment from the ->state assignment. 1215887d9dc9SPeter Zijlstra * 1216887d9dc9SPeter Zijlstra * As with a regular write barrier, this ensures the read side in 12173f0b9e8eSAnna-Maria Gleixner * hrtimer_active() cannot observe base->running == NULL && 1218887d9dc9SPeter Zijlstra * timer->state == INACTIVE. 1219887d9dc9SPeter Zijlstra */ 12203f0b9e8eSAnna-Maria Gleixner raw_write_seqcount_barrier(&base->seq); 1221887d9dc9SPeter Zijlstra 1222887d9dc9SPeter Zijlstra __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0); 12235cee9645SThomas Gleixner fn = timer->function; 12245cee9645SThomas Gleixner 12255cee9645SThomas Gleixner /* 1226203cbf77SThomas Gleixner * Clear the 'is relative' flag for the TIME_LOW_RES case. If the 1227203cbf77SThomas Gleixner * timer is restarted with a period then it becomes an absolute 1228203cbf77SThomas Gleixner * timer. If its not restarted it does not matter. 1229203cbf77SThomas Gleixner */ 1230203cbf77SThomas Gleixner if (IS_ENABLED(CONFIG_TIME_LOW_RES)) 1231203cbf77SThomas Gleixner timer->is_rel = false; 1232203cbf77SThomas Gleixner 1233203cbf77SThomas Gleixner /* 1234d05ca13bSThomas Gleixner * The timer is marked as running in the CPU base, so it is 1235d05ca13bSThomas Gleixner * protected against migration to a different CPU even if the lock 1236d05ca13bSThomas Gleixner * is dropped. 12375cee9645SThomas Gleixner */ 1238dd934aa8SAnna-Maria Gleixner raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 12395cee9645SThomas Gleixner trace_hrtimer_expire_entry(timer, now); 12405cee9645SThomas Gleixner restart = fn(timer); 12415cee9645SThomas Gleixner trace_hrtimer_expire_exit(timer); 1242dd934aa8SAnna-Maria Gleixner raw_spin_lock_irq(&cpu_base->lock); 12435cee9645SThomas Gleixner 12445cee9645SThomas Gleixner /* 1245887d9dc9SPeter Zijlstra * Note: We clear the running state after enqueue_hrtimer and 1246b4d90e9fSPratyush Patel * we do not reprogram the event hardware. Happens either in 12475cee9645SThomas Gleixner * hrtimer_start_range_ns() or in hrtimer_interrupt() 12485de2755cSPeter Zijlstra * 12495de2755cSPeter Zijlstra * Note: Because we dropped the cpu_base->lock above, 12505de2755cSPeter Zijlstra * hrtimer_start_range_ns() can have popped in and enqueued the timer 12515de2755cSPeter Zijlstra * for us already. 12525cee9645SThomas Gleixner */ 12535de2755cSPeter Zijlstra if (restart != HRTIMER_NORESTART && 12545de2755cSPeter Zijlstra !(timer->state & HRTIMER_STATE_ENQUEUED)) 125563e2ed36SAnna-Maria Gleixner enqueue_hrtimer(timer, base, HRTIMER_MODE_ABS); 12565cee9645SThomas Gleixner 12575cee9645SThomas Gleixner /* 1258887d9dc9SPeter Zijlstra * Separate the ->running assignment from the ->state assignment. 1259887d9dc9SPeter Zijlstra * 1260887d9dc9SPeter Zijlstra * As with a regular write barrier, this ensures the read side in 12613f0b9e8eSAnna-Maria Gleixner * hrtimer_active() cannot observe base->running.timer == NULL && 1262887d9dc9SPeter Zijlstra * timer->state == INACTIVE. 12635cee9645SThomas Gleixner */ 12643f0b9e8eSAnna-Maria Gleixner raw_write_seqcount_barrier(&base->seq); 12655cee9645SThomas Gleixner 12663f0b9e8eSAnna-Maria Gleixner WARN_ON_ONCE(base->running != timer); 12673f0b9e8eSAnna-Maria Gleixner base->running = NULL; 12685cee9645SThomas Gleixner } 12695cee9645SThomas Gleixner 1270dd934aa8SAnna-Maria Gleixner static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now, 1271*c458b1d1SAnna-Maria Gleixner unsigned long flags, unsigned int active_mask) 12725cee9645SThomas Gleixner { 1273c272ca58SAnna-Maria Gleixner struct hrtimer_clock_base *base; 1274*c458b1d1SAnna-Maria Gleixner unsigned int active = cpu_base->active_bases & active_mask; 12755cee9645SThomas Gleixner 1276c272ca58SAnna-Maria Gleixner for_each_active_base(base, cpu_base, active) { 12775cee9645SThomas Gleixner struct timerqueue_node *node; 12785cee9645SThomas Gleixner ktime_t basenow; 12795cee9645SThomas Gleixner 12805cee9645SThomas Gleixner basenow = ktime_add(now, base->offset); 12815cee9645SThomas Gleixner 12825cee9645SThomas Gleixner while ((node = timerqueue_getnext(&base->active))) { 12835cee9645SThomas Gleixner struct hrtimer *timer; 12845cee9645SThomas Gleixner 12855cee9645SThomas Gleixner timer = container_of(node, struct hrtimer, node); 12865cee9645SThomas Gleixner 12875cee9645SThomas Gleixner /* 12885cee9645SThomas Gleixner * The immediate goal for using the softexpires is 12895cee9645SThomas Gleixner * minimizing wakeups, not running timers at the 12905cee9645SThomas Gleixner * earliest interrupt after their soft expiration. 12915cee9645SThomas Gleixner * This allows us to avoid using a Priority Search 12925cee9645SThomas Gleixner * Tree, which can answer a stabbing querry for 12935cee9645SThomas Gleixner * overlapping intervals and instead use the simple 12945cee9645SThomas Gleixner * BST we already have. 12955cee9645SThomas Gleixner * We don't add extra wakeups by delaying timers that 12965cee9645SThomas Gleixner * are right-of a not yet expired timer, because that 12975cee9645SThomas Gleixner * timer will have to trigger a wakeup anyway. 12985cee9645SThomas Gleixner */ 12992456e855SThomas Gleixner if (basenow < hrtimer_get_softexpires_tv64(timer)) 13005cee9645SThomas Gleixner break; 13015cee9645SThomas Gleixner 1302dd934aa8SAnna-Maria Gleixner __run_hrtimer(cpu_base, base, timer, &basenow, flags); 13035cee9645SThomas Gleixner } 13045cee9645SThomas Gleixner } 130521d6d52aSThomas Gleixner } 130621d6d52aSThomas Gleixner 130721d6d52aSThomas Gleixner #ifdef CONFIG_HIGH_RES_TIMERS 130821d6d52aSThomas Gleixner 130921d6d52aSThomas Gleixner /* 131021d6d52aSThomas Gleixner * High resolution timer interrupt 131121d6d52aSThomas Gleixner * Called with interrupts disabled 131221d6d52aSThomas Gleixner */ 131321d6d52aSThomas Gleixner void hrtimer_interrupt(struct clock_event_device *dev) 131421d6d52aSThomas Gleixner { 131521d6d52aSThomas Gleixner struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); 131621d6d52aSThomas Gleixner ktime_t expires_next, now, entry_time, delta; 1317dd934aa8SAnna-Maria Gleixner unsigned long flags; 131821d6d52aSThomas Gleixner int retries = 0; 131921d6d52aSThomas Gleixner 132021d6d52aSThomas Gleixner BUG_ON(!cpu_base->hres_active); 132121d6d52aSThomas Gleixner cpu_base->nr_events++; 13222456e855SThomas Gleixner dev->next_event = KTIME_MAX; 132321d6d52aSThomas Gleixner 1324dd934aa8SAnna-Maria Gleixner raw_spin_lock_irqsave(&cpu_base->lock, flags); 132521d6d52aSThomas Gleixner entry_time = now = hrtimer_update_base(cpu_base); 132621d6d52aSThomas Gleixner retry: 132721d6d52aSThomas Gleixner cpu_base->in_hrtirq = 1; 132821d6d52aSThomas Gleixner /* 132921d6d52aSThomas Gleixner * We set expires_next to KTIME_MAX here with cpu_base->lock 133021d6d52aSThomas Gleixner * held to prevent that a timer is enqueued in our queue via 133121d6d52aSThomas Gleixner * the migration code. This does not affect enqueueing of 133221d6d52aSThomas Gleixner * timers which run their callback and need to be requeued on 133321d6d52aSThomas Gleixner * this CPU. 133421d6d52aSThomas Gleixner */ 13352456e855SThomas Gleixner cpu_base->expires_next = KTIME_MAX; 133621d6d52aSThomas Gleixner 1337*c458b1d1SAnna-Maria Gleixner __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD); 133821d6d52aSThomas Gleixner 13399bc74919SThomas Gleixner /* Reevaluate the clock bases for the next expiry */ 1340*c458b1d1SAnna-Maria Gleixner expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD); 13415cee9645SThomas Gleixner /* 13425cee9645SThomas Gleixner * Store the new expiry value so the migration code can verify 13435cee9645SThomas Gleixner * against it. 13445cee9645SThomas Gleixner */ 13455cee9645SThomas Gleixner cpu_base->expires_next = expires_next; 13469bc74919SThomas Gleixner cpu_base->in_hrtirq = 0; 1347dd934aa8SAnna-Maria Gleixner raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 13485cee9645SThomas Gleixner 13495cee9645SThomas Gleixner /* Reprogramming necessary ? */ 1350d2540875SViresh Kumar if (!tick_program_event(expires_next, 0)) { 13515cee9645SThomas Gleixner cpu_base->hang_detected = 0; 13525cee9645SThomas Gleixner return; 13535cee9645SThomas Gleixner } 13545cee9645SThomas Gleixner 13555cee9645SThomas Gleixner /* 13565cee9645SThomas Gleixner * The next timer was already expired due to: 13575cee9645SThomas Gleixner * - tracing 13585cee9645SThomas Gleixner * - long lasting callbacks 13595cee9645SThomas Gleixner * - being scheduled away when running in a VM 13605cee9645SThomas Gleixner * 13615cee9645SThomas Gleixner * We need to prevent that we loop forever in the hrtimer 13625cee9645SThomas Gleixner * interrupt routine. We give it 3 attempts to avoid 13635cee9645SThomas Gleixner * overreacting on some spurious event. 13645cee9645SThomas Gleixner * 13655cee9645SThomas Gleixner * Acquire base lock for updating the offsets and retrieving 13665cee9645SThomas Gleixner * the current time. 13675cee9645SThomas Gleixner */ 1368dd934aa8SAnna-Maria Gleixner raw_spin_lock_irqsave(&cpu_base->lock, flags); 13695cee9645SThomas Gleixner now = hrtimer_update_base(cpu_base); 13705cee9645SThomas Gleixner cpu_base->nr_retries++; 13715cee9645SThomas Gleixner if (++retries < 3) 13725cee9645SThomas Gleixner goto retry; 13735cee9645SThomas Gleixner /* 13745cee9645SThomas Gleixner * Give the system a chance to do something else than looping 13755cee9645SThomas Gleixner * here. We stored the entry time, so we know exactly how long 13765cee9645SThomas Gleixner * we spent here. We schedule the next event this amount of 13775cee9645SThomas Gleixner * time away. 13785cee9645SThomas Gleixner */ 13795cee9645SThomas Gleixner cpu_base->nr_hangs++; 13805cee9645SThomas Gleixner cpu_base->hang_detected = 1; 1381dd934aa8SAnna-Maria Gleixner raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 1382dd934aa8SAnna-Maria Gleixner 13835cee9645SThomas Gleixner delta = ktime_sub(now, entry_time); 13842456e855SThomas Gleixner if ((unsigned int)delta > cpu_base->max_hang_time) 13852456e855SThomas Gleixner cpu_base->max_hang_time = (unsigned int) delta; 13865cee9645SThomas Gleixner /* 13875cee9645SThomas Gleixner * Limit it to a sensible value as we enforce a longer 13885cee9645SThomas Gleixner * delay. Give the CPU at least 100ms to catch up. 13895cee9645SThomas Gleixner */ 13902456e855SThomas Gleixner if (delta > 100 * NSEC_PER_MSEC) 13915cee9645SThomas Gleixner expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC); 13925cee9645SThomas Gleixner else 13935cee9645SThomas Gleixner expires_next = ktime_add(now, delta); 13945cee9645SThomas Gleixner tick_program_event(expires_next, 1); 13955cee9645SThomas Gleixner printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n", 13965cee9645SThomas Gleixner ktime_to_ns(delta)); 13975cee9645SThomas Gleixner } 13985cee9645SThomas Gleixner 1399016da201SStephen Boyd /* called with interrupts disabled */ 1400c6eb3f70SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void) 14015cee9645SThomas Gleixner { 14025cee9645SThomas Gleixner struct tick_device *td; 14035cee9645SThomas Gleixner 14045cee9645SThomas Gleixner if (!hrtimer_hres_active()) 14055cee9645SThomas Gleixner return; 14065cee9645SThomas Gleixner 140722127e93SChristoph Lameter td = this_cpu_ptr(&tick_cpu_device); 14085cee9645SThomas Gleixner if (td && td->evtdev) 14095cee9645SThomas Gleixner hrtimer_interrupt(td->evtdev); 14105cee9645SThomas Gleixner } 14115cee9645SThomas Gleixner 14125cee9645SThomas Gleixner #else /* CONFIG_HIGH_RES_TIMERS */ 14135cee9645SThomas Gleixner 14145cee9645SThomas Gleixner static inline void __hrtimer_peek_ahead_timers(void) { } 14155cee9645SThomas Gleixner 14165cee9645SThomas Gleixner #endif /* !CONFIG_HIGH_RES_TIMERS */ 14175cee9645SThomas Gleixner 14185cee9645SThomas Gleixner /* 1419c6eb3f70SThomas Gleixner * Called from run_local_timers in hardirq context every jiffy 14205cee9645SThomas Gleixner */ 14215cee9645SThomas Gleixner void hrtimer_run_queues(void) 14225cee9645SThomas Gleixner { 1423dc5df73bSChristoph Lameter struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); 1424dd934aa8SAnna-Maria Gleixner unsigned long flags; 142521d6d52aSThomas Gleixner ktime_t now; 14265cee9645SThomas Gleixner 1427e19ffe8bSThomas Gleixner if (__hrtimer_hres_active(cpu_base)) 14285cee9645SThomas Gleixner return; 14295cee9645SThomas Gleixner 1430c6eb3f70SThomas Gleixner /* 1431c6eb3f70SThomas Gleixner * This _is_ ugly: We have to check periodically, whether we 1432c6eb3f70SThomas Gleixner * can switch to highres and / or nohz mode. The clocksource 1433c6eb3f70SThomas Gleixner * switch happens with xtime_lock held. Notification from 1434c6eb3f70SThomas Gleixner * there only sets the check bit in the tick_oneshot code, 1435c6eb3f70SThomas Gleixner * otherwise we might deadlock vs. xtime_lock. 1436c6eb3f70SThomas Gleixner */ 1437c6eb3f70SThomas Gleixner if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) { 1438c6eb3f70SThomas Gleixner hrtimer_switch_to_hres(); 1439c6eb3f70SThomas Gleixner return; 14405cee9645SThomas Gleixner } 14415cee9645SThomas Gleixner 1442dd934aa8SAnna-Maria Gleixner raw_spin_lock_irqsave(&cpu_base->lock, flags); 144321d6d52aSThomas Gleixner now = hrtimer_update_base(cpu_base); 1444*c458b1d1SAnna-Maria Gleixner __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD); 1445dd934aa8SAnna-Maria Gleixner raw_spin_unlock_irqrestore(&cpu_base->lock, flags); 14465cee9645SThomas Gleixner } 14475cee9645SThomas Gleixner 14485cee9645SThomas Gleixner /* 14495cee9645SThomas Gleixner * Sleep related functions: 14505cee9645SThomas Gleixner */ 14515cee9645SThomas Gleixner static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) 14525cee9645SThomas Gleixner { 14535cee9645SThomas Gleixner struct hrtimer_sleeper *t = 14545cee9645SThomas Gleixner container_of(timer, struct hrtimer_sleeper, timer); 14555cee9645SThomas Gleixner struct task_struct *task = t->task; 14565cee9645SThomas Gleixner 14575cee9645SThomas Gleixner t->task = NULL; 14585cee9645SThomas Gleixner if (task) 14595cee9645SThomas Gleixner wake_up_process(task); 14605cee9645SThomas Gleixner 14615cee9645SThomas Gleixner return HRTIMER_NORESTART; 14625cee9645SThomas Gleixner } 14635cee9645SThomas Gleixner 14645cee9645SThomas Gleixner void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) 14655cee9645SThomas Gleixner { 14665cee9645SThomas Gleixner sl->timer.function = hrtimer_wakeup; 14675cee9645SThomas Gleixner sl->task = task; 14685cee9645SThomas Gleixner } 14695cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); 14705cee9645SThomas Gleixner 1471c0edd7c9SDeepa Dinamani int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts) 1472ce41aaf4SAl Viro { 1473ce41aaf4SAl Viro switch(restart->nanosleep.type) { 1474ce41aaf4SAl Viro #ifdef CONFIG_COMPAT 1475ce41aaf4SAl Viro case TT_COMPAT: 1476c0edd7c9SDeepa Dinamani if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp)) 1477ce41aaf4SAl Viro return -EFAULT; 1478ce41aaf4SAl Viro break; 1479ce41aaf4SAl Viro #endif 1480ce41aaf4SAl Viro case TT_NATIVE: 1481c0edd7c9SDeepa Dinamani if (put_timespec64(ts, restart->nanosleep.rmtp)) 1482ce41aaf4SAl Viro return -EFAULT; 1483ce41aaf4SAl Viro break; 1484ce41aaf4SAl Viro default: 1485ce41aaf4SAl Viro BUG(); 1486ce41aaf4SAl Viro } 1487ce41aaf4SAl Viro return -ERESTART_RESTARTBLOCK; 1488ce41aaf4SAl Viro } 1489ce41aaf4SAl Viro 14905cee9645SThomas Gleixner static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) 14915cee9645SThomas Gleixner { 1492edbeda46SAl Viro struct restart_block *restart; 1493edbeda46SAl Viro 14945cee9645SThomas Gleixner hrtimer_init_sleeper(t, current); 14955cee9645SThomas Gleixner 14965cee9645SThomas Gleixner do { 14975cee9645SThomas Gleixner set_current_state(TASK_INTERRUPTIBLE); 14985cee9645SThomas Gleixner hrtimer_start_expires(&t->timer, mode); 14995cee9645SThomas Gleixner 15005cee9645SThomas Gleixner if (likely(t->task)) 15015cee9645SThomas Gleixner freezable_schedule(); 15025cee9645SThomas Gleixner 15035cee9645SThomas Gleixner hrtimer_cancel(&t->timer); 15045cee9645SThomas Gleixner mode = HRTIMER_MODE_ABS; 15055cee9645SThomas Gleixner 15065cee9645SThomas Gleixner } while (t->task && !signal_pending(current)); 15075cee9645SThomas Gleixner 15085cee9645SThomas Gleixner __set_current_state(TASK_RUNNING); 15095cee9645SThomas Gleixner 1510a7602681SAl Viro if (!t->task) 1511a7602681SAl Viro return 0; 15125cee9645SThomas Gleixner 1513edbeda46SAl Viro restart = ¤t->restart_block; 1514edbeda46SAl Viro if (restart->nanosleep.type != TT_NONE) { 1515a7602681SAl Viro ktime_t rem = hrtimer_expires_remaining(&t->timer); 1516c0edd7c9SDeepa Dinamani struct timespec64 rmt; 1517edbeda46SAl Viro 15182456e855SThomas Gleixner if (rem <= 0) 15195cee9645SThomas Gleixner return 0; 1520c0edd7c9SDeepa Dinamani rmt = ktime_to_timespec64(rem); 15215cee9645SThomas Gleixner 1522ce41aaf4SAl Viro return nanosleep_copyout(restart, &rmt); 1523a7602681SAl Viro } 1524a7602681SAl Viro return -ERESTART_RESTARTBLOCK; 15255cee9645SThomas Gleixner } 15265cee9645SThomas Gleixner 1527fb923c4aSAl Viro static long __sched hrtimer_nanosleep_restart(struct restart_block *restart) 15285cee9645SThomas Gleixner { 15295cee9645SThomas Gleixner struct hrtimer_sleeper t; 1530a7602681SAl Viro int ret; 15315cee9645SThomas Gleixner 15325cee9645SThomas Gleixner hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid, 15335cee9645SThomas Gleixner HRTIMER_MODE_ABS); 15345cee9645SThomas Gleixner hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); 15355cee9645SThomas Gleixner 1536a7602681SAl Viro ret = do_nanosleep(&t, HRTIMER_MODE_ABS); 15375cee9645SThomas Gleixner destroy_hrtimer_on_stack(&t.timer); 15385cee9645SThomas Gleixner return ret; 15395cee9645SThomas Gleixner } 15405cee9645SThomas Gleixner 1541938e7cf2SThomas Gleixner long hrtimer_nanosleep(const struct timespec64 *rqtp, 15425cee9645SThomas Gleixner const enum hrtimer_mode mode, const clockid_t clockid) 15435cee9645SThomas Gleixner { 1544a7602681SAl Viro struct restart_block *restart; 15455cee9645SThomas Gleixner struct hrtimer_sleeper t; 15465cee9645SThomas Gleixner int ret = 0; 1547da8b44d5SJohn Stultz u64 slack; 15485cee9645SThomas Gleixner 15495cee9645SThomas Gleixner slack = current->timer_slack_ns; 15505cee9645SThomas Gleixner if (dl_task(current) || rt_task(current)) 15515cee9645SThomas Gleixner slack = 0; 15525cee9645SThomas Gleixner 15535cee9645SThomas Gleixner hrtimer_init_on_stack(&t.timer, clockid, mode); 1554ad196384SDeepa Dinamani hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); 1555a7602681SAl Viro ret = do_nanosleep(&t, mode); 1556a7602681SAl Viro if (ret != -ERESTART_RESTARTBLOCK) 15575cee9645SThomas Gleixner goto out; 15585cee9645SThomas Gleixner 15595cee9645SThomas Gleixner /* Absolute timers do not update the rmtp value and restart: */ 15605cee9645SThomas Gleixner if (mode == HRTIMER_MODE_ABS) { 15615cee9645SThomas Gleixner ret = -ERESTARTNOHAND; 15625cee9645SThomas Gleixner goto out; 15635cee9645SThomas Gleixner } 15645cee9645SThomas Gleixner 1565a7602681SAl Viro restart = ¤t->restart_block; 15665cee9645SThomas Gleixner restart->fn = hrtimer_nanosleep_restart; 15675cee9645SThomas Gleixner restart->nanosleep.clockid = t.timer.base->clockid; 15685cee9645SThomas Gleixner restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); 15695cee9645SThomas Gleixner out: 15705cee9645SThomas Gleixner destroy_hrtimer_on_stack(&t.timer); 15715cee9645SThomas Gleixner return ret; 15725cee9645SThomas Gleixner } 15735cee9645SThomas Gleixner 15745cee9645SThomas Gleixner SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp, 15755cee9645SThomas Gleixner struct timespec __user *, rmtp) 15765cee9645SThomas Gleixner { 1577c0edd7c9SDeepa Dinamani struct timespec64 tu; 15785cee9645SThomas Gleixner 1579c0edd7c9SDeepa Dinamani if (get_timespec64(&tu, rqtp)) 15805cee9645SThomas Gleixner return -EFAULT; 15815cee9645SThomas Gleixner 1582c0edd7c9SDeepa Dinamani if (!timespec64_valid(&tu)) 15835cee9645SThomas Gleixner return -EINVAL; 15845cee9645SThomas Gleixner 1585edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; 1586192a82f9SAl Viro current->restart_block.nanosleep.rmtp = rmtp; 1587c0edd7c9SDeepa Dinamani return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); 15885cee9645SThomas Gleixner } 15895cee9645SThomas Gleixner 1590edbeda46SAl Viro #ifdef CONFIG_COMPAT 1591edbeda46SAl Viro 1592edbeda46SAl Viro COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp, 1593edbeda46SAl Viro struct compat_timespec __user *, rmtp) 1594edbeda46SAl Viro { 1595c0edd7c9SDeepa Dinamani struct timespec64 tu; 1596edbeda46SAl Viro 1597c0edd7c9SDeepa Dinamani if (compat_get_timespec64(&tu, rqtp)) 1598edbeda46SAl Viro return -EFAULT; 1599edbeda46SAl Viro 1600c0edd7c9SDeepa Dinamani if (!timespec64_valid(&tu)) 1601edbeda46SAl Viro return -EINVAL; 1602edbeda46SAl Viro 1603edbeda46SAl Viro current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; 1604edbeda46SAl Viro current->restart_block.nanosleep.compat_rmtp = rmtp; 1605c0edd7c9SDeepa Dinamani return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); 1606edbeda46SAl Viro } 1607edbeda46SAl Viro #endif 1608edbeda46SAl Viro 16095cee9645SThomas Gleixner /* 16105cee9645SThomas Gleixner * Functions related to boot-time initialization: 16115cee9645SThomas Gleixner */ 161227590dc1SThomas Gleixner int hrtimers_prepare_cpu(unsigned int cpu) 16135cee9645SThomas Gleixner { 16145cee9645SThomas Gleixner struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); 16155cee9645SThomas Gleixner int i; 16165cee9645SThomas Gleixner 16175cee9645SThomas Gleixner for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { 16185cee9645SThomas Gleixner cpu_base->clock_base[i].cpu_base = cpu_base; 16195cee9645SThomas Gleixner timerqueue_init_head(&cpu_base->clock_base[i].active); 16205cee9645SThomas Gleixner } 16215cee9645SThomas Gleixner 1622cddd0248SViresh Kumar cpu_base->cpu = cpu; 162328bfd18bSAnna-Maria Gleixner cpu_base->hres_active = 0; 162407a9a7eaSAnna-Maria Gleixner cpu_base->expires_next = KTIME_MAX; 162527590dc1SThomas Gleixner return 0; 16265cee9645SThomas Gleixner } 16275cee9645SThomas Gleixner 16285cee9645SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 16295cee9645SThomas Gleixner 16305cee9645SThomas Gleixner static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, 16315cee9645SThomas Gleixner struct hrtimer_clock_base *new_base) 16325cee9645SThomas Gleixner { 16335cee9645SThomas Gleixner struct hrtimer *timer; 16345cee9645SThomas Gleixner struct timerqueue_node *node; 16355cee9645SThomas Gleixner 16365cee9645SThomas Gleixner while ((node = timerqueue_getnext(&old_base->active))) { 16375cee9645SThomas Gleixner timer = container_of(node, struct hrtimer, node); 16385cee9645SThomas Gleixner BUG_ON(hrtimer_callback_running(timer)); 16395cee9645SThomas Gleixner debug_deactivate(timer); 16405cee9645SThomas Gleixner 16415cee9645SThomas Gleixner /* 1642c04dca02SOleg Nesterov * Mark it as ENQUEUED not INACTIVE otherwise the 16435cee9645SThomas Gleixner * timer could be seen as !active and just vanish away 16445cee9645SThomas Gleixner * under us on another CPU 16455cee9645SThomas Gleixner */ 1646c04dca02SOleg Nesterov __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0); 16475cee9645SThomas Gleixner timer->base = new_base; 16485cee9645SThomas Gleixner /* 16495cee9645SThomas Gleixner * Enqueue the timers on the new cpu. This does not 16505cee9645SThomas Gleixner * reprogram the event device in case the timer 16515cee9645SThomas Gleixner * expires before the earliest on this CPU, but we run 16525cee9645SThomas Gleixner * hrtimer_interrupt after we migrated everything to 16535cee9645SThomas Gleixner * sort out already expired timers and reprogram the 16545cee9645SThomas Gleixner * event device. 16555cee9645SThomas Gleixner */ 165663e2ed36SAnna-Maria Gleixner enqueue_hrtimer(timer, new_base, HRTIMER_MODE_ABS); 16575cee9645SThomas Gleixner } 16585cee9645SThomas Gleixner } 16595cee9645SThomas Gleixner 166027590dc1SThomas Gleixner int hrtimers_dead_cpu(unsigned int scpu) 16615cee9645SThomas Gleixner { 16625cee9645SThomas Gleixner struct hrtimer_cpu_base *old_base, *new_base; 16635cee9645SThomas Gleixner int i; 16645cee9645SThomas Gleixner 16655cee9645SThomas Gleixner BUG_ON(cpu_online(scpu)); 16665cee9645SThomas Gleixner tick_cancel_sched_timer(scpu); 16675cee9645SThomas Gleixner 16685cee9645SThomas Gleixner local_irq_disable(); 16695cee9645SThomas Gleixner old_base = &per_cpu(hrtimer_bases, scpu); 1670dc5df73bSChristoph Lameter new_base = this_cpu_ptr(&hrtimer_bases); 16715cee9645SThomas Gleixner /* 16725cee9645SThomas Gleixner * The caller is globally serialized and nobody else 16735cee9645SThomas Gleixner * takes two locks at once, deadlock is not possible. 16745cee9645SThomas Gleixner */ 16755cee9645SThomas Gleixner raw_spin_lock(&new_base->lock); 16765cee9645SThomas Gleixner raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); 16775cee9645SThomas Gleixner 16785cee9645SThomas Gleixner for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { 16795cee9645SThomas Gleixner migrate_hrtimer_list(&old_base->clock_base[i], 16805cee9645SThomas Gleixner &new_base->clock_base[i]); 16815cee9645SThomas Gleixner } 16825cee9645SThomas Gleixner 16835cee9645SThomas Gleixner raw_spin_unlock(&old_base->lock); 16845cee9645SThomas Gleixner raw_spin_unlock(&new_base->lock); 16855cee9645SThomas Gleixner 16865cee9645SThomas Gleixner /* Check, if we got expired work to do */ 16875cee9645SThomas Gleixner __hrtimer_peek_ahead_timers(); 16885cee9645SThomas Gleixner local_irq_enable(); 168927590dc1SThomas Gleixner return 0; 16905cee9645SThomas Gleixner } 16915cee9645SThomas Gleixner 16925cee9645SThomas Gleixner #endif /* CONFIG_HOTPLUG_CPU */ 16935cee9645SThomas Gleixner 16945cee9645SThomas Gleixner void __init hrtimers_init(void) 16955cee9645SThomas Gleixner { 169627590dc1SThomas Gleixner hrtimers_prepare_cpu(smp_processor_id()); 16975cee9645SThomas Gleixner } 16985cee9645SThomas Gleixner 16995cee9645SThomas Gleixner /** 17005cee9645SThomas Gleixner * schedule_hrtimeout_range_clock - sleep until timeout 17015cee9645SThomas Gleixner * @expires: timeout value (ktime_t) 17025cee9645SThomas Gleixner * @delta: slack in expires timeout (ktime_t) 170390777713SAnna-Maria Gleixner * @mode: timer mode 170490777713SAnna-Maria Gleixner * @clock_id: timer clock to be used 17055cee9645SThomas Gleixner */ 17065cee9645SThomas Gleixner int __sched 1707da8b44d5SJohn Stultz schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta, 170890777713SAnna-Maria Gleixner const enum hrtimer_mode mode, clockid_t clock_id) 17095cee9645SThomas Gleixner { 17105cee9645SThomas Gleixner struct hrtimer_sleeper t; 17115cee9645SThomas Gleixner 17125cee9645SThomas Gleixner /* 17135cee9645SThomas Gleixner * Optimize when a zero timeout value is given. It does not 17145cee9645SThomas Gleixner * matter whether this is an absolute or a relative time. 17155cee9645SThomas Gleixner */ 17162456e855SThomas Gleixner if (expires && *expires == 0) { 17175cee9645SThomas Gleixner __set_current_state(TASK_RUNNING); 17185cee9645SThomas Gleixner return 0; 17195cee9645SThomas Gleixner } 17205cee9645SThomas Gleixner 17215cee9645SThomas Gleixner /* 17225cee9645SThomas Gleixner * A NULL parameter means "infinite" 17235cee9645SThomas Gleixner */ 17245cee9645SThomas Gleixner if (!expires) { 17255cee9645SThomas Gleixner schedule(); 17265cee9645SThomas Gleixner return -EINTR; 17275cee9645SThomas Gleixner } 17285cee9645SThomas Gleixner 172990777713SAnna-Maria Gleixner hrtimer_init_on_stack(&t.timer, clock_id, mode); 17305cee9645SThomas Gleixner hrtimer_set_expires_range_ns(&t.timer, *expires, delta); 17315cee9645SThomas Gleixner 17325cee9645SThomas Gleixner hrtimer_init_sleeper(&t, current); 17335cee9645SThomas Gleixner 17345cee9645SThomas Gleixner hrtimer_start_expires(&t.timer, mode); 17355cee9645SThomas Gleixner 17365cee9645SThomas Gleixner if (likely(t.task)) 17375cee9645SThomas Gleixner schedule(); 17385cee9645SThomas Gleixner 17395cee9645SThomas Gleixner hrtimer_cancel(&t.timer); 17405cee9645SThomas Gleixner destroy_hrtimer_on_stack(&t.timer); 17415cee9645SThomas Gleixner 17425cee9645SThomas Gleixner __set_current_state(TASK_RUNNING); 17435cee9645SThomas Gleixner 17445cee9645SThomas Gleixner return !t.task ? 0 : -EINTR; 17455cee9645SThomas Gleixner } 17465cee9645SThomas Gleixner 17475cee9645SThomas Gleixner /** 17485cee9645SThomas Gleixner * schedule_hrtimeout_range - sleep until timeout 17495cee9645SThomas Gleixner * @expires: timeout value (ktime_t) 17505cee9645SThomas Gleixner * @delta: slack in expires timeout (ktime_t) 175190777713SAnna-Maria Gleixner * @mode: timer mode 17525cee9645SThomas Gleixner * 17535cee9645SThomas Gleixner * Make the current task sleep until the given expiry time has 17545cee9645SThomas Gleixner * elapsed. The routine will return immediately unless 17555cee9645SThomas Gleixner * the current task state has been set (see set_current_state()). 17565cee9645SThomas Gleixner * 17575cee9645SThomas Gleixner * The @delta argument gives the kernel the freedom to schedule the 17585cee9645SThomas Gleixner * actual wakeup to a time that is both power and performance friendly. 17595cee9645SThomas Gleixner * The kernel give the normal best effort behavior for "@expires+@delta", 17605cee9645SThomas Gleixner * but may decide to fire the timer earlier, but no earlier than @expires. 17615cee9645SThomas Gleixner * 17625cee9645SThomas Gleixner * You can set the task state as follows - 17635cee9645SThomas Gleixner * 17645cee9645SThomas Gleixner * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to 17654b7e9cf9SDouglas Anderson * pass before the routine returns unless the current task is explicitly 17664b7e9cf9SDouglas Anderson * woken up, (e.g. by wake_up_process()). 17675cee9645SThomas Gleixner * 17685cee9645SThomas Gleixner * %TASK_INTERRUPTIBLE - the routine may return early if a signal is 17694b7e9cf9SDouglas Anderson * delivered to the current task or the current task is explicitly woken 17704b7e9cf9SDouglas Anderson * up. 17715cee9645SThomas Gleixner * 17725cee9645SThomas Gleixner * The current task state is guaranteed to be TASK_RUNNING when this 17735cee9645SThomas Gleixner * routine returns. 17745cee9645SThomas Gleixner * 17754b7e9cf9SDouglas Anderson * Returns 0 when the timer has expired. If the task was woken before the 17764b7e9cf9SDouglas Anderson * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or 17774b7e9cf9SDouglas Anderson * by an explicit wakeup, it returns -EINTR. 17785cee9645SThomas Gleixner */ 1779da8b44d5SJohn Stultz int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta, 17805cee9645SThomas Gleixner const enum hrtimer_mode mode) 17815cee9645SThomas Gleixner { 17825cee9645SThomas Gleixner return schedule_hrtimeout_range_clock(expires, delta, mode, 17835cee9645SThomas Gleixner CLOCK_MONOTONIC); 17845cee9645SThomas Gleixner } 17855cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout_range); 17865cee9645SThomas Gleixner 17875cee9645SThomas Gleixner /** 17885cee9645SThomas Gleixner * schedule_hrtimeout - sleep until timeout 17895cee9645SThomas Gleixner * @expires: timeout value (ktime_t) 179090777713SAnna-Maria Gleixner * @mode: timer mode 17915cee9645SThomas Gleixner * 17925cee9645SThomas Gleixner * Make the current task sleep until the given expiry time has 17935cee9645SThomas Gleixner * elapsed. The routine will return immediately unless 17945cee9645SThomas Gleixner * the current task state has been set (see set_current_state()). 17955cee9645SThomas Gleixner * 17965cee9645SThomas Gleixner * You can set the task state as follows - 17975cee9645SThomas Gleixner * 17985cee9645SThomas Gleixner * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to 17994b7e9cf9SDouglas Anderson * pass before the routine returns unless the current task is explicitly 18004b7e9cf9SDouglas Anderson * woken up, (e.g. by wake_up_process()). 18015cee9645SThomas Gleixner * 18025cee9645SThomas Gleixner * %TASK_INTERRUPTIBLE - the routine may return early if a signal is 18034b7e9cf9SDouglas Anderson * delivered to the current task or the current task is explicitly woken 18044b7e9cf9SDouglas Anderson * up. 18055cee9645SThomas Gleixner * 18065cee9645SThomas Gleixner * The current task state is guaranteed to be TASK_RUNNING when this 18075cee9645SThomas Gleixner * routine returns. 18085cee9645SThomas Gleixner * 18094b7e9cf9SDouglas Anderson * Returns 0 when the timer has expired. If the task was woken before the 18104b7e9cf9SDouglas Anderson * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or 18114b7e9cf9SDouglas Anderson * by an explicit wakeup, it returns -EINTR. 18125cee9645SThomas Gleixner */ 18135cee9645SThomas Gleixner int __sched schedule_hrtimeout(ktime_t *expires, 18145cee9645SThomas Gleixner const enum hrtimer_mode mode) 18155cee9645SThomas Gleixner { 18165cee9645SThomas Gleixner return schedule_hrtimeout_range(expires, 0, mode); 18175cee9645SThomas Gleixner } 18185cee9645SThomas Gleixner EXPORT_SYMBOL_GPL(schedule_hrtimeout); 1819