blob: 59ec50c1e90533649e1c5e8cdb7814167a113cc2 [file] [log] [blame]
Thomas Gleixnerc0a31322006-01-09 20:52:32 -08001/*
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 *
Thomas Gleixner66188fa2006-02-01 03:05:13 -080024 * Help, testing, suggestions, bugfixes, improvements were
25 * provided by:
26 *
27 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
28 * et. al.
29 *
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080030 * 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:
George Anzinger7978672c2006-02-01 03:05:11 -080075 *
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.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -080081 */
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}
Matt Helsley69778e32006-01-09 20:52:39 -0800123EXPORT_SYMBOL_GPL(ktime_get_ts);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800124
125/*
Thomas Gleixner92127c72006-03-26 01:38:05 -0800126 * 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
145/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800146 * 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
Roman Zippel44f21472006-03-26 01:38:06 -0800304 * @now: forward past this time
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800305 * @interval: the interval to forward
306 *
307 * Forward the timer expiry so it will expire in the future.
Jonathan Corbet8dca6f32006-01-16 15:58:55 -0700308 * Returns the number of overruns.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800309 */
310unsigned long
Roman Zippel44f21472006-03-26 01:38:06 -0800311hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800312{
313 unsigned long orun = 1;
Roman Zippel44f21472006-03-26 01:38:06 -0800314 ktime_t delta;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800315
316 delta = ktime_sub(now, timer->expires);
317
318 if (delta.tv64 < 0)
319 return 0;
320
Thomas Gleixnerc9db4fa2006-01-12 11:47:34 +0100321 if (interval.tv64 < timer->base->resolution.tv64)
322 interval.tv64 = timer->base->resolution.tv64;
323
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800324 if (unlikely(delta.tv64 >= interval.tv64)) {
325 nsec_t incr = ktime_to_ns(interval);
326
327 orun = ktime_divns(delta, incr);
328 timer->expires = ktime_add_ns(timer->expires, incr * orun);
329 if (timer->expires.tv64 > now.tv64)
330 return orun;
331 /*
332 * This (and the ktime_add() below) is the
333 * correction for exact:
334 */
335 orun++;
336 }
337 timer->expires = ktime_add(timer->expires, interval);
338
339 return orun;
340}
341
342/*
343 * enqueue_hrtimer - internal function to (re)start a timer
344 *
345 * The timer is inserted in expiry order. Insertion into the
346 * red black tree is O(log(n)). Must hold the base lock.
347 */
348static void enqueue_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
349{
350 struct rb_node **link = &base->active.rb_node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800351 struct rb_node *parent = NULL;
352 struct hrtimer *entry;
353
354 /*
355 * Find the right place in the rbtree:
356 */
357 while (*link) {
358 parent = *link;
359 entry = rb_entry(parent, struct hrtimer, node);
360 /*
361 * We dont care about collisions. Nodes with
362 * the same expiry time stay together.
363 */
364 if (timer->expires.tv64 < entry->expires.tv64)
365 link = &(*link)->rb_left;
Thomas Gleixner288867e2006-01-12 11:25:54 +0100366 else
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800367 link = &(*link)->rb_right;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800368 }
369
370 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100371 * Insert the timer to the rbtree and check whether it
372 * replaces the first pending timer
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800373 */
374 rb_link_node(&timer->node, parent, link);
375 rb_insert_color(&timer->node, &base->active);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800376
377 timer->state = HRTIMER_PENDING;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800378
Thomas Gleixner288867e2006-01-12 11:25:54 +0100379 if (!base->first || timer->expires.tv64 <
380 rb_entry(base->first, struct hrtimer, node)->expires.tv64)
381 base->first = &timer->node;
382}
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800383
384/*
385 * __remove_hrtimer - internal function to remove a timer
386 *
387 * Caller must hold the base lock.
388 */
389static void __remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
390{
391 /*
Thomas Gleixner288867e2006-01-12 11:25:54 +0100392 * Remove the timer from the rbtree and replace the
393 * first entry pointer if necessary.
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800394 */
Thomas Gleixner288867e2006-01-12 11:25:54 +0100395 if (base->first == &timer->node)
396 base->first = rb_next(&timer->node);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800397 rb_erase(&timer->node, &base->active);
398}
399
400/*
401 * remove hrtimer, called with base lock held
402 */
403static inline int
404remove_hrtimer(struct hrtimer *timer, struct hrtimer_base *base)
405{
406 if (hrtimer_active(timer)) {
407 __remove_hrtimer(timer, base);
408 timer->state = HRTIMER_INACTIVE;
409 return 1;
410 }
411 return 0;
412}
413
414/**
415 * hrtimer_start - (re)start an relative timer on the current CPU
416 *
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 */
425int
426hrtimer_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
Ingo Molnar06027bd2006-02-14 13:53:15 -0800440 if (mode == HRTIMER_REL) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800441 tim = ktime_add(tim, new_base->get_time());
Ingo Molnar06027bd2006-02-14 13:53:15 -0800442 /*
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 }
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800453 timer->expires = tim;
454
455 enqueue_hrtimer(timer, new_base);
456
457 unlock_hrtimer_base(timer, &flags);
458
459 return ret;
460}
461
462/**
463 * hrtimer_try_to_cancel - try to deactivate a timer
464 *
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 * can not be stopped
472 */
473int 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
490/**
491 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
492 *
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 */
499int 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 }
507}
508
509/**
510 * hrtimer_get_remaining - get remaining time for the timer
511 *
512 * @timer: the timer to read
513 */
514ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
515{
516 struct hrtimer_base *base;
517 unsigned long flags;
518 ktime_t rem;
519
520 base = lock_hrtimer_base(timer, &flags);
521 rem = ktime_sub(timer->expires, timer->base->get_time());
522 unlock_hrtimer_base(timer, &flags);
523
524 return rem;
525}
526
Tony Lindgren69239742006-03-06 15:42:45 -0800527#ifdef CONFIG_NO_IDLE_HZ
528/**
529 * hrtimer_get_next_event - get the time until next expiry event
530 *
531 * Returns the delta to the next expiry event or KTIME_MAX if no timer
532 * is pending.
533 */
534ktime_t hrtimer_get_next_event(void)
535{
536 struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
537 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
538 unsigned long flags;
539 int i;
540
541 for (i = 0; i < MAX_HRTIMER_BASES; i++, base++) {
542 struct hrtimer *timer;
543
544 spin_lock_irqsave(&base->lock, flags);
545 if (!base->first) {
546 spin_unlock_irqrestore(&base->lock, flags);
547 continue;
548 }
549 timer = rb_entry(base->first, struct hrtimer, node);
550 delta.tv64 = timer->expires.tv64;
551 spin_unlock_irqrestore(&base->lock, flags);
552 delta = ktime_sub(delta, base->get_time());
553 if (delta.tv64 < mindelta.tv64)
554 mindelta.tv64 = delta.tv64;
555 }
556 if (mindelta.tv64 < 0)
557 mindelta.tv64 = 0;
558 return mindelta;
559}
560#endif
561
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800562/**
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800563 * hrtimer_init - initialize a timer to the given clock
564 *
565 * @timer: the timer to be initialized
566 * @clock_id: the clock to be used
George Anzinger7978672c2006-02-01 03:05:11 -0800567 * @mode: timer mode abs/rel
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800568 */
George Anzinger7978672c2006-02-01 03:05:11 -0800569void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
570 enum hrtimer_mode mode)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800571{
George Anzinger7978672c2006-02-01 03:05:11 -0800572 struct hrtimer_base *bases;
573
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800574 memset(timer, 0, sizeof(struct hrtimer));
George Anzinger7978672c2006-02-01 03:05:11 -0800575
576 bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
577
578 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_ABS)
579 clock_id = CLOCK_MONOTONIC;
580
581 timer->base = &bases[clock_id];
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800582}
583
584/**
585 * hrtimer_get_res - get the timer resolution for a clock
586 *
587 * @which_clock: which clock to query
588 * @tp: pointer to timespec variable to store the resolution
589 *
590 * Store the resolution of the clock selected by which_clock in the
591 * variable pointed to by tp.
592 */
593int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
594{
595 struct hrtimer_base *bases;
596
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800597 bases = per_cpu(hrtimer_bases, raw_smp_processor_id());
Thomas Gleixnere2787632006-01-12 11:36:14 +0100598 *tp = ktime_to_timespec(bases[which_clock].resolution);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800599
600 return 0;
601}
602
603/*
604 * Expire the per base hrtimer-queue:
605 */
606static inline void run_hrtimer_queue(struct hrtimer_base *base)
607{
Thomas Gleixner288867e2006-01-12 11:25:54 +0100608 struct rb_node *node;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800609
Thomas Gleixner92127c72006-03-26 01:38:05 -0800610 if (base->get_softirq_time)
611 base->softirq_time = base->get_softirq_time();
612
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800613 spin_lock_irq(&base->lock);
614
Thomas Gleixner288867e2006-01-12 11:25:54 +0100615 while ((node = base->first)) {
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800616 struct hrtimer *timer;
617 int (*fn)(void *);
618 int restart;
619 void *data;
620
Thomas Gleixner288867e2006-01-12 11:25:54 +0100621 timer = rb_entry(node, struct hrtimer, node);
Thomas Gleixner92127c72006-03-26 01:38:05 -0800622 if (base->softirq_time.tv64 <= timer->expires.tv64)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800623 break;
624
625 fn = timer->function;
626 data = timer->data;
627 set_curr_timer(base, timer);
Roman Zippel432569b2006-03-26 01:38:08 -0800628 timer->state = HRTIMER_INACTIVE;
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800629 __remove_hrtimer(timer, base);
630 spin_unlock_irq(&base->lock);
631
Roman Zippel432569b2006-03-26 01:38:08 -0800632 restart = fn(data);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800633
634 spin_lock_irq(&base->lock);
635
akpm@osdl.orgff60a5d2006-02-01 03:05:10 -0800636 /* Another CPU has added back the timer */
Roman Zippel432569b2006-03-26 01:38:08 -0800637 if (timer->state != HRTIMER_INACTIVE)
akpm@osdl.orgff60a5d2006-02-01 03:05:10 -0800638 continue;
639
Roman Zippel432569b2006-03-26 01:38:08 -0800640 if (restart != HRTIMER_NORESTART)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800641 enqueue_hrtimer(timer, base);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800642 }
643 set_curr_timer(base, NULL);
644 spin_unlock_irq(&base->lock);
645}
646
647/*
648 * Called from timer softirq every jiffy, expire hrtimers:
649 */
650void hrtimer_run_queues(void)
651{
652 struct hrtimer_base *base = __get_cpu_var(hrtimer_bases);
653 int i;
654
Thomas Gleixner92127c72006-03-26 01:38:05 -0800655 hrtimer_get_softirq_time(base);
656
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800657 for (i = 0; i < MAX_HRTIMER_BASES; i++)
658 run_hrtimer_queue(&base[i]);
659}
660
661/*
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800662 * Sleep related functions:
663 */
664
Roman Zippel432569b2006-03-26 01:38:08 -0800665struct sleep_hrtimer {
666 struct hrtimer timer;
667 struct task_struct *task;
668 int expired;
669};
670
671static int nanosleep_wakeup(void *data)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800672{
Roman Zippel432569b2006-03-26 01:38:08 -0800673 struct sleep_hrtimer *t = data;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800674
Roman Zippel432569b2006-03-26 01:38:08 -0800675 t->expired = 1;
676 wake_up_process(t->task);
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800677
Roman Zippel432569b2006-03-26 01:38:08 -0800678 return HRTIMER_NORESTART;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800679}
680
Roman Zippel432569b2006-03-26 01:38:08 -0800681static int __sched do_nanosleep(struct sleep_hrtimer *t, enum hrtimer_mode mode)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800682{
Roman Zippel432569b2006-03-26 01:38:08 -0800683 t->timer.function = nanosleep_wakeup;
684 t->timer.data = t;
685 t->task = current;
686 t->expired = 0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800687
Roman Zippel432569b2006-03-26 01:38:08 -0800688 do {
689 set_current_state(TASK_INTERRUPTIBLE);
690 hrtimer_start(&t->timer, t->timer.expires, mode);
691
692 schedule();
693
694 if (unlikely(!t->expired)) {
695 hrtimer_cancel(&t->timer);
696 mode = HRTIMER_ABS;
697 }
698 } while (!t->expired && !signal_pending(current));
699
700 return t->expired;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800701}
702
George Anzinger7978672c2006-02-01 03:05:11 -0800703static long __sched nanosleep_restart(struct restart_block *restart)
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800704{
Roman Zippel432569b2006-03-26 01:38:08 -0800705 struct sleep_hrtimer t;
Ingo Molnarea13dbc2006-01-16 10:59:41 +0100706 struct timespec __user *rmtp;
707 struct timespec tu;
Roman Zippel432569b2006-03-26 01:38:08 -0800708 ktime_t time;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800709
710 restart->fn = do_no_restart_syscall;
711
Roman Zippel432569b2006-03-26 01:38:08 -0800712 hrtimer_init(&t.timer, restart->arg3, HRTIMER_ABS);
713 t.timer.expires.tv64 = ((u64)restart->arg1 << 32) | (u64) restart->arg0;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800714
Roman Zippel432569b2006-03-26 01:38:08 -0800715 if (do_nanosleep(&t, HRTIMER_ABS))
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800716 return 0;
717
718 rmtp = (struct timespec __user *) restart->arg2;
Roman Zippel432569b2006-03-26 01:38:08 -0800719 if (rmtp) {
720 time = ktime_sub(t.timer.expires, t.timer.base->get_time());
721 if (time.tv64 <= 0)
722 return 0;
723 tu = ktime_to_timespec(time);
724 if (copy_to_user(rmtp, &tu, sizeof(tu)))
725 return -EFAULT;
726 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800727
Roman Zippel432569b2006-03-26 01:38:08 -0800728 restart->fn = nanosleep_restart;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800729
730 /* The other values in restart are already filled in */
731 return -ERESTART_RESTARTBLOCK;
732}
733
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800734long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
735 const enum hrtimer_mode mode, const clockid_t clockid)
736{
737 struct restart_block *restart;
Roman Zippel432569b2006-03-26 01:38:08 -0800738 struct sleep_hrtimer t;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800739 struct timespec tu;
740 ktime_t rem;
741
Roman Zippel432569b2006-03-26 01:38:08 -0800742 hrtimer_init(&t.timer, clockid, mode);
743 t.timer.expires = timespec_to_ktime(*rqtp);
744 if (do_nanosleep(&t, mode))
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800745 return 0;
746
George Anzinger7978672c2006-02-01 03:05:11 -0800747 /* Absolute timers do not update the rmtp value and restart: */
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800748 if (mode == HRTIMER_ABS)
749 return -ERESTARTNOHAND;
750
Roman Zippel432569b2006-03-26 01:38:08 -0800751 if (rmtp) {
752 rem = ktime_sub(t.timer.expires, t.timer.base->get_time());
753 if (rem.tv64 <= 0)
754 return 0;
755 tu = ktime_to_timespec(rem);
756 if (copy_to_user(rmtp, &tu, sizeof(tu)))
757 return -EFAULT;
758 }
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800759
760 restart = &current_thread_info()->restart_block;
George Anzinger7978672c2006-02-01 03:05:11 -0800761 restart->fn = nanosleep_restart;
Roman Zippel432569b2006-03-26 01:38:08 -0800762 restart->arg0 = t.timer.expires.tv64 & 0xFFFFFFFF;
763 restart->arg1 = t.timer.expires.tv64 >> 32;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800764 restart->arg2 = (unsigned long) rmtp;
Roman Zippel432569b2006-03-26 01:38:08 -0800765 restart->arg3 = (unsigned long) t.timer.base->index;
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800766
767 return -ERESTART_RESTARTBLOCK;
768}
769
Thomas Gleixner6ba1b912006-01-09 20:52:36 -0800770asmlinkage long
771sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
772{
773 struct timespec tu;
774
775 if (copy_from_user(&tu, rqtp, sizeof(tu)))
776 return -EFAULT;
777
778 if (!timespec_valid(&tu))
779 return -EINVAL;
780
781 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_REL, CLOCK_MONOTONIC);
782}
783
Thomas Gleixner10c94ec2006-01-09 20:52:35 -0800784/*
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800785 * Functions related to boot-time initialization:
786 */
787static void __devinit init_hrtimers_cpu(int cpu)
788{
789 struct hrtimer_base *base = per_cpu(hrtimer_bases, cpu);
790 int i;
791
George Anzinger7978672c2006-02-01 03:05:11 -0800792 for (i = 0; i < MAX_HRTIMER_BASES; i++, base++)
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800793 spin_lock_init(&base->lock);
Thomas Gleixnerc0a31322006-01-09 20:52:32 -0800794}
795
796#ifdef CONFIG_HOTPLUG_CPU
797
798static void migrate_hrtimer_list(struct hrtimer_base *old_base,
799 struct hrtimer_base *new_base)
800{
801 struct hrtimer *timer;
802 struct rb_node *node;
803
804 while ((node = rb_first(&old_base->active))) {
805 timer = rb_entry(node, struct hrtimer, node);
806 __remove_hrtimer(timer, old_base);
807 timer->base = new_base;
808 enqueue_hrtimer(timer, new_base);
809 }
810}
811
812static void migrate_hrtimers(int cpu)
813{
814 struct hrtimer_base *old_base, *new_base;
815 int i;
816
817 BUG_ON(cpu_online(cpu));
818 old_base = per_cpu(hrtimer_bases, cpu);
819 new_base = get_cpu_var(hrtimer_bases);
820
821 local_irq_disable();
822
823 for (i = 0; i < MAX_HRTIMER_BASES; i++) {
824
825 spin_lock(&new_base->lock);
826 spin_lock(&old_base->lock);
827
828 BUG_ON(old_base->curr_timer);
829
830 migrate_hrtimer_list(old_base, new_base);
831
832 spin_unlock(&old_base->lock);
833 spin_unlock(&new_base->lock);
834 old_base++;
835 new_base++;
836 }
837
838 local_irq_enable();
839 put_cpu_var(hrtimer_bases);
840}
841#endif /* CONFIG_HOTPLUG_CPU */
842
843static int __devinit hrtimer_cpu_notify(struct notifier_block *self,
844 unsigned long action, void *hcpu)
845{
846 long cpu = (long)hcpu;
847
848 switch (action) {
849
850 case CPU_UP_PREPARE:
851 init_hrtimers_cpu(cpu);
852 break;
853
854#ifdef CONFIG_HOTPLUG_CPU
855 case CPU_DEAD:
856 migrate_hrtimers(cpu);
857 break;
858#endif
859
860 default:
861 break;
862 }
863
864 return NOTIFY_OK;
865}
866
867static struct notifier_block __devinitdata hrtimers_nb = {
868 .notifier_call = hrtimer_cpu_notify,
869};
870
871void __init hrtimers_init(void)
872{
873 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
874 (void *)(long)smp_processor_id());
875 register_cpu_notifier(&hrtimers_nb);
876}
877