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