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