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> |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 35 | #include <linux/irq.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 36 | #include <linux/module.h> |
| 37 | #include <linux/percpu.h> |
| 38 | #include <linux/hrtimer.h> |
| 39 | #include <linux/notifier.h> |
| 40 | #include <linux/syscalls.h> |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 41 | #include <linux/kallsyms.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 42 | #include <linux/interrupt.h> |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 43 | #include <linux/tick.h> |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
| 45 | #include <linux/err.h> |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 46 | #include <linux/debugobjects.h> |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 47 | |
| 48 | #include <asm/uaccess.h> |
| 49 | |
| 50 | /** |
| 51 | * ktime_get - get the monotonic time in ktime_t format |
| 52 | * |
| 53 | * returns the time in ktime_t format |
| 54 | */ |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 55 | ktime_t ktime_get(void) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 56 | { |
| 57 | struct timespec now; |
| 58 | |
| 59 | ktime_get_ts(&now); |
| 60 | |
| 61 | return timespec_to_ktime(now); |
| 62 | } |
Patrick McHardy | 641b9e0 | 2007-03-16 01:18:42 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(ktime_get); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 67 | * |
| 68 | * returns the time in ktime_t format |
| 69 | */ |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 70 | ktime_t ktime_get_real(void) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 71 | { |
| 72 | struct timespec now; |
| 73 | |
| 74 | getnstimeofday(&now); |
| 75 | |
| 76 | return timespec_to_ktime(now); |
| 77 | } |
| 78 | |
| 79 | EXPORT_SYMBOL_GPL(ktime_get_real); |
| 80 | |
| 81 | /* |
| 82 | * The timer bases: |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 83 | * |
| 84 | * Note: If we want to add new timer bases, we have to skip the two |
| 85 | * clock ids captured by the cpu-timers. We do this by holding empty |
| 86 | * entries rather than doing math adjustment of the clock ids. |
| 87 | * This ensures that we capture erroneous accesses to these clock ids |
| 88 | * rather than moving them into the range of valid clock id's. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 89 | */ |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 90 | DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 91 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 92 | |
| 93 | .clock_base = |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 94 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 95 | { |
| 96 | .index = CLOCK_REALTIME, |
| 97 | .get_time = &ktime_get_real, |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 98 | .resolution = KTIME_LOW_RES, |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 99 | }, |
| 100 | { |
| 101 | .index = CLOCK_MONOTONIC, |
| 102 | .get_time = &ktime_get, |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 103 | .resolution = KTIME_LOW_RES, |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 104 | }, |
| 105 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * ktime_get_ts - get the monotonic clock in timespec format |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 110 | * @ts: pointer to timespec variable |
| 111 | * |
| 112 | * The function calculates the monotonic clock from the realtime |
| 113 | * clock and the wall_to_monotonic offset and stores the result |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 114 | * in normalized timespec format in the variable pointed to by @ts. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 115 | */ |
| 116 | void ktime_get_ts(struct timespec *ts) |
| 117 | { |
| 118 | struct timespec tomono; |
| 119 | unsigned long seq; |
| 120 | |
| 121 | do { |
| 122 | seq = read_seqbegin(&xtime_lock); |
| 123 | getnstimeofday(ts); |
| 124 | tomono = wall_to_monotonic; |
| 125 | |
| 126 | } while (read_seqretry(&xtime_lock, seq)); |
| 127 | |
| 128 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, |
| 129 | ts->tv_nsec + tomono.tv_nsec); |
| 130 | } |
Matt Helsley | 69778e3 | 2006-01-09 20:52:39 -0800 | [diff] [blame] | 131 | EXPORT_SYMBOL_GPL(ktime_get_ts); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 132 | |
| 133 | /* |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 134 | * Get the coarse grained time at the softirq based on xtime and |
| 135 | * wall_to_monotonic. |
| 136 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 137 | static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base) |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 138 | { |
| 139 | ktime_t xtim, tomono; |
Thomas Gleixner | ad28d94 | 2007-03-16 13:38:21 -0800 | [diff] [blame] | 140 | struct timespec xts, tom; |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 141 | unsigned long seq; |
| 142 | |
| 143 | do { |
| 144 | seq = read_seqbegin(&xtime_lock); |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 145 | xts = current_kernel_time(); |
Thomas Gleixner | ad28d94 | 2007-03-16 13:38:21 -0800 | [diff] [blame] | 146 | tom = wall_to_monotonic; |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 147 | } while (read_seqretry(&xtime_lock, seq)); |
| 148 | |
john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 149 | xtim = timespec_to_ktime(xts); |
Thomas Gleixner | ad28d94 | 2007-03-16 13:38:21 -0800 | [diff] [blame] | 150 | tomono = timespec_to_ktime(tom); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 151 | base->clock_base[CLOCK_REALTIME].softirq_time = xtim; |
| 152 | base->clock_base[CLOCK_MONOTONIC].softirq_time = |
| 153 | ktime_add(xtim, tomono); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 157 | * Functions and macros which are different for UP/SMP systems are kept in a |
| 158 | * single place |
| 159 | */ |
| 160 | #ifdef CONFIG_SMP |
| 161 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 162 | /* |
| 163 | * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock |
| 164 | * means that all timers which are tied to this base via timer->base are |
| 165 | * locked, and the base itself is locked too. |
| 166 | * |
| 167 | * So __run_timers/migrate_timers can safely modify all timers which could |
| 168 | * be found on the lists/queues. |
| 169 | * |
| 170 | * When the timer's base is locked, and the timer removed from list, it is |
| 171 | * possible to set timer->base = NULL and drop the lock: the timer remains |
| 172 | * locked. |
| 173 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 174 | static |
| 175 | struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer, |
| 176 | unsigned long *flags) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 177 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 178 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 179 | |
| 180 | for (;;) { |
| 181 | base = timer->base; |
| 182 | if (likely(base != NULL)) { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 183 | spin_lock_irqsave(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 184 | if (likely(base == timer->base)) |
| 185 | return base; |
| 186 | /* The timer has migrated to another CPU: */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 187 | spin_unlock_irqrestore(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 188 | } |
| 189 | cpu_relax(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Switch the timer base to the current CPU when possible. |
| 195 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 196 | static inline struct hrtimer_clock_base * |
| 197 | switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 198 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 199 | struct hrtimer_clock_base *new_base; |
| 200 | struct hrtimer_cpu_base *new_cpu_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 201 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 202 | new_cpu_base = &__get_cpu_var(hrtimer_bases); |
| 203 | new_base = &new_cpu_base->clock_base[base->index]; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 204 | |
| 205 | if (base != new_base) { |
| 206 | /* |
| 207 | * We are trying to schedule the timer on the local CPU. |
| 208 | * However we can't change timer's base while it is running, |
| 209 | * so we keep it on the same CPU. No hassle vs. reprogramming |
| 210 | * the event source in the high resolution case. The softirq |
| 211 | * code will take care of this when the timer function has |
| 212 | * completed. There is no conflict as we hold the lock until |
| 213 | * the timer is enqueued. |
| 214 | */ |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 215 | if (unlikely(hrtimer_callback_running(timer))) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 216 | return base; |
| 217 | |
| 218 | /* See the comment in lock_timer_base() */ |
| 219 | timer->base = NULL; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 220 | spin_unlock(&base->cpu_base->lock); |
| 221 | spin_lock(&new_base->cpu_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 222 | timer->base = new_base; |
| 223 | } |
| 224 | return new_base; |
| 225 | } |
| 226 | |
| 227 | #else /* CONFIG_SMP */ |
| 228 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 229 | static inline struct hrtimer_clock_base * |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 230 | lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 231 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 232 | struct hrtimer_clock_base *base = timer->base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 233 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 234 | spin_lock_irqsave(&base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 235 | |
| 236 | return base; |
| 237 | } |
| 238 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 239 | # define switch_hrtimer_base(t, b) (b) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 240 | |
| 241 | #endif /* !CONFIG_SMP */ |
| 242 | |
| 243 | /* |
| 244 | * Functions for the union type storage format of ktime_t which are |
| 245 | * too large for inlining: |
| 246 | */ |
| 247 | #if BITS_PER_LONG < 64 |
| 248 | # ifndef CONFIG_KTIME_SCALAR |
| 249 | /** |
| 250 | * 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] | 251 | * @kt: addend |
| 252 | * @nsec: the scalar nsec value to add |
| 253 | * |
| 254 | * Returns the sum of kt and nsec in ktime_t format |
| 255 | */ |
| 256 | ktime_t ktime_add_ns(const ktime_t kt, u64 nsec) |
| 257 | { |
| 258 | ktime_t tmp; |
| 259 | |
| 260 | if (likely(nsec < NSEC_PER_SEC)) { |
| 261 | tmp.tv64 = nsec; |
| 262 | } else { |
| 263 | unsigned long rem = do_div(nsec, NSEC_PER_SEC); |
| 264 | |
| 265 | tmp = ktime_set((long)nsec, rem); |
| 266 | } |
| 267 | |
| 268 | return ktime_add(kt, tmp); |
| 269 | } |
David Howells | b8b8fd2 | 2007-04-27 15:31:24 -0700 | [diff] [blame] | 270 | |
| 271 | EXPORT_SYMBOL_GPL(ktime_add_ns); |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable |
| 275 | * @kt: minuend |
| 276 | * @nsec: the scalar nsec value to subtract |
| 277 | * |
| 278 | * Returns the subtraction of @nsec from @kt in ktime_t format |
| 279 | */ |
| 280 | ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec) |
| 281 | { |
| 282 | ktime_t tmp; |
| 283 | |
| 284 | if (likely(nsec < NSEC_PER_SEC)) { |
| 285 | tmp.tv64 = nsec; |
| 286 | } else { |
| 287 | unsigned long rem = do_div(nsec, NSEC_PER_SEC); |
| 288 | |
| 289 | tmp = ktime_set((long)nsec, rem); |
| 290 | } |
| 291 | |
| 292 | return ktime_sub(kt, tmp); |
| 293 | } |
| 294 | |
| 295 | EXPORT_SYMBOL_GPL(ktime_sub_ns); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 296 | # endif /* !CONFIG_KTIME_SCALAR */ |
| 297 | |
| 298 | /* |
| 299 | * Divide a ktime value by a nanosecond value |
| 300 | */ |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 301 | u64 ktime_divns(const ktime_t kt, s64 div) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 302 | { |
| 303 | u64 dclc, inc, dns; |
| 304 | int sft = 0; |
| 305 | |
| 306 | dclc = dns = ktime_to_ns(kt); |
| 307 | inc = div; |
| 308 | /* Make sure the divisor is less than 2^32: */ |
| 309 | while (div >> 32) { |
| 310 | sft++; |
| 311 | div >>= 1; |
| 312 | } |
| 313 | dclc >>= sft; |
| 314 | do_div(dclc, (unsigned long) div); |
| 315 | |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 316 | return dclc; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 317 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 318 | #endif /* BITS_PER_LONG >= 64 */ |
| 319 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 320 | /* |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 321 | * Add two ktime values and do a safety check for overflow: |
| 322 | */ |
| 323 | ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs) |
| 324 | { |
| 325 | ktime_t res = ktime_add(lhs, rhs); |
| 326 | |
| 327 | /* |
| 328 | * We use KTIME_SEC_MAX here, the maximum timeout which we can |
| 329 | * return to user space in a timespec: |
| 330 | */ |
| 331 | if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64) |
| 332 | res = ktime_set(KTIME_SEC_MAX, 0); |
| 333 | |
| 334 | return res; |
| 335 | } |
| 336 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 337 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
| 338 | |
| 339 | static struct debug_obj_descr hrtimer_debug_descr; |
| 340 | |
| 341 | /* |
| 342 | * fixup_init is called when: |
| 343 | * - an active object is initialized |
| 344 | */ |
| 345 | static int hrtimer_fixup_init(void *addr, enum debug_obj_state state) |
| 346 | { |
| 347 | struct hrtimer *timer = addr; |
| 348 | |
| 349 | switch (state) { |
| 350 | case ODEBUG_STATE_ACTIVE: |
| 351 | hrtimer_cancel(timer); |
| 352 | debug_object_init(timer, &hrtimer_debug_descr); |
| 353 | return 1; |
| 354 | default: |
| 355 | return 0; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * fixup_activate is called when: |
| 361 | * - an active object is activated |
| 362 | * - an unknown object is activated (might be a statically initialized object) |
| 363 | */ |
| 364 | static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state) |
| 365 | { |
| 366 | switch (state) { |
| 367 | |
| 368 | case ODEBUG_STATE_NOTAVAILABLE: |
| 369 | WARN_ON_ONCE(1); |
| 370 | return 0; |
| 371 | |
| 372 | case ODEBUG_STATE_ACTIVE: |
| 373 | WARN_ON(1); |
| 374 | |
| 375 | default: |
| 376 | return 0; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * fixup_free is called when: |
| 382 | * - an active object is freed |
| 383 | */ |
| 384 | static int hrtimer_fixup_free(void *addr, enum debug_obj_state state) |
| 385 | { |
| 386 | struct hrtimer *timer = addr; |
| 387 | |
| 388 | switch (state) { |
| 389 | case ODEBUG_STATE_ACTIVE: |
| 390 | hrtimer_cancel(timer); |
| 391 | debug_object_free(timer, &hrtimer_debug_descr); |
| 392 | return 1; |
| 393 | default: |
| 394 | return 0; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static struct debug_obj_descr hrtimer_debug_descr = { |
| 399 | .name = "hrtimer", |
| 400 | .fixup_init = hrtimer_fixup_init, |
| 401 | .fixup_activate = hrtimer_fixup_activate, |
| 402 | .fixup_free = hrtimer_fixup_free, |
| 403 | }; |
| 404 | |
| 405 | static inline void debug_hrtimer_init(struct hrtimer *timer) |
| 406 | { |
| 407 | debug_object_init(timer, &hrtimer_debug_descr); |
| 408 | } |
| 409 | |
| 410 | static inline void debug_hrtimer_activate(struct hrtimer *timer) |
| 411 | { |
| 412 | debug_object_activate(timer, &hrtimer_debug_descr); |
| 413 | } |
| 414 | |
| 415 | static inline void debug_hrtimer_deactivate(struct hrtimer *timer) |
| 416 | { |
| 417 | debug_object_deactivate(timer, &hrtimer_debug_descr); |
| 418 | } |
| 419 | |
| 420 | static inline void debug_hrtimer_free(struct hrtimer *timer) |
| 421 | { |
| 422 | debug_object_free(timer, &hrtimer_debug_descr); |
| 423 | } |
| 424 | |
| 425 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 426 | enum hrtimer_mode mode); |
| 427 | |
| 428 | void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id, |
| 429 | enum hrtimer_mode mode) |
| 430 | { |
| 431 | debug_object_init_on_stack(timer, &hrtimer_debug_descr); |
| 432 | __hrtimer_init(timer, clock_id, mode); |
| 433 | } |
| 434 | |
| 435 | void destroy_hrtimer_on_stack(struct hrtimer *timer) |
| 436 | { |
| 437 | debug_object_free(timer, &hrtimer_debug_descr); |
| 438 | } |
| 439 | |
| 440 | #else |
| 441 | static inline void debug_hrtimer_init(struct hrtimer *timer) { } |
| 442 | static inline void debug_hrtimer_activate(struct hrtimer *timer) { } |
| 443 | static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } |
| 444 | #endif |
| 445 | |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 446 | /* |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 447 | * Check, whether the timer is on the callback pending list |
| 448 | */ |
| 449 | static inline int hrtimer_cb_pending(const struct hrtimer *timer) |
| 450 | { |
| 451 | return timer->state & HRTIMER_STATE_PENDING; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Remove a timer from the callback pending list |
| 456 | */ |
| 457 | static inline void hrtimer_remove_cb_pending(struct hrtimer *timer) |
| 458 | { |
| 459 | list_del_init(&timer->cb_entry); |
| 460 | } |
| 461 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 462 | /* High resolution timer related functions */ |
| 463 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 464 | |
| 465 | /* |
| 466 | * High resolution timer enabled ? |
| 467 | */ |
| 468 | static int hrtimer_hres_enabled __read_mostly = 1; |
| 469 | |
| 470 | /* |
| 471 | * Enable / Disable high resolution mode |
| 472 | */ |
| 473 | static int __init setup_hrtimer_hres(char *str) |
| 474 | { |
| 475 | if (!strcmp(str, "off")) |
| 476 | hrtimer_hres_enabled = 0; |
| 477 | else if (!strcmp(str, "on")) |
| 478 | hrtimer_hres_enabled = 1; |
| 479 | else |
| 480 | return 0; |
| 481 | return 1; |
| 482 | } |
| 483 | |
| 484 | __setup("highres=", setup_hrtimer_hres); |
| 485 | |
| 486 | /* |
| 487 | * hrtimer_high_res_enabled - query, if the highres mode is enabled |
| 488 | */ |
| 489 | static inline int hrtimer_is_hres_enabled(void) |
| 490 | { |
| 491 | return hrtimer_hres_enabled; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Is the high resolution mode active ? |
| 496 | */ |
| 497 | static inline int hrtimer_hres_active(void) |
| 498 | { |
| 499 | return __get_cpu_var(hrtimer_bases).hres_active; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * Reprogram the event source with checking both queues for the |
| 504 | * next event |
| 505 | * Called with interrupts disabled and base->lock held |
| 506 | */ |
| 507 | static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base) |
| 508 | { |
| 509 | int i; |
| 510 | struct hrtimer_clock_base *base = cpu_base->clock_base; |
| 511 | ktime_t expires; |
| 512 | |
| 513 | cpu_base->expires_next.tv64 = KTIME_MAX; |
| 514 | |
| 515 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { |
| 516 | struct hrtimer *timer; |
| 517 | |
| 518 | if (!base->first) |
| 519 | continue; |
| 520 | timer = rb_entry(base->first, struct hrtimer, node); |
| 521 | expires = ktime_sub(timer->expires, base->offset); |
| 522 | if (expires.tv64 < cpu_base->expires_next.tv64) |
| 523 | cpu_base->expires_next = expires; |
| 524 | } |
| 525 | |
| 526 | if (cpu_base->expires_next.tv64 != KTIME_MAX) |
| 527 | tick_program_event(cpu_base->expires_next, 1); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Shared reprogramming for clock_realtime and clock_monotonic |
| 532 | * |
| 533 | * When a timer is enqueued and expires earlier than the already enqueued |
| 534 | * timers, we have to check, whether it expires earlier than the timer for |
| 535 | * which the clock event device was armed. |
| 536 | * |
| 537 | * Called with interrupts disabled and base->cpu_base.lock held |
| 538 | */ |
| 539 | static int hrtimer_reprogram(struct hrtimer *timer, |
| 540 | struct hrtimer_clock_base *base) |
| 541 | { |
| 542 | ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next; |
| 543 | ktime_t expires = ktime_sub(timer->expires, base->offset); |
| 544 | int res; |
| 545 | |
Thomas Gleixner | 63070a7 | 2008-02-14 00:58:36 +0100 | [diff] [blame] | 546 | WARN_ON_ONCE(timer->expires.tv64 < 0); |
| 547 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 548 | /* |
| 549 | * When the callback is running, we do not reprogram the clock event |
| 550 | * 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] | 551 | * the callback is executed in the hrtimer_interrupt context. The |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 552 | * reprogramming is handled either by the softirq, which called the |
| 553 | * callback or at the end of the hrtimer_interrupt. |
| 554 | */ |
| 555 | if (hrtimer_callback_running(timer)) |
| 556 | return 0; |
| 557 | |
Thomas Gleixner | 63070a7 | 2008-02-14 00:58:36 +0100 | [diff] [blame] | 558 | /* |
| 559 | * CLOCK_REALTIME timer might be requested with an absolute |
| 560 | * expiry time which is less than base->offset. Nothing wrong |
| 561 | * about that, just avoid to call into the tick code, which |
| 562 | * has now objections against negative expiry values. |
| 563 | */ |
| 564 | if (expires.tv64 < 0) |
| 565 | return -ETIME; |
| 566 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 567 | if (expires.tv64 >= expires_next->tv64) |
| 568 | return 0; |
| 569 | |
| 570 | /* |
| 571 | * Clockevents returns -ETIME, when the event was in the past. |
| 572 | */ |
| 573 | res = tick_program_event(expires, 0); |
| 574 | if (!IS_ERR_VALUE(res)) |
| 575 | *expires_next = expires; |
| 576 | return res; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | /* |
| 581 | * Retrigger next event is called after clock was set |
| 582 | * |
| 583 | * Called with interrupts disabled via on_each_cpu() |
| 584 | */ |
| 585 | static void retrigger_next_event(void *arg) |
| 586 | { |
| 587 | struct hrtimer_cpu_base *base; |
| 588 | struct timespec realtime_offset; |
| 589 | unsigned long seq; |
| 590 | |
| 591 | if (!hrtimer_hres_active()) |
| 592 | return; |
| 593 | |
| 594 | do { |
| 595 | seq = read_seqbegin(&xtime_lock); |
| 596 | set_normalized_timespec(&realtime_offset, |
| 597 | -wall_to_monotonic.tv_sec, |
| 598 | -wall_to_monotonic.tv_nsec); |
| 599 | } while (read_seqretry(&xtime_lock, seq)); |
| 600 | |
| 601 | base = &__get_cpu_var(hrtimer_bases); |
| 602 | |
| 603 | /* Adjust CLOCK_REALTIME offset */ |
| 604 | spin_lock(&base->lock); |
| 605 | base->clock_base[CLOCK_REALTIME].offset = |
| 606 | timespec_to_ktime(realtime_offset); |
| 607 | |
| 608 | hrtimer_force_reprogram(base); |
| 609 | spin_unlock(&base->lock); |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Clock realtime was set |
| 614 | * |
| 615 | * Change the offset of the realtime clock vs. the monotonic |
| 616 | * clock. |
| 617 | * |
| 618 | * We might have to reprogram the high resolution timer interrupt. On |
| 619 | * SMP we call the architecture specific code to retrigger _all_ high |
| 620 | * resolution timer interrupts. On UP we just disable interrupts and |
| 621 | * call the high resolution interrupt code. |
| 622 | */ |
| 623 | void clock_was_set(void) |
| 624 | { |
| 625 | /* Retrigger the CPU local events everywhere */ |
| 626 | on_each_cpu(retrigger_next_event, NULL, 0, 1); |
| 627 | } |
| 628 | |
| 629 | /* |
Ingo Molnar | 995f054 | 2007-04-07 12:05:00 +0200 | [diff] [blame] | 630 | * During resume we might have to reprogram the high resolution timer |
| 631 | * interrupt (on the local CPU): |
| 632 | */ |
| 633 | void hres_timers_resume(void) |
| 634 | { |
| 635 | WARN_ON_ONCE(num_online_cpus() > 1); |
| 636 | |
| 637 | /* Retrigger the CPU local events: */ |
| 638 | retrigger_next_event(NULL); |
| 639 | } |
| 640 | |
| 641 | /* |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 642 | * Initialize the high resolution related parts of cpu_base |
| 643 | */ |
| 644 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) |
| 645 | { |
| 646 | base->expires_next.tv64 = KTIME_MAX; |
| 647 | base->hres_active = 0; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Initialize the high resolution related parts of a hrtimer |
| 652 | */ |
| 653 | static inline void hrtimer_init_timer_hres(struct hrtimer *timer) |
| 654 | { |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* |
| 658 | * When High resolution timers are active, try to reprogram. Note, that in case |
| 659 | * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry |
| 660 | * check happens. The timer gets enqueued into the rbtree. The reprogramming |
| 661 | * and expiry check is done in the hrtimer_interrupt or in the softirq. |
| 662 | */ |
| 663 | static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, |
| 664 | struct hrtimer_clock_base *base) |
| 665 | { |
| 666 | if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) { |
| 667 | |
| 668 | /* Timer is expired, act upon the callback mode */ |
| 669 | switch(timer->cb_mode) { |
| 670 | case HRTIMER_CB_IRQSAFE_NO_RESTART: |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 671 | debug_hrtimer_deactivate(timer); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 672 | /* |
| 673 | * We can call the callback from here. No restart |
| 674 | * happens, so no danger of recursion |
| 675 | */ |
| 676 | BUG_ON(timer->function(timer) != HRTIMER_NORESTART); |
| 677 | return 1; |
| 678 | case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ: |
| 679 | /* |
| 680 | * This is solely for the sched tick emulation with |
| 681 | * dynamic tick support to ensure that we do not |
| 682 | * restart the tick right on the edge and end up with |
| 683 | * the tick timer in the softirq ! The calling site |
| 684 | * takes care of this. |
| 685 | */ |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 686 | debug_hrtimer_deactivate(timer); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 687 | return 1; |
| 688 | case HRTIMER_CB_IRQSAFE: |
| 689 | case HRTIMER_CB_SOFTIRQ: |
| 690 | /* |
| 691 | * Move everything else into the softirq pending list ! |
| 692 | */ |
| 693 | list_add_tail(&timer->cb_entry, |
| 694 | &base->cpu_base->cb_pending); |
| 695 | timer->state = HRTIMER_STATE_PENDING; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 696 | return 1; |
| 697 | default: |
| 698 | BUG(); |
| 699 | } |
| 700 | } |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 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 | { |
Ingo Molnar | 820de5c | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 709 | int cpu = smp_processor_id(); |
| 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; |
| 725 | base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES; |
| 726 | base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES; |
| 727 | |
| 728 | tick_setup_sched_timer(); |
| 729 | |
| 730 | /* "Retrigger" the interrupt to get things going */ |
| 731 | retrigger_next_event(NULL); |
| 732 | local_irq_restore(flags); |
Michael Ellerman | edfed66 | 2007-10-29 16:35:29 +1100 | [diff] [blame] | 733 | printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n", |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 734 | smp_processor_id()); |
Thomas Gleixner | f895385 | 2007-03-06 01:42:08 -0800 | [diff] [blame] | 735 | return 1; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Thomas Gleixner | 0c96c59 | 2008-04-28 09:23:24 +0200 | [diff] [blame] | 738 | static inline void hrtimer_raise_softirq(void) |
| 739 | { |
| 740 | raise_softirq(HRTIMER_SOFTIRQ); |
| 741 | } |
| 742 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 743 | #else |
| 744 | |
| 745 | static inline int hrtimer_hres_active(void) { return 0; } |
| 746 | static inline int hrtimer_is_hres_enabled(void) { return 0; } |
Thomas Gleixner | f895385 | 2007-03-06 01:42:08 -0800 | [diff] [blame] | 747 | static inline int hrtimer_switch_to_hres(void) { return 0; } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 748 | static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { } |
| 749 | static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, |
| 750 | struct hrtimer_clock_base *base) |
| 751 | { |
| 752 | return 0; |
| 753 | } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 754 | static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { } |
| 755 | static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { } |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 756 | static inline int hrtimer_reprogram(struct hrtimer *timer, |
| 757 | struct hrtimer_clock_base *base) |
| 758 | { |
| 759 | return 0; |
| 760 | } |
Thomas Gleixner | 0c96c59 | 2008-04-28 09:23:24 +0200 | [diff] [blame] | 761 | static inline void hrtimer_raise_softirq(void) { } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 762 | |
| 763 | #endif /* CONFIG_HIGH_RES_TIMERS */ |
| 764 | |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 765 | #ifdef CONFIG_TIMER_STATS |
| 766 | void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr) |
| 767 | { |
| 768 | if (timer->start_site) |
| 769 | return; |
| 770 | |
| 771 | timer->start_site = addr; |
| 772 | memcpy(timer->start_comm, current->comm, TASK_COMM_LEN); |
| 773 | timer->start_pid = current->pid; |
| 774 | } |
| 775 | #endif |
| 776 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 777 | /* |
Uwe Kleine-König | 6506f2a | 2007-10-20 01:56:53 +0200 | [diff] [blame] | 778 | * Counterpart to lock_hrtimer_base above: |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 779 | */ |
| 780 | static inline |
| 781 | void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 782 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 783 | spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /** |
| 787 | * hrtimer_forward - forward the timer expiry |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 788 | * @timer: hrtimer to forward |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 789 | * @now: forward past this time |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 790 | * @interval: the interval to forward |
| 791 | * |
| 792 | * Forward the timer expiry so it will expire in the future. |
Jonathan Corbet | 8dca6f3 | 2006-01-16 15:58:55 -0700 | [diff] [blame] | 793 | * Returns the number of overruns. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 794 | */ |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 795 | u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 796 | { |
Davide Libenzi | 4d672e7 | 2008-02-04 22:27:26 -0800 | [diff] [blame] | 797 | u64 orun = 1; |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 798 | ktime_t delta; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 799 | |
| 800 | delta = ktime_sub(now, timer->expires); |
| 801 | |
| 802 | if (delta.tv64 < 0) |
| 803 | return 0; |
| 804 | |
Thomas Gleixner | c9db4fa | 2006-01-12 11:47:34 +0100 | [diff] [blame] | 805 | if (interval.tv64 < timer->base->resolution.tv64) |
| 806 | interval.tv64 = timer->base->resolution.tv64; |
| 807 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 808 | if (unlikely(delta.tv64 >= interval.tv64)) { |
Roman Zippel | df869b6 | 2006-03-26 01:38:11 -0800 | [diff] [blame] | 809 | s64 incr = ktime_to_ns(interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 810 | |
| 811 | orun = ktime_divns(delta, incr); |
| 812 | timer->expires = ktime_add_ns(timer->expires, incr * orun); |
| 813 | if (timer->expires.tv64 > now.tv64) |
| 814 | return orun; |
| 815 | /* |
| 816 | * This (and the ktime_add() below) is the |
| 817 | * correction for exact: |
| 818 | */ |
| 819 | orun++; |
| 820 | } |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 821 | timer->expires = ktime_add_safe(timer->expires, interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 822 | |
| 823 | return orun; |
| 824 | } |
Stas Sergeev | 6bdb6b6 | 2007-05-08 00:31:58 -0700 | [diff] [blame] | 825 | EXPORT_SYMBOL_GPL(hrtimer_forward); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 826 | |
| 827 | /* |
| 828 | * enqueue_hrtimer - internal function to (re)start a timer |
| 829 | * |
| 830 | * The timer is inserted in expiry order. Insertion into the |
| 831 | * red black tree is O(log(n)). Must hold the base lock. |
| 832 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 833 | static void enqueue_hrtimer(struct hrtimer *timer, |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 834 | struct hrtimer_clock_base *base, int reprogram) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 835 | { |
| 836 | struct rb_node **link = &base->active.rb_node; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 837 | struct rb_node *parent = NULL; |
| 838 | struct hrtimer *entry; |
Ingo Molnar | 99bc2fc | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 839 | int leftmost = 1; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 840 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 841 | debug_hrtimer_activate(timer); |
| 842 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 843 | /* |
| 844 | * Find the right place in the rbtree: |
| 845 | */ |
| 846 | while (*link) { |
| 847 | parent = *link; |
| 848 | entry = rb_entry(parent, struct hrtimer, node); |
| 849 | /* |
| 850 | * We dont care about collisions. Nodes with |
| 851 | * the same expiry time stay together. |
| 852 | */ |
Ingo Molnar | 99bc2fc | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 853 | if (timer->expires.tv64 < entry->expires.tv64) { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 854 | link = &(*link)->rb_left; |
Ingo Molnar | 99bc2fc | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 855 | } else { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 856 | link = &(*link)->rb_right; |
Ingo Molnar | 99bc2fc | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 857 | leftmost = 0; |
| 858 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 862 | * Insert the timer to the rbtree and check whether it |
| 863 | * replaces the first pending timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 864 | */ |
Ingo Molnar | 99bc2fc | 2007-07-21 04:37:36 -0700 | [diff] [blame] | 865 | if (leftmost) { |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 866 | /* |
| 867 | * Reprogram the clock event device. When the timer is already |
| 868 | * expired hrtimer_enqueue_reprogram has either called the |
| 869 | * callback or added it to the pending list and raised the |
| 870 | * softirq. |
| 871 | * |
| 872 | * This is a NOP for !HIGHRES |
| 873 | */ |
| 874 | if (reprogram && hrtimer_enqueue_reprogram(timer, base)) |
| 875 | return; |
| 876 | |
| 877 | base->first = &timer->node; |
| 878 | } |
| 879 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 880 | rb_link_node(&timer->node, parent, link); |
| 881 | rb_insert_color(&timer->node, &base->active); |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 882 | /* |
| 883 | * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the |
| 884 | * state of a possibly running callback. |
| 885 | */ |
| 886 | timer->state |= HRTIMER_STATE_ENQUEUED; |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 887 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 888 | |
| 889 | /* |
| 890 | * __remove_hrtimer - internal function to remove a timer |
| 891 | * |
| 892 | * Caller must hold the base lock. |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 893 | * |
| 894 | * High resolution timer mode reprograms the clock event device when the |
| 895 | * timer is the one which expires next. The caller can disable this by setting |
| 896 | * reprogram to zero. This is useful, when the context does a reprogramming |
| 897 | * anyway (e.g. timer interrupt) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 898 | */ |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 899 | static void __remove_hrtimer(struct hrtimer *timer, |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 900 | struct hrtimer_clock_base *base, |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 901 | unsigned long newstate, int reprogram) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 902 | { |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 903 | /* High res. callback list. NOP for !HIGHRES */ |
| 904 | if (hrtimer_cb_pending(timer)) |
| 905 | hrtimer_remove_cb_pending(timer); |
| 906 | else { |
| 907 | /* |
| 908 | * Remove the timer from the rbtree and replace the |
| 909 | * first entry pointer if necessary. |
| 910 | */ |
| 911 | if (base->first == &timer->node) { |
| 912 | base->first = rb_next(&timer->node); |
| 913 | /* Reprogram the clock event device. if enabled */ |
| 914 | if (reprogram && hrtimer_hres_active()) |
| 915 | hrtimer_force_reprogram(base->cpu_base); |
| 916 | } |
| 917 | rb_erase(&timer->node, &base->active); |
| 918 | } |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 919 | timer->state = newstate; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /* |
| 923 | * remove hrtimer, called with base lock held |
| 924 | */ |
| 925 | static inline int |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 926 | remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 927 | { |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 928 | if (hrtimer_is_queued(timer)) { |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 929 | int reprogram; |
| 930 | |
| 931 | /* |
| 932 | * Remove the timer and force reprogramming when high |
| 933 | * resolution mode is active and the timer is on the current |
| 934 | * CPU. If we remove a timer on another CPU, reprogramming is |
| 935 | * skipped. The interrupt event on this CPU is fired and |
| 936 | * reprogramming happens in the interrupt handler. This is a |
| 937 | * rare case and less expensive than a smp call. |
| 938 | */ |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 939 | debug_hrtimer_deactivate(timer); |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 940 | timer_stats_hrtimer_clear_start_info(timer); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 941 | reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); |
| 942 | __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, |
| 943 | reprogram); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 944 | return 1; |
| 945 | } |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * hrtimer_start - (re)start an relative timer on the current CPU |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 951 | * @timer: the timer to be added |
| 952 | * @tim: expiry time |
| 953 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) |
| 954 | * |
| 955 | * Returns: |
| 956 | * 0 on success |
| 957 | * 1 when the timer was active |
| 958 | */ |
| 959 | int |
| 960 | hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) |
| 961 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 962 | struct hrtimer_clock_base *base, *new_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 963 | unsigned long flags; |
Thomas Gleixner | 0c96c59 | 2008-04-28 09:23:24 +0200 | [diff] [blame] | 964 | int ret, raise; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 965 | |
| 966 | base = lock_hrtimer_base(timer, &flags); |
| 967 | |
| 968 | /* Remove an active timer from the queue: */ |
| 969 | ret = remove_hrtimer(timer, base); |
| 970 | |
| 971 | /* Switch the timer base, if necessary: */ |
| 972 | new_base = switch_hrtimer_base(timer, base); |
| 973 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 974 | if (mode == HRTIMER_MODE_REL) { |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 975 | tim = ktime_add_safe(tim, new_base->get_time()); |
Ingo Molnar | 06027bd | 2006-02-14 13:53:15 -0800 | [diff] [blame] | 976 | /* |
| 977 | * CONFIG_TIME_LOW_RES is a temporary way for architectures |
| 978 | * to signal that they simply return xtime in |
| 979 | * do_gettimeoffset(). In this case we want to round up by |
| 980 | * resolution when starting a relative timer, to avoid short |
| 981 | * timeouts. This will go away with the GTOD framework. |
| 982 | */ |
| 983 | #ifdef CONFIG_TIME_LOW_RES |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 984 | tim = ktime_add_safe(tim, base->resolution); |
Ingo Molnar | 06027bd | 2006-02-14 13:53:15 -0800 | [diff] [blame] | 985 | #endif |
| 986 | } |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 987 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 988 | timer->expires = tim; |
| 989 | |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 990 | timer_stats_hrtimer_set_start_info(timer); |
| 991 | |
Ingo Molnar | 935c631 | 2007-03-28 13:17:18 +0200 | [diff] [blame] | 992 | /* |
| 993 | * Only allow reprogramming if the new base is on this CPU. |
| 994 | * (it might still be on another CPU if the timer was pending) |
| 995 | */ |
| 996 | enqueue_hrtimer(timer, new_base, |
| 997 | new_base->cpu_base == &__get_cpu_var(hrtimer_bases)); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 998 | |
Thomas Gleixner | 0c96c59 | 2008-04-28 09:23:24 +0200 | [diff] [blame] | 999 | /* |
| 1000 | * The timer may be expired and moved to the cb_pending |
| 1001 | * list. We can not raise the softirq with base lock held due |
| 1002 | * to a possible deadlock with runqueue lock. |
| 1003 | */ |
| 1004 | raise = timer->state == HRTIMER_STATE_PENDING; |
| 1005 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1006 | unlock_hrtimer_base(timer, &flags); |
| 1007 | |
Thomas Gleixner | 0c96c59 | 2008-04-28 09:23:24 +0200 | [diff] [blame] | 1008 | if (raise) |
| 1009 | hrtimer_raise_softirq(); |
| 1010 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1011 | return ret; |
| 1012 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1013 | EXPORT_SYMBOL_GPL(hrtimer_start); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1014 | |
| 1015 | /** |
| 1016 | * hrtimer_try_to_cancel - try to deactivate a timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1017 | * @timer: hrtimer to stop |
| 1018 | * |
| 1019 | * Returns: |
| 1020 | * 0 when the timer was not active |
| 1021 | * 1 when the timer was active |
| 1022 | * -1 when the timer is currently excuting the callback function and |
Randy Dunlap | fa9799e | 2006-06-25 05:49:15 -0700 | [diff] [blame] | 1023 | * cannot be stopped |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1024 | */ |
| 1025 | int hrtimer_try_to_cancel(struct hrtimer *timer) |
| 1026 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1027 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1028 | unsigned long flags; |
| 1029 | int ret = -1; |
| 1030 | |
| 1031 | base = lock_hrtimer_base(timer, &flags); |
| 1032 | |
Thomas Gleixner | 303e967 | 2007-02-16 01:27:51 -0800 | [diff] [blame] | 1033 | if (!hrtimer_callback_running(timer)) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1034 | ret = remove_hrtimer(timer, base); |
| 1035 | |
| 1036 | unlock_hrtimer_base(timer, &flags); |
| 1037 | |
| 1038 | return ret; |
| 1039 | |
| 1040 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1041 | EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1042 | |
| 1043 | /** |
| 1044 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1045 | * @timer: the timer to be cancelled |
| 1046 | * |
| 1047 | * Returns: |
| 1048 | * 0 when the timer was not active |
| 1049 | * 1 when the timer was active |
| 1050 | */ |
| 1051 | int hrtimer_cancel(struct hrtimer *timer) |
| 1052 | { |
| 1053 | for (;;) { |
| 1054 | int ret = hrtimer_try_to_cancel(timer); |
| 1055 | |
| 1056 | if (ret >= 0) |
| 1057 | return ret; |
Joe Korty | 5ef37b1 | 2006-04-10 22:54:13 -0700 | [diff] [blame] | 1058 | cpu_relax(); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1059 | } |
| 1060 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1061 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1062 | |
| 1063 | /** |
| 1064 | * hrtimer_get_remaining - get remaining time for the timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1065 | * @timer: the timer to read |
| 1066 | */ |
| 1067 | ktime_t hrtimer_get_remaining(const struct hrtimer *timer) |
| 1068 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1069 | struct hrtimer_clock_base *base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1070 | unsigned long flags; |
| 1071 | ktime_t rem; |
| 1072 | |
| 1073 | base = lock_hrtimer_base(timer, &flags); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1074 | rem = ktime_sub(timer->expires, base->get_time()); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1075 | unlock_hrtimer_base(timer, &flags); |
| 1076 | |
| 1077 | return rem; |
| 1078 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1079 | EXPORT_SYMBOL_GPL(hrtimer_get_remaining); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1080 | |
Thomas Gleixner | fd064b9 | 2007-02-16 01:27:47 -0800 | [diff] [blame] | 1081 | #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ) |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1082 | /** |
| 1083 | * hrtimer_get_next_event - get the time until next expiry event |
| 1084 | * |
| 1085 | * Returns the delta to the next expiry event or KTIME_MAX if no timer |
| 1086 | * is pending. |
| 1087 | */ |
| 1088 | ktime_t hrtimer_get_next_event(void) |
| 1089 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1090 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
| 1091 | struct hrtimer_clock_base *base = cpu_base->clock_base; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1092 | ktime_t delta, mindelta = { .tv64 = KTIME_MAX }; |
| 1093 | unsigned long flags; |
| 1094 | int i; |
| 1095 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1096 | spin_lock_irqsave(&cpu_base->lock, flags); |
| 1097 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1098 | if (!hrtimer_hres_active()) { |
| 1099 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) { |
| 1100 | struct hrtimer *timer; |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1101 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1102 | if (!base->first) |
| 1103 | continue; |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1104 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1105 | timer = rb_entry(base->first, struct hrtimer, node); |
| 1106 | delta.tv64 = timer->expires.tv64; |
| 1107 | delta = ktime_sub(delta, base->get_time()); |
| 1108 | if (delta.tv64 < mindelta.tv64) |
| 1109 | mindelta.tv64 = delta.tv64; |
| 1110 | } |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1111 | } |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1112 | |
| 1113 | spin_unlock_irqrestore(&cpu_base->lock, flags); |
| 1114 | |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 1115 | if (mindelta.tv64 < 0) |
| 1116 | mindelta.tv64 = 0; |
| 1117 | return mindelta; |
| 1118 | } |
| 1119 | #endif |
| 1120 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1121 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 1122 | enum hrtimer_mode mode) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1123 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1124 | struct hrtimer_cpu_base *cpu_base; |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1125 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1126 | memset(timer, 0, sizeof(struct hrtimer)); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1127 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1128 | cpu_base = &__raw_get_cpu_var(hrtimer_bases); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1129 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1130 | if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS) |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1131 | clock_id = CLOCK_MONOTONIC; |
| 1132 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1133 | timer->base = &cpu_base->clock_base[clock_id]; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1134 | INIT_LIST_HEAD(&timer->cb_entry); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1135 | hrtimer_init_timer_hres(timer); |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1136 | |
| 1137 | #ifdef CONFIG_TIMER_STATS |
| 1138 | timer->start_site = NULL; |
| 1139 | timer->start_pid = -1; |
| 1140 | memset(timer->start_comm, 0, TASK_COMM_LEN); |
| 1141 | #endif |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1142 | } |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1143 | |
| 1144 | /** |
| 1145 | * hrtimer_init - initialize a timer to the given clock |
| 1146 | * @timer: the timer to be initialized |
| 1147 | * @clock_id: the clock to be used |
| 1148 | * @mode: timer mode abs/rel |
| 1149 | */ |
| 1150 | void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 1151 | enum hrtimer_mode mode) |
| 1152 | { |
| 1153 | debug_hrtimer_init(timer); |
| 1154 | __hrtimer_init(timer, clock_id, mode); |
| 1155 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1156 | EXPORT_SYMBOL_GPL(hrtimer_init); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1157 | |
| 1158 | /** |
| 1159 | * hrtimer_get_res - get the timer resolution for a clock |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1160 | * @which_clock: which clock to query |
| 1161 | * @tp: pointer to timespec variable to store the resolution |
| 1162 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1163 | * Store the resolution of the clock selected by @which_clock in the |
| 1164 | * variable pointed to by @tp. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1165 | */ |
| 1166 | int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp) |
| 1167 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1168 | struct hrtimer_cpu_base *cpu_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1169 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1170 | cpu_base = &__raw_get_cpu_var(hrtimer_bases); |
| 1171 | *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1172 | |
| 1173 | return 0; |
| 1174 | } |
Stephen Hemminger | 8d16b76 | 2006-05-30 21:26:09 -0700 | [diff] [blame] | 1175 | EXPORT_SYMBOL_GPL(hrtimer_get_res); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1176 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1177 | static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base) |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1178 | { |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1179 | spin_lock_irq(&cpu_base->lock); |
| 1180 | |
| 1181 | while (!list_empty(&cpu_base->cb_pending)) { |
| 1182 | enum hrtimer_restart (*fn)(struct hrtimer *); |
| 1183 | struct hrtimer *timer; |
| 1184 | int restart; |
| 1185 | |
| 1186 | timer = list_entry(cpu_base->cb_pending.next, |
| 1187 | struct hrtimer, cb_entry); |
| 1188 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1189 | debug_hrtimer_deactivate(timer); |
Ingo Molnar | 82f67cd | 2007-02-16 01:28:13 -0800 | [diff] [blame] | 1190 | timer_stats_account_hrtimer(timer); |
| 1191 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1192 | fn = timer->function; |
| 1193 | __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0); |
| 1194 | spin_unlock_irq(&cpu_base->lock); |
| 1195 | |
| 1196 | restart = fn(timer); |
| 1197 | |
| 1198 | spin_lock_irq(&cpu_base->lock); |
| 1199 | |
| 1200 | timer->state &= ~HRTIMER_STATE_CALLBACK; |
| 1201 | if (restart == HRTIMER_RESTART) { |
| 1202 | BUG_ON(hrtimer_active(timer)); |
| 1203 | /* |
| 1204 | * Enqueue the timer, allow reprogramming of the event |
| 1205 | * device |
| 1206 | */ |
| 1207 | enqueue_hrtimer(timer, timer->base, 1); |
| 1208 | } else if (hrtimer_active(timer)) { |
| 1209 | /* |
| 1210 | * If the timer was rearmed on another CPU, reprogram |
| 1211 | * the event device. |
| 1212 | */ |
Bodo Stroesser | d7b41a2 | 2008-04-26 14:10:16 -0700 | [diff] [blame] | 1213 | struct hrtimer_clock_base *base = timer->base; |
| 1214 | |
| 1215 | if (base->first == &timer->node && |
| 1216 | hrtimer_reprogram(timer, base)) { |
| 1217 | /* |
| 1218 | * Timer is expired. Thus move it from tree to |
| 1219 | * pending list again. |
| 1220 | */ |
| 1221 | __remove_hrtimer(timer, base, |
| 1222 | HRTIMER_STATE_PENDING, 0); |
| 1223 | list_add_tail(&timer->cb_entry, |
| 1224 | &base->cpu_base->cb_pending); |
| 1225 | } |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | spin_unlock_irq(&cpu_base->lock); |
| 1229 | } |
| 1230 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1231 | static void __run_hrtimer(struct hrtimer *timer) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1232 | { |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1233 | struct hrtimer_clock_base *base = timer->base; |
| 1234 | struct hrtimer_cpu_base *cpu_base = base->cpu_base; |
| 1235 | enum hrtimer_restart (*fn)(struct hrtimer *); |
| 1236 | int restart; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1237 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1238 | debug_hrtimer_deactivate(timer); |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1239 | __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); |
| 1240 | timer_stats_account_hrtimer(timer); |
Dimitri Sivanich | 3055add | 2006-03-31 02:31:20 -0800 | [diff] [blame] | 1241 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1242 | fn = timer->function; |
| 1243 | if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) { |
| 1244 | /* |
| 1245 | * Used for scheduler timers, avoid lock inversion with |
| 1246 | * rq->lock and tasklist_lock. |
| 1247 | * |
| 1248 | * These timers are required to deal with enqueue expiry |
| 1249 | * themselves and are not allowed to migrate. |
| 1250 | */ |
| 1251 | spin_unlock(&cpu_base->lock); |
| 1252 | restart = fn(timer); |
| 1253 | spin_lock(&cpu_base->lock); |
| 1254 | } else |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame] | 1255 | restart = fn(timer); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1256 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1257 | /* |
| 1258 | * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid |
| 1259 | * reprogramming of the event hardware. This happens at the end of this |
| 1260 | * function anyway. |
| 1261 | */ |
| 1262 | if (restart != HRTIMER_NORESTART) { |
| 1263 | BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); |
| 1264 | enqueue_hrtimer(timer, base, 0); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1265 | } |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1266 | timer->state &= ~HRTIMER_STATE_CALLBACK; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1269 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 1270 | |
| 1271 | /* |
| 1272 | * High resolution timer interrupt |
| 1273 | * Called with interrupts disabled |
| 1274 | */ |
| 1275 | void hrtimer_interrupt(struct clock_event_device *dev) |
| 1276 | { |
| 1277 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
| 1278 | struct hrtimer_clock_base *base; |
| 1279 | ktime_t expires_next, now; |
| 1280 | int i, raise = 0; |
| 1281 | |
| 1282 | BUG_ON(!cpu_base->hres_active); |
| 1283 | cpu_base->nr_events++; |
| 1284 | dev->next_event.tv64 = KTIME_MAX; |
| 1285 | |
| 1286 | retry: |
| 1287 | now = ktime_get(); |
| 1288 | |
| 1289 | expires_next.tv64 = KTIME_MAX; |
| 1290 | |
| 1291 | base = cpu_base->clock_base; |
| 1292 | |
| 1293 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
| 1294 | ktime_t basenow; |
| 1295 | struct rb_node *node; |
| 1296 | |
| 1297 | spin_lock(&cpu_base->lock); |
| 1298 | |
| 1299 | basenow = ktime_add(now, base->offset); |
| 1300 | |
| 1301 | while ((node = base->first)) { |
| 1302 | struct hrtimer *timer; |
| 1303 | |
| 1304 | timer = rb_entry(node, struct hrtimer, node); |
| 1305 | |
| 1306 | if (basenow.tv64 < timer->expires.tv64) { |
| 1307 | ktime_t expires; |
| 1308 | |
| 1309 | expires = ktime_sub(timer->expires, |
| 1310 | base->offset); |
| 1311 | if (expires.tv64 < expires_next.tv64) |
| 1312 | expires_next = expires; |
| 1313 | break; |
| 1314 | } |
| 1315 | |
| 1316 | /* Move softirq callbacks to the pending list */ |
| 1317 | if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) { |
| 1318 | __remove_hrtimer(timer, base, |
| 1319 | HRTIMER_STATE_PENDING, 0); |
| 1320 | list_add_tail(&timer->cb_entry, |
| 1321 | &base->cpu_base->cb_pending); |
| 1322 | raise = 1; |
| 1323 | continue; |
| 1324 | } |
| 1325 | |
| 1326 | __run_hrtimer(timer); |
| 1327 | } |
| 1328 | spin_unlock(&cpu_base->lock); |
| 1329 | base++; |
| 1330 | } |
| 1331 | |
| 1332 | cpu_base->expires_next = expires_next; |
| 1333 | |
| 1334 | /* Reprogramming necessary ? */ |
| 1335 | if (expires_next.tv64 != KTIME_MAX) { |
| 1336 | if (tick_program_event(expires_next, 0)) |
| 1337 | goto retry; |
| 1338 | } |
| 1339 | |
| 1340 | /* Raise softirq ? */ |
| 1341 | if (raise) |
| 1342 | raise_softirq(HRTIMER_SOFTIRQ); |
| 1343 | } |
| 1344 | |
| 1345 | static void run_hrtimer_softirq(struct softirq_action *h) |
| 1346 | { |
| 1347 | run_hrtimer_pending(&__get_cpu_var(hrtimer_bases)); |
| 1348 | } |
| 1349 | |
| 1350 | #endif /* CONFIG_HIGH_RES_TIMERS */ |
| 1351 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1352 | /* |
| 1353 | * Called from timer softirq every jiffy, expire hrtimers: |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1354 | * |
| 1355 | * For HRT its the fall back code to run the softirq in the timer |
| 1356 | * softirq context in case the hrtimer initialization failed or has |
| 1357 | * not been done yet. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1358 | */ |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1359 | void hrtimer_run_pending(void) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1360 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1361 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1362 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1363 | if (hrtimer_hres_active()) |
| 1364 | return; |
| 1365 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1366 | /* |
| 1367 | * This _is_ ugly: We have to check in the softirq context, |
| 1368 | * whether we can switch to highres and / or nohz mode. The |
| 1369 | * clocksource switch happens in the timer interrupt with |
| 1370 | * xtime_lock held. Notification from there only sets the |
| 1371 | * check bit in the tick_oneshot code, otherwise we might |
| 1372 | * deadlock vs. xtime_lock. |
| 1373 | */ |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1374 | if (tick_check_oneshot_change(!hrtimer_is_hres_enabled())) |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1375 | hrtimer_switch_to_hres(); |
| 1376 | |
| 1377 | run_hrtimer_pending(cpu_base); |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * Called from hardirq context every jiffy |
| 1382 | */ |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1383 | void hrtimer_run_queues(void) |
| 1384 | { |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1385 | struct rb_node *node; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1386 | struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases); |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1387 | struct hrtimer_clock_base *base; |
| 1388 | int index, gettime = 1; |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1389 | |
| 1390 | if (hrtimer_hres_active()) |
| 1391 | return; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1392 | |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1393 | for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) { |
| 1394 | base = &cpu_base->clock_base[index]; |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 1395 | |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1396 | if (!base->first) |
| 1397 | continue; |
| 1398 | |
Thomas Gleixner | 259aae8 | 2008-04-19 21:31:26 +0200 | [diff] [blame] | 1399 | if (base->get_softirq_time) |
| 1400 | base->softirq_time = base->get_softirq_time(); |
| 1401 | else if (gettime) { |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1402 | hrtimer_get_softirq_time(cpu_base); |
| 1403 | gettime = 0; |
| 1404 | } |
| 1405 | |
Dimitri Sivanich | 833883d | 2008-04-18 13:39:00 -0700 | [diff] [blame] | 1406 | spin_lock(&cpu_base->lock); |
| 1407 | |
| 1408 | while ((node = base->first)) { |
| 1409 | struct hrtimer *timer; |
| 1410 | |
| 1411 | timer = rb_entry(node, struct hrtimer, node); |
| 1412 | if (base->softirq_time.tv64 <= timer->expires.tv64) |
| 1413 | break; |
| 1414 | |
| 1415 | if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) { |
| 1416 | __remove_hrtimer(timer, base, |
| 1417 | HRTIMER_STATE_PENDING, 0); |
| 1418 | list_add_tail(&timer->cb_entry, |
| 1419 | &base->cpu_base->cb_pending); |
| 1420 | continue; |
| 1421 | } |
| 1422 | |
| 1423 | __run_hrtimer(timer); |
| 1424 | } |
| 1425 | spin_unlock(&cpu_base->lock); |
| 1426 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | /* |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1430 | * Sleep related functions: |
| 1431 | */ |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1432 | static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer) |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 1433 | { |
| 1434 | struct hrtimer_sleeper *t = |
| 1435 | container_of(timer, struct hrtimer_sleeper, timer); |
| 1436 | struct task_struct *task = t->task; |
| 1437 | |
| 1438 | t->task = NULL; |
| 1439 | if (task) |
| 1440 | wake_up_process(task); |
| 1441 | |
| 1442 | return HRTIMER_NORESTART; |
| 1443 | } |
| 1444 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1445 | void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 1446 | { |
| 1447 | sl->timer.function = hrtimer_wakeup; |
| 1448 | sl->task = task; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1449 | #ifdef CONFIG_HIGH_RES_TIMERS |
Peter Zijlstra | 37bb6cb | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 1450 | sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1451 | #endif |
Thomas Gleixner | 00362e3 | 2006-03-31 02:31:17 -0800 | [diff] [blame] | 1452 | } |
| 1453 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1454 | 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] | 1455 | { |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1456 | hrtimer_init_sleeper(t, current); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1457 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1458 | do { |
| 1459 | set_current_state(TASK_INTERRUPTIBLE); |
| 1460 | hrtimer_start(&t->timer, t->timer.expires, mode); |
Peter Zijlstra | 37bb6cb | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 1461 | if (!hrtimer_active(&t->timer)) |
| 1462 | t->task = NULL; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1463 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1464 | if (likely(t->task)) |
| 1465 | schedule(); |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1466 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1467 | hrtimer_cancel(&t->timer); |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1468 | mode = HRTIMER_MODE_ABS; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1469 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1470 | } while (t->task && !signal_pending(current)); |
| 1471 | |
Peter Zijlstra | 3588a08 | 2008-02-01 17:45:13 +0100 | [diff] [blame] | 1472 | __set_current_state(TASK_RUNNING); |
| 1473 | |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1474 | return t->task == NULL; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1477 | static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp) |
| 1478 | { |
| 1479 | struct timespec rmt; |
| 1480 | ktime_t rem; |
| 1481 | |
| 1482 | rem = ktime_sub(timer->expires, timer->base->get_time()); |
| 1483 | if (rem.tv64 <= 0) |
| 1484 | return 0; |
| 1485 | rmt = ktime_to_timespec(rem); |
| 1486 | |
| 1487 | if (copy_to_user(rmtp, &rmt, sizeof(*rmtp))) |
| 1488 | return -EFAULT; |
| 1489 | |
| 1490 | return 1; |
| 1491 | } |
| 1492 | |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1493 | long __sched hrtimer_nanosleep_restart(struct restart_block *restart) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1494 | { |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1495 | struct hrtimer_sleeper t; |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1496 | struct timespec __user *rmtp; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1497 | int ret = 0; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1498 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1499 | hrtimer_init_on_stack(&t.timer, restart->nanosleep.index, |
| 1500 | HRTIMER_MODE_ABS); |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 1501 | t.timer.expires.tv64 = restart->nanosleep.expires; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1502 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 1503 | if (do_nanosleep(&t, HRTIMER_MODE_ABS)) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1504 | goto out; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1505 | |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 1506 | rmtp = restart->nanosleep.rmtp; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1507 | if (rmtp) { |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1508 | ret = update_rmtp(&t.timer, rmtp); |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1509 | if (ret <= 0) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1510 | goto out; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1511 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1512 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1513 | /* The other values in restart are already filled in */ |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1514 | ret = -ERESTART_RESTARTBLOCK; |
| 1515 | out: |
| 1516 | destroy_hrtimer_on_stack(&t.timer); |
| 1517 | return ret; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1520 | long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1521 | const enum hrtimer_mode mode, const clockid_t clockid) |
| 1522 | { |
| 1523 | struct restart_block *restart; |
Thomas Gleixner | 669d786 | 2006-03-31 02:31:19 -0800 | [diff] [blame] | 1524 | struct hrtimer_sleeper t; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1525 | int ret = 0; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1526 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1527 | hrtimer_init_on_stack(&t.timer, clockid, mode); |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1528 | t.timer.expires = timespec_to_ktime(*rqtp); |
| 1529 | if (do_nanosleep(&t, mode)) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1530 | goto out; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1531 | |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 1532 | /* Absolute timers do not update the rmtp value and restart: */ |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1533 | if (mode == HRTIMER_MODE_ABS) { |
| 1534 | ret = -ERESTARTNOHAND; |
| 1535 | goto out; |
| 1536 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1537 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1538 | if (rmtp) { |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1539 | ret = update_rmtp(&t.timer, rmtp); |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1540 | if (ret <= 0) |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1541 | goto out; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 1542 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1543 | |
| 1544 | restart = ¤t_thread_info()->restart_block; |
Toyo Abe | 1711ef3 | 2006-09-29 02:00:28 -0700 | [diff] [blame] | 1545 | restart->fn = hrtimer_nanosleep_restart; |
Thomas Gleixner | 029a07e | 2008-02-10 09:17:43 +0100 | [diff] [blame] | 1546 | restart->nanosleep.index = t.timer.base->index; |
| 1547 | restart->nanosleep.rmtp = rmtp; |
| 1548 | restart->nanosleep.expires = t.timer.expires.tv64; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1549 | |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1550 | ret = -ERESTART_RESTARTBLOCK; |
| 1551 | out: |
| 1552 | destroy_hrtimer_on_stack(&t.timer); |
| 1553 | return ret; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1554 | } |
| 1555 | |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1556 | asmlinkage long |
| 1557 | sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp) |
| 1558 | { |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1559 | struct timespec tu; |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1560 | |
| 1561 | if (copy_from_user(&tu, rqtp, sizeof(tu))) |
| 1562 | return -EFAULT; |
| 1563 | |
| 1564 | if (!timespec_valid(&tu)) |
| 1565 | return -EINVAL; |
| 1566 | |
Oleg Nesterov | 080344b | 2008-02-01 17:29:05 +0300 | [diff] [blame] | 1567 | return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 1568 | } |
| 1569 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 1570 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1571 | * Functions related to boot-time initialization: |
| 1572 | */ |
Randy Dunlap | 0ec160d | 2008-01-21 17:18:24 -0800 | [diff] [blame] | 1573 | static void __cpuinit init_hrtimers_cpu(int cpu) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1574 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1575 | struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1576 | int i; |
| 1577 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1578 | spin_lock_init(&cpu_base->lock); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1579 | |
| 1580 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) |
| 1581 | cpu_base->clock_base[i].cpu_base = cpu_base; |
| 1582 | |
Peter Zijlstra | d3d7445 | 2008-01-25 21:08:31 +0100 | [diff] [blame] | 1583 | INIT_LIST_HEAD(&cpu_base->cb_pending); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1584 | hrtimer_init_hres(cpu_base); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | #ifdef CONFIG_HOTPLUG_CPU |
| 1588 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1589 | static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, |
| 1590 | struct hrtimer_clock_base *new_base) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1591 | { |
| 1592 | struct hrtimer *timer; |
| 1593 | struct rb_node *node; |
| 1594 | |
| 1595 | while ((node = rb_first(&old_base->active))) { |
| 1596 | timer = rb_entry(node, struct hrtimer, node); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1597 | BUG_ON(hrtimer_callback_running(timer)); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 1598 | debug_hrtimer_deactivate(timer); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1599 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1600 | timer->base = new_base; |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1601 | /* |
| 1602 | * Enqueue the timer. Allow reprogramming of the event device |
| 1603 | */ |
| 1604 | enqueue_hrtimer(timer, new_base, 1); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | static void migrate_hrtimers(int cpu) |
| 1609 | { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1610 | struct hrtimer_cpu_base *old_base, *new_base; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1611 | int i; |
| 1612 | |
| 1613 | BUG_ON(cpu_online(cpu)); |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1614 | old_base = &per_cpu(hrtimer_bases, cpu); |
| 1615 | new_base = &get_cpu_var(hrtimer_bases); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1616 | |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1617 | tick_cancel_sched_timer(cpu); |
| 1618 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1619 | local_irq_disable(); |
Oleg Nesterov | 8e60e05 | 2008-04-04 20:54:10 +0200 | [diff] [blame] | 1620 | spin_lock(&new_base->lock); |
| 1621 | spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1622 | |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1623 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
Thomas Gleixner | 3c8aa39 | 2007-02-16 01:27:50 -0800 | [diff] [blame] | 1624 | migrate_hrtimer_list(&old_base->clock_base[i], |
| 1625 | &new_base->clock_base[i]); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
Oleg Nesterov | 8e60e05 | 2008-04-04 20:54:10 +0200 | [diff] [blame] | 1628 | spin_unlock(&old_base->lock); |
| 1629 | spin_unlock(&new_base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1630 | local_irq_enable(); |
| 1631 | put_cpu_var(hrtimer_bases); |
| 1632 | } |
| 1633 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 1634 | |
Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1635 | static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self, |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1636 | unsigned long action, void *hcpu) |
| 1637 | { |
David Miller | 7713a7d | 2007-07-16 17:17:44 -0700 | [diff] [blame] | 1638 | unsigned int cpu = (long)hcpu; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1639 | |
| 1640 | switch (action) { |
| 1641 | |
| 1642 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1643 | case CPU_UP_PREPARE_FROZEN: |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1644 | init_hrtimers_cpu(cpu); |
| 1645 | break; |
| 1646 | |
| 1647 | #ifdef CONFIG_HOTPLUG_CPU |
| 1648 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1649 | case CPU_DEAD_FROZEN: |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 1650 | clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1651 | migrate_hrtimers(cpu); |
| 1652 | break; |
| 1653 | #endif |
| 1654 | |
| 1655 | default: |
| 1656 | break; |
| 1657 | } |
| 1658 | |
| 1659 | return NOTIFY_OK; |
| 1660 | } |
| 1661 | |
Chandra Seetharaman | 8c78f30 | 2006-07-30 03:03:35 -0700 | [diff] [blame] | 1662 | static struct notifier_block __cpuinitdata hrtimers_nb = { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1663 | .notifier_call = hrtimer_cpu_notify, |
| 1664 | }; |
| 1665 | |
| 1666 | void __init hrtimers_init(void) |
| 1667 | { |
| 1668 | hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE, |
| 1669 | (void *)(long)smp_processor_id()); |
| 1670 | register_cpu_notifier(&hrtimers_nb); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1671 | #ifdef CONFIG_HIGH_RES_TIMERS |
Carlos R. Mafra | 962cf36 | 2008-05-15 11:15:37 -0300 | [diff] [blame^] | 1672 | open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq); |
Thomas Gleixner | 54cdfdb | 2007-02-16 01:28:11 -0800 | [diff] [blame] | 1673 | #endif |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |