Linux 3.8-rc7
[cris-mirror.git] / kernel / time / tick-sched.c
blobd58e552d9fd154b39fffaa012a6028838b9955fd
1 /*
2 * linux/kernel/time/tick-sched.c
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
8 * No idle tick implementation for low and high resolution timers
10 * Started by: Thomas Gleixner and Ingo Molnar
12 * Distribute under GPLv2.
14 #include <linux/cpu.h>
15 #include <linux/err.h>
16 #include <linux/hrtimer.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/percpu.h>
20 #include <linux/profile.h>
21 #include <linux/sched.h>
22 #include <linux/module.h>
24 #include <asm/irq_regs.h>
26 #include "tick-internal.h"
29 * Per cpu nohz control structure
31 static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
34 * The time, when the last jiffy update happened. Protected by jiffies_lock.
36 static ktime_t last_jiffies_update;
38 struct tick_sched *tick_get_tick_sched(int cpu)
40 return &per_cpu(tick_cpu_sched, cpu);
44 * Must be called with interrupts disabled !
46 static void tick_do_update_jiffies64(ktime_t now)
48 unsigned long ticks = 0;
49 ktime_t delta;
52 * Do a quick check without holding jiffies_lock:
54 delta = ktime_sub(now, last_jiffies_update);
55 if (delta.tv64 < tick_period.tv64)
56 return;
58 /* Reevalute with jiffies_lock held */
59 write_seqlock(&jiffies_lock);
61 delta = ktime_sub(now, last_jiffies_update);
62 if (delta.tv64 >= tick_period.tv64) {
64 delta = ktime_sub(delta, tick_period);
65 last_jiffies_update = ktime_add(last_jiffies_update,
66 tick_period);
68 /* Slow path for long timeouts */
69 if (unlikely(delta.tv64 >= tick_period.tv64)) {
70 s64 incr = ktime_to_ns(tick_period);
72 ticks = ktime_divns(delta, incr);
74 last_jiffies_update = ktime_add_ns(last_jiffies_update,
75 incr * ticks);
77 do_timer(++ticks);
79 /* Keep the tick_next_period variable up to date */
80 tick_next_period = ktime_add(last_jiffies_update, tick_period);
82 write_sequnlock(&jiffies_lock);
86 * Initialize and return retrieve the jiffies update.
88 static ktime_t tick_init_jiffy_update(void)
90 ktime_t period;
92 write_seqlock(&jiffies_lock);
93 /* Did we start the jiffies update yet ? */
94 if (last_jiffies_update.tv64 == 0)
95 last_jiffies_update = tick_next_period;
96 period = last_jiffies_update;
97 write_sequnlock(&jiffies_lock);
98 return period;
102 static void tick_sched_do_timer(ktime_t now)
104 int cpu = smp_processor_id();
106 #ifdef CONFIG_NO_HZ
108 * Check if the do_timer duty was dropped. We don't care about
109 * concurrency: This happens only when the cpu in charge went
110 * into a long sleep. If two cpus happen to assign themself to
111 * this duty, then the jiffies update is still serialized by
112 * jiffies_lock.
114 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
115 tick_do_timer_cpu = cpu;
116 #endif
118 /* Check, if the jiffies need an update */
119 if (tick_do_timer_cpu == cpu)
120 tick_do_update_jiffies64(now);
123 static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
125 #ifdef CONFIG_NO_HZ
127 * When we are idle and the tick is stopped, we have to touch
128 * the watchdog as we might not schedule for a really long
129 * time. This happens on complete idle SMP systems while
130 * waiting on the login prompt. We also increment the "start of
131 * idle" jiffy stamp so the idle accounting adjustment we do
132 * when we go busy again does not account too much ticks.
134 if (ts->tick_stopped) {
135 touch_softlockup_watchdog();
136 if (is_idle_task(current))
137 ts->idle_jiffies++;
139 #endif
140 update_process_times(user_mode(regs));
141 profile_tick(CPU_PROFILING);
145 * NOHZ - aka dynamic tick functionality
147 #ifdef CONFIG_NO_HZ
149 * NO HZ enabled ?
151 int tick_nohz_enabled __read_mostly = 1;
154 * Enable / Disable tickless mode
156 static int __init setup_tick_nohz(char *str)
158 if (!strcmp(str, "off"))
159 tick_nohz_enabled = 0;
160 else if (!strcmp(str, "on"))
161 tick_nohz_enabled = 1;
162 else
163 return 0;
164 return 1;
167 __setup("nohz=", setup_tick_nohz);
170 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
172 * Called from interrupt entry when the CPU was idle
174 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
175 * must be updated. Otherwise an interrupt handler could use a stale jiffy
176 * value. We do this unconditionally on any cpu, as we don't know whether the
177 * cpu, which has the update task assigned is in a long sleep.
179 static void tick_nohz_update_jiffies(ktime_t now)
181 int cpu = smp_processor_id();
182 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
183 unsigned long flags;
185 ts->idle_waketime = now;
187 local_irq_save(flags);
188 tick_do_update_jiffies64(now);
189 local_irq_restore(flags);
191 touch_softlockup_watchdog();
195 * Updates the per cpu time idle statistics counters
197 static void
198 update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
200 ktime_t delta;
202 if (ts->idle_active) {
203 delta = ktime_sub(now, ts->idle_entrytime);
204 if (nr_iowait_cpu(cpu) > 0)
205 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
206 else
207 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
208 ts->idle_entrytime = now;
211 if (last_update_time)
212 *last_update_time = ktime_to_us(now);
216 static void tick_nohz_stop_idle(int cpu, ktime_t now)
218 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
220 update_ts_time_stats(cpu, ts, now, NULL);
221 ts->idle_active = 0;
223 sched_clock_idle_wakeup_event(0);
226 static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
228 ktime_t now = ktime_get();
230 ts->idle_entrytime = now;
231 ts->idle_active = 1;
232 sched_clock_idle_sleep_event();
233 return now;
237 * get_cpu_idle_time_us - get the total idle time of a cpu
238 * @cpu: CPU number to query
239 * @last_update_time: variable to store update time in. Do not update
240 * counters if NULL.
242 * Return the cummulative idle time (since boot) for a given
243 * CPU, in microseconds.
245 * This time is measured via accounting rather than sampling,
246 * and is as accurate as ktime_get() is.
248 * This function returns -1 if NOHZ is not enabled.
250 u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
252 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
253 ktime_t now, idle;
255 if (!tick_nohz_enabled)
256 return -1;
258 now = ktime_get();
259 if (last_update_time) {
260 update_ts_time_stats(cpu, ts, now, last_update_time);
261 idle = ts->idle_sleeptime;
262 } else {
263 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
264 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
266 idle = ktime_add(ts->idle_sleeptime, delta);
267 } else {
268 idle = ts->idle_sleeptime;
272 return ktime_to_us(idle);
275 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
278 * get_cpu_iowait_time_us - get the total iowait time of a cpu
279 * @cpu: CPU number to query
280 * @last_update_time: variable to store update time in. Do not update
281 * counters if NULL.
283 * Return the cummulative iowait time (since boot) for a given
284 * CPU, in microseconds.
286 * This time is measured via accounting rather than sampling,
287 * and is as accurate as ktime_get() is.
289 * This function returns -1 if NOHZ is not enabled.
291 u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
293 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
294 ktime_t now, iowait;
296 if (!tick_nohz_enabled)
297 return -1;
299 now = ktime_get();
300 if (last_update_time) {
301 update_ts_time_stats(cpu, ts, now, last_update_time);
302 iowait = ts->iowait_sleeptime;
303 } else {
304 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
305 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
307 iowait = ktime_add(ts->iowait_sleeptime, delta);
308 } else {
309 iowait = ts->iowait_sleeptime;
313 return ktime_to_us(iowait);
315 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
317 static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
318 ktime_t now, int cpu)
320 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
321 ktime_t last_update, expires, ret = { .tv64 = 0 };
322 unsigned long rcu_delta_jiffies;
323 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
324 u64 time_delta;
326 /* Read jiffies and the time when jiffies were updated last */
327 do {
328 seq = read_seqbegin(&jiffies_lock);
329 last_update = last_jiffies_update;
330 last_jiffies = jiffies;
331 time_delta = timekeeping_max_deferment();
332 } while (read_seqretry(&jiffies_lock, seq));
334 if (rcu_needs_cpu(cpu, &rcu_delta_jiffies) || printk_needs_cpu(cpu) ||
335 arch_needs_cpu(cpu)) {
336 next_jiffies = last_jiffies + 1;
337 delta_jiffies = 1;
338 } else {
339 /* Get the next timer wheel timer */
340 next_jiffies = get_next_timer_interrupt(last_jiffies);
341 delta_jiffies = next_jiffies - last_jiffies;
342 if (rcu_delta_jiffies < delta_jiffies) {
343 next_jiffies = last_jiffies + rcu_delta_jiffies;
344 delta_jiffies = rcu_delta_jiffies;
348 * Do not stop the tick, if we are only one off
349 * or if the cpu is required for rcu
351 if (!ts->tick_stopped && delta_jiffies == 1)
352 goto out;
354 /* Schedule the tick, if we are at least one jiffie off */
355 if ((long)delta_jiffies >= 1) {
358 * If this cpu is the one which updates jiffies, then
359 * give up the assignment and let it be taken by the
360 * cpu which runs the tick timer next, which might be
361 * this cpu as well. If we don't drop this here the
362 * jiffies might be stale and do_timer() never
363 * invoked. Keep track of the fact that it was the one
364 * which had the do_timer() duty last. If this cpu is
365 * the one which had the do_timer() duty last, we
366 * limit the sleep time to the timekeeping
367 * max_deferement value which we retrieved
368 * above. Otherwise we can sleep as long as we want.
370 if (cpu == tick_do_timer_cpu) {
371 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
372 ts->do_timer_last = 1;
373 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
374 time_delta = KTIME_MAX;
375 ts->do_timer_last = 0;
376 } else if (!ts->do_timer_last) {
377 time_delta = KTIME_MAX;
381 * calculate the expiry time for the next timer wheel
382 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
383 * that there is no timer pending or at least extremely
384 * far into the future (12 days for HZ=1000). In this
385 * case we set the expiry to the end of time.
387 if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
389 * Calculate the time delta for the next timer event.
390 * If the time delta exceeds the maximum time delta
391 * permitted by the current clocksource then adjust
392 * the time delta accordingly to ensure the
393 * clocksource does not wrap.
395 time_delta = min_t(u64, time_delta,
396 tick_period.tv64 * delta_jiffies);
399 if (time_delta < KTIME_MAX)
400 expires = ktime_add_ns(last_update, time_delta);
401 else
402 expires.tv64 = KTIME_MAX;
404 /* Skip reprogram of event if its not changed */
405 if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
406 goto out;
408 ret = expires;
411 * nohz_stop_sched_tick can be called several times before
412 * the nohz_restart_sched_tick is called. This happens when
413 * interrupts arrive which do not cause a reschedule. In the
414 * first call we save the current tick time, so we can restart
415 * the scheduler tick in nohz_restart_sched_tick.
417 if (!ts->tick_stopped) {
418 nohz_balance_enter_idle(cpu);
419 calc_load_enter_idle();
421 ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
422 ts->tick_stopped = 1;
426 * If the expiration time == KTIME_MAX, then
427 * in this case we simply stop the tick timer.
429 if (unlikely(expires.tv64 == KTIME_MAX)) {
430 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
431 hrtimer_cancel(&ts->sched_timer);
432 goto out;
435 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
436 hrtimer_start(&ts->sched_timer, expires,
437 HRTIMER_MODE_ABS_PINNED);
438 /* Check, if the timer was already in the past */
439 if (hrtimer_active(&ts->sched_timer))
440 goto out;
441 } else if (!tick_program_event(expires, 0))
442 goto out;
444 * We are past the event already. So we crossed a
445 * jiffie boundary. Update jiffies and raise the
446 * softirq.
448 tick_do_update_jiffies64(ktime_get());
450 raise_softirq_irqoff(TIMER_SOFTIRQ);
451 out:
452 ts->next_jiffies = next_jiffies;
453 ts->last_jiffies = last_jiffies;
454 ts->sleep_length = ktime_sub(dev->next_event, now);
456 return ret;
459 static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
462 * If this cpu is offline and it is the one which updates
463 * jiffies, then give up the assignment and let it be taken by
464 * the cpu which runs the tick timer next. If we don't drop
465 * this here the jiffies might be stale and do_timer() never
466 * invoked.
468 if (unlikely(!cpu_online(cpu))) {
469 if (cpu == tick_do_timer_cpu)
470 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
473 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
474 return false;
476 if (need_resched())
477 return false;
479 if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
480 static int ratelimit;
482 if (ratelimit < 10 &&
483 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
484 printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
485 (unsigned int) local_softirq_pending());
486 ratelimit++;
488 return false;
491 return true;
494 static void __tick_nohz_idle_enter(struct tick_sched *ts)
496 ktime_t now, expires;
497 int cpu = smp_processor_id();
499 now = tick_nohz_start_idle(cpu, ts);
501 if (can_stop_idle_tick(cpu, ts)) {
502 int was_stopped = ts->tick_stopped;
504 ts->idle_calls++;
506 expires = tick_nohz_stop_sched_tick(ts, now, cpu);
507 if (expires.tv64 > 0LL) {
508 ts->idle_sleeps++;
509 ts->idle_expires = expires;
512 if (!was_stopped && ts->tick_stopped)
513 ts->idle_jiffies = ts->last_jiffies;
518 * tick_nohz_idle_enter - stop the idle tick from the idle task
520 * When the next event is more than a tick into the future, stop the idle tick
521 * Called when we start the idle loop.
523 * The arch is responsible of calling:
525 * - rcu_idle_enter() after its last use of RCU before the CPU is put
526 * to sleep.
527 * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
529 void tick_nohz_idle_enter(void)
531 struct tick_sched *ts;
533 WARN_ON_ONCE(irqs_disabled());
536 * Update the idle state in the scheduler domain hierarchy
537 * when tick_nohz_stop_sched_tick() is called from the idle loop.
538 * State will be updated to busy during the first busy tick after
539 * exiting idle.
541 set_cpu_sd_state_idle();
543 local_irq_disable();
545 ts = &__get_cpu_var(tick_cpu_sched);
547 * set ts->inidle unconditionally. even if the system did not
548 * switch to nohz mode the cpu frequency governers rely on the
549 * update of the idle time accounting in tick_nohz_start_idle().
551 ts->inidle = 1;
552 __tick_nohz_idle_enter(ts);
554 local_irq_enable();
558 * tick_nohz_irq_exit - update next tick event from interrupt exit
560 * When an interrupt fires while we are idle and it doesn't cause
561 * a reschedule, it may still add, modify or delete a timer, enqueue
562 * an RCU callback, etc...
563 * So we need to re-calculate and reprogram the next tick event.
565 void tick_nohz_irq_exit(void)
567 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
569 if (!ts->inidle)
570 return;
572 /* Cancel the timer because CPU already waken up from the C-states*/
573 menu_hrtimer_cancel();
574 __tick_nohz_idle_enter(ts);
578 * tick_nohz_get_sleep_length - return the length of the current sleep
580 * Called from power state control code with interrupts disabled
582 ktime_t tick_nohz_get_sleep_length(void)
584 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
586 return ts->sleep_length;
589 static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
591 hrtimer_cancel(&ts->sched_timer);
592 hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
594 while (1) {
595 /* Forward the time to expire in the future */
596 hrtimer_forward(&ts->sched_timer, now, tick_period);
598 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
599 hrtimer_start_expires(&ts->sched_timer,
600 HRTIMER_MODE_ABS_PINNED);
601 /* Check, if the timer was already in the past */
602 if (hrtimer_active(&ts->sched_timer))
603 break;
604 } else {
605 if (!tick_program_event(
606 hrtimer_get_expires(&ts->sched_timer), 0))
607 break;
609 /* Reread time and update jiffies */
610 now = ktime_get();
611 tick_do_update_jiffies64(now);
615 static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
617 /* Update jiffies first */
618 tick_do_update_jiffies64(now);
619 update_cpu_load_nohz();
621 calc_load_exit_idle();
622 touch_softlockup_watchdog();
624 * Cancel the scheduled timer and restore the tick
626 ts->tick_stopped = 0;
627 ts->idle_exittime = now;
629 tick_nohz_restart(ts, now);
632 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
634 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
635 unsigned long ticks;
637 * We stopped the tick in idle. Update process times would miss the
638 * time we slept as update_process_times does only a 1 tick
639 * accounting. Enforce that this is accounted to idle !
641 ticks = jiffies - ts->idle_jiffies;
643 * We might be one off. Do not randomly account a huge number of ticks!
645 if (ticks && ticks < LONG_MAX)
646 account_idle_ticks(ticks);
647 #endif
651 * tick_nohz_idle_exit - restart the idle tick from the idle task
653 * Restart the idle tick when the CPU is woken up from idle
654 * This also exit the RCU extended quiescent state. The CPU
655 * can use RCU again after this function is called.
657 void tick_nohz_idle_exit(void)
659 int cpu = smp_processor_id();
660 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
661 ktime_t now;
663 local_irq_disable();
665 WARN_ON_ONCE(!ts->inidle);
667 ts->inidle = 0;
669 /* Cancel the timer because CPU already waken up from the C-states*/
670 menu_hrtimer_cancel();
671 if (ts->idle_active || ts->tick_stopped)
672 now = ktime_get();
674 if (ts->idle_active)
675 tick_nohz_stop_idle(cpu, now);
677 if (ts->tick_stopped) {
678 tick_nohz_restart_sched_tick(ts, now);
679 tick_nohz_account_idle_ticks(ts);
682 local_irq_enable();
685 static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
687 hrtimer_forward(&ts->sched_timer, now, tick_period);
688 return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
692 * The nohz low res interrupt handler
694 static void tick_nohz_handler(struct clock_event_device *dev)
696 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
697 struct pt_regs *regs = get_irq_regs();
698 ktime_t now = ktime_get();
700 dev->next_event.tv64 = KTIME_MAX;
702 tick_sched_do_timer(now);
703 tick_sched_handle(ts, regs);
705 while (tick_nohz_reprogram(ts, now)) {
706 now = ktime_get();
707 tick_do_update_jiffies64(now);
712 * tick_nohz_switch_to_nohz - switch to nohz mode
714 static void tick_nohz_switch_to_nohz(void)
716 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
717 ktime_t next;
719 if (!tick_nohz_enabled)
720 return;
722 local_irq_disable();
723 if (tick_switch_to_oneshot(tick_nohz_handler)) {
724 local_irq_enable();
725 return;
728 ts->nohz_mode = NOHZ_MODE_LOWRES;
731 * Recycle the hrtimer in ts, so we can share the
732 * hrtimer_forward with the highres code.
734 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
735 /* Get the next period */
736 next = tick_init_jiffy_update();
738 for (;;) {
739 hrtimer_set_expires(&ts->sched_timer, next);
740 if (!tick_program_event(next, 0))
741 break;
742 next = ktime_add(next, tick_period);
744 local_irq_enable();
748 * When NOHZ is enabled and the tick is stopped, we need to kick the
749 * tick timer from irq_enter() so that the jiffies update is kept
750 * alive during long running softirqs. That's ugly as hell, but
751 * correctness is key even if we need to fix the offending softirq in
752 * the first place.
754 * Note, this is different to tick_nohz_restart. We just kick the
755 * timer and do not touch the other magic bits which need to be done
756 * when idle is left.
758 static void tick_nohz_kick_tick(int cpu, ktime_t now)
760 #if 0
761 /* Switch back to 2.6.27 behaviour */
763 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
764 ktime_t delta;
767 * Do not touch the tick device, when the next expiry is either
768 * already reached or less/equal than the tick period.
770 delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
771 if (delta.tv64 <= tick_period.tv64)
772 return;
774 tick_nohz_restart(ts, now);
775 #endif
778 static inline void tick_check_nohz(int cpu)
780 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
781 ktime_t now;
783 if (!ts->idle_active && !ts->tick_stopped)
784 return;
785 now = ktime_get();
786 if (ts->idle_active)
787 tick_nohz_stop_idle(cpu, now);
788 if (ts->tick_stopped) {
789 tick_nohz_update_jiffies(now);
790 tick_nohz_kick_tick(cpu, now);
794 #else
796 static inline void tick_nohz_switch_to_nohz(void) { }
797 static inline void tick_check_nohz(int cpu) { }
799 #endif /* NO_HZ */
802 * Called from irq_enter to notify about the possible interruption of idle()
804 void tick_check_idle(int cpu)
806 tick_check_oneshot_broadcast(cpu);
807 tick_check_nohz(cpu);
811 * High resolution timer specific code
813 #ifdef CONFIG_HIGH_RES_TIMERS
815 * We rearm the timer until we get disabled by the idle code.
816 * Called with interrupts disabled.
818 static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
820 struct tick_sched *ts =
821 container_of(timer, struct tick_sched, sched_timer);
822 struct pt_regs *regs = get_irq_regs();
823 ktime_t now = ktime_get();
825 tick_sched_do_timer(now);
828 * Do not call, when we are not in irq context and have
829 * no valid regs pointer
831 if (regs)
832 tick_sched_handle(ts, regs);
834 hrtimer_forward(timer, now, tick_period);
836 return HRTIMER_RESTART;
839 static int sched_skew_tick;
841 static int __init skew_tick(char *str)
843 get_option(&str, &sched_skew_tick);
845 return 0;
847 early_param("skew_tick", skew_tick);
850 * tick_setup_sched_timer - setup the tick emulation timer
852 void tick_setup_sched_timer(void)
854 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
855 ktime_t now = ktime_get();
858 * Emulate tick processing via per-CPU hrtimers:
860 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
861 ts->sched_timer.function = tick_sched_timer;
863 /* Get the next period (per cpu) */
864 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
866 /* Offset the tick to avert jiffies_lock contention. */
867 if (sched_skew_tick) {
868 u64 offset = ktime_to_ns(tick_period) >> 1;
869 do_div(offset, num_possible_cpus());
870 offset *= smp_processor_id();
871 hrtimer_add_expires_ns(&ts->sched_timer, offset);
874 for (;;) {
875 hrtimer_forward(&ts->sched_timer, now, tick_period);
876 hrtimer_start_expires(&ts->sched_timer,
877 HRTIMER_MODE_ABS_PINNED);
878 /* Check, if the timer was already in the past */
879 if (hrtimer_active(&ts->sched_timer))
880 break;
881 now = ktime_get();
884 #ifdef CONFIG_NO_HZ
885 if (tick_nohz_enabled)
886 ts->nohz_mode = NOHZ_MODE_HIGHRES;
887 #endif
889 #endif /* HIGH_RES_TIMERS */
891 #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
892 void tick_cancel_sched_timer(int cpu)
894 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
896 # ifdef CONFIG_HIGH_RES_TIMERS
897 if (ts->sched_timer.base)
898 hrtimer_cancel(&ts->sched_timer);
899 # endif
901 ts->nohz_mode = NOHZ_MODE_INACTIVE;
903 #endif
906 * Async notification about clocksource changes
908 void tick_clock_notify(void)
910 int cpu;
912 for_each_possible_cpu(cpu)
913 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
917 * Async notification about clock event changes
919 void tick_oneshot_notify(void)
921 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
923 set_bit(0, &ts->check_clocks);
927 * Check, if a change happened, which makes oneshot possible.
929 * Called cyclic from the hrtimer softirq (driven by the timer
930 * softirq) allow_nohz signals, that we can switch into low-res nohz
931 * mode, because high resolution timers are disabled (either compile
932 * or runtime).
934 int tick_check_oneshot_change(int allow_nohz)
936 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
938 if (!test_and_clear_bit(0, &ts->check_clocks))
939 return 0;
941 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
942 return 0;
944 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
945 return 0;
947 if (!allow_nohz)
948 return 1;
950 tick_nohz_switch_to_nohz();
951 return 0;