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