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