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