mm/hmm.c: remove superfluous RCU protection around radix tree lookup
[linux/fpc-iii.git] / kernel / time / tick-sched.c
blobf3ab08caa2c3aa6bc61dbe1415b16f783b6ae18f
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/nmi.h>
21 #include <linux/profile.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/clock.h>
24 #include <linux/sched/stat.h>
25 #include <linux/sched/nohz.h>
26 #include <linux/module.h>
27 #include <linux/irq_work.h>
28 #include <linux/posix-timers.h>
29 #include <linux/context_tracking.h>
30 #include <linux/mm.h>
32 #include <asm/irq_regs.h>
34 #include "tick-internal.h"
36 #include <trace/events/timer.h>
39 * Per-CPU nohz control structure
41 static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
43 struct tick_sched *tick_get_tick_sched(int cpu)
45 return &per_cpu(tick_cpu_sched, cpu);
48 #if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
50 * The time, when the last jiffy update happened. Protected by jiffies_lock.
52 static ktime_t last_jiffies_update;
55 * Called after resume. Make sure that jiffies are not fast forwarded due to
56 * clock monotonic being forwarded by the suspended time.
58 void tick_sched_forward_next_period(void)
60 last_jiffies_update = tick_next_period;
64 * Must be called with interrupts disabled !
66 static void tick_do_update_jiffies64(ktime_t now)
68 unsigned long ticks = 0;
69 ktime_t delta;
72 * Do a quick check without holding jiffies_lock:
74 delta = ktime_sub(now, last_jiffies_update);
75 if (delta < tick_period)
76 return;
78 /* Reevaluate with jiffies_lock held */
79 write_seqlock(&jiffies_lock);
81 delta = ktime_sub(now, last_jiffies_update);
82 if (delta >= tick_period) {
84 delta = ktime_sub(delta, tick_period);
85 last_jiffies_update = ktime_add(last_jiffies_update,
86 tick_period);
88 /* Slow path for long timeouts */
89 if (unlikely(delta >= tick_period)) {
90 s64 incr = ktime_to_ns(tick_period);
92 ticks = ktime_divns(delta, incr);
94 last_jiffies_update = ktime_add_ns(last_jiffies_update,
95 incr * ticks);
97 do_timer(++ticks);
99 /* Keep the tick_next_period variable up to date */
100 tick_next_period = ktime_add(last_jiffies_update, tick_period);
101 } else {
102 write_sequnlock(&jiffies_lock);
103 return;
105 write_sequnlock(&jiffies_lock);
106 update_wall_time();
110 * Initialize and return retrieve the jiffies update.
112 static ktime_t tick_init_jiffy_update(void)
114 ktime_t period;
116 write_seqlock(&jiffies_lock);
117 /* Did we start the jiffies update yet ? */
118 if (last_jiffies_update == 0)
119 last_jiffies_update = tick_next_period;
120 period = last_jiffies_update;
121 write_sequnlock(&jiffies_lock);
122 return period;
126 static void tick_sched_do_timer(ktime_t now)
128 int cpu = smp_processor_id();
130 #ifdef CONFIG_NO_HZ_COMMON
132 * Check if the do_timer duty was dropped. We don't care about
133 * concurrency: This happens only when the CPU in charge went
134 * into a long sleep. If two CPUs happen to assign themselves to
135 * this duty, then the jiffies update is still serialized by
136 * jiffies_lock.
138 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
139 && !tick_nohz_full_cpu(cpu))
140 tick_do_timer_cpu = cpu;
141 #endif
143 /* Check, if the jiffies need an update */
144 if (tick_do_timer_cpu == cpu)
145 tick_do_update_jiffies64(now);
148 static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
150 #ifdef CONFIG_NO_HZ_COMMON
152 * When we are idle and the tick is stopped, we have to touch
153 * the watchdog as we might not schedule for a really long
154 * time. This happens on complete idle SMP systems while
155 * waiting on the login prompt. We also increment the "start of
156 * idle" jiffy stamp so the idle accounting adjustment we do
157 * when we go busy again does not account too much ticks.
159 if (ts->tick_stopped) {
160 touch_softlockup_watchdog_sched();
161 if (is_idle_task(current))
162 ts->idle_jiffies++;
164 * In case the current tick fired too early past its expected
165 * expiration, make sure we don't bypass the next clock reprogramming
166 * to the same deadline.
168 ts->next_tick = 0;
170 #endif
171 update_process_times(user_mode(regs));
172 profile_tick(CPU_PROFILING);
174 #endif
176 #ifdef CONFIG_NO_HZ_FULL
177 cpumask_var_t tick_nohz_full_mask;
178 bool tick_nohz_full_running;
179 static atomic_t tick_dep_mask;
181 static bool check_tick_dependency(atomic_t *dep)
183 int val = atomic_read(dep);
185 if (val & TICK_DEP_MASK_POSIX_TIMER) {
186 trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
187 return true;
190 if (val & TICK_DEP_MASK_PERF_EVENTS) {
191 trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
192 return true;
195 if (val & TICK_DEP_MASK_SCHED) {
196 trace_tick_stop(0, TICK_DEP_MASK_SCHED);
197 return true;
200 if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
201 trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
202 return true;
205 return false;
208 static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
210 lockdep_assert_irqs_disabled();
212 if (unlikely(!cpu_online(cpu)))
213 return false;
215 if (check_tick_dependency(&tick_dep_mask))
216 return false;
218 if (check_tick_dependency(&ts->tick_dep_mask))
219 return false;
221 if (check_tick_dependency(&current->tick_dep_mask))
222 return false;
224 if (check_tick_dependency(&current->signal->tick_dep_mask))
225 return false;
227 return true;
230 static void nohz_full_kick_func(struct irq_work *work)
232 /* Empty, the tick restart happens on tick_nohz_irq_exit() */
235 static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
236 .func = nohz_full_kick_func,
240 * Kick this CPU if it's full dynticks in order to force it to
241 * re-evaluate its dependency on the tick and restart it if necessary.
242 * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
243 * is NMI safe.
245 static void tick_nohz_full_kick(void)
247 if (!tick_nohz_full_cpu(smp_processor_id()))
248 return;
250 irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
254 * Kick the CPU if it's full dynticks in order to force it to
255 * re-evaluate its dependency on the tick and restart it if necessary.
257 void tick_nohz_full_kick_cpu(int cpu)
259 if (!tick_nohz_full_cpu(cpu))
260 return;
262 irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
266 * Kick all full dynticks CPUs in order to force these to re-evaluate
267 * their dependency on the tick and restart it if necessary.
269 static void tick_nohz_full_kick_all(void)
271 int cpu;
273 if (!tick_nohz_full_running)
274 return;
276 preempt_disable();
277 for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
278 tick_nohz_full_kick_cpu(cpu);
279 preempt_enable();
282 static void tick_nohz_dep_set_all(atomic_t *dep,
283 enum tick_dep_bits bit)
285 int prev;
287 prev = atomic_fetch_or(BIT(bit), dep);
288 if (!prev)
289 tick_nohz_full_kick_all();
293 * Set a global tick dependency. Used by perf events that rely on freq and
294 * by unstable clock.
296 void tick_nohz_dep_set(enum tick_dep_bits bit)
298 tick_nohz_dep_set_all(&tick_dep_mask, bit);
301 void tick_nohz_dep_clear(enum tick_dep_bits bit)
303 atomic_andnot(BIT(bit), &tick_dep_mask);
307 * Set per-CPU tick dependency. Used by scheduler and perf events in order to
308 * manage events throttling.
310 void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
312 int prev;
313 struct tick_sched *ts;
315 ts = per_cpu_ptr(&tick_cpu_sched, cpu);
317 prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
318 if (!prev) {
319 preempt_disable();
320 /* Perf needs local kick that is NMI safe */
321 if (cpu == smp_processor_id()) {
322 tick_nohz_full_kick();
323 } else {
324 /* Remote irq work not NMI-safe */
325 if (!WARN_ON_ONCE(in_nmi()))
326 tick_nohz_full_kick_cpu(cpu);
328 preempt_enable();
332 void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
334 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
336 atomic_andnot(BIT(bit), &ts->tick_dep_mask);
340 * Set a per-task tick dependency. Posix CPU timers need this in order to elapse
341 * per task timers.
343 void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
346 * We could optimize this with just kicking the target running the task
347 * if that noise matters for nohz full users.
349 tick_nohz_dep_set_all(&tsk->tick_dep_mask, bit);
352 void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
354 atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
358 * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
359 * per process timers.
361 void tick_nohz_dep_set_signal(struct signal_struct *sig, enum tick_dep_bits bit)
363 tick_nohz_dep_set_all(&sig->tick_dep_mask, bit);
366 void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
368 atomic_andnot(BIT(bit), &sig->tick_dep_mask);
372 * Re-evaluate the need for the tick as we switch the current task.
373 * It might need the tick due to per task/process properties:
374 * perf events, posix CPU timers, ...
376 void __tick_nohz_task_switch(void)
378 unsigned long flags;
379 struct tick_sched *ts;
381 local_irq_save(flags);
383 if (!tick_nohz_full_cpu(smp_processor_id()))
384 goto out;
386 ts = this_cpu_ptr(&tick_cpu_sched);
388 if (ts->tick_stopped) {
389 if (atomic_read(&current->tick_dep_mask) ||
390 atomic_read(&current->signal->tick_dep_mask))
391 tick_nohz_full_kick();
393 out:
394 local_irq_restore(flags);
397 /* Get the boot-time nohz CPU list from the kernel parameters. */
398 void __init tick_nohz_full_setup(cpumask_var_t cpumask)
400 alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
401 cpumask_copy(tick_nohz_full_mask, cpumask);
402 tick_nohz_full_running = true;
405 static int tick_nohz_cpu_down(unsigned int cpu)
408 * The boot CPU handles housekeeping duty (unbound timers,
409 * workqueues, timekeeping, ...) on behalf of full dynticks
410 * CPUs. It must remain online when nohz full is enabled.
412 if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
413 return -EBUSY;
414 return 0;
417 void __init tick_nohz_init(void)
419 int cpu, ret;
421 if (!tick_nohz_full_running)
422 return;
425 * Full dynticks uses irq work to drive the tick rescheduling on safe
426 * locking contexts. But then we need irq work to raise its own
427 * interrupts to avoid circular dependency on the tick
429 if (!arch_irq_work_has_interrupt()) {
430 pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
431 cpumask_clear(tick_nohz_full_mask);
432 tick_nohz_full_running = false;
433 return;
436 cpu = smp_processor_id();
438 if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
439 pr_warn("NO_HZ: Clearing %d from nohz_full range for timekeeping\n",
440 cpu);
441 cpumask_clear_cpu(cpu, tick_nohz_full_mask);
444 for_each_cpu(cpu, tick_nohz_full_mask)
445 context_tracking_cpu_set(cpu);
447 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
448 "kernel/nohz:predown", NULL,
449 tick_nohz_cpu_down);
450 WARN_ON(ret < 0);
451 pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
452 cpumask_pr_args(tick_nohz_full_mask));
454 #endif
457 * NOHZ - aka dynamic tick functionality
459 #ifdef CONFIG_NO_HZ_COMMON
461 * NO HZ enabled ?
463 bool tick_nohz_enabled __read_mostly = true;
464 unsigned long tick_nohz_active __read_mostly;
466 * Enable / Disable tickless mode
468 static int __init setup_tick_nohz(char *str)
470 return (kstrtobool(str, &tick_nohz_enabled) == 0);
473 __setup("nohz=", setup_tick_nohz);
475 bool tick_nohz_tick_stopped(void)
477 return __this_cpu_read(tick_cpu_sched.tick_stopped);
480 bool tick_nohz_tick_stopped_cpu(int cpu)
482 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
484 return ts->tick_stopped;
488 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
490 * Called from interrupt entry when the CPU was idle
492 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
493 * must be updated. Otherwise an interrupt handler could use a stale jiffy
494 * value. We do this unconditionally on any CPU, as we don't know whether the
495 * CPU, which has the update task assigned is in a long sleep.
497 static void tick_nohz_update_jiffies(ktime_t now)
499 unsigned long flags;
501 __this_cpu_write(tick_cpu_sched.idle_waketime, now);
503 local_irq_save(flags);
504 tick_do_update_jiffies64(now);
505 local_irq_restore(flags);
507 touch_softlockup_watchdog_sched();
511 * Updates the per-CPU time idle statistics counters
513 static void
514 update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
516 ktime_t delta;
518 if (ts->idle_active) {
519 delta = ktime_sub(now, ts->idle_entrytime);
520 if (nr_iowait_cpu(cpu) > 0)
521 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
522 else
523 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
524 ts->idle_entrytime = now;
527 if (last_update_time)
528 *last_update_time = ktime_to_us(now);
532 static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
534 update_ts_time_stats(smp_processor_id(), ts, now, NULL);
535 ts->idle_active = 0;
537 sched_clock_idle_wakeup_event();
540 static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
542 ktime_t now = ktime_get();
544 ts->idle_entrytime = now;
545 ts->idle_active = 1;
546 sched_clock_idle_sleep_event();
547 return now;
551 * get_cpu_idle_time_us - get the total idle time of a CPU
552 * @cpu: CPU number to query
553 * @last_update_time: variable to store update time in. Do not update
554 * counters if NULL.
556 * Return the cumulative idle time (since boot) for a given
557 * CPU, in microseconds.
559 * This time is measured via accounting rather than sampling,
560 * and is as accurate as ktime_get() is.
562 * This function returns -1 if NOHZ is not enabled.
564 u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
566 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
567 ktime_t now, idle;
569 if (!tick_nohz_active)
570 return -1;
572 now = ktime_get();
573 if (last_update_time) {
574 update_ts_time_stats(cpu, ts, now, last_update_time);
575 idle = ts->idle_sleeptime;
576 } else {
577 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
578 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
580 idle = ktime_add(ts->idle_sleeptime, delta);
581 } else {
582 idle = ts->idle_sleeptime;
586 return ktime_to_us(idle);
589 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
592 * get_cpu_iowait_time_us - get the total iowait time of a CPU
593 * @cpu: CPU number to query
594 * @last_update_time: variable to store update time in. Do not update
595 * counters if NULL.
597 * Return the cumulative iowait time (since boot) for a given
598 * CPU, in microseconds.
600 * This time is measured via accounting rather than sampling,
601 * and is as accurate as ktime_get() is.
603 * This function returns -1 if NOHZ is not enabled.
605 u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
607 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
608 ktime_t now, iowait;
610 if (!tick_nohz_active)
611 return -1;
613 now = ktime_get();
614 if (last_update_time) {
615 update_ts_time_stats(cpu, ts, now, last_update_time);
616 iowait = ts->iowait_sleeptime;
617 } else {
618 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
619 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
621 iowait = ktime_add(ts->iowait_sleeptime, delta);
622 } else {
623 iowait = ts->iowait_sleeptime;
627 return ktime_to_us(iowait);
629 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
631 static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
633 hrtimer_cancel(&ts->sched_timer);
634 hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
636 /* Forward the time to expire in the future */
637 hrtimer_forward(&ts->sched_timer, now, tick_period);
639 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
640 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
641 else
642 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
645 * Reset to make sure next tick stop doesn't get fooled by past
646 * cached clock deadline.
648 ts->next_tick = 0;
651 static inline bool local_timer_softirq_pending(void)
653 return local_softirq_pending() & TIMER_SOFTIRQ;
656 static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
657 ktime_t now, int cpu)
659 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
660 u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
661 unsigned long seq, basejiff;
662 ktime_t tick;
664 /* Read jiffies and the time when jiffies were updated last */
665 do {
666 seq = read_seqbegin(&jiffies_lock);
667 basemono = last_jiffies_update;
668 basejiff = jiffies;
669 } while (read_seqretry(&jiffies_lock, seq));
670 ts->last_jiffies = basejiff;
673 * Keep the periodic tick, when RCU, architecture or irq_work
674 * requests it.
675 * Aside of that check whether the local timer softirq is
676 * pending. If so its a bad idea to call get_next_timer_interrupt()
677 * because there is an already expired timer, so it will request
678 * immeditate expiry, which rearms the hardware timer with a
679 * minimal delta which brings us back to this place
680 * immediately. Lather, rinse and repeat...
682 if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
683 irq_work_needs_cpu() || local_timer_softirq_pending()) {
684 next_tick = basemono + TICK_NSEC;
685 } else {
687 * Get the next pending timer. If high resolution
688 * timers are enabled this only takes the timer wheel
689 * timers into account. If high resolution timers are
690 * disabled this also looks at the next expiring
691 * hrtimer.
693 next_tmr = get_next_timer_interrupt(basejiff, basemono);
694 ts->next_timer = next_tmr;
695 /* Take the next rcu event into account */
696 next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
700 * If the tick is due in the next period, keep it ticking or
701 * force prod the timer.
703 delta = next_tick - basemono;
704 if (delta <= (u64)TICK_NSEC) {
706 * Tell the timer code that the base is not idle, i.e. undo
707 * the effect of get_next_timer_interrupt():
709 timer_clear_idle();
711 * We've not stopped the tick yet, and there's a timer in the
712 * next period, so no point in stopping it either, bail.
714 if (!ts->tick_stopped) {
715 tick = 0;
716 goto out;
721 * If this CPU is the one which updates jiffies, then give up
722 * the assignment and let it be taken by the CPU which runs
723 * the tick timer next, which might be this CPU as well. If we
724 * don't drop this here the jiffies might be stale and
725 * do_timer() never invoked. Keep track of the fact that it
726 * was the one which had the do_timer() duty last. If this CPU
727 * is the one which had the do_timer() duty last, we limit the
728 * sleep time to the timekeeping max_deferment value.
729 * Otherwise we can sleep as long as we want.
731 delta = timekeeping_max_deferment();
732 if (cpu == tick_do_timer_cpu) {
733 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
734 ts->do_timer_last = 1;
735 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
736 delta = KTIME_MAX;
737 ts->do_timer_last = 0;
738 } else if (!ts->do_timer_last) {
739 delta = KTIME_MAX;
742 /* Calculate the next expiry time */
743 if (delta < (KTIME_MAX - basemono))
744 expires = basemono + delta;
745 else
746 expires = KTIME_MAX;
748 expires = min_t(u64, expires, next_tick);
749 tick = expires;
751 /* Skip reprogram of event if its not changed */
752 if (ts->tick_stopped && (expires == ts->next_tick)) {
753 /* Sanity check: make sure clockevent is actually programmed */
754 if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
755 goto out;
757 WARN_ON_ONCE(1);
758 printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
759 basemono, ts->next_tick, dev->next_event,
760 hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
764 * nohz_stop_sched_tick can be called several times before
765 * the nohz_restart_sched_tick is called. This happens when
766 * interrupts arrive which do not cause a reschedule. In the
767 * first call we save the current tick time, so we can restart
768 * the scheduler tick in nohz_restart_sched_tick.
770 if (!ts->tick_stopped) {
771 calc_load_nohz_start();
772 cpu_load_update_nohz_start();
773 quiet_vmstat();
775 ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
776 ts->tick_stopped = 1;
777 trace_tick_stop(1, TICK_DEP_MASK_NONE);
780 ts->next_tick = tick;
783 * If the expiration time == KTIME_MAX, then we simply stop
784 * the tick timer.
786 if (unlikely(expires == KTIME_MAX)) {
787 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
788 hrtimer_cancel(&ts->sched_timer);
789 goto out;
792 hrtimer_set_expires(&ts->sched_timer, tick);
794 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
795 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
796 else
797 tick_program_event(tick, 1);
798 out:
800 * Update the estimated sleep length until the next timer
801 * (not only the tick).
803 ts->sleep_length = ktime_sub(dev->next_event, now);
804 return tick;
807 static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
809 /* Update jiffies first */
810 tick_do_update_jiffies64(now);
811 cpu_load_update_nohz_stop();
814 * Clear the timer idle flag, so we avoid IPIs on remote queueing and
815 * the clock forward checks in the enqueue path:
817 timer_clear_idle();
819 calc_load_nohz_stop();
820 touch_softlockup_watchdog_sched();
822 * Cancel the scheduled timer and restore the tick
824 ts->tick_stopped = 0;
825 ts->idle_exittime = now;
827 tick_nohz_restart(ts, now);
830 static void tick_nohz_full_update_tick(struct tick_sched *ts)
832 #ifdef CONFIG_NO_HZ_FULL
833 int cpu = smp_processor_id();
835 if (!tick_nohz_full_cpu(cpu))
836 return;
838 if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
839 return;
841 if (can_stop_full_tick(cpu, ts))
842 tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
843 else if (ts->tick_stopped)
844 tick_nohz_restart_sched_tick(ts, ktime_get());
845 #endif
848 static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
851 * If this CPU is offline and it is the one which updates
852 * jiffies, then give up the assignment and let it be taken by
853 * the CPU which runs the tick timer next. If we don't drop
854 * this here the jiffies might be stale and do_timer() never
855 * invoked.
857 if (unlikely(!cpu_online(cpu))) {
858 if (cpu == tick_do_timer_cpu)
859 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
861 * Make sure the CPU doesn't get fooled by obsolete tick
862 * deadline if it comes back online later.
864 ts->next_tick = 0;
865 return false;
868 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) {
869 ts->sleep_length = NSEC_PER_SEC / HZ;
870 return false;
873 if (need_resched())
874 return false;
876 if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
877 static int ratelimit;
879 if (ratelimit < 10 &&
880 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
881 pr_warn("NOHZ: local_softirq_pending %02x\n",
882 (unsigned int) local_softirq_pending());
883 ratelimit++;
885 return false;
888 if (tick_nohz_full_enabled()) {
890 * Keep the tick alive to guarantee timekeeping progression
891 * if there are full dynticks CPUs around
893 if (tick_do_timer_cpu == cpu)
894 return false;
896 * Boot safety: make sure the timekeeping duty has been
897 * assigned before entering dyntick-idle mode,
899 if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
900 return false;
903 return true;
906 static void __tick_nohz_idle_enter(struct tick_sched *ts)
908 ktime_t now, expires;
909 int cpu = smp_processor_id();
911 now = tick_nohz_start_idle(ts);
913 if (can_stop_idle_tick(cpu, ts)) {
914 int was_stopped = ts->tick_stopped;
916 ts->idle_calls++;
918 expires = tick_nohz_stop_sched_tick(ts, now, cpu);
919 if (expires > 0LL) {
920 ts->idle_sleeps++;
921 ts->idle_expires = expires;
924 if (!was_stopped && ts->tick_stopped) {
925 ts->idle_jiffies = ts->last_jiffies;
926 nohz_balance_enter_idle(cpu);
932 * tick_nohz_idle_enter - stop the idle tick from the idle task
934 * When the next event is more than a tick into the future, stop the idle tick
935 * Called when we start the idle loop.
937 * The arch is responsible of calling:
939 * - rcu_idle_enter() after its last use of RCU before the CPU is put
940 * to sleep.
941 * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
943 void tick_nohz_idle_enter(void)
945 struct tick_sched *ts;
947 lockdep_assert_irqs_enabled();
949 local_irq_disable();
951 ts = this_cpu_ptr(&tick_cpu_sched);
952 ts->inidle = 1;
953 __tick_nohz_idle_enter(ts);
955 local_irq_enable();
959 * tick_nohz_irq_exit - update next tick event from interrupt exit
961 * When an interrupt fires while we are idle and it doesn't cause
962 * a reschedule, it may still add, modify or delete a timer, enqueue
963 * an RCU callback, etc...
964 * So we need to re-calculate and reprogram the next tick event.
966 void tick_nohz_irq_exit(void)
968 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
970 if (ts->inidle)
971 __tick_nohz_idle_enter(ts);
972 else
973 tick_nohz_full_update_tick(ts);
977 * tick_nohz_get_sleep_length - return the length of the current sleep
979 * Called from power state control code with interrupts disabled
981 ktime_t tick_nohz_get_sleep_length(void)
983 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
985 return ts->sleep_length;
989 * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
990 * for a particular CPU.
992 * Called from the schedutil frequency scaling governor in scheduler context.
994 unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
996 struct tick_sched *ts = tick_get_tick_sched(cpu);
998 return ts->idle_calls;
1002 * tick_nohz_get_idle_calls - return the current idle calls counter value
1004 * Called from the schedutil frequency scaling governor in scheduler context.
1006 unsigned long tick_nohz_get_idle_calls(void)
1008 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1010 return ts->idle_calls;
1013 static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
1015 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
1016 unsigned long ticks;
1018 if (vtime_accounting_cpu_enabled())
1019 return;
1021 * We stopped the tick in idle. Update process times would miss the
1022 * time we slept as update_process_times does only a 1 tick
1023 * accounting. Enforce that this is accounted to idle !
1025 ticks = jiffies - ts->idle_jiffies;
1027 * We might be one off. Do not randomly account a huge number of ticks!
1029 if (ticks && ticks < LONG_MAX)
1030 account_idle_ticks(ticks);
1031 #endif
1035 * tick_nohz_idle_exit - restart the idle tick from the idle task
1037 * Restart the idle tick when the CPU is woken up from idle
1038 * This also exit the RCU extended quiescent state. The CPU
1039 * can use RCU again after this function is called.
1041 void tick_nohz_idle_exit(void)
1043 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1044 ktime_t now;
1046 local_irq_disable();
1048 WARN_ON_ONCE(!ts->inidle);
1050 ts->inidle = 0;
1052 if (ts->idle_active || ts->tick_stopped)
1053 now = ktime_get();
1055 if (ts->idle_active)
1056 tick_nohz_stop_idle(ts, now);
1058 if (ts->tick_stopped) {
1059 tick_nohz_restart_sched_tick(ts, now);
1060 tick_nohz_account_idle_ticks(ts);
1063 local_irq_enable();
1067 * The nohz low res interrupt handler
1069 static void tick_nohz_handler(struct clock_event_device *dev)
1071 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1072 struct pt_regs *regs = get_irq_regs();
1073 ktime_t now = ktime_get();
1075 dev->next_event = KTIME_MAX;
1077 tick_sched_do_timer(now);
1078 tick_sched_handle(ts, regs);
1080 /* No need to reprogram if we are running tickless */
1081 if (unlikely(ts->tick_stopped))
1082 return;
1084 hrtimer_forward(&ts->sched_timer, now, tick_period);
1085 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
1088 static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
1090 if (!tick_nohz_enabled)
1091 return;
1092 ts->nohz_mode = mode;
1093 /* One update is enough */
1094 if (!test_and_set_bit(0, &tick_nohz_active))
1095 timers_update_nohz();
1099 * tick_nohz_switch_to_nohz - switch to nohz mode
1101 static void tick_nohz_switch_to_nohz(void)
1103 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1104 ktime_t next;
1106 if (!tick_nohz_enabled)
1107 return;
1109 if (tick_switch_to_oneshot(tick_nohz_handler))
1110 return;
1113 * Recycle the hrtimer in ts, so we can share the
1114 * hrtimer_forward with the highres code.
1116 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1117 /* Get the next period */
1118 next = tick_init_jiffy_update();
1120 hrtimer_set_expires(&ts->sched_timer, next);
1121 hrtimer_forward_now(&ts->sched_timer, tick_period);
1122 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
1123 tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
1126 static inline void tick_nohz_irq_enter(void)
1128 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1129 ktime_t now;
1131 if (!ts->idle_active && !ts->tick_stopped)
1132 return;
1133 now = ktime_get();
1134 if (ts->idle_active)
1135 tick_nohz_stop_idle(ts, now);
1136 if (ts->tick_stopped)
1137 tick_nohz_update_jiffies(now);
1140 #else
1142 static inline void tick_nohz_switch_to_nohz(void) { }
1143 static inline void tick_nohz_irq_enter(void) { }
1144 static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
1146 #endif /* CONFIG_NO_HZ_COMMON */
1149 * Called from irq_enter to notify about the possible interruption of idle()
1151 void tick_irq_enter(void)
1153 tick_check_oneshot_broadcast_this_cpu();
1154 tick_nohz_irq_enter();
1158 * High resolution timer specific code
1160 #ifdef CONFIG_HIGH_RES_TIMERS
1162 * We rearm the timer until we get disabled by the idle code.
1163 * Called with interrupts disabled.
1165 static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
1167 struct tick_sched *ts =
1168 container_of(timer, struct tick_sched, sched_timer);
1169 struct pt_regs *regs = get_irq_regs();
1170 ktime_t now = ktime_get();
1172 tick_sched_do_timer(now);
1175 * Do not call, when we are not in irq context and have
1176 * no valid regs pointer
1178 if (regs)
1179 tick_sched_handle(ts, regs);
1180 else
1181 ts->next_tick = 0;
1183 /* No need to reprogram if we are in idle or full dynticks mode */
1184 if (unlikely(ts->tick_stopped))
1185 return HRTIMER_NORESTART;
1187 hrtimer_forward(timer, now, tick_period);
1189 return HRTIMER_RESTART;
1192 static int sched_skew_tick;
1194 static int __init skew_tick(char *str)
1196 get_option(&str, &sched_skew_tick);
1198 return 0;
1200 early_param("skew_tick", skew_tick);
1203 * tick_setup_sched_timer - setup the tick emulation timer
1205 void tick_setup_sched_timer(void)
1207 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1208 ktime_t now = ktime_get();
1211 * Emulate tick processing via per-CPU hrtimers:
1213 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1214 ts->sched_timer.function = tick_sched_timer;
1216 /* Get the next period (per-CPU) */
1217 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
1219 /* Offset the tick to avert jiffies_lock contention. */
1220 if (sched_skew_tick) {
1221 u64 offset = ktime_to_ns(tick_period) >> 1;
1222 do_div(offset, num_possible_cpus());
1223 offset *= smp_processor_id();
1224 hrtimer_add_expires_ns(&ts->sched_timer, offset);
1227 hrtimer_forward(&ts->sched_timer, now, tick_period);
1228 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
1229 tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
1231 #endif /* HIGH_RES_TIMERS */
1233 #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
1234 void tick_cancel_sched_timer(int cpu)
1236 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
1238 # ifdef CONFIG_HIGH_RES_TIMERS
1239 if (ts->sched_timer.base)
1240 hrtimer_cancel(&ts->sched_timer);
1241 # endif
1243 memset(ts, 0, sizeof(*ts));
1245 #endif
1248 * Async notification about clocksource changes
1250 void tick_clock_notify(void)
1252 int cpu;
1254 for_each_possible_cpu(cpu)
1255 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
1259 * Async notification about clock event changes
1261 void tick_oneshot_notify(void)
1263 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1265 set_bit(0, &ts->check_clocks);
1269 * Check, if a change happened, which makes oneshot possible.
1271 * Called cyclic from the hrtimer softirq (driven by the timer
1272 * softirq) allow_nohz signals, that we can switch into low-res nohz
1273 * mode, because high resolution timers are disabled (either compile
1274 * or runtime). Called with interrupts disabled.
1276 int tick_check_oneshot_change(int allow_nohz)
1278 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1280 if (!test_and_clear_bit(0, &ts->check_clocks))
1281 return 0;
1283 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
1284 return 0;
1286 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
1287 return 0;
1289 if (!allow_nohz)
1290 return 1;
1292 tick_nohz_switch_to_nohz();
1293 return 0;