Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[deliverable/linux.git] / kernel / hrtimer.c
1 /*
2 * linux/kernel/hrtimer.c
3 *
4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
7 *
8 * High-resolution kernel timers
9 *
10 * In contrast to the low-resolution timeout API implemented in
11 * kernel/timer.c, hrtimers provide finer resolution and accuracy
12 * depending on system configuration and capabilities.
13 *
14 * These timers are currently used for:
15 * - itimers
16 * - POSIX timers
17 * - nanosleep
18 * - precise in-kernel timing
19 *
20 * Started by: Thomas Gleixner and Ingo Molnar
21 *
22 * Credits:
23 * based on kernel/timer.c
24 *
25 * Help, testing, suggestions, bugfixes, improvements were
26 * provided by:
27 *
28 * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel
29 * et. al.
30 *
31 * For licencing details see kernel-base/COPYING
32 */
33
34 #include <linux/cpu.h>
35 #include <linux/irq.h>
36 #include <linux/module.h>
37 #include <linux/percpu.h>
38 #include <linux/hrtimer.h>
39 #include <linux/notifier.h>
40 #include <linux/syscalls.h>
41 #include <linux/kallsyms.h>
42 #include <linux/interrupt.h>
43 #include <linux/tick.h>
44 #include <linux/seq_file.h>
45 #include <linux/err.h>
46 #include <linux/debugobjects.h>
47
48 #include <asm/uaccess.h>
49
50 /**
51 * ktime_get - get the monotonic time in ktime_t format
52 *
53 * returns the time in ktime_t format
54 */
55 ktime_t ktime_get(void)
56 {
57 struct timespec now;
58
59 ktime_get_ts(&now);
60
61 return timespec_to_ktime(now);
62 }
63 EXPORT_SYMBOL_GPL(ktime_get);
64
65 /**
66 * ktime_get_real - get the real (wall-) time in ktime_t format
67 *
68 * returns the time in ktime_t format
69 */
70 ktime_t ktime_get_real(void)
71 {
72 struct timespec now;
73
74 getnstimeofday(&now);
75
76 return timespec_to_ktime(now);
77 }
78
79 EXPORT_SYMBOL_GPL(ktime_get_real);
80
81 /*
82 * The timer bases:
83 *
84 * Note: If we want to add new timer bases, we have to skip the two
85 * clock ids captured by the cpu-timers. We do this by holding empty
86 * entries rather than doing math adjustment of the clock ids.
87 * This ensures that we capture erroneous accesses to these clock ids
88 * rather than moving them into the range of valid clock id's.
89 */
90 DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
91 {
92
93 .clock_base =
94 {
95 {
96 .index = CLOCK_REALTIME,
97 .get_time = &ktime_get_real,
98 .resolution = KTIME_LOW_RES,
99 },
100 {
101 .index = CLOCK_MONOTONIC,
102 .get_time = &ktime_get,
103 .resolution = KTIME_LOW_RES,
104 },
105 }
106 };
107
108 /**
109 * ktime_get_ts - get the monotonic clock in timespec format
110 * @ts: pointer to timespec variable
111 *
112 * The function calculates the monotonic clock from the realtime
113 * clock and the wall_to_monotonic offset and stores the result
114 * in normalized timespec format in the variable pointed to by @ts.
115 */
116 void ktime_get_ts(struct timespec *ts)
117 {
118 struct timespec tomono;
119 unsigned long seq;
120
121 do {
122 seq = read_seqbegin(&xtime_lock);
123 getnstimeofday(ts);
124 tomono = wall_to_monotonic;
125
126 } while (read_seqretry(&xtime_lock, seq));
127
128 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec,
129 ts->tv_nsec + tomono.tv_nsec);
130 }
131 EXPORT_SYMBOL_GPL(ktime_get_ts);
132
133 /*
134 * Get the coarse grained time at the softirq based on xtime and
135 * wall_to_monotonic.
136 */
137 static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
138 {
139 ktime_t xtim, tomono;
140 struct timespec xts, tom;
141 unsigned long seq;
142
143 do {
144 seq = read_seqbegin(&xtime_lock);
145 xts = current_kernel_time();
146 tom = wall_to_monotonic;
147 } while (read_seqretry(&xtime_lock, seq));
148
149 xtim = timespec_to_ktime(xts);
150 tomono = timespec_to_ktime(tom);
151 base->clock_base[CLOCK_REALTIME].softirq_time = xtim;
152 base->clock_base[CLOCK_MONOTONIC].softirq_time =
153 ktime_add(xtim, tomono);
154 }
155
156 /*
157 * Helper function to check, whether the timer is running the callback
158 * function
159 */
160 static inline int hrtimer_callback_running(struct hrtimer *timer)
161 {
162 return timer->state & HRTIMER_STATE_CALLBACK;
163 }
164
165 /*
166 * Functions and macros which are different for UP/SMP systems are kept in a
167 * single place
168 */
169 #ifdef CONFIG_SMP
170
171 /*
172 * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock
173 * means that all timers which are tied to this base via timer->base are
174 * locked, and the base itself is locked too.
175 *
176 * So __run_timers/migrate_timers can safely modify all timers which could
177 * be found on the lists/queues.
178 *
179 * When the timer's base is locked, and the timer removed from list, it is
180 * possible to set timer->base = NULL and drop the lock: the timer remains
181 * locked.
182 */
183 static
184 struct hrtimer_clock_base *lock_hrtimer_base(const struct hrtimer *timer,
185 unsigned long *flags)
186 {
187 struct hrtimer_clock_base *base;
188
189 for (;;) {
190 base = timer->base;
191 if (likely(base != NULL)) {
192 spin_lock_irqsave(&base->cpu_base->lock, *flags);
193 if (likely(base == timer->base))
194 return base;
195 /* The timer has migrated to another CPU: */
196 spin_unlock_irqrestore(&base->cpu_base->lock, *flags);
197 }
198 cpu_relax();
199 }
200 }
201
202 /*
203 * Switch the timer base to the current CPU when possible.
204 */
205 static inline struct hrtimer_clock_base *
206 switch_hrtimer_base(struct hrtimer *timer, struct hrtimer_clock_base *base)
207 {
208 struct hrtimer_clock_base *new_base;
209 struct hrtimer_cpu_base *new_cpu_base;
210
211 new_cpu_base = &__get_cpu_var(hrtimer_bases);
212 new_base = &new_cpu_base->clock_base[base->index];
213
214 if (base != new_base) {
215 /*
216 * We are trying to schedule the timer on the local CPU.
217 * However we can't change timer's base while it is running,
218 * so we keep it on the same CPU. No hassle vs. reprogramming
219 * the event source in the high resolution case. The softirq
220 * code will take care of this when the timer function has
221 * completed. There is no conflict as we hold the lock until
222 * the timer is enqueued.
223 */
224 if (unlikely(hrtimer_callback_running(timer)))
225 return base;
226
227 /* See the comment in lock_timer_base() */
228 timer->base = NULL;
229 spin_unlock(&base->cpu_base->lock);
230 spin_lock(&new_base->cpu_base->lock);
231 timer->base = new_base;
232 }
233 return new_base;
234 }
235
236 #else /* CONFIG_SMP */
237
238 static inline struct hrtimer_clock_base *
239 lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
240 {
241 struct hrtimer_clock_base *base = timer->base;
242
243 spin_lock_irqsave(&base->cpu_base->lock, *flags);
244
245 return base;
246 }
247
248 # define switch_hrtimer_base(t, b) (b)
249
250 #endif /* !CONFIG_SMP */
251
252 /*
253 * Functions for the union type storage format of ktime_t which are
254 * too large for inlining:
255 */
256 #if BITS_PER_LONG < 64
257 # ifndef CONFIG_KTIME_SCALAR
258 /**
259 * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
260 * @kt: addend
261 * @nsec: the scalar nsec value to add
262 *
263 * Returns the sum of kt and nsec in ktime_t format
264 */
265 ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
266 {
267 ktime_t tmp;
268
269 if (likely(nsec < NSEC_PER_SEC)) {
270 tmp.tv64 = nsec;
271 } else {
272 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
273
274 tmp = ktime_set((long)nsec, rem);
275 }
276
277 return ktime_add(kt, tmp);
278 }
279
280 EXPORT_SYMBOL_GPL(ktime_add_ns);
281
282 /**
283 * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
284 * @kt: minuend
285 * @nsec: the scalar nsec value to subtract
286 *
287 * Returns the subtraction of @nsec from @kt in ktime_t format
288 */
289 ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
290 {
291 ktime_t tmp;
292
293 if (likely(nsec < NSEC_PER_SEC)) {
294 tmp.tv64 = nsec;
295 } else {
296 unsigned long rem = do_div(nsec, NSEC_PER_SEC);
297
298 tmp = ktime_set((long)nsec, rem);
299 }
300
301 return ktime_sub(kt, tmp);
302 }
303
304 EXPORT_SYMBOL_GPL(ktime_sub_ns);
305 # endif /* !CONFIG_KTIME_SCALAR */
306
307 /*
308 * Divide a ktime value by a nanosecond value
309 */
310 u64 ktime_divns(const ktime_t kt, s64 div)
311 {
312 u64 dclc, inc, dns;
313 int sft = 0;
314
315 dclc = dns = ktime_to_ns(kt);
316 inc = div;
317 /* Make sure the divisor is less than 2^32: */
318 while (div >> 32) {
319 sft++;
320 div >>= 1;
321 }
322 dclc >>= sft;
323 do_div(dclc, (unsigned long) div);
324
325 return dclc;
326 }
327 #endif /* BITS_PER_LONG >= 64 */
328
329 /*
330 * Add two ktime values and do a safety check for overflow:
331 */
332 ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
333 {
334 ktime_t res = ktime_add(lhs, rhs);
335
336 /*
337 * We use KTIME_SEC_MAX here, the maximum timeout which we can
338 * return to user space in a timespec:
339 */
340 if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
341 res = ktime_set(KTIME_SEC_MAX, 0);
342
343 return res;
344 }
345
346 #ifdef CONFIG_DEBUG_OBJECTS_TIMERS
347
348 static struct debug_obj_descr hrtimer_debug_descr;
349
350 /*
351 * fixup_init is called when:
352 * - an active object is initialized
353 */
354 static int hrtimer_fixup_init(void *addr, enum debug_obj_state state)
355 {
356 struct hrtimer *timer = addr;
357
358 switch (state) {
359 case ODEBUG_STATE_ACTIVE:
360 hrtimer_cancel(timer);
361 debug_object_init(timer, &hrtimer_debug_descr);
362 return 1;
363 default:
364 return 0;
365 }
366 }
367
368 /*
369 * fixup_activate is called when:
370 * - an active object is activated
371 * - an unknown object is activated (might be a statically initialized object)
372 */
373 static int hrtimer_fixup_activate(void *addr, enum debug_obj_state state)
374 {
375 switch (state) {
376
377 case ODEBUG_STATE_NOTAVAILABLE:
378 WARN_ON_ONCE(1);
379 return 0;
380
381 case ODEBUG_STATE_ACTIVE:
382 WARN_ON(1);
383
384 default:
385 return 0;
386 }
387 }
388
389 /*
390 * fixup_free is called when:
391 * - an active object is freed
392 */
393 static int hrtimer_fixup_free(void *addr, enum debug_obj_state state)
394 {
395 struct hrtimer *timer = addr;
396
397 switch (state) {
398 case ODEBUG_STATE_ACTIVE:
399 hrtimer_cancel(timer);
400 debug_object_free(timer, &hrtimer_debug_descr);
401 return 1;
402 default:
403 return 0;
404 }
405 }
406
407 static struct debug_obj_descr hrtimer_debug_descr = {
408 .name = "hrtimer",
409 .fixup_init = hrtimer_fixup_init,
410 .fixup_activate = hrtimer_fixup_activate,
411 .fixup_free = hrtimer_fixup_free,
412 };
413
414 static inline void debug_hrtimer_init(struct hrtimer *timer)
415 {
416 debug_object_init(timer, &hrtimer_debug_descr);
417 }
418
419 static inline void debug_hrtimer_activate(struct hrtimer *timer)
420 {
421 debug_object_activate(timer, &hrtimer_debug_descr);
422 }
423
424 static inline void debug_hrtimer_deactivate(struct hrtimer *timer)
425 {
426 debug_object_deactivate(timer, &hrtimer_debug_descr);
427 }
428
429 static inline void debug_hrtimer_free(struct hrtimer *timer)
430 {
431 debug_object_free(timer, &hrtimer_debug_descr);
432 }
433
434 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
435 enum hrtimer_mode mode);
436
437 void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
438 enum hrtimer_mode mode)
439 {
440 debug_object_init_on_stack(timer, &hrtimer_debug_descr);
441 __hrtimer_init(timer, clock_id, mode);
442 }
443
444 void destroy_hrtimer_on_stack(struct hrtimer *timer)
445 {
446 debug_object_free(timer, &hrtimer_debug_descr);
447 }
448
449 #else
450 static inline void debug_hrtimer_init(struct hrtimer *timer) { }
451 static inline void debug_hrtimer_activate(struct hrtimer *timer) { }
452 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { }
453 #endif
454
455 /*
456 * Check, whether the timer is on the callback pending list
457 */
458 static inline int hrtimer_cb_pending(const struct hrtimer *timer)
459 {
460 return timer->state & HRTIMER_STATE_PENDING;
461 }
462
463 /*
464 * Remove a timer from the callback pending list
465 */
466 static inline void hrtimer_remove_cb_pending(struct hrtimer *timer)
467 {
468 list_del_init(&timer->cb_entry);
469 }
470
471 /* High resolution timer related functions */
472 #ifdef CONFIG_HIGH_RES_TIMERS
473
474 /*
475 * High resolution timer enabled ?
476 */
477 static int hrtimer_hres_enabled __read_mostly = 1;
478
479 /*
480 * Enable / Disable high resolution mode
481 */
482 static int __init setup_hrtimer_hres(char *str)
483 {
484 if (!strcmp(str, "off"))
485 hrtimer_hres_enabled = 0;
486 else if (!strcmp(str, "on"))
487 hrtimer_hres_enabled = 1;
488 else
489 return 0;
490 return 1;
491 }
492
493 __setup("highres=", setup_hrtimer_hres);
494
495 /*
496 * hrtimer_high_res_enabled - query, if the highres mode is enabled
497 */
498 static inline int hrtimer_is_hres_enabled(void)
499 {
500 return hrtimer_hres_enabled;
501 }
502
503 /*
504 * Is the high resolution mode active ?
505 */
506 static inline int hrtimer_hres_active(void)
507 {
508 return __get_cpu_var(hrtimer_bases).hres_active;
509 }
510
511 /*
512 * Reprogram the event source with checking both queues for the
513 * next event
514 * Called with interrupts disabled and base->lock held
515 */
516 static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base)
517 {
518 int i;
519 struct hrtimer_clock_base *base = cpu_base->clock_base;
520 ktime_t expires;
521
522 cpu_base->expires_next.tv64 = KTIME_MAX;
523
524 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
525 struct hrtimer *timer;
526
527 if (!base->first)
528 continue;
529 timer = rb_entry(base->first, struct hrtimer, node);
530 expires = ktime_sub(timer->expires, base->offset);
531 if (expires.tv64 < cpu_base->expires_next.tv64)
532 cpu_base->expires_next = expires;
533 }
534
535 if (cpu_base->expires_next.tv64 != KTIME_MAX)
536 tick_program_event(cpu_base->expires_next, 1);
537 }
538
539 /*
540 * Shared reprogramming for clock_realtime and clock_monotonic
541 *
542 * When a timer is enqueued and expires earlier than the already enqueued
543 * timers, we have to check, whether it expires earlier than the timer for
544 * which the clock event device was armed.
545 *
546 * Called with interrupts disabled and base->cpu_base.lock held
547 */
548 static int hrtimer_reprogram(struct hrtimer *timer,
549 struct hrtimer_clock_base *base)
550 {
551 ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next;
552 ktime_t expires = ktime_sub(timer->expires, base->offset);
553 int res;
554
555 WARN_ON_ONCE(timer->expires.tv64 < 0);
556
557 /*
558 * When the callback is running, we do not reprogram the clock event
559 * device. The timer callback is either running on a different CPU or
560 * the callback is executed in the hrtimer_interrupt context. The
561 * reprogramming is handled either by the softirq, which called the
562 * callback or at the end of the hrtimer_interrupt.
563 */
564 if (hrtimer_callback_running(timer))
565 return 0;
566
567 /*
568 * CLOCK_REALTIME timer might be requested with an absolute
569 * expiry time which is less than base->offset. Nothing wrong
570 * about that, just avoid to call into the tick code, which
571 * has now objections against negative expiry values.
572 */
573 if (expires.tv64 < 0)
574 return -ETIME;
575
576 if (expires.tv64 >= expires_next->tv64)
577 return 0;
578
579 /*
580 * Clockevents returns -ETIME, when the event was in the past.
581 */
582 res = tick_program_event(expires, 0);
583 if (!IS_ERR_VALUE(res))
584 *expires_next = expires;
585 return res;
586 }
587
588
589 /*
590 * Retrigger next event is called after clock was set
591 *
592 * Called with interrupts disabled via on_each_cpu()
593 */
594 static void retrigger_next_event(void *arg)
595 {
596 struct hrtimer_cpu_base *base;
597 struct timespec realtime_offset;
598 unsigned long seq;
599
600 if (!hrtimer_hres_active())
601 return;
602
603 do {
604 seq = read_seqbegin(&xtime_lock);
605 set_normalized_timespec(&realtime_offset,
606 -wall_to_monotonic.tv_sec,
607 -wall_to_monotonic.tv_nsec);
608 } while (read_seqretry(&xtime_lock, seq));
609
610 base = &__get_cpu_var(hrtimer_bases);
611
612 /* Adjust CLOCK_REALTIME offset */
613 spin_lock(&base->lock);
614 base->clock_base[CLOCK_REALTIME].offset =
615 timespec_to_ktime(realtime_offset);
616
617 hrtimer_force_reprogram(base);
618 spin_unlock(&base->lock);
619 }
620
621 /*
622 * Clock realtime was set
623 *
624 * Change the offset of the realtime clock vs. the monotonic
625 * clock.
626 *
627 * We might have to reprogram the high resolution timer interrupt. On
628 * SMP we call the architecture specific code to retrigger _all_ high
629 * resolution timer interrupts. On UP we just disable interrupts and
630 * call the high resolution interrupt code.
631 */
632 void clock_was_set(void)
633 {
634 /* Retrigger the CPU local events everywhere */
635 on_each_cpu(retrigger_next_event, NULL, 0, 1);
636 }
637
638 /*
639 * During resume we might have to reprogram the high resolution timer
640 * interrupt (on the local CPU):
641 */
642 void hres_timers_resume(void)
643 {
644 WARN_ON_ONCE(num_online_cpus() > 1);
645
646 /* Retrigger the CPU local events: */
647 retrigger_next_event(NULL);
648 }
649
650 /*
651 * Initialize the high resolution related parts of cpu_base
652 */
653 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base)
654 {
655 base->expires_next.tv64 = KTIME_MAX;
656 base->hres_active = 0;
657 }
658
659 /*
660 * Initialize the high resolution related parts of a hrtimer
661 */
662 static inline void hrtimer_init_timer_hres(struct hrtimer *timer)
663 {
664 }
665
666 /*
667 * When High resolution timers are active, try to reprogram. Note, that in case
668 * the state has HRTIMER_STATE_CALLBACK set, no reprogramming and no expiry
669 * check happens. The timer gets enqueued into the rbtree. The reprogramming
670 * and expiry check is done in the hrtimer_interrupt or in the softirq.
671 */
672 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
673 struct hrtimer_clock_base *base)
674 {
675 if (base->cpu_base->hres_active && hrtimer_reprogram(timer, base)) {
676
677 /* Timer is expired, act upon the callback mode */
678 switch(timer->cb_mode) {
679 case HRTIMER_CB_IRQSAFE_NO_RESTART:
680 debug_hrtimer_deactivate(timer);
681 /*
682 * We can call the callback from here. No restart
683 * happens, so no danger of recursion
684 */
685 BUG_ON(timer->function(timer) != HRTIMER_NORESTART);
686 return 1;
687 case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ:
688 /*
689 * This is solely for the sched tick emulation with
690 * dynamic tick support to ensure that we do not
691 * restart the tick right on the edge and end up with
692 * the tick timer in the softirq ! The calling site
693 * takes care of this.
694 */
695 debug_hrtimer_deactivate(timer);
696 return 1;
697 case HRTIMER_CB_IRQSAFE:
698 case HRTIMER_CB_SOFTIRQ:
699 /*
700 * Move everything else into the softirq pending list !
701 */
702 list_add_tail(&timer->cb_entry,
703 &base->cpu_base->cb_pending);
704 timer->state = HRTIMER_STATE_PENDING;
705 return 1;
706 default:
707 BUG();
708 }
709 }
710 return 0;
711 }
712
713 /*
714 * Switch to high resolution mode
715 */
716 static int hrtimer_switch_to_hres(void)
717 {
718 int cpu = smp_processor_id();
719 struct hrtimer_cpu_base *base = &per_cpu(hrtimer_bases, cpu);
720 unsigned long flags;
721
722 if (base->hres_active)
723 return 1;
724
725 local_irq_save(flags);
726
727 if (tick_init_highres()) {
728 local_irq_restore(flags);
729 printk(KERN_WARNING "Could not switch to high resolution "
730 "mode on CPU %d\n", cpu);
731 return 0;
732 }
733 base->hres_active = 1;
734 base->clock_base[CLOCK_REALTIME].resolution = KTIME_HIGH_RES;
735 base->clock_base[CLOCK_MONOTONIC].resolution = KTIME_HIGH_RES;
736
737 tick_setup_sched_timer();
738
739 /* "Retrigger" the interrupt to get things going */
740 retrigger_next_event(NULL);
741 local_irq_restore(flags);
742 printk(KERN_DEBUG "Switched to high resolution mode on CPU %d\n",
743 smp_processor_id());
744 return 1;
745 }
746
747 static inline void hrtimer_raise_softirq(void)
748 {
749 raise_softirq(HRTIMER_SOFTIRQ);
750 }
751
752 #else
753
754 static inline int hrtimer_hres_active(void) { return 0; }
755 static inline int hrtimer_is_hres_enabled(void) { return 0; }
756 static inline int hrtimer_switch_to_hres(void) { return 0; }
757 static inline void hrtimer_force_reprogram(struct hrtimer_cpu_base *base) { }
758 static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer,
759 struct hrtimer_clock_base *base)
760 {
761 return 0;
762 }
763 static inline void hrtimer_init_hres(struct hrtimer_cpu_base *base) { }
764 static inline void hrtimer_init_timer_hres(struct hrtimer *timer) { }
765 static inline int hrtimer_reprogram(struct hrtimer *timer,
766 struct hrtimer_clock_base *base)
767 {
768 return 0;
769 }
770 static inline void hrtimer_raise_softirq(void) { }
771
772 #endif /* CONFIG_HIGH_RES_TIMERS */
773
774 #ifdef CONFIG_TIMER_STATS
775 void __timer_stats_hrtimer_set_start_info(struct hrtimer *timer, void *addr)
776 {
777 if (timer->start_site)
778 return;
779
780 timer->start_site = addr;
781 memcpy(timer->start_comm, current->comm, TASK_COMM_LEN);
782 timer->start_pid = current->pid;
783 }
784 #endif
785
786 /*
787 * Counterpart to lock_hrtimer_base above:
788 */
789 static inline
790 void unlock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
791 {
792 spin_unlock_irqrestore(&timer->base->cpu_base->lock, *flags);
793 }
794
795 /**
796 * hrtimer_forward - forward the timer expiry
797 * @timer: hrtimer to forward
798 * @now: forward past this time
799 * @interval: the interval to forward
800 *
801 * Forward the timer expiry so it will expire in the future.
802 * Returns the number of overruns.
803 */
804 u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
805 {
806 u64 orun = 1;
807 ktime_t delta;
808
809 delta = ktime_sub(now, timer->expires);
810
811 if (delta.tv64 < 0)
812 return 0;
813
814 if (interval.tv64 < timer->base->resolution.tv64)
815 interval.tv64 = timer->base->resolution.tv64;
816
817 if (unlikely(delta.tv64 >= interval.tv64)) {
818 s64 incr = ktime_to_ns(interval);
819
820 orun = ktime_divns(delta, incr);
821 timer->expires = ktime_add_ns(timer->expires, incr * orun);
822 if (timer->expires.tv64 > now.tv64)
823 return orun;
824 /*
825 * This (and the ktime_add() below) is the
826 * correction for exact:
827 */
828 orun++;
829 }
830 timer->expires = ktime_add_safe(timer->expires, interval);
831
832 return orun;
833 }
834 EXPORT_SYMBOL_GPL(hrtimer_forward);
835
836 /*
837 * enqueue_hrtimer - internal function to (re)start a timer
838 *
839 * The timer is inserted in expiry order. Insertion into the
840 * red black tree is O(log(n)). Must hold the base lock.
841 */
842 static void enqueue_hrtimer(struct hrtimer *timer,
843 struct hrtimer_clock_base *base, int reprogram)
844 {
845 struct rb_node **link = &base->active.rb_node;
846 struct rb_node *parent = NULL;
847 struct hrtimer *entry;
848 int leftmost = 1;
849
850 debug_hrtimer_activate(timer);
851
852 /*
853 * Find the right place in the rbtree:
854 */
855 while (*link) {
856 parent = *link;
857 entry = rb_entry(parent, struct hrtimer, node);
858 /*
859 * We dont care about collisions. Nodes with
860 * the same expiry time stay together.
861 */
862 if (timer->expires.tv64 < entry->expires.tv64) {
863 link = &(*link)->rb_left;
864 } else {
865 link = &(*link)->rb_right;
866 leftmost = 0;
867 }
868 }
869
870 /*
871 * Insert the timer to the rbtree and check whether it
872 * replaces the first pending timer
873 */
874 if (leftmost) {
875 /*
876 * Reprogram the clock event device. When the timer is already
877 * expired hrtimer_enqueue_reprogram has either called the
878 * callback or added it to the pending list and raised the
879 * softirq.
880 *
881 * This is a NOP for !HIGHRES
882 */
883 if (reprogram && hrtimer_enqueue_reprogram(timer, base))
884 return;
885
886 base->first = &timer->node;
887 }
888
889 rb_link_node(&timer->node, parent, link);
890 rb_insert_color(&timer->node, &base->active);
891 /*
892 * HRTIMER_STATE_ENQUEUED is or'ed to the current state to preserve the
893 * state of a possibly running callback.
894 */
895 timer->state |= HRTIMER_STATE_ENQUEUED;
896 }
897
898 /*
899 * __remove_hrtimer - internal function to remove a timer
900 *
901 * Caller must hold the base lock.
902 *
903 * High resolution timer mode reprograms the clock event device when the
904 * timer is the one which expires next. The caller can disable this by setting
905 * reprogram to zero. This is useful, when the context does a reprogramming
906 * anyway (e.g. timer interrupt)
907 */
908 static void __remove_hrtimer(struct hrtimer *timer,
909 struct hrtimer_clock_base *base,
910 unsigned long newstate, int reprogram)
911 {
912 /* High res. callback list. NOP for !HIGHRES */
913 if (hrtimer_cb_pending(timer))
914 hrtimer_remove_cb_pending(timer);
915 else {
916 /*
917 * Remove the timer from the rbtree and replace the
918 * first entry pointer if necessary.
919 */
920 if (base->first == &timer->node) {
921 base->first = rb_next(&timer->node);
922 /* Reprogram the clock event device. if enabled */
923 if (reprogram && hrtimer_hres_active())
924 hrtimer_force_reprogram(base->cpu_base);
925 }
926 rb_erase(&timer->node, &base->active);
927 }
928 timer->state = newstate;
929 }
930
931 /*
932 * remove hrtimer, called with base lock held
933 */
934 static inline int
935 remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base)
936 {
937 if (hrtimer_is_queued(timer)) {
938 int reprogram;
939
940 /*
941 * Remove the timer and force reprogramming when high
942 * resolution mode is active and the timer is on the current
943 * CPU. If we remove a timer on another CPU, reprogramming is
944 * skipped. The interrupt event on this CPU is fired and
945 * reprogramming happens in the interrupt handler. This is a
946 * rare case and less expensive than a smp call.
947 */
948 debug_hrtimer_deactivate(timer);
949 timer_stats_hrtimer_clear_start_info(timer);
950 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases);
951 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE,
952 reprogram);
953 return 1;
954 }
955 return 0;
956 }
957
958 /**
959 * hrtimer_start - (re)start an relative timer on the current CPU
960 * @timer: the timer to be added
961 * @tim: expiry time
962 * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL)
963 *
964 * Returns:
965 * 0 on success
966 * 1 when the timer was active
967 */
968 int
969 hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
970 {
971 struct hrtimer_clock_base *base, *new_base;
972 unsigned long flags;
973 int ret, raise;
974
975 base = lock_hrtimer_base(timer, &flags);
976
977 /* Remove an active timer from the queue: */
978 ret = remove_hrtimer(timer, base);
979
980 /* Switch the timer base, if necessary: */
981 new_base = switch_hrtimer_base(timer, base);
982
983 if (mode == HRTIMER_MODE_REL) {
984 tim = ktime_add_safe(tim, new_base->get_time());
985 /*
986 * CONFIG_TIME_LOW_RES is a temporary way for architectures
987 * to signal that they simply return xtime in
988 * do_gettimeoffset(). In this case we want to round up by
989 * resolution when starting a relative timer, to avoid short
990 * timeouts. This will go away with the GTOD framework.
991 */
992 #ifdef CONFIG_TIME_LOW_RES
993 tim = ktime_add_safe(tim, base->resolution);
994 #endif
995 }
996
997 timer->expires = tim;
998
999 timer_stats_hrtimer_set_start_info(timer);
1000
1001 /*
1002 * Only allow reprogramming if the new base is on this CPU.
1003 * (it might still be on another CPU if the timer was pending)
1004 */
1005 enqueue_hrtimer(timer, new_base,
1006 new_base->cpu_base == &__get_cpu_var(hrtimer_bases));
1007
1008 /*
1009 * The timer may be expired and moved to the cb_pending
1010 * list. We can not raise the softirq with base lock held due
1011 * to a possible deadlock with runqueue lock.
1012 */
1013 raise = timer->state == HRTIMER_STATE_PENDING;
1014
1015 unlock_hrtimer_base(timer, &flags);
1016
1017 if (raise)
1018 hrtimer_raise_softirq();
1019
1020 return ret;
1021 }
1022 EXPORT_SYMBOL_GPL(hrtimer_start);
1023
1024 /**
1025 * hrtimer_try_to_cancel - try to deactivate a timer
1026 * @timer: hrtimer to stop
1027 *
1028 * Returns:
1029 * 0 when the timer was not active
1030 * 1 when the timer was active
1031 * -1 when the timer is currently excuting the callback function and
1032 * cannot be stopped
1033 */
1034 int hrtimer_try_to_cancel(struct hrtimer *timer)
1035 {
1036 struct hrtimer_clock_base *base;
1037 unsigned long flags;
1038 int ret = -1;
1039
1040 base = lock_hrtimer_base(timer, &flags);
1041
1042 if (!hrtimer_callback_running(timer))
1043 ret = remove_hrtimer(timer, base);
1044
1045 unlock_hrtimer_base(timer, &flags);
1046
1047 return ret;
1048
1049 }
1050 EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel);
1051
1052 /**
1053 * hrtimer_cancel - cancel a timer and wait for the handler to finish.
1054 * @timer: the timer to be cancelled
1055 *
1056 * Returns:
1057 * 0 when the timer was not active
1058 * 1 when the timer was active
1059 */
1060 int hrtimer_cancel(struct hrtimer *timer)
1061 {
1062 for (;;) {
1063 int ret = hrtimer_try_to_cancel(timer);
1064
1065 if (ret >= 0)
1066 return ret;
1067 cpu_relax();
1068 }
1069 }
1070 EXPORT_SYMBOL_GPL(hrtimer_cancel);
1071
1072 /**
1073 * hrtimer_get_remaining - get remaining time for the timer
1074 * @timer: the timer to read
1075 */
1076 ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
1077 {
1078 struct hrtimer_clock_base *base;
1079 unsigned long flags;
1080 ktime_t rem;
1081
1082 base = lock_hrtimer_base(timer, &flags);
1083 rem = ktime_sub(timer->expires, base->get_time());
1084 unlock_hrtimer_base(timer, &flags);
1085
1086 return rem;
1087 }
1088 EXPORT_SYMBOL_GPL(hrtimer_get_remaining);
1089
1090 #if defined(CONFIG_NO_IDLE_HZ) || defined(CONFIG_NO_HZ)
1091 /**
1092 * hrtimer_get_next_event - get the time until next expiry event
1093 *
1094 * Returns the delta to the next expiry event or KTIME_MAX if no timer
1095 * is pending.
1096 */
1097 ktime_t hrtimer_get_next_event(void)
1098 {
1099 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1100 struct hrtimer_clock_base *base = cpu_base->clock_base;
1101 ktime_t delta, mindelta = { .tv64 = KTIME_MAX };
1102 unsigned long flags;
1103 int i;
1104
1105 spin_lock_irqsave(&cpu_base->lock, flags);
1106
1107 if (!hrtimer_hres_active()) {
1108 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++, base++) {
1109 struct hrtimer *timer;
1110
1111 if (!base->first)
1112 continue;
1113
1114 timer = rb_entry(base->first, struct hrtimer, node);
1115 delta.tv64 = timer->expires.tv64;
1116 delta = ktime_sub(delta, base->get_time());
1117 if (delta.tv64 < mindelta.tv64)
1118 mindelta.tv64 = delta.tv64;
1119 }
1120 }
1121
1122 spin_unlock_irqrestore(&cpu_base->lock, flags);
1123
1124 if (mindelta.tv64 < 0)
1125 mindelta.tv64 = 0;
1126 return mindelta;
1127 }
1128 #endif
1129
1130 static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1131 enum hrtimer_mode mode)
1132 {
1133 struct hrtimer_cpu_base *cpu_base;
1134
1135 memset(timer, 0, sizeof(struct hrtimer));
1136
1137 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1138
1139 if (clock_id == CLOCK_REALTIME && mode != HRTIMER_MODE_ABS)
1140 clock_id = CLOCK_MONOTONIC;
1141
1142 timer->base = &cpu_base->clock_base[clock_id];
1143 INIT_LIST_HEAD(&timer->cb_entry);
1144 hrtimer_init_timer_hres(timer);
1145
1146 #ifdef CONFIG_TIMER_STATS
1147 timer->start_site = NULL;
1148 timer->start_pid = -1;
1149 memset(timer->start_comm, 0, TASK_COMM_LEN);
1150 #endif
1151 }
1152
1153 /**
1154 * hrtimer_init - initialize a timer to the given clock
1155 * @timer: the timer to be initialized
1156 * @clock_id: the clock to be used
1157 * @mode: timer mode abs/rel
1158 */
1159 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
1160 enum hrtimer_mode mode)
1161 {
1162 debug_hrtimer_init(timer);
1163 __hrtimer_init(timer, clock_id, mode);
1164 }
1165 EXPORT_SYMBOL_GPL(hrtimer_init);
1166
1167 /**
1168 * hrtimer_get_res - get the timer resolution for a clock
1169 * @which_clock: which clock to query
1170 * @tp: pointer to timespec variable to store the resolution
1171 *
1172 * Store the resolution of the clock selected by @which_clock in the
1173 * variable pointed to by @tp.
1174 */
1175 int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
1176 {
1177 struct hrtimer_cpu_base *cpu_base;
1178
1179 cpu_base = &__raw_get_cpu_var(hrtimer_bases);
1180 *tp = ktime_to_timespec(cpu_base->clock_base[which_clock].resolution);
1181
1182 return 0;
1183 }
1184 EXPORT_SYMBOL_GPL(hrtimer_get_res);
1185
1186 static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base)
1187 {
1188 spin_lock_irq(&cpu_base->lock);
1189
1190 while (!list_empty(&cpu_base->cb_pending)) {
1191 enum hrtimer_restart (*fn)(struct hrtimer *);
1192 struct hrtimer *timer;
1193 int restart;
1194
1195 timer = list_entry(cpu_base->cb_pending.next,
1196 struct hrtimer, cb_entry);
1197
1198 debug_hrtimer_deactivate(timer);
1199 timer_stats_account_hrtimer(timer);
1200
1201 fn = timer->function;
1202 __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0);
1203 spin_unlock_irq(&cpu_base->lock);
1204
1205 restart = fn(timer);
1206
1207 spin_lock_irq(&cpu_base->lock);
1208
1209 timer->state &= ~HRTIMER_STATE_CALLBACK;
1210 if (restart == HRTIMER_RESTART) {
1211 BUG_ON(hrtimer_active(timer));
1212 /*
1213 * Enqueue the timer, allow reprogramming of the event
1214 * device
1215 */
1216 enqueue_hrtimer(timer, timer->base, 1);
1217 } else if (hrtimer_active(timer)) {
1218 /*
1219 * If the timer was rearmed on another CPU, reprogram
1220 * the event device.
1221 */
1222 struct hrtimer_clock_base *base = timer->base;
1223
1224 if (base->first == &timer->node &&
1225 hrtimer_reprogram(timer, base)) {
1226 /*
1227 * Timer is expired. Thus move it from tree to
1228 * pending list again.
1229 */
1230 __remove_hrtimer(timer, base,
1231 HRTIMER_STATE_PENDING, 0);
1232 list_add_tail(&timer->cb_entry,
1233 &base->cpu_base->cb_pending);
1234 }
1235 }
1236 }
1237 spin_unlock_irq(&cpu_base->lock);
1238 }
1239
1240 static void __run_hrtimer(struct hrtimer *timer)
1241 {
1242 struct hrtimer_clock_base *base = timer->base;
1243 struct hrtimer_cpu_base *cpu_base = base->cpu_base;
1244 enum hrtimer_restart (*fn)(struct hrtimer *);
1245 int restart;
1246
1247 debug_hrtimer_deactivate(timer);
1248 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0);
1249 timer_stats_account_hrtimer(timer);
1250
1251 fn = timer->function;
1252 if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) {
1253 /*
1254 * Used for scheduler timers, avoid lock inversion with
1255 * rq->lock and tasklist_lock.
1256 *
1257 * These timers are required to deal with enqueue expiry
1258 * themselves and are not allowed to migrate.
1259 */
1260 spin_unlock(&cpu_base->lock);
1261 restart = fn(timer);
1262 spin_lock(&cpu_base->lock);
1263 } else
1264 restart = fn(timer);
1265
1266 /*
1267 * Note: We clear the CALLBACK bit after enqueue_hrtimer to avoid
1268 * reprogramming of the event hardware. This happens at the end of this
1269 * function anyway.
1270 */
1271 if (restart != HRTIMER_NORESTART) {
1272 BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
1273 enqueue_hrtimer(timer, base, 0);
1274 }
1275 timer->state &= ~HRTIMER_STATE_CALLBACK;
1276 }
1277
1278 #ifdef CONFIG_HIGH_RES_TIMERS
1279
1280 /*
1281 * High resolution timer interrupt
1282 * Called with interrupts disabled
1283 */
1284 void hrtimer_interrupt(struct clock_event_device *dev)
1285 {
1286 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1287 struct hrtimer_clock_base *base;
1288 ktime_t expires_next, now;
1289 int i, raise = 0;
1290
1291 BUG_ON(!cpu_base->hres_active);
1292 cpu_base->nr_events++;
1293 dev->next_event.tv64 = KTIME_MAX;
1294
1295 retry:
1296 now = ktime_get();
1297
1298 expires_next.tv64 = KTIME_MAX;
1299
1300 base = cpu_base->clock_base;
1301
1302 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1303 ktime_t basenow;
1304 struct rb_node *node;
1305
1306 spin_lock(&cpu_base->lock);
1307
1308 basenow = ktime_add(now, base->offset);
1309
1310 while ((node = base->first)) {
1311 struct hrtimer *timer;
1312
1313 timer = rb_entry(node, struct hrtimer, node);
1314
1315 if (basenow.tv64 < timer->expires.tv64) {
1316 ktime_t expires;
1317
1318 expires = ktime_sub(timer->expires,
1319 base->offset);
1320 if (expires.tv64 < expires_next.tv64)
1321 expires_next = expires;
1322 break;
1323 }
1324
1325 /* Move softirq callbacks to the pending list */
1326 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1327 __remove_hrtimer(timer, base,
1328 HRTIMER_STATE_PENDING, 0);
1329 list_add_tail(&timer->cb_entry,
1330 &base->cpu_base->cb_pending);
1331 raise = 1;
1332 continue;
1333 }
1334
1335 __run_hrtimer(timer);
1336 }
1337 spin_unlock(&cpu_base->lock);
1338 base++;
1339 }
1340
1341 cpu_base->expires_next = expires_next;
1342
1343 /* Reprogramming necessary ? */
1344 if (expires_next.tv64 != KTIME_MAX) {
1345 if (tick_program_event(expires_next, 0))
1346 goto retry;
1347 }
1348
1349 /* Raise softirq ? */
1350 if (raise)
1351 raise_softirq(HRTIMER_SOFTIRQ);
1352 }
1353
1354 static void run_hrtimer_softirq(struct softirq_action *h)
1355 {
1356 run_hrtimer_pending(&__get_cpu_var(hrtimer_bases));
1357 }
1358
1359 #endif /* CONFIG_HIGH_RES_TIMERS */
1360
1361 /*
1362 * Called from timer softirq every jiffy, expire hrtimers:
1363 *
1364 * For HRT its the fall back code to run the softirq in the timer
1365 * softirq context in case the hrtimer initialization failed or has
1366 * not been done yet.
1367 */
1368 void hrtimer_run_pending(void)
1369 {
1370 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1371
1372 if (hrtimer_hres_active())
1373 return;
1374
1375 /*
1376 * This _is_ ugly: We have to check in the softirq context,
1377 * whether we can switch to highres and / or nohz mode. The
1378 * clocksource switch happens in the timer interrupt with
1379 * xtime_lock held. Notification from there only sets the
1380 * check bit in the tick_oneshot code, otherwise we might
1381 * deadlock vs. xtime_lock.
1382 */
1383 if (tick_check_oneshot_change(!hrtimer_is_hres_enabled()))
1384 hrtimer_switch_to_hres();
1385
1386 run_hrtimer_pending(cpu_base);
1387 }
1388
1389 /*
1390 * Called from hardirq context every jiffy
1391 */
1392 void hrtimer_run_queues(void)
1393 {
1394 struct rb_node *node;
1395 struct hrtimer_cpu_base *cpu_base = &__get_cpu_var(hrtimer_bases);
1396 struct hrtimer_clock_base *base;
1397 int index, gettime = 1;
1398
1399 if (hrtimer_hres_active())
1400 return;
1401
1402 for (index = 0; index < HRTIMER_MAX_CLOCK_BASES; index++) {
1403 base = &cpu_base->clock_base[index];
1404
1405 if (!base->first)
1406 continue;
1407
1408 if (base->get_softirq_time)
1409 base->softirq_time = base->get_softirq_time();
1410 else if (gettime) {
1411 hrtimer_get_softirq_time(cpu_base);
1412 gettime = 0;
1413 }
1414
1415 spin_lock(&cpu_base->lock);
1416
1417 while ((node = base->first)) {
1418 struct hrtimer *timer;
1419
1420 timer = rb_entry(node, struct hrtimer, node);
1421 if (base->softirq_time.tv64 <= timer->expires.tv64)
1422 break;
1423
1424 if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) {
1425 __remove_hrtimer(timer, base,
1426 HRTIMER_STATE_PENDING, 0);
1427 list_add_tail(&timer->cb_entry,
1428 &base->cpu_base->cb_pending);
1429 continue;
1430 }
1431
1432 __run_hrtimer(timer);
1433 }
1434 spin_unlock(&cpu_base->lock);
1435 }
1436 }
1437
1438 /*
1439 * Sleep related functions:
1440 */
1441 static enum hrtimer_restart hrtimer_wakeup(struct hrtimer *timer)
1442 {
1443 struct hrtimer_sleeper *t =
1444 container_of(timer, struct hrtimer_sleeper, timer);
1445 struct task_struct *task = t->task;
1446
1447 t->task = NULL;
1448 if (task)
1449 wake_up_process(task);
1450
1451 return HRTIMER_NORESTART;
1452 }
1453
1454 void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
1455 {
1456 sl->timer.function = hrtimer_wakeup;
1457 sl->task = task;
1458 #ifdef CONFIG_HIGH_RES_TIMERS
1459 sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1460 #endif
1461 }
1462
1463 static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
1464 {
1465 hrtimer_init_sleeper(t, current);
1466
1467 do {
1468 set_current_state(TASK_INTERRUPTIBLE);
1469 hrtimer_start(&t->timer, t->timer.expires, mode);
1470 if (!hrtimer_active(&t->timer))
1471 t->task = NULL;
1472
1473 if (likely(t->task))
1474 schedule();
1475
1476 hrtimer_cancel(&t->timer);
1477 mode = HRTIMER_MODE_ABS;
1478
1479 } while (t->task && !signal_pending(current));
1480
1481 __set_current_state(TASK_RUNNING);
1482
1483 return t->task == NULL;
1484 }
1485
1486 static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1487 {
1488 struct timespec rmt;
1489 ktime_t rem;
1490
1491 rem = ktime_sub(timer->expires, timer->base->get_time());
1492 if (rem.tv64 <= 0)
1493 return 0;
1494 rmt = ktime_to_timespec(rem);
1495
1496 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1497 return -EFAULT;
1498
1499 return 1;
1500 }
1501
1502 long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
1503 {
1504 struct hrtimer_sleeper t;
1505 struct timespec __user *rmtp;
1506 int ret = 0;
1507
1508 hrtimer_init_on_stack(&t.timer, restart->nanosleep.index,
1509 HRTIMER_MODE_ABS);
1510 t.timer.expires.tv64 = restart->nanosleep.expires;
1511
1512 if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1513 goto out;
1514
1515 rmtp = restart->nanosleep.rmtp;
1516 if (rmtp) {
1517 ret = update_rmtp(&t.timer, rmtp);
1518 if (ret <= 0)
1519 goto out;
1520 }
1521
1522 /* The other values in restart are already filled in */
1523 ret = -ERESTART_RESTARTBLOCK;
1524 out:
1525 destroy_hrtimer_on_stack(&t.timer);
1526 return ret;
1527 }
1528
1529 long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp,
1530 const enum hrtimer_mode mode, const clockid_t clockid)
1531 {
1532 struct restart_block *restart;
1533 struct hrtimer_sleeper t;
1534 int ret = 0;
1535
1536 hrtimer_init_on_stack(&t.timer, clockid, mode);
1537 t.timer.expires = timespec_to_ktime(*rqtp);
1538 if (do_nanosleep(&t, mode))
1539 goto out;
1540
1541 /* Absolute timers do not update the rmtp value and restart: */
1542 if (mode == HRTIMER_MODE_ABS) {
1543 ret = -ERESTARTNOHAND;
1544 goto out;
1545 }
1546
1547 if (rmtp) {
1548 ret = update_rmtp(&t.timer, rmtp);
1549 if (ret <= 0)
1550 goto out;
1551 }
1552
1553 restart = &current_thread_info()->restart_block;
1554 restart->fn = hrtimer_nanosleep_restart;
1555 restart->nanosleep.index = t.timer.base->index;
1556 restart->nanosleep.rmtp = rmtp;
1557 restart->nanosleep.expires = t.timer.expires.tv64;
1558
1559 ret = -ERESTART_RESTARTBLOCK;
1560 out:
1561 destroy_hrtimer_on_stack(&t.timer);
1562 return ret;
1563 }
1564
1565 asmlinkage long
1566 sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
1567 {
1568 struct timespec tu;
1569
1570 if (copy_from_user(&tu, rqtp, sizeof(tu)))
1571 return -EFAULT;
1572
1573 if (!timespec_valid(&tu))
1574 return -EINVAL;
1575
1576 return hrtimer_nanosleep(&tu, rmtp, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1577 }
1578
1579 /*
1580 * Functions related to boot-time initialization:
1581 */
1582 static void __cpuinit init_hrtimers_cpu(int cpu)
1583 {
1584 struct hrtimer_cpu_base *cpu_base = &per_cpu(hrtimer_bases, cpu);
1585 int i;
1586
1587 spin_lock_init(&cpu_base->lock);
1588
1589 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++)
1590 cpu_base->clock_base[i].cpu_base = cpu_base;
1591
1592 INIT_LIST_HEAD(&cpu_base->cb_pending);
1593 hrtimer_init_hres(cpu_base);
1594 }
1595
1596 #ifdef CONFIG_HOTPLUG_CPU
1597
1598 static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base,
1599 struct hrtimer_clock_base *new_base)
1600 {
1601 struct hrtimer *timer;
1602 struct rb_node *node;
1603
1604 while ((node = rb_first(&old_base->active))) {
1605 timer = rb_entry(node, struct hrtimer, node);
1606 BUG_ON(hrtimer_callback_running(timer));
1607 debug_hrtimer_deactivate(timer);
1608 __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0);
1609 timer->base = new_base;
1610 /*
1611 * Enqueue the timer. Allow reprogramming of the event device
1612 */
1613 enqueue_hrtimer(timer, new_base, 1);
1614 }
1615 }
1616
1617 static void migrate_hrtimers(int cpu)
1618 {
1619 struct hrtimer_cpu_base *old_base, *new_base;
1620 int i;
1621
1622 BUG_ON(cpu_online(cpu));
1623 old_base = &per_cpu(hrtimer_bases, cpu);
1624 new_base = &get_cpu_var(hrtimer_bases);
1625
1626 tick_cancel_sched_timer(cpu);
1627
1628 local_irq_disable();
1629 spin_lock(&new_base->lock);
1630 spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING);
1631
1632 for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) {
1633 migrate_hrtimer_list(&old_base->clock_base[i],
1634 &new_base->clock_base[i]);
1635 }
1636
1637 spin_unlock(&old_base->lock);
1638 spin_unlock(&new_base->lock);
1639 local_irq_enable();
1640 put_cpu_var(hrtimer_bases);
1641 }
1642 #endif /* CONFIG_HOTPLUG_CPU */
1643
1644 static int __cpuinit hrtimer_cpu_notify(struct notifier_block *self,
1645 unsigned long action, void *hcpu)
1646 {
1647 unsigned int cpu = (long)hcpu;
1648
1649 switch (action) {
1650
1651 case CPU_UP_PREPARE:
1652 case CPU_UP_PREPARE_FROZEN:
1653 init_hrtimers_cpu(cpu);
1654 break;
1655
1656 #ifdef CONFIG_HOTPLUG_CPU
1657 case CPU_DEAD:
1658 case CPU_DEAD_FROZEN:
1659 clockevents_notify(CLOCK_EVT_NOTIFY_CPU_DEAD, &cpu);
1660 migrate_hrtimers(cpu);
1661 break;
1662 #endif
1663
1664 default:
1665 break;
1666 }
1667
1668 return NOTIFY_OK;
1669 }
1670
1671 static struct notifier_block __cpuinitdata hrtimers_nb = {
1672 .notifier_call = hrtimer_cpu_notify,
1673 };
1674
1675 void __init hrtimers_init(void)
1676 {
1677 hrtimer_cpu_notify(&hrtimers_nb, (unsigned long)CPU_UP_PREPARE,
1678 (void *)(long)smp_processor_id());
1679 register_cpu_notifier(&hrtimers_nb);
1680 #ifdef CONFIG_HIGH_RES_TIMERS
1681 open_softirq(HRTIMER_SOFTIRQ, run_hrtimer_softirq, NULL);
1682 #endif
1683 }
1684
This page took 0.090696 seconds and 6 git commands to generate.