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