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