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