Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/hrtimer.c |
| 3 | * |
| 4 | * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de> |
| 5 | * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar |
| 6 | * |
| 7 | * High-resolution kernel timers |
| 8 | * |
| 9 | * In contrast to the low-resolution timeout API implemented in |
| 10 | * kernel/timer.c, hrtimers provide finer resolution and accuracy |
| 11 | * depending on system configuration and capabilities. |
| 12 | * |
| 13 | * These timers are currently used for: |
| 14 | * - itimers |
| 15 | * - POSIX timers |
| 16 | * - nanosleep |
| 17 | * - precise in-kernel timing |
| 18 | * |
| 19 | * Started by: Thomas Gleixner and Ingo Molnar |
| 20 | * |
| 21 | * Credits: |
| 22 | * based on kernel/timer.c |
| 23 | * |
Thomas Gleixner | 66188fa | 2006-02-01 03:05:13 -0800 | [diff] [blame] | 24 | * Help, testing, suggestions, bugfixes, improvements were |
| 25 | * provided by: |
| 26 | * |
| 27 | * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel |
| 28 | * et. al. |
| 29 | * |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 30 | * For licencing details see kernel-base/COPYING |
| 31 | */ |
| 32 | |
| 33 | #include <linux/cpu.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/percpu.h> |
| 36 | #include <linux/hrtimer.h> |
| 37 | #include <linux/notifier.h> |
| 38 | #include <linux/syscalls.h> |
| 39 | #include <linux/interrupt.h> |
| 40 | |
| 41 | #include <asm/uaccess.h> |
| 42 | |
| 43 | /** |
| 44 | * ktime_get - get the monotonic time in ktime_t format |
| 45 | * |
| 46 | * returns the time in ktime_t format |
| 47 | */ |
| 48 | static ktime_t ktime_get(void) |
| 49 | { |
| 50 | struct timespec now; |
| 51 | |
| 52 | ktime_get_ts(&now); |
| 53 | |
| 54 | return timespec_to_ktime(now); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * ktime_get_real - get the real (wall-) time in ktime_t format |
| 59 | * |
| 60 | * returns the time in ktime_t format |
| 61 | */ |
| 62 | static ktime_t ktime_get_real(void) |
| 63 | { |
| 64 | struct timespec now; |
| 65 | |
| 66 | getnstimeofday(&now); |
| 67 | |
| 68 | return timespec_to_ktime(now); |
| 69 | } |
| 70 | |
| 71 | EXPORT_SYMBOL_GPL(ktime_get_real); |
| 72 | |
| 73 | /* |
| 74 | * The timer bases: |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 75 | * |
| 76 | * Note: If we want to add new timer bases, we have to skip the two |
| 77 | * clock ids captured by the cpu-timers. We do this by holding empty |
| 78 | * entries rather than doing math adjustment of the clock ids. |
| 79 | * This ensures that we capture erroneous accesses to these clock ids |
| 80 | * rather than moving them into the range of valid clock id's. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | #define MAX_HRTIMER_BASES 2 |
| 84 | |
| 85 | static DEFINE_PER_CPU(struct hrtimer_base, hrtimer_bases[MAX_HRTIMER_BASES]) = |
| 86 | { |
| 87 | { |
| 88 | .index = CLOCK_REALTIME, |
| 89 | .get_time = &ktime_get_real, |
| 90 | .resolution = KTIME_REALTIME_RES, |
| 91 | }, |
| 92 | { |
| 93 | .index = CLOCK_MONOTONIC, |
| 94 | .get_time = &ktime_get, |
| 95 | .resolution = KTIME_MONOTONIC_RES, |
| 96 | }, |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * ktime_get_ts - get the monotonic clock in timespec format |
| 101 | * |
| 102 | * @ts: pointer to timespec variable |
| 103 | * |
| 104 | * The function calculates the monotonic clock from the realtime |
| 105 | * clock and the wall_to_monotonic offset and stores the result |
| 106 | * in normalized timespec format in the variable pointed to by ts. |
| 107 | */ |
| 108 | void ktime_get_ts(struct timespec *ts) |
| 109 | { |
| 110 | struct timespec tomono; |
| 111 | unsigned long seq; |
| 112 | |
| 113 | do { |
| 114 | seq = read_seqbegin(&xtime_lock); |
| 115 | getnstimeofday(ts); |
| 116 | tomono = wall_to_monotonic; |
| 117 | |
| 118 | } while (read_seqretry(&xtime_lock, seq)); |
| 119 | |
| 120 | set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec, |
| 121 | ts->tv_nsec + tomono.tv_nsec); |
| 122 | } |
Matt Helsley | 69778e3 | 2006-01-09 20:52:39 -0800 | [diff] [blame] | 123 | EXPORT_SYMBOL_GPL(ktime_get_ts); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 124 | |
| 125 | /* |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 126 | * Get the coarse grained time at the softirq based on xtime and |
| 127 | * wall_to_monotonic. |
| 128 | */ |
| 129 | static void hrtimer_get_softirq_time(struct hrtimer_base *base) |
| 130 | { |
| 131 | ktime_t xtim, tomono; |
| 132 | unsigned long seq; |
| 133 | |
| 134 | do { |
| 135 | seq = read_seqbegin(&xtime_lock); |
| 136 | xtim = timespec_to_ktime(xtime); |
| 137 | tomono = timespec_to_ktime(wall_to_monotonic); |
| 138 | |
| 139 | } while (read_seqretry(&xtime_lock, seq)); |
| 140 | |
| 141 | base[CLOCK_REALTIME].softirq_time = xtim; |
| 142 | base[CLOCK_MONOTONIC].softirq_time = ktime_add(xtim, tomono); |
| 143 | } |
| 144 | |
| 145 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 146 | * Functions and macros which are different for UP/SMP systems are kept in a |
| 147 | * single place |
| 148 | */ |
| 149 | #ifdef CONFIG_SMP |
| 150 | |
| 151 | #define set_curr_timer(b, t) do { (b)->curr_timer = (t); } while (0) |
| 152 | |
| 153 | /* |
| 154 | * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock |
| 155 | * means that all timers which are tied to this base via timer->base are |
| 156 | * locked, and the base itself is locked too. |
| 157 | * |
| 158 | * So __run_timers/migrate_timers can safely modify all timers which could |
| 159 | * be found on the lists/queues. |
| 160 | * |
| 161 | * When the timer's base is locked, and the timer removed from list, it is |
| 162 | * possible to set timer->base = NULL and drop the lock: the timer remains |
| 163 | * locked. |
| 164 | */ |
| 165 | static struct hrtimer_base *lock_hrtimer_base(const struct hrtimer *timer, |
| 166 | unsigned long *flags) |
| 167 | { |
| 168 | struct hrtimer_base *base; |
| 169 | |
| 170 | for (;;) { |
| 171 | base = timer->base; |
| 172 | if (likely(base != NULL)) { |
| 173 | spin_lock_irqsave(&base->lock, *flags); |
| 174 | if (likely(base == timer->base)) |
| 175 | return base; |
| 176 | /* The timer has migrated to another CPU: */ |
| 177 | spin_unlock_irqrestore(&base->lock, *flags); |
| 178 | } |
| 179 | cpu_relax(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Switch the timer base to the current CPU when possible. |
| 185 | */ |
| 186 | static inline struct hrtimer_base * |
| 187 | switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_base *base) |
| 188 | { |
| 189 | struct hrtimer_base *new_base; |
| 190 | |
| 191 | new_base = &__get_cpu_var(hrtimer_bases[base->index]); |
| 192 | |
| 193 | if (base != new_base) { |
| 194 | /* |
| 195 | * We are trying to schedule the timer on the local CPU. |
| 196 | * However we can't change timer's base while it is running, |
| 197 | * so we keep it on the same CPU. No hassle vs. reprogramming |
| 198 | * the event source in the high resolution case. The softirq |
| 199 | * code will take care of this when the timer function has |
| 200 | * completed. There is no conflict as we hold the lock until |
| 201 | * the timer is enqueued. |
| 202 | */ |
| 203 | if (unlikely(base->curr_timer == timer)) |
| 204 | return base; |
| 205 | |
| 206 | /* See the comment in lock_timer_base() */ |
| 207 | timer->base = NULL; |
| 208 | spin_unlock(&base->lock); |
| 209 | spin_lock(&new_base->lock); |
| 210 | timer->base = new_base; |
| 211 | } |
| 212 | return new_base; |
| 213 | } |
| 214 | |
| 215 | #else /* CONFIG_SMP */ |
| 216 | |
| 217 | #define set_curr_timer(b, t) do { } while (0) |
| 218 | |
| 219 | static inline struct hrtimer_base * |
| 220 | lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 221 | { |
| 222 | struct hrtimer_base *base = timer->base; |
| 223 | |
| 224 | spin_lock_irqsave(&base->lock, *flags); |
| 225 | |
| 226 | return base; |
| 227 | } |
| 228 | |
| 229 | #define switch_hrtimer_base(t, b) (b) |
| 230 | |
| 231 | #endif /* !CONFIG_SMP */ |
| 232 | |
| 233 | /* |
| 234 | * Functions for the union type storage format of ktime_t which are |
| 235 | * too large for inlining: |
| 236 | */ |
| 237 | #if BITS_PER_LONG < 64 |
| 238 | # ifndef CONFIG_KTIME_SCALAR |
| 239 | /** |
| 240 | * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable |
| 241 | * |
| 242 | * @kt: addend |
| 243 | * @nsec: the scalar nsec value to add |
| 244 | * |
| 245 | * Returns the sum of kt and nsec in ktime_t format |
| 246 | */ |
| 247 | ktime_t ktime_add_ns(const ktime_t kt, u64 nsec) |
| 248 | { |
| 249 | ktime_t tmp; |
| 250 | |
| 251 | if (likely(nsec < NSEC_PER_SEC)) { |
| 252 | tmp.tv64 = nsec; |
| 253 | } else { |
| 254 | unsigned long rem = do_div(nsec, NSEC_PER_SEC); |
| 255 | |
| 256 | tmp = ktime_set((long)nsec, rem); |
| 257 | } |
| 258 | |
| 259 | return ktime_add(kt, tmp); |
| 260 | } |
| 261 | |
| 262 | #else /* CONFIG_KTIME_SCALAR */ |
| 263 | |
| 264 | # endif /* !CONFIG_KTIME_SCALAR */ |
| 265 | |
| 266 | /* |
| 267 | * Divide a ktime value by a nanosecond value |
| 268 | */ |
Roman Zippel | df869b6 | 2006-03-26 01:38:11 -0800 | [diff] [blame] | 269 | static unsigned long ktime_divns(const ktime_t kt, s64 div) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 270 | { |
| 271 | u64 dclc, inc, dns; |
| 272 | int sft = 0; |
| 273 | |
| 274 | dclc = dns = ktime_to_ns(kt); |
| 275 | inc = div; |
| 276 | /* Make sure the divisor is less than 2^32: */ |
| 277 | while (div >> 32) { |
| 278 | sft++; |
| 279 | div >>= 1; |
| 280 | } |
| 281 | dclc >>= sft; |
| 282 | do_div(dclc, (unsigned long) div); |
| 283 | |
| 284 | return (unsigned long) dclc; |
| 285 | } |
| 286 | |
| 287 | #else /* BITS_PER_LONG < 64 */ |
| 288 | # define ktime_divns(kt, div) (unsigned long)((kt).tv64 / (div)) |
| 289 | #endif /* BITS_PER_LONG >= 64 */ |
| 290 | |
| 291 | /* |
| 292 | * Counterpart to lock_timer_base above: |
| 293 | */ |
| 294 | static inline |
| 295 | void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
| 296 | { |
| 297 | spin_unlock_irqrestore(&timer->base->lock, *flags); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * hrtimer_forward - forward the timer expiry |
| 302 | * |
| 303 | * @timer: hrtimer to forward |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 304 | * @now: forward past this time |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 305 | * @interval: the interval to forward |
| 306 | * |
| 307 | * Forward the timer expiry so it will expire in the future. |
Jonathan Corbet | 8dca6f3 | 2006-01-16 15:58:55 -0700 | [diff] [blame] | 308 | * Returns the number of overruns. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 309 | */ |
| 310 | unsigned long |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 311 | hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 312 | { |
| 313 | unsigned long orun = 1; |
Roman Zippel | 44f2147 | 2006-03-26 01:38:06 -0800 | [diff] [blame] | 314 | ktime_t delta; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 315 | |
| 316 | delta = ktime_sub(now, timer->expires); |
| 317 | |
| 318 | if (delta.tv64 < 0) |
| 319 | return 0; |
| 320 | |
Thomas Gleixner | c9db4fa | 2006-01-12 11:47:34 +0100 | [diff] [blame] | 321 | if (interval.tv64 < timer->base->resolution.tv64) |
| 322 | interval.tv64 = timer->base->resolution.tv64; |
| 323 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 324 | if (unlikely(delta.tv64 >= interval.tv64)) { |
Roman Zippel | df869b6 | 2006-03-26 01:38:11 -0800 | [diff] [blame] | 325 | s64 incr = ktime_to_ns(interval); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 326 | |
| 327 | orun = ktime_divns(delta, incr); |
| 328 | timer->expires = ktime_add_ns(timer->expires, incr * orun); |
| 329 | if (timer->expires.tv64 > now.tv64) |
| 330 | return orun; |
| 331 | /* |
| 332 | * This (and the ktime_add() below) is the |
| 333 | * correction for exact: |
| 334 | */ |
| 335 | orun++; |
| 336 | } |
| 337 | timer->expires = ktime_add(timer->expires, interval); |
| 338 | |
| 339 | return orun; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * enqueue_hrtimer - internal function to (re)start a timer |
| 344 | * |
| 345 | * The timer is inserted in expiry order. Insertion into the |
| 346 | * red black tree is O(log(n)). Must hold the base lock. |
| 347 | */ |
| 348 | static void enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_base *base) |
| 349 | { |
| 350 | struct rb_node **link = &base->active.rb_node; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 351 | struct rb_node *parent = NULL; |
| 352 | struct hrtimer *entry; |
| 353 | |
| 354 | /* |
| 355 | * Find the right place in the rbtree: |
| 356 | */ |
| 357 | while (*link) { |
| 358 | parent = *link; |
| 359 | entry = rb_entry(parent, struct hrtimer, node); |
| 360 | /* |
| 361 | * We dont care about collisions. Nodes with |
| 362 | * the same expiry time stay together. |
| 363 | */ |
| 364 | if (timer->expires.tv64 < entry->expires.tv64) |
| 365 | link = &(*link)->rb_left; |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 366 | else |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 367 | link = &(*link)->rb_right; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 371 | * Insert the timer to the rbtree and check whether it |
| 372 | * replaces the first pending timer |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 373 | */ |
| 374 | rb_link_node(&timer->node, parent, link); |
| 375 | rb_insert_color(&timer->node, &base->active); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 376 | |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 377 | if (!base->first || timer->expires.tv64 < |
| 378 | rb_entry(base->first, struct hrtimer, node)->expires.tv64) |
| 379 | base->first = &timer->node; |
| 380 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * __remove_hrtimer - internal function to remove a timer |
| 384 | * |
| 385 | * Caller must hold the base lock. |
| 386 | */ |
| 387 | static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base) |
| 388 | { |
| 389 | /* |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 390 | * Remove the timer from the rbtree and replace the |
| 391 | * first entry pointer if necessary. |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 392 | */ |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 393 | if (base->first == &timer->node) |
| 394 | base->first = rb_next(&timer->node); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 395 | rb_erase(&timer->node, &base->active); |
Roman Zippel | b75f7a5 | 2006-03-26 01:38:09 -0800 | [diff] [blame] | 396 | timer->node.rb_parent = HRTIMER_INACTIVE; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
| 400 | * remove hrtimer, called with base lock held |
| 401 | */ |
| 402 | static inline int |
| 403 | remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base) |
| 404 | { |
| 405 | if (hrtimer_active(timer)) { |
| 406 | __remove_hrtimer(timer, base); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 407 | return 1; |
| 408 | } |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * hrtimer_start - (re)start an relative timer on the current CPU |
| 414 | * |
| 415 | * @timer: the timer to be added |
| 416 | * @tim: expiry time |
| 417 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) |
| 418 | * |
| 419 | * Returns: |
| 420 | * 0 on success |
| 421 | * 1 when the timer was active |
| 422 | */ |
| 423 | int |
| 424 | hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) |
| 425 | { |
| 426 | struct hrtimer_base *base, *new_base; |
| 427 | unsigned long flags; |
| 428 | int ret; |
| 429 | |
| 430 | base = lock_hrtimer_base(timer, &flags); |
| 431 | |
| 432 | /* Remove an active timer from the queue: */ |
| 433 | ret = remove_hrtimer(timer, base); |
| 434 | |
| 435 | /* Switch the timer base, if necessary: */ |
| 436 | new_base = switch_hrtimer_base(timer, base); |
| 437 | |
Ingo Molnar | 06027bd | 2006-02-14 13:53:15 -0800 | [diff] [blame] | 438 | if (mode == HRTIMER_REL) { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 439 | tim = ktime_add(tim, new_base->get_time()); |
Ingo Molnar | 06027bd | 2006-02-14 13:53:15 -0800 | [diff] [blame] | 440 | /* |
| 441 | * CONFIG_TIME_LOW_RES is a temporary way for architectures |
| 442 | * to signal that they simply return xtime in |
| 443 | * do_gettimeoffset(). In this case we want to round up by |
| 444 | * resolution when starting a relative timer, to avoid short |
| 445 | * timeouts. This will go away with the GTOD framework. |
| 446 | */ |
| 447 | #ifdef CONFIG_TIME_LOW_RES |
| 448 | tim = ktime_add(tim, base->resolution); |
| 449 | #endif |
| 450 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 451 | timer->expires = tim; |
| 452 | |
| 453 | enqueue_hrtimer(timer, new_base); |
| 454 | |
| 455 | unlock_hrtimer_base(timer, &flags); |
| 456 | |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * hrtimer_try_to_cancel - try to deactivate a timer |
| 462 | * |
| 463 | * @timer: hrtimer to stop |
| 464 | * |
| 465 | * Returns: |
| 466 | * 0 when the timer was not active |
| 467 | * 1 when the timer was active |
| 468 | * -1 when the timer is currently excuting the callback function and |
| 469 | * can not be stopped |
| 470 | */ |
| 471 | int hrtimer_try_to_cancel(struct hrtimer *timer) |
| 472 | { |
| 473 | struct hrtimer_base *base; |
| 474 | unsigned long flags; |
| 475 | int ret = -1; |
| 476 | |
| 477 | base = lock_hrtimer_base(timer, &flags); |
| 478 | |
| 479 | if (base->curr_timer != timer) |
| 480 | ret = remove_hrtimer(timer, base); |
| 481 | |
| 482 | unlock_hrtimer_base(timer, &flags); |
| 483 | |
| 484 | return ret; |
| 485 | |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. |
| 490 | * |
| 491 | * @timer: the timer to be cancelled |
| 492 | * |
| 493 | * Returns: |
| 494 | * 0 when the timer was not active |
| 495 | * 1 when the timer was active |
| 496 | */ |
| 497 | int hrtimer_cancel(struct hrtimer *timer) |
| 498 | { |
| 499 | for (;;) { |
| 500 | int ret = hrtimer_try_to_cancel(timer); |
| 501 | |
| 502 | if (ret >= 0) |
| 503 | return ret; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * hrtimer_get_remaining - get remaining time for the timer |
| 509 | * |
| 510 | * @timer: the timer to read |
| 511 | */ |
| 512 | ktime_t hrtimer_get_remaining(const struct hrtimer *timer) |
| 513 | { |
| 514 | struct hrtimer_base *base; |
| 515 | unsigned long flags; |
| 516 | ktime_t rem; |
| 517 | |
| 518 | base = lock_hrtimer_base(timer, &flags); |
| 519 | rem = ktime_sub(timer->expires, timer->base->get_time()); |
| 520 | unlock_hrtimer_base(timer, &flags); |
| 521 | |
| 522 | return rem; |
| 523 | } |
| 524 | |
Tony Lindgren | 6923974 | 2006-03-06 15:42:45 -0800 | [diff] [blame] | 525 | #ifdef CONFIG_NO_IDLE_HZ |
| 526 | /** |
| 527 | * hrtimer_get_next_event - get the time until next expiry event |
| 528 | * |
| 529 | * Returns the delta to the next expiry event or KTIME_MAX if no timer |
| 530 | * is pending. |
| 531 | */ |
| 532 | ktime_t hrtimer_get_next_event(void) |
| 533 | { |
| 534 | struct hrtimer_base *base = __get_cpu_var(hrtimer_bases); |
| 535 | ktime_t delta, mindelta = { .tv64 = KTIME_MAX }; |
| 536 | unsigned long flags; |
| 537 | int i; |
| 538 | |
| 539 | for (i = 0; i < MAX_HRTIMER_BASES; i++, base++) { |
| 540 | struct hrtimer *timer; |
| 541 | |
| 542 | spin_lock_irqsave(&base->lock, flags); |
| 543 | if (!base->first) { |
| 544 | spin_unlock_irqrestore(&base->lock, flags); |
| 545 | continue; |
| 546 | } |
| 547 | timer = rb_entry(base->first, struct hrtimer, node); |
| 548 | delta.tv64 = timer->expires.tv64; |
| 549 | spin_unlock_irqrestore(&base->lock, flags); |
| 550 | delta = ktime_sub(delta, base->get_time()); |
| 551 | if (delta.tv64 < mindelta.tv64) |
| 552 | mindelta.tv64 = delta.tv64; |
| 553 | } |
| 554 | if (mindelta.tv64 < 0) |
| 555 | mindelta.tv64 = 0; |
| 556 | return mindelta; |
| 557 | } |
| 558 | #endif |
| 559 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 560 | /** |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 561 | * hrtimer_init - initialize a timer to the given clock |
| 562 | * |
| 563 | * @timer: the timer to be initialized |
| 564 | * @clock_id: the clock to be used |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 565 | * @mode: timer mode abs/rel |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 566 | */ |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 567 | void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
| 568 | enum hrtimer_mode mode) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 569 | { |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 570 | struct hrtimer_base *bases; |
| 571 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 572 | memset(timer, 0, sizeof(struct hrtimer)); |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 573 | |
| 574 | bases = per_cpu(hrtimer_bases, raw_smp_processor_id()); |
| 575 | |
| 576 | if (clock_id == CLOCK_REALTIME && mode != HRTIMER_ABS) |
| 577 | clock_id = CLOCK_MONOTONIC; |
| 578 | |
| 579 | timer->base = &bases[clock_id]; |
Roman Zippel | b75f7a5 | 2006-03-26 01:38:09 -0800 | [diff] [blame] | 580 | timer->node.rb_parent = HRTIMER_INACTIVE; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
| 584 | * hrtimer_get_res - get the timer resolution for a clock |
| 585 | * |
| 586 | * @which_clock: which clock to query |
| 587 | * @tp: pointer to timespec variable to store the resolution |
| 588 | * |
| 589 | * Store the resolution of the clock selected by which_clock in the |
| 590 | * variable pointed to by tp. |
| 591 | */ |
| 592 | int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp) |
| 593 | { |
| 594 | struct hrtimer_base *bases; |
| 595 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 596 | bases = per_cpu(hrtimer_bases, raw_smp_processor_id()); |
Thomas Gleixner | e278763 | 2006-01-12 11:36:14 +0100 | [diff] [blame] | 597 | *tp = ktime_to_timespec(bases[which_clock].resolution); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Expire the per base hrtimer-queue: |
| 604 | */ |
| 605 | static inline void run_hrtimer_queue(struct hrtimer_base *base) |
| 606 | { |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 607 | struct rb_node *node; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 608 | |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 609 | if (base->get_softirq_time) |
| 610 | base->softirq_time = base->get_softirq_time(); |
| 611 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 612 | spin_lock_irq(&base->lock); |
| 613 | |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 614 | while ((node = base->first)) { |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 615 | struct hrtimer *timer; |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame^] | 616 | int (*fn)(struct hrtimer *); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 617 | int restart; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 618 | |
Thomas Gleixner | 288867e | 2006-01-12 11:25:54 +0100 | [diff] [blame] | 619 | timer = rb_entry(node, struct hrtimer, node); |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 620 | if (base->softirq_time.tv64 <= timer->expires.tv64) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 621 | break; |
| 622 | |
| 623 | fn = timer->function; |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 624 | set_curr_timer(base, timer); |
| 625 | __remove_hrtimer(timer, base); |
| 626 | spin_unlock_irq(&base->lock); |
| 627 | |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame^] | 628 | restart = fn(timer); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 629 | |
| 630 | spin_lock_irq(&base->lock); |
| 631 | |
Roman Zippel | b75f7a5 | 2006-03-26 01:38:09 -0800 | [diff] [blame] | 632 | if (restart != HRTIMER_NORESTART) { |
| 633 | BUG_ON(hrtimer_active(timer)); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 634 | enqueue_hrtimer(timer, base); |
Roman Zippel | b75f7a5 | 2006-03-26 01:38:09 -0800 | [diff] [blame] | 635 | } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 636 | } |
| 637 | set_curr_timer(base, NULL); |
| 638 | spin_unlock_irq(&base->lock); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Called from timer softirq every jiffy, expire hrtimers: |
| 643 | */ |
| 644 | void hrtimer_run_queues(void) |
| 645 | { |
| 646 | struct hrtimer_base *base = __get_cpu_var(hrtimer_bases); |
| 647 | int i; |
| 648 | |
Thomas Gleixner | 92127c7 | 2006-03-26 01:38:05 -0800 | [diff] [blame] | 649 | hrtimer_get_softirq_time(base); |
| 650 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 651 | for (i = 0; i < MAX_HRTIMER_BASES; i++) |
| 652 | run_hrtimer_queue(&base[i]); |
| 653 | } |
| 654 | |
| 655 | /* |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 656 | * Sleep related functions: |
| 657 | */ |
| 658 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 659 | struct sleep_hrtimer { |
| 660 | struct hrtimer timer; |
| 661 | struct task_struct *task; |
| 662 | int expired; |
| 663 | }; |
| 664 | |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame^] | 665 | static int nanosleep_wakeup(struct hrtimer *timer) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 666 | { |
Roman Zippel | 05cfb61 | 2006-03-26 01:38:12 -0800 | [diff] [blame^] | 667 | struct sleep_hrtimer *t = |
| 668 | container_of(timer, struct sleep_hrtimer, timer); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 669 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 670 | t->expired = 1; |
| 671 | wake_up_process(t->task); |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 672 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 673 | return HRTIMER_NORESTART; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 676 | static int __sched do_nanosleep(struct sleep_hrtimer *t, enum hrtimer_mode mode) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 677 | { |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 678 | t->timer.function = nanosleep_wakeup; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 679 | t->task = current; |
| 680 | t->expired = 0; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 681 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 682 | do { |
| 683 | set_current_state(TASK_INTERRUPTIBLE); |
| 684 | hrtimer_start(&t->timer, t->timer.expires, mode); |
| 685 | |
| 686 | schedule(); |
| 687 | |
| 688 | if (unlikely(!t->expired)) { |
| 689 | hrtimer_cancel(&t->timer); |
| 690 | mode = HRTIMER_ABS; |
| 691 | } |
| 692 | } while (!t->expired && !signal_pending(current)); |
| 693 | |
| 694 | return t->expired; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 695 | } |
| 696 | |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 697 | static long __sched nanosleep_restart(struct restart_block *restart) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 698 | { |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 699 | struct sleep_hrtimer t; |
Ingo Molnar | ea13dbc | 2006-01-16 10:59:41 +0100 | [diff] [blame] | 700 | struct timespec __user *rmtp; |
| 701 | struct timespec tu; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 702 | ktime_t time; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 703 | |
| 704 | restart->fn = do_no_restart_syscall; |
| 705 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 706 | hrtimer_init(&t.timer, restart->arg3, HRTIMER_ABS); |
| 707 | t.timer.expires.tv64 = ((u64)restart->arg1 << 32) | (u64) restart->arg0; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 708 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 709 | if (do_nanosleep(&t, HRTIMER_ABS)) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 710 | return 0; |
| 711 | |
| 712 | rmtp = (struct timespec __user *) restart->arg2; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 713 | if (rmtp) { |
| 714 | time = ktime_sub(t.timer.expires, t.timer.base->get_time()); |
| 715 | if (time.tv64 <= 0) |
| 716 | return 0; |
| 717 | tu = ktime_to_timespec(time); |
| 718 | if (copy_to_user(rmtp, &tu, sizeof(tu))) |
| 719 | return -EFAULT; |
| 720 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 721 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 722 | restart->fn = nanosleep_restart; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 723 | |
| 724 | /* The other values in restart are already filled in */ |
| 725 | return -ERESTART_RESTARTBLOCK; |
| 726 | } |
| 727 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 728 | long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, |
| 729 | const enum hrtimer_mode mode, const clockid_t clockid) |
| 730 | { |
| 731 | struct restart_block *restart; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 732 | struct sleep_hrtimer t; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 733 | struct timespec tu; |
| 734 | ktime_t rem; |
| 735 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 736 | hrtimer_init(&t.timer, clockid, mode); |
| 737 | t.timer.expires = timespec_to_ktime(*rqtp); |
| 738 | if (do_nanosleep(&t, mode)) |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 739 | return 0; |
| 740 | |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 741 | /* Absolute timers do not update the rmtp value and restart: */ |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 742 | if (mode == HRTIMER_ABS) |
| 743 | return -ERESTARTNOHAND; |
| 744 | |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 745 | if (rmtp) { |
| 746 | rem = ktime_sub(t.timer.expires, t.timer.base->get_time()); |
| 747 | if (rem.tv64 <= 0) |
| 748 | return 0; |
| 749 | tu = ktime_to_timespec(rem); |
| 750 | if (copy_to_user(rmtp, &tu, sizeof(tu))) |
| 751 | return -EFAULT; |
| 752 | } |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 753 | |
| 754 | restart = ¤t_thread_info()->restart_block; |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 755 | restart->fn = nanosleep_restart; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 756 | restart->arg0 = t.timer.expires.tv64 & 0xFFFFFFFF; |
| 757 | restart->arg1 = t.timer.expires.tv64 >> 32; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 758 | restart->arg2 = (unsigned long) rmtp; |
Roman Zippel | 432569b | 2006-03-26 01:38:08 -0800 | [diff] [blame] | 759 | restart->arg3 = (unsigned long) t.timer.base->index; |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 760 | |
| 761 | return -ERESTART_RESTARTBLOCK; |
| 762 | } |
| 763 | |
Thomas Gleixner | 6ba1b91 | 2006-01-09 20:52:36 -0800 | [diff] [blame] | 764 | asmlinkage long |
| 765 | sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp) |
| 766 | { |
| 767 | struct timespec tu; |
| 768 | |
| 769 | if (copy_from_user(&tu, rqtp, sizeof(tu))) |
| 770 | return -EFAULT; |
| 771 | |
| 772 | if (!timespec_valid(&tu)) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | return hrtimer_nanosleep(&tu, rmtp, HRTIMER_REL, CLOCK_MONOTONIC); |
| 776 | } |
| 777 | |
Thomas Gleixner | 10c94ec | 2006-01-09 20:52:35 -0800 | [diff] [blame] | 778 | /* |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 779 | * Functions related to boot-time initialization: |
| 780 | */ |
| 781 | static void __devinit init_hrtimers_cpu(int cpu) |
| 782 | { |
| 783 | struct hrtimer_base *base = per_cpu(hrtimer_bases, cpu); |
| 784 | int i; |
| 785 | |
George Anzinger | 7978672c | 2006-02-01 03:05:11 -0800 | [diff] [blame] | 786 | for (i = 0; i < MAX_HRTIMER_BASES; i++, base++) |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 787 | spin_lock_init(&base->lock); |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | #ifdef CONFIG_HOTPLUG_CPU |
| 791 | |
| 792 | static void migrate_hrtimer_list(struct hrtimer_base *old_base, |
| 793 | struct hrtimer_base *new_base) |
| 794 | { |
| 795 | struct hrtimer *timer; |
| 796 | struct rb_node *node; |
| 797 | |
| 798 | while ((node = rb_first(&old_base->active))) { |
| 799 | timer = rb_entry(node, struct hrtimer, node); |
| 800 | __remove_hrtimer(timer, old_base); |
| 801 | timer->base = new_base; |
| 802 | enqueue_hrtimer(timer, new_base); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static void migrate_hrtimers(int cpu) |
| 807 | { |
| 808 | struct hrtimer_base *old_base, *new_base; |
| 809 | int i; |
| 810 | |
| 811 | BUG_ON(cpu_online(cpu)); |
| 812 | old_base = per_cpu(hrtimer_bases, cpu); |
| 813 | new_base = get_cpu_var(hrtimer_bases); |
| 814 | |
| 815 | local_irq_disable(); |
| 816 | |
| 817 | for (i = 0; i < MAX_HRTIMER_BASES; i++) { |
| 818 | |
| 819 | spin_lock(&new_base->lock); |
| 820 | spin_lock(&old_base->lock); |
| 821 | |
| 822 | BUG_ON(old_base->curr_timer); |
| 823 | |
| 824 | migrate_hrtimer_list(old_base, new_base); |
| 825 | |
| 826 | spin_unlock(&old_base->lock); |
| 827 | spin_unlock(&new_base->lock); |
| 828 | old_base++; |
| 829 | new_base++; |
| 830 | } |
| 831 | |
| 832 | local_irq_enable(); |
| 833 | put_cpu_var(hrtimer_bases); |
| 834 | } |
| 835 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 836 | |
| 837 | static int __devinit hrtimer_cpu_notify(struct notifier_block *self, |
| 838 | unsigned long action, void *hcpu) |
| 839 | { |
| 840 | long cpu = (long)hcpu; |
| 841 | |
| 842 | switch (action) { |
| 843 | |
| 844 | case CPU_UP_PREPARE: |
| 845 | init_hrtimers_cpu(cpu); |
| 846 | break; |
| 847 | |
| 848 | #ifdef CONFIG_HOTPLUG_CPU |
| 849 | case CPU_DEAD: |
| 850 | migrate_hrtimers(cpu); |
| 851 | break; |
| 852 | #endif |
| 853 | |
| 854 | default: |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | return NOTIFY_OK; |
| 859 | } |
| 860 | |
| 861 | static struct notifier_block __devinitdata hrtimers_nb = { |
| 862 | .notifier_call = hrtimer_cpu_notify, |
| 863 | }; |
| 864 | |
| 865 | void __init hrtimers_init(void) |
| 866 | { |
| 867 | hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE, |
| 868 | (void *)(long)smp_processor_id()); |
| 869 | register_cpu_notifier(&hrtimers_nb); |
| 870 | } |
| 871 | |