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