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