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