Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/hrtimer.c |
| 3 | * |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 4 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 5 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 6 | * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 7 | * |
| 8 | * High-resolution kernel timers |
| 9 | * |
| 10 | * In contrast to the low-resolution timeout API implemented in |
| 11 | * kernel/timer.c, hrtimers provide finer resolution and accuracy |
| 12 | * depending on system configuration and capabilities. |
| 13 | * |
| 14 | * These timers are currently used for: |
| 15 | * - itimers |
| 16 | * - POSIX timers |
| 17 | * - nanosleep |
| 18 | * - precise in-kernel timing |
| 19 | * |
| 20 | * Started by: Thomas Gleixner and Ingo Molnar |
| 21 | * |
| 22 | * Credits: |
| 23 | * based on kernel/timer.c |
| 24 | * |
Thomas Gleixner | 66188fa | 2006-02-01 03:05:13 -0800 | [diff] [blame] | 25 | * Help, testing, suggestions, bugfixes, improvements were |
| 26 | * provided by: |
| 27 | * |
| 28 | * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel |
| 29 | * et. al. |
| 30 | * |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 31 | * For licencing details see kernel-base/COPYING |
| 32 | */ |
| 33 | |
| 34 | #include <linux/cpu.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 35 | #include <linux/export.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 36 | #include <linux/percpu.h> |
| 37 | #include <linux/hrtimer.h> |
| 38 | #include <linux/notifier.h> |
| 39 | #include <linux/syscalls.h> |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 40 | #include <linux/kallsyms.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 41 | #include <linux/interrupt.h> |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 42 | #include <linux/tick.h> |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 43 | #include <linux/seq_file.h> |
| 44 | #include <linux/err.h> |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 45 | #include <linux/debugobjects.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 46 | #include <linux/sched/signal.h> |
Clark Williams | cf4aebc2 | 2013-02-07 09:46:59 -0600 | [diff] [blame] | 47 | #include <linux/sched/sysctl.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 48 | #include <linux/sched/rt.h> |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 49 | #include <linux/sched/deadline.h> |
Ingo Molnar | 370c913 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 50 | #include <linux/sched/nohz.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 51 | #include <linux/sched/debug.h> |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 52 | #include <linux/timer.h> |
Colin Cross | b0f8c44 | 2013-05-06 23:50:19 +0000 | [diff] [blame] | 53 | #include <linux/freezer.h> |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 54 | #include <linux/compat.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 55 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 56 | #include <linux/uaccess.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 57 | |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 58 | #include <trace/events/timer.h> |
| 59 | |
Thomas Gleixner | c1797ba | 2015-03-25 13:07:37 +0100 | [diff] [blame] | 60 | #include "tick-internal.h" |
Thomas Gleixner | 8b094cd | 2014-07-16 21:04:02 +0000 | [diff] [blame] | 61 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 62 | /* |
| 63 | * The timer bases: |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 64 | * |
Zhen Lei | 571af55 | 2015-08-25 14:42:53 +0800 | [diff] [blame] | 65 | * There are more clockids than hrtimer bases. Thus, we index |
John Stultz | e06383d | 2010-12-14 19:37:07 -0800 | [diff] [blame] | 66 | * into the timer bases by the hrtimer_base_type enum. When trying |
| 67 | * to reach a base using a clockid, hrtimer_clockid_to_base() |
| 68 | * is used to convert from clockid to the proper hrtimer_base_type. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 69 | */ |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 70 | DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 71 | { |
Michael Bohan | 84cc8fd | 2013-03-19 19:19:25 -0700 | [diff] [blame] | 72 | .lock = __RAW_SPIN_LOCK_UNLOCKED(hrtimer_bases.lock), |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 73 | .seq = SEQCNT_ZERO(hrtimer_bases.seq), |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 74 | .clock_base = |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 75 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 76 | { |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 77 | .index = HRTIMER_BASE_MONOTONIC, |
| 78 | .clockid = CLOCK_MONOTONIC, |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 79 | .get_time = &ktime_get, |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 80 | }, |
John Stultz | 70a08cc | 2011-02-15 10:45:16 -0800 | [diff] [blame] | 81 | { |
Thomas Gleixner | 68fa61c | 2011-05-20 23:14:04 +0200 | [diff] [blame] | 82 | .index = HRTIMER_BASE_REALTIME, |
| 83 | .clockid = CLOCK_REALTIME, |
| 84 | .get_time = &ktime_get_real, |
Thomas Gleixner | 68fa61c | 2011-05-20 23:14:04 +0200 | [diff] [blame] | 85 | }, |
| 86 | { |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 87 | .index = HRTIMER_BASE_BOOTTIME, |
| 88 | .clockid = CLOCK_BOOTTIME, |
John Stultz | 70a08cc | 2011-02-15 10:45:16 -0800 | [diff] [blame] | 89 | .get_time = &ktime_get_boottime, |
John Stultz | 70a08cc | 2011-02-15 10:45:16 -0800 | [diff] [blame] | 90 | }, |
John Stultz | 90adda9 | 2013-01-21 17:00:11 -0800 | [diff] [blame] | 91 | { |
| 92 | .index = HRTIMER_BASE_TAI, |
| 93 | .clockid = CLOCK_TAI, |
| 94 | .get_time = &ktime_get_clocktai, |
John Stultz | 90adda9 | 2013-01-21 17:00:11 -0800 | [diff] [blame] | 95 | }, |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 96 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Mike Frysinger | 942c3c5 | 2011-05-02 15:24:27 -0400 | [diff] [blame] | 99 | static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = { |
Marc Zyngier | 336a9cd | 2016-01-15 17:41:09 +0000 | [diff] [blame] | 100 | /* Make sure we catch unsupported clockids */ |
| 101 | [0 ... MAX_CLOCKS - 1] = HRTIMER_MAX_CLOCK_BASES, |
| 102 | |
Thomas Gleixner | ce31332 | 2011-04-29 00:02:00 +0200 | [diff] [blame] | 103 | [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME, |
| 104 | [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC, |
| 105 | [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME, |
John Stultz | 90adda9 | 2013-01-21 17:00:11 -0800 | [diff] [blame] | 106 | [CLOCK_TAI] = HRTIMER_BASE_TAI, |
Thomas Gleixner | ce31332 | 2011-04-29 00:02:00 +0200 | [diff] [blame] | 107 | }; |
John Stultz | e06383d | 2010-12-14 19:37:07 -0800 | [diff] [blame] | 108 | |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 109 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 110 | * Functions and macros which are different for UP/SMP systems are kept in a |
| 111 | * single place |
| 112 | */ |
| 113 | #ifdef CONFIG_SMP |
| 114 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 115 | /* |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 116 | * We require the migration_base for lock_hrtimer_base()/switch_hrtimer_base() |
| 117 | * such that hrtimer_callback_running() can unconditionally dereference |
| 118 | * timer->base->cpu_base |
| 119 | */ |
| 120 | static struct hrtimer_cpu_base migration_cpu_base = { |
| 121 | .seq = SEQCNT_ZERO(migration_cpu_base), |
| 122 | .clock_base = { { .cpu_base = &migration_cpu_base, }, }, |
| 123 | }; |
| 124 | |
| 125 | #define migration_base migration_cpu_base.clock_base[0] |
| 126 | |
| 127 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 128 | * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock |
| 129 | * means that all timers which are tied to this base via timer->base are |
| 130 | * locked, and the base itself is locked too. |
| 131 | * |
| 132 | * So __run_timers/migrate_timers can safely modify all timers which could |
| 133 | * be found on the lists/queues. |
| 134 | * |
| 135 | * When the timer's base is locked, and the timer removed from list, it is |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 136 | * possible to set timer->base = &migration_base and drop the lock: the timer |
| 137 | * remains locked. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 138 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 139 | static |
| 140 | struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, |
| 141 | unsigned long *flags) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 142 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 143 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 144 | |
| 145 | for (;;) { |
| 146 | base = timer->base; |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 147 | if (likely(base != &migration_base)) { |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 148 | raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 149 | if (likely(base == timer->base)) |
| 150 | return base; |
| 151 | /* The timer has migrated to another CPU: */ |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 152 | raw_spin_unlock_irqrestore(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 153 | } |
| 154 | cpu_relax(); |
| 155 | } |
| 156 | } |
| 157 | |
Thomas Gleixner | 6ff7041 | 2009-07-10 14:57:05 +0200 | [diff] [blame] | 158 | /* |
| 159 | * With HIGHRES=y we do not migrate the timer when it is expiring |
| 160 | * before the next event on the target cpu because we cannot reprogram |
| 161 | * the target cpu hardware and we would cause it to fire late. |
| 162 | * |
| 163 | * Called with cpu_base->lock of target cpu held. |
| 164 | */ |
| 165 | static int |
| 166 | hrtimer_check_target(struct hrtimer *timer, struct hrtimer_clock_base *new_base) |
| 167 | { |
| 168 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 169 | ktime_t expires; |
| 170 | |
| 171 | if (!new_base->cpu_base->hres_active) |
| 172 | return 0; |
| 173 | |
| 174 | expires = ktime_sub(hrtimer_get_expires(timer), new_base->offset); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 175 | return expires <= new_base->cpu_base->expires_next; |
Thomas Gleixner | 6ff7041 | 2009-07-10 14:57:05 +0200 | [diff] [blame] | 176 | #else |
| 177 | return 0; |
| 178 | #endif |
| 179 | } |
| 180 | |
Thomas Gleixner | bc7a34b | 2015-05-26 22:50:33 +0000 | [diff] [blame] | 181 | static inline |
| 182 | struct hrtimer_cpu_base *get_target_base(struct hrtimer_cpu_base *base, |
| 183 | int pinned) |
| 184 | { |
Thomas Gleixner | ae67bad | 2018-01-14 23:30:51 +0100 | [diff] [blame] | 185 | #if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ_COMMON) |
| 186 | if (static_branch_likely(&timers_migration_enabled) && !pinned) |
| 187 | return &per_cpu(hrtimer_bases, get_nohz_timer_target()); |
| 188 | #endif |
Frederic Weisbecker | 662b3e1 | 2015-08-18 16:18:28 +0200 | [diff] [blame] | 189 | return base; |
Thomas Gleixner | bc7a34b | 2015-05-26 22:50:33 +0000 | [diff] [blame] | 190 | } |
Thomas Gleixner | bc7a34b | 2015-05-26 22:50:33 +0000 | [diff] [blame] | 191 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 192 | /* |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 193 | * We switch the timer base to a power-optimized selected CPU target, |
| 194 | * if: |
| 195 | * - NO_HZ_COMMON is enabled |
| 196 | * - timer migration is enabled |
| 197 | * - the timer callback is not running |
| 198 | * - the timer is not the first expiring timer on the new target |
| 199 | * |
| 200 | * If one of the above requirements is not fulfilled we move the timer |
| 201 | * to the current CPU or leave it on the previously assigned CPU if |
| 202 | * the timer callback is currently running. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 203 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 204 | static inline struct hrtimer_clock_base * |
Arun R Bharadwaj | 597d027 | 2009-04-16 12:13:26 +0530 | [diff] [blame] | 205 | switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base, |
| 206 | int pinned) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 207 | { |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 208 | struct hrtimer_cpu_base *new_cpu_base, *this_cpu_base; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 209 | struct hrtimer_clock_base *new_base; |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 210 | int basenum = base->index; |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 211 | |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 212 | this_cpu_base = this_cpu_ptr(&hrtimer_bases); |
| 213 | new_cpu_base = get_target_base(this_cpu_base, pinned); |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 214 | again: |
John Stultz | e06383d | 2010-12-14 19:37:07 -0800 | [diff] [blame] | 215 | new_base = &new_cpu_base->clock_base[basenum]; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 216 | |
| 217 | if (base != new_base) { |
| 218 | /* |
Thomas Gleixner | 6ff7041 | 2009-07-10 14:57:05 +0200 | [diff] [blame] | 219 | * We are trying to move timer to new_base. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 220 | * However we can't change timer's base while it is running, |
| 221 | * so we keep it on the same CPU. No hassle vs. reprogramming |
| 222 | * the event source in the high resolution case. The softirq |
| 223 | * code will take care of this when the timer function has |
| 224 | * completed. There is no conflict as we hold the lock until |
| 225 | * the timer is enqueued. |
| 226 | */ |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 227 | if (unlikely(hrtimer_callback_running(timer))) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 228 | return base; |
| 229 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 230 | /* See the comment in lock_hrtimer_base() */ |
| 231 | timer->base = &migration_base; |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 232 | raw_spin_unlock(&base->cpu_base->lock); |
| 233 | raw_spin_lock(&new_base->cpu_base->lock); |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 234 | |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 235 | if (new_cpu_base != this_cpu_base && |
Thomas Gleixner | bc7a34b | 2015-05-26 22:50:33 +0000 | [diff] [blame] | 236 | hrtimer_check_target(timer, new_base)) { |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 237 | raw_spin_unlock(&new_base->cpu_base->lock); |
| 238 | raw_spin_lock(&base->cpu_base->lock); |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 239 | new_cpu_base = this_cpu_base; |
Thomas Gleixner | 6ff7041 | 2009-07-10 14:57:05 +0200 | [diff] [blame] | 240 | timer->base = base; |
| 241 | goto again; |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 242 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 243 | timer->base = new_base; |
Leon Ma | 012a45e | 2014-04-30 16:43:10 +0800 | [diff] [blame] | 244 | } else { |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 245 | if (new_cpu_base != this_cpu_base && |
Thomas Gleixner | bc7a34b | 2015-05-26 22:50:33 +0000 | [diff] [blame] | 246 | hrtimer_check_target(timer, new_base)) { |
Frederic Weisbecker | b48362d | 2015-08-18 16:18:29 +0200 | [diff] [blame] | 247 | new_cpu_base = this_cpu_base; |
Leon Ma | 012a45e | 2014-04-30 16:43:10 +0800 | [diff] [blame] | 248 | goto again; |
| 249 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 250 | } |
| 251 | return new_base; |
| 252 | } |
| 253 | |
| 254 | #else /* CONFIG_SMP */ |
| 255 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 256 | static inline struct hrtimer_clock_base * |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 257 | lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 258 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 259 | struct hrtimer_clock_base *base = timer->base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 260 | |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 261 | raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 262 | |
| 263 | return base; |
| 264 | } |
| 265 | |
Arun R Bharadwaj | eea08f3 | 2009-04-16 12:16:41 +0530 | [diff] [blame] | 266 | # define switch_hrtimer_base(t, b, p) (b) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 267 | |
| 268 | #endif /* !CONFIG_SMP */ |
| 269 | |
| 270 | /* |
| 271 | * Functions for the union type storage format of ktime_t which are |
| 272 | * too large for inlining: |
| 273 | */ |
| 274 | #if BITS_PER_LONG < 64 |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 275 | /* |
| 276 | * Divide a ktime value by a nanosecond value |
| 277 | */ |
John Stultz | f7bcb70 | 2015-05-08 13:47:23 -0700 | [diff] [blame] | 278 | s64 __ktime_divns(const ktime_t kt, s64 div) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 279 | { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 280 | int sft = 0; |
John Stultz | f7bcb70 | 2015-05-08 13:47:23 -0700 | [diff] [blame] | 281 | s64 dclc; |
| 282 | u64 tmp; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 283 | |
Carlos R. Mafra | 900cfa4 | 2008-05-22 19:25:11 -0300 | [diff] [blame] | 284 | dclc = ktime_to_ns(kt); |
John Stultz | f7bcb70 | 2015-05-08 13:47:23 -0700 | [diff] [blame] | 285 | tmp = dclc < 0 ? -dclc : dclc; |
| 286 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 287 | /* Make sure the divisor is less than 2^32: */ |
| 288 | while (div >> 32) { |
| 289 | sft++; |
| 290 | div >>= 1; |
| 291 | } |
John Stultz | f7bcb70 | 2015-05-08 13:47:23 -0700 | [diff] [blame] | 292 | tmp >>= sft; |
| 293 | do_div(tmp, (unsigned long) div); |
| 294 | return dclc < 0 ? -tmp : tmp; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 295 | } |
Nicolas Pitre | 8b61862 | 2014-12-03 14:43:06 -0500 | [diff] [blame] | 296 | EXPORT_SYMBOL_GPL(__ktime_divns); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 297 | #endif /* BITS_PER_LONG >= 64 */ |
| 298 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 299 | /* |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 300 | * Add two ktime values and do a safety check for overflow: |
| 301 | */ |
| 302 | ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs) |
| 303 | { |
Vegard Nossum | 979515c | 2016-08-13 01:37:04 +0200 | [diff] [blame] | 304 | ktime_t res = ktime_add_unsafe(lhs, rhs); |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * We use KTIME_SEC_MAX here, the maximum timeout which we can |
| 308 | * return to user space in a timespec: |
| 309 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 310 | if (res < 0 || res < lhs || res < rhs) |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 311 | res = ktime_set(KTIME_SEC_MAX, 0); |
| 312 | |
| 313 | return res; |
| 314 | } |
| 315 | |
Artem Bityutskiy | 8daa21e | 2009-05-28 16:21:24 +0300 | [diff] [blame] | 316 | EXPORT_SYMBOL_GPL(ktime_add_safe); |
| 317 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 318 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
| 319 | |
| 320 | static struct debug_obj_descr hrtimer_debug_descr; |
| 321 | |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 322 | static void *hrtimer_debug_hint(void *addr) |
| 323 | { |
| 324 | return ((struct hrtimer *) addr)->function; |
| 325 | } |
| 326 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 327 | /* |
| 328 | * fixup_init is called when: |
| 329 | * - an active object is initialized |
| 330 | */ |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 331 | static bool hrtimer_fixup_init(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 332 | { |
| 333 | struct hrtimer *timer = addr; |
| 334 | |
| 335 | switch (state) { |
| 336 | case ODEBUG_STATE_ACTIVE: |
| 337 | hrtimer_cancel(timer); |
| 338 | debug_object_init(timer, &hrtimer_debug_descr); |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 339 | return true; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 340 | default: |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 341 | return false; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * fixup_activate is called when: |
| 347 | * - an active object is activated |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 348 | * - an unknown non-static object is activated |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 349 | */ |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 350 | static bool hrtimer_fixup_activate(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 351 | { |
| 352 | switch (state) { |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 353 | case ODEBUG_STATE_ACTIVE: |
| 354 | WARN_ON(1); |
| 355 | |
| 356 | default: |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 357 | return false; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * fixup_free is called when: |
| 363 | * - an active object is freed |
| 364 | */ |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 365 | static bool hrtimer_fixup_free(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 366 | { |
| 367 | struct hrtimer *timer = addr; |
| 368 | |
| 369 | switch (state) { |
| 370 | case ODEBUG_STATE_ACTIVE: |
| 371 | hrtimer_cancel(timer); |
| 372 | debug_object_free(timer, &hrtimer_debug_descr); |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 373 | return true; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 374 | default: |
Du, Changbin | e325246 | 2016-05-19 17:09:29 -0700 | [diff] [blame] | 375 | return false; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | static struct debug_obj_descr hrtimer_debug_descr = { |
| 380 | .name = "hrtimer", |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 381 | .debug_hint = hrtimer_debug_hint, |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 382 | .fixup_init = hrtimer_fixup_init, |
| 383 | .fixup_activate = hrtimer_fixup_activate, |
| 384 | .fixup_free = hrtimer_fixup_free, |
| 385 | }; |
| 386 | |
| 387 | static inline void debug_hrtimer_init(struct hrtimer *timer) |
| 388 | { |
| 389 | debug_object_init(timer, &hrtimer_debug_descr); |
| 390 | } |
| 391 | |
| 392 | static inline void debug_hrtimer_activate(struct hrtimer *timer) |
| 393 | { |
| 394 | debug_object_activate(timer, &hrtimer_debug_descr); |
| 395 | } |
| 396 | |
| 397 | static inline void debug_hrtimer_deactivate(struct hrtimer *timer) |
| 398 | { |
| 399 | debug_object_deactivate(timer, &hrtimer_debug_descr); |
| 400 | } |
| 401 | |
| 402 | static inline void debug_hrtimer_free(struct hrtimer *timer) |
| 403 | { |
| 404 | debug_object_free(timer, &hrtimer_debug_descr); |
| 405 | } |
| 406 | |
| 407 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 408 | enum hrtimer_mode mode); |
| 409 | |
| 410 | void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id, |
| 411 | enum hrtimer_mode mode) |
| 412 | { |
| 413 | debug_object_init_on_stack(timer, &hrtimer_debug_descr); |
| 414 | __hrtimer_init(timer, clock_id, mode); |
| 415 | } |
Stephen Hemminger | 2bc481c | 2009-08-28 23:41:29 -0700 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(hrtimer_init_on_stack); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 417 | |
| 418 | void destroy_hrtimer_on_stack(struct hrtimer *timer) |
| 419 | { |
| 420 | debug_object_free(timer, &hrtimer_debug_descr); |
| 421 | } |
Guenter Roeck | c08376a | 2016-05-26 17:21:05 -0700 | [diff] [blame] | 422 | EXPORT_SYMBOL_GPL(destroy_hrtimer_on_stack); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 423 | |
| 424 | #else |
| 425 | static inline void debug_hrtimer_init(struct hrtimer *timer) { } |
| 426 | static inline void debug_hrtimer_activate(struct hrtimer *timer) { } |
| 427 | static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } |
| 428 | #endif |
| 429 | |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 430 | static inline void |
| 431 | debug_init(struct hrtimer *timer, clockid_t clockid, |
| 432 | enum hrtimer_mode mode) |
| 433 | { |
| 434 | debug_hrtimer_init(timer); |
| 435 | trace_hrtimer_init(timer, clockid, mode); |
| 436 | } |
| 437 | |
| 438 | static inline void debug_activate(struct hrtimer *timer) |
| 439 | { |
| 440 | debug_hrtimer_activate(timer); |
| 441 | trace_hrtimer_start(timer); |
| 442 | } |
| 443 | |
| 444 | static inline void debug_deactivate(struct hrtimer *timer) |
| 445 | { |
| 446 | debug_hrtimer_deactivate(timer); |
| 447 | trace_hrtimer_cancel(timer); |
| 448 | } |
| 449 | |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 450 | #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS) |
Thomas Gleixner | 895bdfa | 2015-04-14 21:08:49 +0000 | [diff] [blame] | 451 | static inline void hrtimer_update_next_timer(struct hrtimer_cpu_base *cpu_base, |
| 452 | struct hrtimer *timer) |
| 453 | { |
| 454 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 455 | cpu_base->next_timer = timer; |
| 456 | #endif |
| 457 | } |
| 458 | |
kbuild test robot | 4ebbda5 | 2015-01-23 20:12:06 +0800 | [diff] [blame] | 459 | static ktime_t __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base) |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 460 | { |
| 461 | struct hrtimer_clock_base *base = cpu_base->clock_base; |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 462 | unsigned int active = cpu_base->active_bases; |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 463 | ktime_t expires, expires_next = KTIME_MAX; |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 464 | |
Thomas Gleixner | 895bdfa | 2015-04-14 21:08:49 +0000 | [diff] [blame] | 465 | hrtimer_update_next_timer(cpu_base, NULL); |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 466 | for (; active; base++, active >>= 1) { |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 467 | struct timerqueue_node *next; |
| 468 | struct hrtimer *timer; |
| 469 | |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 470 | if (!(active & 0x01)) |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 471 | continue; |
| 472 | |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 473 | next = timerqueue_getnext(&base->active); |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 474 | timer = container_of(next, struct hrtimer, node); |
| 475 | expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 476 | if (expires < expires_next) { |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 477 | expires_next = expires; |
Thomas Gleixner | 895bdfa | 2015-04-14 21:08:49 +0000 | [diff] [blame] | 478 | hrtimer_update_next_timer(cpu_base, timer); |
| 479 | } |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 480 | } |
| 481 | /* |
| 482 | * clock_was_set() might have changed base->offset of any of |
| 483 | * the clock bases so the result might be negative. Fix it up |
| 484 | * to prevent a false positive in clockevents_program_event(). |
| 485 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 486 | if (expires_next < 0) |
| 487 | expires_next = 0; |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 488 | return expires_next; |
| 489 | } |
| 490 | #endif |
| 491 | |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 492 | static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base) |
| 493 | { |
| 494 | ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset; |
| 495 | ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset; |
| 496 | ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset; |
| 497 | |
Thomas Gleixner | 868a3e9 | 2015-04-14 21:08:37 +0000 | [diff] [blame] | 498 | return ktime_get_update_offsets_now(&base->clock_was_set_seq, |
| 499 | offs_real, offs_boot, offs_tai); |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 502 | /* High resolution timer related functions */ |
| 503 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 504 | |
| 505 | /* |
| 506 | * High resolution timer enabled ? |
| 507 | */ |
Kees Cook | 4cc7ecb7 | 2016-03-17 14:23:00 -0700 | [diff] [blame] | 508 | static bool hrtimer_hres_enabled __read_mostly = true; |
Thomas Gleixner | 398ca17 | 2015-04-14 21:08:27 +0000 | [diff] [blame] | 509 | unsigned int hrtimer_resolution __read_mostly = LOW_RES_NSEC; |
| 510 | EXPORT_SYMBOL_GPL(hrtimer_resolution); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 511 | |
| 512 | /* |
| 513 | * Enable / Disable high resolution mode |
| 514 | */ |
| 515 | static int __init setup_hrtimer_hres(char *str) |
| 516 | { |
Kees Cook | 4cc7ecb7 | 2016-03-17 14:23:00 -0700 | [diff] [blame] | 517 | return (kstrtobool(str, &hrtimer_hres_enabled) == 0); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | __setup("highres=", setup_hrtimer_hres); |
| 521 | |
| 522 | /* |
| 523 | * hrtimer_high_res_enabled - query, if the highres mode is enabled |
| 524 | */ |
| 525 | static inline int hrtimer_is_hres_enabled(void) |
| 526 | { |
| 527 | return hrtimer_hres_enabled; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Is the high resolution mode active ? |
| 532 | */ |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 533 | static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *cpu_base) |
| 534 | { |
| 535 | return cpu_base->hres_active; |
| 536 | } |
| 537 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 538 | static inline int hrtimer_hres_active(void) |
| 539 | { |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 540 | return __hrtimer_hres_active(this_cpu_ptr(&hrtimer_bases)); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Reprogram the event source with checking both queues for the |
| 545 | * next event |
| 546 | * Called with interrupts disabled and base->lock held |
| 547 | */ |
Ashwin Chaugule | 7403f41 | 2009-09-01 23:03:33 -0400 | [diff] [blame] | 548 | static void |
| 549 | hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal) |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 550 | { |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 551 | ktime_t expires_next; |
| 552 | |
| 553 | if (!cpu_base->hres_active) |
| 554 | return; |
| 555 | |
| 556 | expires_next = __hrtimer_get_next_event(cpu_base); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 557 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 558 | if (skip_equal && expires_next == cpu_base->expires_next) |
Ashwin Chaugule | 7403f41 | 2009-09-01 23:03:33 -0400 | [diff] [blame] | 559 | return; |
| 560 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 561 | cpu_base->expires_next = expires_next; |
Ashwin Chaugule | 7403f41 | 2009-09-01 23:03:33 -0400 | [diff] [blame] | 562 | |
Stuart Hayes | 6c6c0d5 | 2014-04-29 17:55:02 -0500 | [diff] [blame] | 563 | /* |
| 564 | * If a hang was detected in the last timer interrupt then we |
| 565 | * leave the hang delay active in the hardware. We want the |
| 566 | * system to make progress. That also prevents the following |
| 567 | * scenario: |
| 568 | * T1 expires 50ms from now |
| 569 | * T2 expires 5s from now |
| 570 | * |
| 571 | * T1 is removed, so this code is called and would reprogram |
| 572 | * the hardware to 5s from now. Any hrtimer_start after that |
| 573 | * will not reprogram the hardware due to hang_detected being |
| 574 | * set. So we'd effectivly block all timers until the T2 event |
| 575 | * fires. |
| 576 | */ |
| 577 | if (cpu_base->hang_detected) |
| 578 | return; |
| 579 | |
Viresh Kumar | d2540875 | 2015-04-03 09:04:05 +0530 | [diff] [blame] | 580 | tick_program_event(cpu_base->expires_next, 1); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 584 | * When a timer is enqueued and expires earlier than the already enqueued |
| 585 | * timers, we have to check, whether it expires earlier than the timer for |
| 586 | * which the clock event device was armed. |
| 587 | * |
| 588 | * Called with interrupts disabled and base->cpu_base.lock held |
| 589 | */ |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 590 | static void hrtimer_reprogram(struct hrtimer *timer, |
| 591 | struct hrtimer_clock_base *base) |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 592 | { |
Christoph Lameter | dc5df73 | 2014-08-17 12:30:26 -0500 | [diff] [blame] | 593 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 594 | ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 595 | |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 596 | WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0); |
Thomas Gleixner | 63070a7 | 2008-02-14 00:58:36 +0100 | [diff] [blame] | 597 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 598 | /* |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 599 | * If the timer is not on the current cpu, we cannot reprogram |
| 600 | * the other cpus clock event device. |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 601 | */ |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 602 | if (base->cpu_base != cpu_base) |
| 603 | return; |
| 604 | |
| 605 | /* |
| 606 | * If the hrtimer interrupt is running, then it will |
| 607 | * reevaluate the clock bases and reprogram the clock event |
| 608 | * device. The callbacks are always executed in hard interrupt |
| 609 | * context so we don't need an extra check for a running |
| 610 | * callback. |
| 611 | */ |
| 612 | if (cpu_base->in_hrtirq) |
| 613 | return; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 614 | |
Thomas Gleixner | 63070a7 | 2008-02-14 00:58:36 +0100 | [diff] [blame] | 615 | /* |
| 616 | * CLOCK_REALTIME timer might be requested with an absolute |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 617 | * expiry time which is less than base->offset. Set it to 0. |
Thomas Gleixner | 63070a7 | 2008-02-14 00:58:36 +0100 | [diff] [blame] | 618 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 619 | if (expires < 0) |
| 620 | expires = 0; |
Thomas Gleixner | 63070a7 | 2008-02-14 00:58:36 +0100 | [diff] [blame] | 621 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 622 | if (expires >= cpu_base->expires_next) |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 623 | return; |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 624 | |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 625 | /* Update the pointer to the next expiring timer */ |
Thomas Gleixner | 895bdfa | 2015-04-14 21:08:49 +0000 | [diff] [blame] | 626 | cpu_base->next_timer = timer; |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 627 | |
| 628 | /* |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 629 | * If a hang was detected in the last timer interrupt then we |
| 630 | * do not schedule a timer which is earlier than the expiry |
| 631 | * which we enforced in the hang detection. We want the system |
| 632 | * to make progress. |
| 633 | */ |
| 634 | if (cpu_base->hang_detected) |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 635 | return; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 636 | |
| 637 | /* |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 638 | * Program the timer hardware. We enforce the expiry for |
| 639 | * events which are already in the past. |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 640 | */ |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 641 | cpu_base->expires_next = expires; |
| 642 | tick_program_event(expires, 1); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Ingo Molnar | 995f054 | 2007-04-07 12:05:00 +0200 | [diff] [blame] | 645 | /* |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 646 | * Initialize the high resolution related parts of cpu_base |
| 647 | */ |
| 648 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) |
| 649 | { |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 650 | base->expires_next = KTIME_MAX; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 651 | base->hres_active = 0; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 654 | /* |
| 655 | * Retrigger next event is called after clock was set |
| 656 | * |
| 657 | * Called with interrupts disabled via on_each_cpu() |
| 658 | */ |
| 659 | static void retrigger_next_event(void *arg) |
| 660 | { |
Christoph Lameter | dc5df73 | 2014-08-17 12:30:26 -0500 | [diff] [blame] | 661 | struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases); |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 662 | |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 663 | if (!base->hres_active) |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 664 | return; |
| 665 | |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 666 | raw_spin_lock(&base->lock); |
John Stultz | 5baefd6 | 2012-07-10 18:43:25 -0400 | [diff] [blame] | 667 | hrtimer_update_base(base); |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 668 | hrtimer_force_reprogram(base, 0); |
| 669 | raw_spin_unlock(&base->lock); |
| 670 | } |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 671 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 672 | /* |
| 673 | * Switch to high resolution mode |
| 674 | */ |
Luiz Capitulino | 75e3b37 | 2015-08-11 16:40:43 -0400 | [diff] [blame] | 675 | static void hrtimer_switch_to_hres(void) |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 676 | { |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 677 | struct hrtimer_cpu_base *base = this_cpu_ptr(&hrtimer_bases); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 678 | |
| 679 | if (tick_init_highres()) { |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 680 | printk(KERN_WARNING "Could not switch to high resolution " |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 681 | "mode on CPU %d\n", base->cpu); |
Guenter Roeck | 85e1cd6 | 2015-08-22 01:10:47 -0700 | [diff] [blame] | 682 | return; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 683 | } |
| 684 | base->hres_active = 1; |
Thomas Gleixner | 398ca17 | 2015-04-14 21:08:27 +0000 | [diff] [blame] | 685 | hrtimer_resolution = HIGH_RES_NSEC; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 686 | |
| 687 | tick_setup_sched_timer(); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 688 | /* "Retrigger" the interrupt to get things going */ |
| 689 | retrigger_next_event(NULL); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Thomas Gleixner | 5ec2481 | 2013-07-05 12:09:18 +0200 | [diff] [blame] | 692 | static void clock_was_set_work(struct work_struct *work) |
| 693 | { |
| 694 | clock_was_set(); |
| 695 | } |
| 696 | |
| 697 | static DECLARE_WORK(hrtimer_work, clock_was_set_work); |
| 698 | |
John Stultz | f55a6fa | 2012-07-10 18:43:19 -0400 | [diff] [blame] | 699 | /* |
Pratyush Patel | b4d90e9 | 2016-06-23 20:50:37 +0200 | [diff] [blame] | 700 | * Called from timekeeping and resume code to reprogram the hrtimer |
Thomas Gleixner | 5ec2481 | 2013-07-05 12:09:18 +0200 | [diff] [blame] | 701 | * interrupt device on all cpus. |
John Stultz | f55a6fa | 2012-07-10 18:43:19 -0400 | [diff] [blame] | 702 | */ |
| 703 | void clock_was_set_delayed(void) |
| 704 | { |
Thomas Gleixner | 5ec2481 | 2013-07-05 12:09:18 +0200 | [diff] [blame] | 705 | schedule_work(&hrtimer_work); |
John Stultz | f55a6fa | 2012-07-10 18:43:19 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 708 | #else |
| 709 | |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 710 | static inline int __hrtimer_hres_active(struct hrtimer_cpu_base *b) { return 0; } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 711 | static inline int hrtimer_hres_active(void) { return 0; } |
| 712 | static inline int hrtimer_is_hres_enabled(void) { return 0; } |
Luiz Capitulino | 75e3b37 | 2015-08-11 16:40:43 -0400 | [diff] [blame] | 713 | static inline void hrtimer_switch_to_hres(void) { } |
Ashwin Chaugule | 7403f41 | 2009-09-01 23:03:33 -0400 | [diff] [blame] | 714 | static inline void |
| 715 | hrtimer_force_reprogram(struct hrtimer_cpu_base *base, int skip_equal) { } |
Viresh Kumar | 9e1e01d | 2014-06-22 01:29:17 +0200 | [diff] [blame] | 716 | static inline int hrtimer_reprogram(struct hrtimer *timer, |
| 717 | struct hrtimer_clock_base *base) |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 718 | { |
| 719 | return 0; |
| 720 | } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 721 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 722 | static inline void retrigger_next_event(void *arg) { } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 723 | |
| 724 | #endif /* CONFIG_HIGH_RES_TIMERS */ |
| 725 | |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 726 | /* |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 727 | * Clock realtime was set |
| 728 | * |
| 729 | * Change the offset of the realtime clock vs. the monotonic |
| 730 | * clock. |
| 731 | * |
| 732 | * We might have to reprogram the high resolution timer interrupt. On |
| 733 | * SMP we call the architecture specific code to retrigger _all_ high |
| 734 | * resolution timer interrupts. On UP we just disable interrupts and |
| 735 | * call the high resolution interrupt code. |
| 736 | */ |
| 737 | void clock_was_set(void) |
| 738 | { |
Thomas Gleixner | 90ff1f3 | 2011-05-25 23:08:17 +0200 | [diff] [blame] | 739 | #ifdef CONFIG_HIGH_RES_TIMERS |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 740 | /* Retrigger the CPU local events everywhere */ |
| 741 | on_each_cpu(retrigger_next_event, NULL, 1); |
Thomas Gleixner | 9ec2690 | 2011-05-20 16:18:50 +0200 | [diff] [blame] | 742 | #endif |
| 743 | timerfd_clock_was_set(); |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* |
| 747 | * During resume we might have to reprogram the high resolution timer |
David Vrabel | 7c4c3a0 | 2013-06-27 11:35:44 +0100 | [diff] [blame] | 748 | * interrupt on all online CPUs. However, all other CPUs will be |
| 749 | * stopped with IRQs interrupts disabled so the clock_was_set() call |
Thomas Gleixner | 5ec2481 | 2013-07-05 12:09:18 +0200 | [diff] [blame] | 750 | * must be deferred. |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 751 | */ |
| 752 | void hrtimers_resume(void) |
| 753 | { |
Frederic Weisbecker | 53bef3f | 2017-11-06 16:01:21 +0100 | [diff] [blame] | 754 | lockdep_assert_irqs_disabled(); |
Thomas Gleixner | 5ec2481 | 2013-07-05 12:09:18 +0200 | [diff] [blame] | 755 | /* Retrigger on the local CPU */ |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 756 | retrigger_next_event(NULL); |
Thomas Gleixner | 5ec2481 | 2013-07-05 12:09:18 +0200 | [diff] [blame] | 757 | /* And schedule a retrigger for all others */ |
| 758 | clock_was_set_delayed(); |
Thomas Gleixner | b12a03c | 2011-05-02 16:48:57 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 761 | /* |
Uwe Kleine-König | 6506f2a | 2007-10-20 01:56:53 +0200 | [diff] [blame] | 762 | * Counterpart to lock_hrtimer_base above: |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 763 | */ |
| 764 | static inline |
| 765 | void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 766 | { |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 767 | raw_spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /** |
| 771 | * hrtimer_forward - forward the timer expiry |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 772 | * @timer: hrtimer to forward |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 773 | * @now: forward past this time |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 774 | * @interval: the interval to forward |
| 775 | * |
| 776 | * Forward the timer expiry so it will expire in the future. |
Jonathan Corbet | 8dca6f3 | 2006-01-16 15:58:55 -0700 | [diff] [blame] | 777 | * Returns the number of overruns. |
Thomas Gleixner | 91e5a217 | 2015-04-13 21:02:22 +0000 | [diff] [blame] | 778 | * |
| 779 | * Can be safely called from the callback function of @timer. If |
| 780 | * called from other contexts @timer must neither be enqueued nor |
| 781 | * running the callback and the caller needs to take care of |
| 782 | * serialization. |
| 783 | * |
| 784 | * Note: This only updates the timer expiry value and does not requeue |
| 785 | * the timer. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 786 | */ |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 787 | u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 788 | { |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 789 | u64 orun = 1; |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 790 | ktime_t delta; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 791 | |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 792 | delta = ktime_sub(now, hrtimer_get_expires(timer)); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 793 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 794 | if (delta < 0) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 795 | return 0; |
| 796 | |
Peter Zijlstra | 5de2755 | 2014-05-20 15:49:48 +0200 | [diff] [blame] | 797 | if (WARN_ON(timer->state & HRTIMER_STATE_ENQUEUED)) |
| 798 | return 0; |
| 799 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 800 | if (interval < hrtimer_resolution) |
| 801 | interval = hrtimer_resolution; |
Thomas Gleixner | c9db4fa | 2006-01-12 11:47:34 +0100 | [diff] [blame] | 802 | |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 803 | if (unlikely(delta >= interval)) { |
Roman Zippel | df869b6 | 2006-03-26 01:38:11 -0800 | [diff] [blame] | 804 | s64 incr = ktime_to_ns(interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 805 | |
| 806 | orun = ktime_divns(delta, incr); |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 807 | hrtimer_add_expires_ns(timer, incr * orun); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 808 | if (hrtimer_get_expires_tv64(timer) > now) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 809 | return orun; |
| 810 | /* |
| 811 | * This (and the ktime_add() below) is the |
| 812 | * correction for exact: |
| 813 | */ |
| 814 | orun++; |
| 815 | } |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 816 | hrtimer_add_expires(timer, interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 817 | |
| 818 | return orun; |
| 819 | } |
Stas Sergeev | 6bdb6b6 | 2007-05-08 00:31:58 -0700 | [diff] [blame] | 820 | EXPORT_SYMBOL_GPL(hrtimer_forward); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 821 | |
| 822 | /* |
| 823 | * enqueue_hrtimer - internal function to (re)start a timer |
| 824 | * |
| 825 | * The timer is inserted in expiry order. Insertion into the |
| 826 | * red black tree is O(log(n)). Must hold the base lock. |
Peter Zijlstra | a6037b6 | 2009-01-05 11:28:22 +0100 | [diff] [blame] | 827 | * |
| 828 | * Returns 1 when the new timer is the leftmost timer in the tree. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 829 | */ |
Peter Zijlstra | a6037b6 | 2009-01-05 11:28:22 +0100 | [diff] [blame] | 830 | static int enqueue_hrtimer(struct hrtimer *timer, |
| 831 | struct hrtimer_clock_base *base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 832 | { |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 833 | debug_activate(timer); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 834 | |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 835 | base->cpu_base->active_bases |= 1 << base->index; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 836 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 837 | timer->state = HRTIMER_STATE_ENQUEUED; |
Peter Zijlstra | a6037b6 | 2009-01-05 11:28:22 +0100 | [diff] [blame] | 838 | |
Thomas Gleixner | b97f44c | 2015-04-14 21:08:47 +0000 | [diff] [blame] | 839 | return timerqueue_add(&base->active, &timer->node); |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 840 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 841 | |
| 842 | /* |
| 843 | * __remove_hrtimer - internal function to remove a timer |
| 844 | * |
| 845 | * Caller must hold the base lock. |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 846 | * |
| 847 | * High resolution timer mode reprograms the clock event device when the |
| 848 | * timer is the one which expires next. The caller can disable this by setting |
| 849 | * reprogram to zero. This is useful, when the context does a reprogramming |
| 850 | * anyway (e.g. timer interrupt) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 851 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 852 | static void __remove_hrtimer(struct hrtimer *timer, |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 853 | struct hrtimer_clock_base *base, |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 854 | u8 newstate, int reprogram) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 855 | { |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 856 | struct hrtimer_cpu_base *cpu_base = base->cpu_base; |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 857 | u8 state = timer->state; |
Ashwin Chaugule | 7403f41 | 2009-09-01 23:03:33 -0400 | [diff] [blame] | 858 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 859 | timer->state = newstate; |
Thomas Gleixner | 895bdfa | 2015-04-14 21:08:49 +0000 | [diff] [blame] | 860 | if (!(state & HRTIMER_STATE_ENQUEUED)) |
| 861 | return; |
David Woodhouse | ed198cb | 2006-04-22 02:38:50 +0100 | [diff] [blame] | 862 | |
Thomas Gleixner | b97f44c | 2015-04-14 21:08:47 +0000 | [diff] [blame] | 863 | if (!timerqueue_del(&base->active, &timer->node)) |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 864 | cpu_base->active_bases &= ~(1 << base->index); |
Viresh Kumar | d9f0acd | 2015-04-14 21:08:25 +0000 | [diff] [blame] | 865 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 866 | #ifdef CONFIG_HIGH_RES_TIMERS |
Thomas Gleixner | 895bdfa | 2015-04-14 21:08:49 +0000 | [diff] [blame] | 867 | /* |
| 868 | * Note: If reprogram is false we do not update |
| 869 | * cpu_base->next_timer. This happens when we remove the first |
| 870 | * timer on a remote cpu. No harm as we never dereference |
| 871 | * cpu_base->next_timer. So the worst thing what can happen is |
| 872 | * an superflous call to hrtimer_force_reprogram() on the |
| 873 | * remote cpu later on if the same timer gets enqueued again. |
| 874 | */ |
| 875 | if (reprogram && timer == cpu_base->next_timer) |
| 876 | hrtimer_force_reprogram(cpu_base, 1); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 877 | #endif |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
| 881 | * remove hrtimer, called with base lock held |
| 882 | */ |
| 883 | static inline int |
Peter Zijlstra | 8edfb03 | 2015-06-11 14:46:45 +0200 | [diff] [blame] | 884 | remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base, bool restart) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 885 | { |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 886 | if (hrtimer_is_queued(timer)) { |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 887 | u8 state = timer->state; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 888 | int reprogram; |
| 889 | |
| 890 | /* |
| 891 | * Remove the timer and force reprogramming when high |
| 892 | * resolution mode is active and the timer is on the current |
| 893 | * CPU. If we remove a timer on another CPU, reprogramming is |
| 894 | * skipped. The interrupt event on this CPU is fired and |
| 895 | * reprogramming happens in the interrupt handler. This is a |
| 896 | * rare case and less expensive than a smp call. |
| 897 | */ |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 898 | debug_deactivate(timer); |
Christoph Lameter | dc5df73 | 2014-08-17 12:30:26 -0500 | [diff] [blame] | 899 | reprogram = base->cpu_base == this_cpu_ptr(&hrtimer_bases); |
Peter Zijlstra | 8edfb03 | 2015-06-11 14:46:45 +0200 | [diff] [blame] | 900 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 901 | if (!restart) |
| 902 | state = HRTIMER_STATE_INACTIVE; |
| 903 | |
Salman Qazi | f13d4f9 | 2010-10-12 07:25:19 -0700 | [diff] [blame] | 904 | __remove_hrtimer(timer, base, state, reprogram); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 905 | return 1; |
| 906 | } |
| 907 | return 0; |
| 908 | } |
| 909 | |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 910 | static inline ktime_t hrtimer_update_lowres(struct hrtimer *timer, ktime_t tim, |
| 911 | const enum hrtimer_mode mode) |
| 912 | { |
| 913 | #ifdef CONFIG_TIME_LOW_RES |
| 914 | /* |
| 915 | * CONFIG_TIME_LOW_RES indicates that the system has no way to return |
| 916 | * granular time values. For relative timers we add hrtimer_resolution |
| 917 | * (i.e. one jiffie) to prevent short timeouts. |
| 918 | */ |
| 919 | timer->is_rel = mode & HRTIMER_MODE_REL; |
| 920 | if (timer->is_rel) |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 921 | tim = ktime_add_safe(tim, hrtimer_resolution); |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 922 | #endif |
| 923 | return tim; |
| 924 | } |
| 925 | |
Thomas Gleixner | 58f1f80 | 2015-04-14 21:09:08 +0000 | [diff] [blame] | 926 | /** |
| 927 | * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU |
| 928 | * @timer: the timer to be added |
| 929 | * @tim: expiry time |
| 930 | * @delta_ns: "slack" range for the timer |
| 931 | * @mode: expiry mode: absolute (HRTIMER_MODE_ABS) or |
| 932 | * relative (HRTIMER_MODE_REL) |
Thomas Gleixner | 58f1f80 | 2015-04-14 21:09:08 +0000 | [diff] [blame] | 933 | */ |
Thomas Gleixner | 61699e13 | 2015-04-14 21:09:23 +0000 | [diff] [blame] | 934 | void hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, |
John Stultz | da8b44d | 2016-03-17 14:20:51 -0700 | [diff] [blame] | 935 | u64 delta_ns, const enum hrtimer_mode mode) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 936 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 937 | struct hrtimer_clock_base *base, *new_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 938 | unsigned long flags; |
Thomas Gleixner | 61699e13 | 2015-04-14 21:09:23 +0000 | [diff] [blame] | 939 | int leftmost; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 940 | |
| 941 | base = lock_hrtimer_base(timer, &flags); |
| 942 | |
| 943 | /* Remove an active timer from the queue: */ |
Peter Zijlstra | 8edfb03 | 2015-06-11 14:46:45 +0200 | [diff] [blame] | 944 | remove_hrtimer(timer, base, true); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 945 | |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 946 | if (mode & HRTIMER_MODE_REL) |
Viresh Kumar | 84ea7fe | 2014-05-12 13:42:29 +0530 | [diff] [blame] | 947 | tim = ktime_add_safe(tim, base->get_time()); |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 948 | |
| 949 | tim = hrtimer_update_lowres(timer, tim, mode); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 950 | |
Arjan van de Ven | da8f2e1 | 2008-09-07 10:47:46 -0700 | [diff] [blame] | 951 | hrtimer_set_expires_range_ns(timer, tim, delta_ns); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 952 | |
Viresh Kumar | 84ea7fe | 2014-05-12 13:42:29 +0530 | [diff] [blame] | 953 | /* Switch the timer base, if necessary: */ |
| 954 | new_base = switch_hrtimer_base(timer, base, mode & HRTIMER_MODE_PINNED); |
| 955 | |
Peter Zijlstra | a6037b6 | 2009-01-05 11:28:22 +0100 | [diff] [blame] | 956 | leftmost = enqueue_hrtimer(timer, new_base); |
Thomas Gleixner | 61699e13 | 2015-04-14 21:09:23 +0000 | [diff] [blame] | 957 | if (!leftmost) |
| 958 | goto unlock; |
Viresh Kumar | 49a2a07 | 2014-06-23 13:39:37 +0530 | [diff] [blame] | 959 | |
| 960 | if (!hrtimer_is_hres_active(timer)) { |
| 961 | /* |
| 962 | * Kick to reschedule the next tick to handle the new timer |
| 963 | * on dynticks target. |
| 964 | */ |
Thomas Gleixner | ae67bad | 2018-01-14 23:30:51 +0100 | [diff] [blame] | 965 | if (is_timers_nohz_active()) |
Thomas Gleixner | 683be13 | 2015-05-26 22:50:35 +0000 | [diff] [blame] | 966 | wake_up_nohz_cpu(new_base->cpu_base->cpu); |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 967 | } else { |
| 968 | hrtimer_reprogram(timer, new_base); |
Leonid Shatz | b22affe | 2013-02-04 14:33:37 +0200 | [diff] [blame] | 969 | } |
Thomas Gleixner | 61699e13 | 2015-04-14 21:09:23 +0000 | [diff] [blame] | 970 | unlock: |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 971 | unlock_hrtimer_base(timer, &flags); |
Peter Zijlstra | 7f1e2ca | 2009-03-13 12:21:27 +0100 | [diff] [blame] | 972 | } |
Arjan van de Ven | da8f2e1 | 2008-09-07 10:47:46 -0700 | [diff] [blame] | 973 | EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); |
| 974 | |
| 975 | /** |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 976 | * hrtimer_try_to_cancel - try to deactivate a timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 977 | * @timer: hrtimer to stop |
| 978 | * |
| 979 | * Returns: |
| 980 | * 0 when the timer was not active |
| 981 | * 1 when the timer was active |
Masanari Iida | 0ba42a5 | 2017-03-07 20:48:02 +0900 | [diff] [blame] | 982 | * -1 when the timer is currently executing the callback function and |
Randy Dunlap | fa9799e | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 983 | * cannot be stopped |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 984 | */ |
| 985 | int hrtimer_try_to_cancel(struct hrtimer *timer) |
| 986 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 987 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 988 | unsigned long flags; |
| 989 | int ret = -1; |
| 990 | |
Thomas Gleixner | 19d9f42 | 2015-04-14 21:09:25 +0000 | [diff] [blame] | 991 | /* |
| 992 | * Check lockless first. If the timer is not active (neither |
| 993 | * enqueued nor running the callback, nothing to do here. The |
| 994 | * base lock does not serialize against a concurrent enqueue, |
| 995 | * so we can avoid taking it. |
| 996 | */ |
| 997 | if (!hrtimer_active(timer)) |
| 998 | return 0; |
| 999 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1000 | base = lock_hrtimer_base(timer, &flags); |
| 1001 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 1002 | if (!hrtimer_callback_running(timer)) |
Peter Zijlstra | 8edfb03 | 2015-06-11 14:46:45 +0200 | [diff] [blame] | 1003 | ret = remove_hrtimer(timer, base, false); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1004 | |
| 1005 | unlock_hrtimer_base(timer, &flags); |
| 1006 | |
| 1007 | return ret; |
| 1008 | |
| 1009 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1010 | EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1011 | |
| 1012 | /** |
| 1013 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1014 | * @timer: the timer to be cancelled |
| 1015 | * |
| 1016 | * Returns: |
| 1017 | * 0 when the timer was not active |
| 1018 | * 1 when the timer was active |
| 1019 | */ |
| 1020 | int hrtimer_cancel(struct hrtimer *timer) |
| 1021 | { |
| 1022 | for (;;) { |
| 1023 | int ret = hrtimer_try_to_cancel(timer); |
| 1024 | |
| 1025 | if (ret >= 0) |
| 1026 | return ret; |
Joe Korty | 5ef37b1 | 2006-04-10 22:54:13 -0700 | [diff] [blame] | 1027 | cpu_relax(); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1030 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1031 | |
| 1032 | /** |
| 1033 | * hrtimer_get_remaining - get remaining time for the timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1034 | * @timer: the timer to read |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 1035 | * @adjust: adjust relative timers when CONFIG_TIME_LOW_RES=y |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1036 | */ |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 1037 | ktime_t __hrtimer_get_remaining(const struct hrtimer *timer, bool adjust) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1038 | { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1039 | unsigned long flags; |
| 1040 | ktime_t rem; |
| 1041 | |
Andi Kleen | b3bd3de | 2010-08-10 14:17:51 -0700 | [diff] [blame] | 1042 | lock_hrtimer_base(timer, &flags); |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 1043 | if (IS_ENABLED(CONFIG_TIME_LOW_RES) && adjust) |
| 1044 | rem = hrtimer_expires_remaining_adjusted(timer); |
| 1045 | else |
| 1046 | rem = hrtimer_expires_remaining(timer); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1047 | unlock_hrtimer_base(timer, &flags); |
| 1048 | |
| 1049 | return rem; |
| 1050 | } |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 1051 | EXPORT_SYMBOL_GPL(__hrtimer_get_remaining); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1052 | |
Frederic Weisbecker | 3451d02 | 2011-08-10 23:21:01 +0200 | [diff] [blame] | 1053 | #ifdef CONFIG_NO_HZ_COMMON |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1054 | /** |
| 1055 | * hrtimer_get_next_event - get the time until next expiry event |
| 1056 | * |
Thomas Gleixner | c1ad348 | 2015-04-14 21:08:58 +0000 | [diff] [blame] | 1057 | * Returns the next expiry time or KTIME_MAX if no timer is pending. |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1058 | */ |
Thomas Gleixner | c1ad348 | 2015-04-14 21:08:58 +0000 | [diff] [blame] | 1059 | u64 hrtimer_get_next_event(void) |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1060 | { |
Christoph Lameter | dc5df73 | 2014-08-17 12:30:26 -0500 | [diff] [blame] | 1061 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
Thomas Gleixner | c1ad348 | 2015-04-14 21:08:58 +0000 | [diff] [blame] | 1062 | u64 expires = KTIME_MAX; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1063 | unsigned long flags; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1064 | |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1065 | raw_spin_lock_irqsave(&cpu_base->lock, flags); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1066 | |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 1067 | if (!__hrtimer_hres_active(cpu_base)) |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1068 | expires = __hrtimer_get_next_event(cpu_base); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1069 | |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1070 | raw_spin_unlock_irqrestore(&cpu_base->lock, flags); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1071 | |
Thomas Gleixner | c1ad348 | 2015-04-14 21:08:58 +0000 | [diff] [blame] | 1072 | return expires; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1073 | } |
| 1074 | #endif |
| 1075 | |
Marc Zyngier | 336a9cd | 2016-01-15 17:41:09 +0000 | [diff] [blame] | 1076 | static inline int hrtimer_clockid_to_base(clockid_t clock_id) |
| 1077 | { |
| 1078 | if (likely(clock_id < MAX_CLOCKS)) { |
| 1079 | int base = hrtimer_clock_to_base_table[clock_id]; |
| 1080 | |
| 1081 | if (likely(base != HRTIMER_MAX_CLOCK_BASES)) |
| 1082 | return base; |
| 1083 | } |
| 1084 | WARN(1, "Invalid clockid %d. Using MONOTONIC\n", clock_id); |
| 1085 | return HRTIMER_BASE_MONOTONIC; |
| 1086 | } |
| 1087 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1088 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 1089 | enum hrtimer_mode mode) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1090 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1091 | struct hrtimer_cpu_base *cpu_base; |
John Stultz | e06383d | 2010-12-14 19:37:07 -0800 | [diff] [blame] | 1092 | int base; |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1093 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1094 | memset(timer, 0, sizeof(struct hrtimer)); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1095 | |
Christoph Lameter | 22127e9 | 2014-08-17 12:30:25 -0500 | [diff] [blame] | 1096 | cpu_base = raw_cpu_ptr(&hrtimer_bases); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1097 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1098 | if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1099 | clock_id = CLOCK_MONOTONIC; |
| 1100 | |
John Stultz | e06383d | 2010-12-14 19:37:07 -0800 | [diff] [blame] | 1101 | base = hrtimer_clockid_to_base(clock_id); |
| 1102 | timer->base = &cpu_base->clock_base[base]; |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1103 | timerqueue_init(&timer->node); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1104 | } |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1105 | |
| 1106 | /** |
| 1107 | * hrtimer_init - initialize a timer to the given clock |
| 1108 | * @timer: the timer to be initialized |
| 1109 | * @clock_id: the clock to be used |
| 1110 | * @mode: timer mode abs/rel |
| 1111 | */ |
| 1112 | void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 1113 | enum hrtimer_mode mode) |
| 1114 | { |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 1115 | debug_init(timer, clock_id, mode); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1116 | __hrtimer_init(timer, clock_id, mode); |
| 1117 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1118 | EXPORT_SYMBOL_GPL(hrtimer_init); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1119 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1120 | /* |
| 1121 | * A timer is active, when it is enqueued into the rbtree or the |
| 1122 | * callback function is running or it's in the state of being migrated |
| 1123 | * to another cpu. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1124 | * |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1125 | * It is important for this function to not return a false negative. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1126 | */ |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1127 | bool hrtimer_active(const struct hrtimer *timer) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1128 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1129 | struct hrtimer_cpu_base *cpu_base; |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1130 | unsigned int seq; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1131 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1132 | do { |
| 1133 | cpu_base = READ_ONCE(timer->base->cpu_base); |
| 1134 | seq = raw_read_seqcount_begin(&cpu_base->seq); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1135 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1136 | if (timer->state != HRTIMER_STATE_INACTIVE || |
| 1137 | cpu_base->running == timer) |
| 1138 | return true; |
| 1139 | |
| 1140 | } while (read_seqcount_retry(&cpu_base->seq, seq) || |
| 1141 | cpu_base != READ_ONCE(timer->base->cpu_base)); |
| 1142 | |
| 1143 | return false; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1144 | } |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1145 | EXPORT_SYMBOL_GPL(hrtimer_active); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1146 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1147 | /* |
| 1148 | * The write_seqcount_barrier()s in __run_hrtimer() split the thing into 3 |
| 1149 | * distinct sections: |
| 1150 | * |
| 1151 | * - queued: the timer is queued |
| 1152 | * - callback: the timer is being ran |
| 1153 | * - post: the timer is inactive or (re)queued |
| 1154 | * |
| 1155 | * On the read side we ensure we observe timer->state and cpu_base->running |
| 1156 | * from the same section, if anything changed while we looked at it, we retry. |
| 1157 | * This includes timer->base changing because sequence numbers alone are |
| 1158 | * insufficient for that. |
| 1159 | * |
| 1160 | * The sequence numbers are required because otherwise we could still observe |
| 1161 | * a false negative if the read side got smeared over multiple consequtive |
| 1162 | * __run_hrtimer() invocations. |
| 1163 | */ |
| 1164 | |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1165 | static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, |
| 1166 | struct hrtimer_clock_base *base, |
| 1167 | struct hrtimer *timer, ktime_t *now) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1168 | { |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1169 | enum hrtimer_restart (*fn)(struct hrtimer *); |
| 1170 | int restart; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1171 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1172 | lockdep_assert_held(&cpu_base->lock); |
Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 1173 | |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 1174 | debug_deactivate(timer); |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1175 | cpu_base->running = timer; |
| 1176 | |
| 1177 | /* |
| 1178 | * Separate the ->running assignment from the ->state assignment. |
| 1179 | * |
| 1180 | * As with a regular write barrier, this ensures the read side in |
| 1181 | * hrtimer_active() cannot observe cpu_base->running == NULL && |
| 1182 | * timer->state == INACTIVE. |
| 1183 | */ |
| 1184 | raw_write_seqcount_barrier(&cpu_base->seq); |
| 1185 | |
| 1186 | __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, 0); |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1187 | fn = timer->function; |
Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 1188 | |
| 1189 | /* |
Thomas Gleixner | 203cbf77 | 2016-01-14 16:54:46 +0000 | [diff] [blame] | 1190 | * Clear the 'is relative' flag for the TIME_LOW_RES case. If the |
| 1191 | * timer is restarted with a period then it becomes an absolute |
| 1192 | * timer. If its not restarted it does not matter. |
| 1193 | */ |
| 1194 | if (IS_ENABLED(CONFIG_TIME_LOW_RES)) |
| 1195 | timer->is_rel = false; |
| 1196 | |
| 1197 | /* |
Thomas Gleixner | d05ca13 | 2017-12-21 11:41:31 +0100 | [diff] [blame^] | 1198 | * The timer is marked as running in the CPU base, so it is |
| 1199 | * protected against migration to a different CPU even if the lock |
| 1200 | * is dropped. |
Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 1201 | */ |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1202 | raw_spin_unlock(&cpu_base->lock); |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 1203 | trace_hrtimer_expire_entry(timer, now); |
Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 1204 | restart = fn(timer); |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 1205 | trace_hrtimer_expire_exit(timer); |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1206 | raw_spin_lock(&cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1207 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1208 | /* |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1209 | * Note: We clear the running state after enqueue_hrtimer and |
Pratyush Patel | b4d90e9 | 2016-06-23 20:50:37 +0200 | [diff] [blame] | 1210 | * we do not reprogram the event hardware. Happens either in |
Thomas Gleixner | e3f1d88 | 2009-01-05 11:28:23 +0100 | [diff] [blame] | 1211 | * hrtimer_start_range_ns() or in hrtimer_interrupt() |
Peter Zijlstra | 5de2755 | 2014-05-20 15:49:48 +0200 | [diff] [blame] | 1212 | * |
| 1213 | * Note: Because we dropped the cpu_base->lock above, |
| 1214 | * hrtimer_start_range_ns() can have popped in and enqueued the timer |
| 1215 | * for us already. |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1216 | */ |
Peter Zijlstra | 5de2755 | 2014-05-20 15:49:48 +0200 | [diff] [blame] | 1217 | if (restart != HRTIMER_NORESTART && |
| 1218 | !(timer->state & HRTIMER_STATE_ENQUEUED)) |
Peter Zijlstra | a6037b6 | 2009-01-05 11:28:22 +0100 | [diff] [blame] | 1219 | enqueue_hrtimer(timer, base); |
Salman Qazi | f13d4f9 | 2010-10-12 07:25:19 -0700 | [diff] [blame] | 1220 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1221 | /* |
| 1222 | * Separate the ->running assignment from the ->state assignment. |
| 1223 | * |
| 1224 | * As with a regular write barrier, this ensures the read side in |
| 1225 | * hrtimer_active() cannot observe cpu_base->running == NULL && |
| 1226 | * timer->state == INACTIVE. |
| 1227 | */ |
| 1228 | raw_write_seqcount_barrier(&cpu_base->seq); |
Salman Qazi | f13d4f9 | 2010-10-12 07:25:19 -0700 | [diff] [blame] | 1229 | |
Peter Zijlstra | 887d9dc | 2015-06-11 14:46:48 +0200 | [diff] [blame] | 1230 | WARN_ON_ONCE(cpu_base->running != timer); |
| 1231 | cpu_base->running = NULL; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1234 | static void __hrtimer_run_queues(struct hrtimer_cpu_base *cpu_base, ktime_t now) |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1235 | { |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 1236 | struct hrtimer_clock_base *base = cpu_base->clock_base; |
| 1237 | unsigned int active = cpu_base->active_bases; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1238 | |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 1239 | for (; active; base++, active >>= 1) { |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1240 | struct timerqueue_node *node; |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1241 | ktime_t basenow; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1242 | |
Thomas Gleixner | 34aee88 | 2015-04-14 21:08:41 +0000 | [diff] [blame] | 1243 | if (!(active & 0x01)) |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1244 | continue; |
| 1245 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1246 | basenow = ktime_add(now, base->offset); |
| 1247 | |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1248 | while ((node = timerqueue_getnext(&base->active))) { |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1249 | struct hrtimer *timer; |
| 1250 | |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1251 | timer = container_of(node, struct hrtimer, node); |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1252 | |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1253 | /* |
| 1254 | * The immediate goal for using the softexpires is |
| 1255 | * minimizing wakeups, not running timers at the |
| 1256 | * earliest interrupt after their soft expiration. |
| 1257 | * This allows us to avoid using a Priority Search |
| 1258 | * Tree, which can answer a stabbing querry for |
| 1259 | * overlapping intervals and instead use the simple |
| 1260 | * BST we already have. |
| 1261 | * We don't add extra wakeups by delaying timers that |
| 1262 | * are right-of a not yet expired timer, because that |
| 1263 | * timer will have to trigger a wakeup anyway. |
| 1264 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1265 | if (basenow < hrtimer_get_softexpires_tv64(timer)) |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1266 | break; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1267 | |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1268 | __run_hrtimer(cpu_base, base, timer, &basenow); |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1269 | } |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1270 | } |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 1274 | |
| 1275 | /* |
| 1276 | * High resolution timer interrupt |
| 1277 | * Called with interrupts disabled |
| 1278 | */ |
| 1279 | void hrtimer_interrupt(struct clock_event_device *dev) |
| 1280 | { |
| 1281 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
| 1282 | ktime_t expires_next, now, entry_time, delta; |
| 1283 | int retries = 0; |
| 1284 | |
| 1285 | BUG_ON(!cpu_base->hres_active); |
| 1286 | cpu_base->nr_events++; |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1287 | dev->next_event = KTIME_MAX; |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1288 | |
| 1289 | raw_spin_lock(&cpu_base->lock); |
| 1290 | entry_time = now = hrtimer_update_base(cpu_base); |
| 1291 | retry: |
| 1292 | cpu_base->in_hrtirq = 1; |
| 1293 | /* |
| 1294 | * We set expires_next to KTIME_MAX here with cpu_base->lock |
| 1295 | * held to prevent that a timer is enqueued in our queue via |
| 1296 | * the migration code. This does not affect enqueueing of |
| 1297 | * timers which run their callback and need to be requeued on |
| 1298 | * this CPU. |
| 1299 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1300 | cpu_base->expires_next = KTIME_MAX; |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1301 | |
| 1302 | __hrtimer_run_queues(cpu_base, now); |
| 1303 | |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 1304 | /* Reevaluate the clock bases for the next expiry */ |
| 1305 | expires_next = __hrtimer_get_next_event(cpu_base); |
Thomas Gleixner | 6ff7041 | 2009-07-10 14:57:05 +0200 | [diff] [blame] | 1306 | /* |
| 1307 | * Store the new expiry value so the migration code can verify |
| 1308 | * against it. |
| 1309 | */ |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1310 | cpu_base->expires_next = expires_next; |
Thomas Gleixner | 9bc74919 | 2015-01-20 21:24:10 +0100 | [diff] [blame] | 1311 | cpu_base->in_hrtirq = 0; |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1312 | raw_spin_unlock(&cpu_base->lock); |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1313 | |
| 1314 | /* Reprogramming necessary ? */ |
Viresh Kumar | d2540875 | 2015-04-03 09:04:05 +0530 | [diff] [blame] | 1315 | if (!tick_program_event(expires_next, 0)) { |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1316 | cpu_base->hang_detected = 0; |
| 1317 | return; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1318 | } |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1319 | |
| 1320 | /* |
| 1321 | * The next timer was already expired due to: |
| 1322 | * - tracing |
| 1323 | * - long lasting callbacks |
| 1324 | * - being scheduled away when running in a VM |
| 1325 | * |
| 1326 | * We need to prevent that we loop forever in the hrtimer |
| 1327 | * interrupt routine. We give it 3 attempts to avoid |
| 1328 | * overreacting on some spurious event. |
John Stultz | 5baefd6 | 2012-07-10 18:43:25 -0400 | [diff] [blame] | 1329 | * |
| 1330 | * Acquire base lock for updating the offsets and retrieving |
| 1331 | * the current time. |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1332 | */ |
Thomas Gleixner | 196951e | 2012-07-10 18:43:23 -0400 | [diff] [blame] | 1333 | raw_spin_lock(&cpu_base->lock); |
John Stultz | 5baefd6 | 2012-07-10 18:43:25 -0400 | [diff] [blame] | 1334 | now = hrtimer_update_base(cpu_base); |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1335 | cpu_base->nr_retries++; |
| 1336 | if (++retries < 3) |
| 1337 | goto retry; |
| 1338 | /* |
| 1339 | * Give the system a chance to do something else than looping |
| 1340 | * here. We stored the entry time, so we know exactly how long |
| 1341 | * we spent here. We schedule the next event this amount of |
| 1342 | * time away. |
| 1343 | */ |
| 1344 | cpu_base->nr_hangs++; |
| 1345 | cpu_base->hang_detected = 1; |
Thomas Gleixner | 196951e | 2012-07-10 18:43:23 -0400 | [diff] [blame] | 1346 | raw_spin_unlock(&cpu_base->lock); |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1347 | delta = ktime_sub(now, entry_time); |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1348 | if ((unsigned int)delta > cpu_base->max_hang_time) |
| 1349 | cpu_base->max_hang_time = (unsigned int) delta; |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1350 | /* |
| 1351 | * Limit it to a sensible value as we enforce a longer |
| 1352 | * delay. Give the CPU at least 100ms to catch up. |
| 1353 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1354 | if (delta > 100 * NSEC_PER_MSEC) |
Thomas Gleixner | 41d2e49 | 2009-11-13 17:05:44 +0100 | [diff] [blame] | 1355 | expires_next = ktime_add_ns(now, 100 * NSEC_PER_MSEC); |
| 1356 | else |
| 1357 | expires_next = ktime_add(now, delta); |
| 1358 | tick_program_event(expires_next, 1); |
| 1359 | printk_once(KERN_WARNING "hrtimer: interrupt took %llu ns\n", |
| 1360 | ktime_to_ns(delta)); |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
Stephen Boyd | 016da20 | 2017-03-16 18:08:13 -0700 | [diff] [blame] | 1363 | /* called with interrupts disabled */ |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 1364 | static inline void __hrtimer_peek_ahead_timers(void) |
Thomas Gleixner | 8bdec95 | 2009-01-05 11:28:19 +0100 | [diff] [blame] | 1365 | { |
| 1366 | struct tick_device *td; |
| 1367 | |
| 1368 | if (!hrtimer_hres_active()) |
| 1369 | return; |
| 1370 | |
Christoph Lameter | 22127e9 | 2014-08-17 12:30:25 -0500 | [diff] [blame] | 1371 | td = this_cpu_ptr(&tick_cpu_device); |
Thomas Gleixner | 8bdec95 | 2009-01-05 11:28:19 +0100 | [diff] [blame] | 1372 | if (td && td->evtdev) |
| 1373 | hrtimer_interrupt(td->evtdev); |
| 1374 | } |
| 1375 | |
Ingo Molnar | 82c5b7b | 2009-01-05 14:11:10 +0100 | [diff] [blame] | 1376 | #else /* CONFIG_HIGH_RES_TIMERS */ |
| 1377 | |
| 1378 | static inline void __hrtimer_peek_ahead_timers(void) { } |
| 1379 | |
| 1380 | #endif /* !CONFIG_HIGH_RES_TIMERS */ |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1381 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1382 | /* |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 1383 | * Called from run_local_timers in hardirq context every jiffy |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1384 | */ |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1385 | void hrtimer_run_queues(void) |
| 1386 | { |
Christoph Lameter | dc5df73 | 2014-08-17 12:30:26 -0500 | [diff] [blame] | 1387 | struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases); |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1388 | ktime_t now; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1389 | |
Thomas Gleixner | e19ffe8 | 2015-04-14 21:08:39 +0000 | [diff] [blame] | 1390 | if (__hrtimer_hres_active(cpu_base)) |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1391 | return; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1392 | |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 1393 | /* |
| 1394 | * This _is_ ugly: We have to check periodically, whether we |
| 1395 | * can switch to highres and / or nohz mode. The clocksource |
| 1396 | * switch happens with xtime_lock held. Notification from |
| 1397 | * there only sets the check bit in the tick_oneshot code, |
| 1398 | * otherwise we might deadlock vs. xtime_lock. |
| 1399 | */ |
| 1400 | if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) { |
| 1401 | hrtimer_switch_to_hres(); |
| 1402 | return; |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1403 | } |
Thomas Gleixner | c6eb3f7 | 2015-04-14 21:08:51 +0000 | [diff] [blame] | 1404 | |
Thomas Gleixner | 21d6d52 | 2015-04-14 21:08:35 +0000 | [diff] [blame] | 1405 | raw_spin_lock(&cpu_base->lock); |
| 1406 | now = hrtimer_update_base(cpu_base); |
| 1407 | __hrtimer_run_queues(cpu_base, now); |
| 1408 | raw_spin_unlock(&cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | /* |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1412 | * Sleep related functions: |
| 1413 | */ |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1414 | static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 1415 | { |
| 1416 | struct hrtimer_sleeper *t = |
| 1417 | container_of(timer, struct hrtimer_sleeper, timer); |
| 1418 | struct task_struct *task = t->task; |
| 1419 | |
| 1420 | t->task = NULL; |
| 1421 | if (task) |
| 1422 | wake_up_process(task); |
| 1423 | |
| 1424 | return HRTIMER_NORESTART; |
| 1425 | } |
| 1426 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1427 | void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 1428 | { |
| 1429 | sl->timer.function = hrtimer_wakeup; |
| 1430 | sl->task = task; |
| 1431 | } |
Stephen Hemminger | 2bc481c | 2009-08-28 23:41:29 -0700 | [diff] [blame] | 1432 | EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 1433 | |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1434 | int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts) |
Al Viro | ce41aaf | 2017-06-07 09:42:32 +0100 | [diff] [blame] | 1435 | { |
| 1436 | switch(restart->nanosleep.type) { |
| 1437 | #ifdef CONFIG_COMPAT |
| 1438 | case TT_COMPAT: |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1439 | if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp)) |
Al Viro | ce41aaf | 2017-06-07 09:42:32 +0100 | [diff] [blame] | 1440 | return -EFAULT; |
| 1441 | break; |
| 1442 | #endif |
| 1443 | case TT_NATIVE: |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1444 | if (put_timespec64(ts, restart->nanosleep.rmtp)) |
Al Viro | ce41aaf | 2017-06-07 09:42:32 +0100 | [diff] [blame] | 1445 | return -EFAULT; |
| 1446 | break; |
| 1447 | default: |
| 1448 | BUG(); |
| 1449 | } |
| 1450 | return -ERESTART_RESTARTBLOCK; |
| 1451 | } |
| 1452 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1453 | static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1454 | { |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1455 | struct restart_block *restart; |
| 1456 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1457 | hrtimer_init_sleeper(t, current); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1458 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1459 | do { |
| 1460 | set_current_state(TASK_INTERRUPTIBLE); |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 1461 | hrtimer_start_expires(&t->timer, mode); |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1462 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1463 | if (likely(t->task)) |
Colin Cross | b0f8c44 | 2013-05-06 23:50:19 +0000 | [diff] [blame] | 1464 | freezable_schedule(); |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1465 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1466 | hrtimer_cancel(&t->timer); |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1467 | mode = HRTIMER_MODE_ABS; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1468 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1469 | } while (t->task && !signal_pending(current)); |
| 1470 | |
Peter Zijlstra | 3588a08 | 2008-02-01 17:45:13 +0100 | [diff] [blame] | 1471 | __set_current_state(TASK_RUNNING); |
| 1472 | |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1473 | if (!t->task) |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1474 | return 0; |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1475 | |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1476 | restart = ¤t->restart_block; |
| 1477 | if (restart->nanosleep.type != TT_NONE) { |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1478 | ktime_t rem = hrtimer_expires_remaining(&t->timer); |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1479 | struct timespec64 rmt; |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1480 | |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1481 | if (rem <= 0) |
| 1482 | return 0; |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1483 | rmt = ktime_to_timespec64(rem); |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1484 | |
Al Viro | ce41aaf | 2017-06-07 09:42:32 +0100 | [diff] [blame] | 1485 | return nanosleep_copyout(restart, &rmt); |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1486 | } |
| 1487 | return -ERESTART_RESTARTBLOCK; |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1488 | } |
| 1489 | |
Al Viro | fb923c4 | 2017-06-07 09:42:33 +0100 | [diff] [blame] | 1490 | static long __sched hrtimer_nanosleep_restart(struct restart_block *restart) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1491 | { |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1492 | struct hrtimer_sleeper t; |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1493 | int ret; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1494 | |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1495 | hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid, |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1496 | HRTIMER_MODE_ABS); |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 1497 | hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1498 | |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1499 | ret = do_nanosleep(&t, HRTIMER_MODE_ABS); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1500 | destroy_hrtimer_on_stack(&t.timer); |
| 1501 | return ret; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Thomas Gleixner | 938e7cf | 2017-06-13 23:34:33 +0200 | [diff] [blame] | 1504 | long hrtimer_nanosleep(const struct timespec64 *rqtp, |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1505 | const enum hrtimer_mode mode, const clockid_t clockid) |
| 1506 | { |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1507 | struct restart_block *restart; |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1508 | struct hrtimer_sleeper t; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1509 | int ret = 0; |
John Stultz | da8b44d | 2016-03-17 14:20:51 -0700 | [diff] [blame] | 1510 | u64 slack; |
Arjan van de Ven | 3bd0120 | 2008-09-08 08:58:59 -0700 | [diff] [blame] | 1511 | |
| 1512 | slack = current->timer_slack_ns; |
Dario Faggioli | aab03e0 | 2013-11-28 11:14:43 +0100 | [diff] [blame] | 1513 | if (dl_task(current) || rt_task(current)) |
Arjan van de Ven | 3bd0120 | 2008-09-08 08:58:59 -0700 | [diff] [blame] | 1514 | slack = 0; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1515 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1516 | hrtimer_init_on_stack(&t.timer, clockid, mode); |
Deepa Dinamani | ad19638 | 2017-03-26 12:04:18 -0700 | [diff] [blame] | 1517 | hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1518 | ret = do_nanosleep(&t, mode); |
| 1519 | if (ret != -ERESTART_RESTARTBLOCK) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1520 | goto out; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1521 | |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1522 | /* Absolute timers do not update the rmtp value and restart: */ |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1523 | if (mode == HRTIMER_MODE_ABS) { |
| 1524 | ret = -ERESTARTNOHAND; |
| 1525 | goto out; |
| 1526 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1527 | |
Al Viro | a760268 | 2017-06-07 09:42:29 +0100 | [diff] [blame] | 1528 | restart = ¤t->restart_block; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1529 | restart->fn = hrtimer_nanosleep_restart; |
Thomas Gleixner | ab8177b | 2011-05-20 13:05:15 +0200 | [diff] [blame] | 1530 | restart->nanosleep.clockid = t.timer.base->clockid; |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 1531 | restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1532 | out: |
| 1533 | destroy_hrtimer_on_stack(&t.timer); |
| 1534 | return ret; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Heiko Carstens | 58fd3aa | 2009-01-14 14:14:03 +0100 | [diff] [blame] | 1537 | SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp, |
| 1538 | struct timespec __user *, rmtp) |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1539 | { |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1540 | struct timespec64 tu; |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1541 | |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1542 | if (get_timespec64(&tu, rqtp)) |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1543 | return -EFAULT; |
| 1544 | |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1545 | if (!timespec64_valid(&tu)) |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1546 | return -EINVAL; |
| 1547 | |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1548 | current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; |
Al Viro | 192a82f | 2017-06-07 09:42:28 +0100 | [diff] [blame] | 1549 | current->restart_block.nanosleep.rmtp = rmtp; |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1550 | return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1553 | #ifdef CONFIG_COMPAT |
| 1554 | |
| 1555 | COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp, |
| 1556 | struct compat_timespec __user *, rmtp) |
| 1557 | { |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1558 | struct timespec64 tu; |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1559 | |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1560 | if (compat_get_timespec64(&tu, rqtp)) |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1561 | return -EFAULT; |
| 1562 | |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1563 | if (!timespec64_valid(&tu)) |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1564 | return -EINVAL; |
| 1565 | |
| 1566 | current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; |
| 1567 | current->restart_block.nanosleep.compat_rmtp = rmtp; |
Deepa Dinamani | c0edd7c | 2017-06-24 11:45:06 -0700 | [diff] [blame] | 1568 | return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
Al Viro | edbeda4 | 2017-06-07 09:42:31 +0100 | [diff] [blame] | 1569 | } |
| 1570 | #endif |
| 1571 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1572 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1573 | * Functions related to boot-time initialization: |
| 1574 | */ |
Thomas Gleixner | 27590dc | 2016-07-15 10:41:04 +0200 | [diff] [blame] | 1575 | int hrtimers_prepare_cpu(unsigned int cpu) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1576 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1577 | struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1578 | int i; |
| 1579 | |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1580 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1581 | cpu_base->clock_base[i].cpu_base = cpu_base; |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1582 | timerqueue_init_head(&cpu_base->clock_base[i].active); |
| 1583 | } |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1584 | |
Viresh Kumar | cddd024 | 2014-06-22 01:29:15 +0200 | [diff] [blame] | 1585 | cpu_base->cpu = cpu; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1586 | hrtimer_init_hres(cpu_base); |
Thomas Gleixner | 27590dc | 2016-07-15 10:41:04 +0200 | [diff] [blame] | 1587 | return 0; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | #ifdef CONFIG_HOTPLUG_CPU |
| 1591 | |
Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 1592 | static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, |
Peter Zijlstra | 3781065 | 2008-12-04 11:17:10 +0100 | [diff] [blame] | 1593 | struct hrtimer_clock_base *new_base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1594 | { |
| 1595 | struct hrtimer *timer; |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1596 | struct timerqueue_node *node; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1597 | |
John Stultz | 998adc3 | 2010-09-20 19:19:17 -0700 | [diff] [blame] | 1598 | while ((node = timerqueue_getnext(&old_base->active))) { |
| 1599 | timer = container_of(node, struct hrtimer, node); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1600 | BUG_ON(hrtimer_callback_running(timer)); |
Xiao Guangrong | c6a2a17 | 2009-08-10 10:51:23 +0800 | [diff] [blame] | 1601 | debug_deactivate(timer); |
Thomas Gleixner | b00c1a9 | 2008-09-29 15:44:46 +0200 | [diff] [blame] | 1602 | |
| 1603 | /* |
Oleg Nesterov | c04dca0 | 2015-06-11 14:46:44 +0200 | [diff] [blame] | 1604 | * Mark it as ENQUEUED not INACTIVE otherwise the |
Thomas Gleixner | b00c1a9 | 2008-09-29 15:44:46 +0200 | [diff] [blame] | 1605 | * timer could be seen as !active and just vanish away |
| 1606 | * under us on another CPU |
| 1607 | */ |
Oleg Nesterov | c04dca0 | 2015-06-11 14:46:44 +0200 | [diff] [blame] | 1608 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_ENQUEUED, 0); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1609 | timer->base = new_base; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1610 | /* |
Thomas Gleixner | e3f1d88 | 2009-01-05 11:28:23 +0100 | [diff] [blame] | 1611 | * Enqueue the timers on the new cpu. This does not |
| 1612 | * reprogram the event device in case the timer |
| 1613 | * expires before the earliest on this CPU, but we run |
| 1614 | * hrtimer_interrupt after we migrated everything to |
| 1615 | * sort out already expired timers and reprogram the |
| 1616 | * event device. |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1617 | */ |
Peter Zijlstra | a6037b6 | 2009-01-05 11:28:22 +0100 | [diff] [blame] | 1618 | enqueue_hrtimer(timer, new_base); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | |
Thomas Gleixner | 27590dc | 2016-07-15 10:41:04 +0200 | [diff] [blame] | 1622 | int hrtimers_dead_cpu(unsigned int scpu) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1623 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1624 | struct hrtimer_cpu_base *old_base, *new_base; |
Thomas Gleixner | 731a55b | 2009-01-05 11:28:21 +0100 | [diff] [blame] | 1625 | int i; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1626 | |
Peter Zijlstra | 3781065 | 2008-12-04 11:17:10 +0100 | [diff] [blame] | 1627 | BUG_ON(cpu_online(scpu)); |
Peter Zijlstra | 3781065 | 2008-12-04 11:17:10 +0100 | [diff] [blame] | 1628 | tick_cancel_sched_timer(scpu); |
Thomas Gleixner | 731a55b | 2009-01-05 11:28:21 +0100 | [diff] [blame] | 1629 | |
| 1630 | local_irq_disable(); |
| 1631 | old_base = &per_cpu(hrtimer_bases, scpu); |
Christoph Lameter | dc5df73 | 2014-08-17 12:30:26 -0500 | [diff] [blame] | 1632 | new_base = this_cpu_ptr(&hrtimer_bases); |
Oleg Nesterov | d82f0b0 | 2008-08-20 16:46:04 -0700 | [diff] [blame] | 1633 | /* |
| 1634 | * The caller is globally serialized and nobody else |
| 1635 | * takes two locks at once, deadlock is not possible. |
| 1636 | */ |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1637 | raw_spin_lock(&new_base->lock); |
| 1638 | raw_spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1639 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1640 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
Peter Zijlstra | ca10949 | 2008-11-25 12:43:51 +0100 | [diff] [blame] | 1641 | migrate_hrtimer_list(&old_base->clock_base[i], |
Peter Zijlstra | 3781065 | 2008-12-04 11:17:10 +0100 | [diff] [blame] | 1642 | &new_base->clock_base[i]); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1643 | } |
| 1644 | |
Thomas Gleixner | ecb49d1 | 2009-11-17 16:36:54 +0100 | [diff] [blame] | 1645 | raw_spin_unlock(&old_base->lock); |
| 1646 | raw_spin_unlock(&new_base->lock); |
Peter Zijlstra | 3781065 | 2008-12-04 11:17:10 +0100 | [diff] [blame] | 1647 | |
Thomas Gleixner | 731a55b | 2009-01-05 11:28:21 +0100 | [diff] [blame] | 1648 | /* Check, if we got expired work to do */ |
| 1649 | __hrtimer_peek_ahead_timers(); |
| 1650 | local_irq_enable(); |
Thomas Gleixner | 27590dc | 2016-07-15 10:41:04 +0200 | [diff] [blame] | 1651 | return 0; |
Peter Zijlstra | 3781065 | 2008-12-04 11:17:10 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1654 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 1655 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1656 | void __init hrtimers_init(void) |
| 1657 | { |
Thomas Gleixner | 27590dc | 2016-07-15 10:41:04 +0200 | [diff] [blame] | 1658 | hrtimers_prepare_cpu(smp_processor_id()); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Arjan van de Ven | 7bb6743 | 2008-08-31 08:05:58 -0700 | [diff] [blame] | 1661 | /** |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1662 | * schedule_hrtimeout_range_clock - sleep until timeout |
| 1663 | * @expires: timeout value (ktime_t) |
| 1664 | * @delta: slack in expires timeout (ktime_t) |
| 1665 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL |
| 1666 | * @clock: timer clock, CLOCK_MONOTONIC or CLOCK_REALTIME |
| 1667 | */ |
| 1668 | int __sched |
John Stultz | da8b44d | 2016-03-17 14:20:51 -0700 | [diff] [blame] | 1669 | schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta, |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1670 | const enum hrtimer_mode mode, int clock) |
| 1671 | { |
| 1672 | struct hrtimer_sleeper t; |
| 1673 | |
| 1674 | /* |
| 1675 | * Optimize when a zero timeout value is given. It does not |
| 1676 | * matter whether this is an absolute or a relative time. |
| 1677 | */ |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 1678 | if (expires && *expires == 0) { |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1679 | __set_current_state(TASK_RUNNING); |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
Namhyung Kim | 43b2101 | 2010-12-22 19:01:47 +0100 | [diff] [blame] | 1684 | * A NULL parameter means "infinite" |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1685 | */ |
| 1686 | if (!expires) { |
| 1687 | schedule(); |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1688 | return -EINTR; |
| 1689 | } |
| 1690 | |
| 1691 | hrtimer_init_on_stack(&t.timer, clock, mode); |
| 1692 | hrtimer_set_expires_range_ns(&t.timer, *expires, delta); |
| 1693 | |
| 1694 | hrtimer_init_sleeper(&t, current); |
| 1695 | |
| 1696 | hrtimer_start_expires(&t.timer, mode); |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1697 | |
| 1698 | if (likely(t.task)) |
| 1699 | schedule(); |
| 1700 | |
| 1701 | hrtimer_cancel(&t.timer); |
| 1702 | destroy_hrtimer_on_stack(&t.timer); |
| 1703 | |
| 1704 | __set_current_state(TASK_RUNNING); |
| 1705 | |
| 1706 | return !t.task ? 0 : -EINTR; |
| 1707 | } |
| 1708 | |
| 1709 | /** |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1710 | * schedule_hrtimeout_range - sleep until timeout |
| 1711 | * @expires: timeout value (ktime_t) |
| 1712 | * @delta: slack in expires timeout (ktime_t) |
| 1713 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL |
| 1714 | * |
| 1715 | * Make the current task sleep until the given expiry time has |
| 1716 | * elapsed. The routine will return immediately unless |
| 1717 | * the current task state has been set (see set_current_state()). |
| 1718 | * |
| 1719 | * The @delta argument gives the kernel the freedom to schedule the |
| 1720 | * actual wakeup to a time that is both power and performance friendly. |
| 1721 | * The kernel give the normal best effort behavior for "@expires+@delta", |
| 1722 | * but may decide to fire the timer earlier, but no earlier than @expires. |
| 1723 | * |
| 1724 | * You can set the task state as follows - |
| 1725 | * |
| 1726 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to |
Douglas Anderson | 4b7e9cf | 2016-10-21 08:58:51 -0700 | [diff] [blame] | 1727 | * pass before the routine returns unless the current task is explicitly |
| 1728 | * woken up, (e.g. by wake_up_process()). |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1729 | * |
| 1730 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is |
Douglas Anderson | 4b7e9cf | 2016-10-21 08:58:51 -0700 | [diff] [blame] | 1731 | * delivered to the current task or the current task is explicitly woken |
| 1732 | * up. |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1733 | * |
| 1734 | * The current task state is guaranteed to be TASK_RUNNING when this |
| 1735 | * routine returns. |
| 1736 | * |
Douglas Anderson | 4b7e9cf | 2016-10-21 08:58:51 -0700 | [diff] [blame] | 1737 | * Returns 0 when the timer has expired. If the task was woken before the |
| 1738 | * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or |
| 1739 | * by an explicit wakeup, it returns -EINTR. |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1740 | */ |
John Stultz | da8b44d | 2016-03-17 14:20:51 -0700 | [diff] [blame] | 1741 | int __sched schedule_hrtimeout_range(ktime_t *expires, u64 delta, |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1742 | const enum hrtimer_mode mode) |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1743 | { |
Carsten Emde | 351b3f7 | 2010-04-02 22:40:19 +0200 | [diff] [blame] | 1744 | return schedule_hrtimeout_range_clock(expires, delta, mode, |
| 1745 | CLOCK_MONOTONIC); |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1746 | } |
| 1747 | EXPORT_SYMBOL_GPL(schedule_hrtimeout_range); |
| 1748 | |
| 1749 | /** |
Arjan van de Ven | 7bb6743 | 2008-08-31 08:05:58 -0700 | [diff] [blame] | 1750 | * schedule_hrtimeout - sleep until timeout |
| 1751 | * @expires: timeout value (ktime_t) |
| 1752 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL |
| 1753 | * |
| 1754 | * Make the current task sleep until the given expiry time has |
| 1755 | * elapsed. The routine will return immediately unless |
| 1756 | * the current task state has been set (see set_current_state()). |
| 1757 | * |
| 1758 | * You can set the task state as follows - |
| 1759 | * |
| 1760 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to |
Douglas Anderson | 4b7e9cf | 2016-10-21 08:58:51 -0700 | [diff] [blame] | 1761 | * pass before the routine returns unless the current task is explicitly |
| 1762 | * woken up, (e.g. by wake_up_process()). |
Arjan van de Ven | 7bb6743 | 2008-08-31 08:05:58 -0700 | [diff] [blame] | 1763 | * |
| 1764 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is |
Douglas Anderson | 4b7e9cf | 2016-10-21 08:58:51 -0700 | [diff] [blame] | 1765 | * delivered to the current task or the current task is explicitly woken |
| 1766 | * up. |
Arjan van de Ven | 7bb6743 | 2008-08-31 08:05:58 -0700 | [diff] [blame] | 1767 | * |
| 1768 | * The current task state is guaranteed to be TASK_RUNNING when this |
| 1769 | * routine returns. |
| 1770 | * |
Douglas Anderson | 4b7e9cf | 2016-10-21 08:58:51 -0700 | [diff] [blame] | 1771 | * Returns 0 when the timer has expired. If the task was woken before the |
| 1772 | * timer expired by a signal (only possible in state TASK_INTERRUPTIBLE) or |
| 1773 | * by an explicit wakeup, it returns -EINTR. |
Arjan van de Ven | 7bb6743 | 2008-08-31 08:05:58 -0700 | [diff] [blame] | 1774 | */ |
| 1775 | int __sched schedule_hrtimeout(ktime_t *expires, |
| 1776 | const enum hrtimer_mode mode) |
| 1777 | { |
Arjan van de Ven | 654c8e0 | 2008-09-01 15:47:08 -0700 | [diff] [blame] | 1778 | return schedule_hrtimeout_range(expires, 0, mode); |
Arjan van de Ven | 7bb6743 | 2008-08-31 08:05:58 -0700 | [diff] [blame] | 1779 | } |
| 1780 | EXPORT_SYMBOL_GPL(schedule_hrtimeout); |