blob: 8f482de0adc001d98c56dc3551af7a3b31e20c38 [file] [log] [blame]
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001/*
2 * linux/kernel/time/tick-sched.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 * No idle tick implementation for low and high resolution timers
9 *
10 * Started by: Thomas Gleixner and Ingo Molnar
11 *
Pavel Machekb10db7f2008-01-30 13:30:00 +010012 * Distribute under GPLv2.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080013 */
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>
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -070022#include <linux/module.h>
Frederic Weisbecker00b42952012-11-07 21:03:07 +010023#include <linux/irq_work.h>
Frederic Weisbecker9014c452013-04-20 15:43:57 +020024#include <linux/posix-timers.h>
Prasad Sodagudi602c4e22017-05-17 23:26:09 -070025#include <linux/timer.h>
Frederic Weisbecker2e709332013-07-10 00:55:25 +020026#include <linux/context_tracking.h>
Kyle Yan36d78702016-08-23 16:07:11 -070027#include <linux/rq_stats.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080028
David S. Miller9e203bc2007-02-24 22:10:13 -080029#include <asm/irq_regs.h>
30
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080031#include "tick-internal.h"
32
Frederic Weisbeckercb41a292013-04-20 17:35:50 +020033#include <trace/events/timer.h>
34
Kyle Yan36d78702016-08-23 16:07:11 -070035struct rq_data rq_info;
36struct workqueue_struct *rq_wq;
37spinlock_t rq_lock;
38
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080039/*
Ingo Molnar0de76112016-07-01 12:42:35 +020040 * Per-CPU nohz control structure
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080041 */
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010042static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080043
Ingo Molnar289f4802007-02-16 01:28:15 -080044struct tick_sched *tick_get_tick_sched(int cpu)
45{
46 return &per_cpu(tick_cpu_sched, cpu);
47}
48
Arnd Bergmann7809998a2016-01-25 16:41:49 +010049#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
50/*
51 * The time, when the last jiffy update happened. Protected by jiffies_lock.
52 */
53static ktime_t last_jiffies_update;
54
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070055u64 jiffy_to_ktime_ns(u64 *now, u64 *jiffy_ktime_ns)
56{
57 u64 cur_jiffies;
58 unsigned long seq;
59
60 do {
61 seq = read_seqbegin(&jiffies_lock);
62 *now = ktime_get_ns();
63 *jiffy_ktime_ns = ktime_to_ns(last_jiffies_update);
64 cur_jiffies = get_jiffies_64();
65 } while (read_seqretry(&jiffies_lock, seq));
66
67 return cur_jiffies;
68}
69
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080070/*
71 * Must be called with interrupts disabled !
72 */
73static void tick_do_update_jiffies64(ktime_t now)
74{
75 unsigned long ticks = 0;
76 ktime_t delta;
77
Ingo Molnar7a14ce12008-05-12 15:43:53 +020078 /*
John Stultzd6ad4182012-02-28 16:50:11 -080079 * Do a quick check without holding jiffies_lock:
Ingo Molnar7a14ce12008-05-12 15:43:53 +020080 */
81 delta = ktime_sub(now, last_jiffies_update);
82 if (delta.tv64 < tick_period.tv64)
83 return;
84
Wei Jiangang6168f8e2016-06-29 12:51:50 +080085 /* Reevaluate with jiffies_lock held */
John Stultzd6ad4182012-02-28 16:50:11 -080086 write_seqlock(&jiffies_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080087
88 delta = ktime_sub(now, last_jiffies_update);
89 if (delta.tv64 >= tick_period.tv64) {
90
91 delta = ktime_sub(delta, tick_period);
92 last_jiffies_update = ktime_add(last_jiffies_update,
93 tick_period);
94
95 /* Slow path for long timeouts */
96 if (unlikely(delta.tv64 >= tick_period.tv64)) {
97 s64 incr = ktime_to_ns(tick_period);
98
99 ticks = ktime_divns(delta, incr);
100
101 last_jiffies_update = ktime_add_ns(last_jiffies_update,
102 incr * ticks);
103 }
104 do_timer(++ticks);
Thomas Gleixner49d670f2008-09-22 18:56:01 +0200105
106 /* Keep the tick_next_period variable up to date */
107 tick_next_period = ktime_add(last_jiffies_update, tick_period);
Viresh Kumar03e6bdc2014-04-15 10:54:40 +0530108 } else {
109 write_sequnlock(&jiffies_lock);
110 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800111 }
John Stultzd6ad4182012-02-28 16:50:11 -0800112 write_sequnlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -0800113 update_wall_time();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800114}
115
116/*
117 * Initialize and return retrieve the jiffies update.
118 */
119static ktime_t tick_init_jiffy_update(void)
120{
121 ktime_t period;
122
John Stultzd6ad4182012-02-28 16:50:11 -0800123 write_seqlock(&jiffies_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800124 /* Did we start the jiffies update yet ? */
125 if (last_jiffies_update.tv64 == 0)
126 last_jiffies_update = tick_next_period;
127 period = last_jiffies_update;
John Stultzd6ad4182012-02-28 16:50:11 -0800128 write_sequnlock(&jiffies_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800129 return period;
130}
131
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200132
133static void tick_sched_do_timer(ktime_t now)
134{
135 int cpu = smp_processor_id();
136
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200137#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200138 /*
139 * Check if the do_timer duty was dropped. We don't care about
Ingo Molnar0de76112016-07-01 12:42:35 +0200140 * concurrency: This happens only when the CPU in charge went
141 * into a long sleep. If two CPUs happen to assign themselves to
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200142 * this duty, then the jiffies update is still serialized by
Thomas Gleixner9c3f9e22012-11-21 20:31:52 +0100143 * jiffies_lock.
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200144 */
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100145 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200146 && !tick_nohz_full_cpu(cpu))
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200147 tick_do_timer_cpu = cpu;
148#endif
149
150 /* Check, if the jiffies need an update */
151 if (tick_do_timer_cpu == cpu)
152 tick_do_update_jiffies64(now);
153}
154
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200155static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
156{
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200157#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200158 /*
159 * When we are idle and the tick is stopped, we have to touch
160 * the watchdog as we might not schedule for a really long
161 * time. This happens on complete idle SMP systems while
162 * waiting on the login prompt. We also increment the "start of
163 * idle" jiffy stamp so the idle accounting adjustment we do
164 * when we go busy again does not account too much ticks.
165 */
166 if (ts->tick_stopped) {
Tejun Heo03e0d462015-12-08 11:28:04 -0500167 touch_softlockup_watchdog_sched();
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200168 if (is_idle_task(current))
169 ts->idle_jiffies++;
170 }
Frederic Weisbecker94a57142012-10-15 16:17:16 +0200171#endif
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200172 update_process_times(user_mode(regs));
173 profile_tick(CPU_PROFILING);
174}
Arnd Bergmann7809998a2016-01-25 16:41:49 +0100175#endif
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200176
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200177#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker460775d2013-07-24 23:52:27 +0200178cpumask_var_t tick_nohz_full_mask;
Paul E. McKenneyc0f489d2014-06-04 13:46:03 -0700179cpumask_var_t housekeeping_mask;
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200180bool tick_nohz_full_running;
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100181static atomic_t tick_dep_mask;
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100182
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100183static bool check_tick_dependency(atomic_t *dep)
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200184{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100185 int val = atomic_read(dep);
186
187 if (val & TICK_DEP_MASK_POSIX_TIMER) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100188 trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100189 return true;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200190 }
191
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100192 if (val & TICK_DEP_MASK_PERF_EVENTS) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100193 trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100194 return true;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200195 }
196
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100197 if (val & TICK_DEP_MASK_SCHED) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100198 trace_tick_stop(0, TICK_DEP_MASK_SCHED);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100199 return true;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200200 }
201
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100202 if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100203 trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100204 return true;
205 }
206
207 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200208}
209
Wanpeng Li57ccdf42016-09-07 18:51:13 +0800210static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
Frederic Weisbecker9014c452013-04-20 15:43:57 +0200211{
212 WARN_ON_ONCE(!irqs_disabled());
213
Wanpeng Li57ccdf42016-09-07 18:51:13 +0800214 if (unlikely(!cpu_online(cpu)))
215 return false;
216
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100217 if (check_tick_dependency(&tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200218 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200219
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100220 if (check_tick_dependency(&ts->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200221 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200222
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100223 if (check_tick_dependency(&current->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200224 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200225
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100226 if (check_tick_dependency(&current->signal->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200227 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200228
Frederic Weisbecker9014c452013-04-20 15:43:57 +0200229 return true;
230}
231
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200232static void nohz_full_kick_func(struct irq_work *work)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200233{
Frederic Weisbecker73738a92015-05-27 19:22:08 +0200234 /* Empty, the tick restart happens on tick_nohz_irq_exit() */
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200235}
236
237static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200238 .func = nohz_full_kick_func,
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200239};
240
241/*
Frederic Weisbecker40bea032014-08-13 18:50:16 +0200242 * Kick this CPU if it's full dynticks in order to force it to
243 * re-evaluate its dependency on the tick and restart it if necessary.
244 * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
245 * is NMI safe.
246 */
Frederic Weisbecker555e0c12015-07-16 17:42:29 +0200247static void tick_nohz_full_kick(void)
Frederic Weisbecker40bea032014-08-13 18:50:16 +0200248{
249 if (!tick_nohz_full_cpu(smp_processor_id()))
250 return;
251
Christoph Lameter56e4dea2014-10-27 10:49:45 -0500252 irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
Frederic Weisbecker40bea032014-08-13 18:50:16 +0200253}
254
255/*
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200256 * Kick the CPU if it's full dynticks in order to force it to
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200257 * re-evaluate its dependency on the tick and restart it if necessary.
258 */
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200259void tick_nohz_full_kick_cpu(int cpu)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200260{
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200261 if (!tick_nohz_full_cpu(cpu))
262 return;
263
264 irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200265}
266
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200267/*
268 * Kick all full dynticks CPUs in order to force these to re-evaluate
269 * their dependency on the tick and restart it if necessary.
270 */
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200271static void tick_nohz_full_kick_all(void)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200272{
Frederic Weisbecker8537bb92015-12-07 16:55:23 +0100273 int cpu;
274
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200275 if (!tick_nohz_full_running)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200276 return;
277
278 preempt_disable();
Frederic Weisbecker8537bb92015-12-07 16:55:23 +0100279 for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
280 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200281 preempt_enable();
282}
283
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100284static void tick_nohz_dep_set_all(atomic_t *dep,
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200285 enum tick_dep_bits bit)
286{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100287 int prev;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200288
Peter Zijlstraa1cc5bc2016-04-21 20:35:25 +0200289 prev = atomic_fetch_or(BIT(bit), dep);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200290 if (!prev)
291 tick_nohz_full_kick_all();
292}
293
294/*
295 * Set a global tick dependency. Used by perf events that rely on freq and
296 * by unstable clock.
297 */
298void tick_nohz_dep_set(enum tick_dep_bits bit)
299{
300 tick_nohz_dep_set_all(&tick_dep_mask, bit);
301}
302
303void tick_nohz_dep_clear(enum tick_dep_bits bit)
304{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100305 atomic_andnot(BIT(bit), &tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200306}
307
308/*
309 * Set per-CPU tick dependency. Used by scheduler and perf events in order to
310 * manage events throttling.
311 */
312void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
313{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100314 int prev;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200315 struct tick_sched *ts;
316
317 ts = per_cpu_ptr(&tick_cpu_sched, cpu);
318
Peter Zijlstraa1cc5bc2016-04-21 20:35:25 +0200319 prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200320 if (!prev) {
321 preempt_disable();
322 /* Perf needs local kick that is NMI safe */
323 if (cpu == smp_processor_id()) {
324 tick_nohz_full_kick();
325 } else {
326 /* Remote irq work not NMI-safe */
327 if (!WARN_ON_ONCE(in_nmi()))
328 tick_nohz_full_kick_cpu(cpu);
329 }
330 preempt_enable();
331 }
332}
333
334void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
335{
336 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
337
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100338 atomic_andnot(BIT(bit), &ts->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200339}
340
341/*
342 * Set a per-task tick dependency. Posix CPU timers need this in order to elapse
343 * per task timers.
344 */
345void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
346{
347 /*
348 * We could optimize this with just kicking the target running the task
349 * if that noise matters for nohz full users.
350 */
351 tick_nohz_dep_set_all(&tsk->tick_dep_mask, bit);
352}
353
354void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
355{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100356 atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200357}
358
359/*
360 * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
361 * per process timers.
362 */
363void tick_nohz_dep_set_signal(struct signal_struct *sig, enum tick_dep_bits bit)
364{
365 tick_nohz_dep_set_all(&sig->tick_dep_mask, bit);
366}
367
368void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
369{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100370 atomic_andnot(BIT(bit), &sig->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200371}
372
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200373/*
374 * Re-evaluate the need for the tick as we switch the current task.
375 * It might need the tick due to per task/process properties:
Ingo Molnar0de76112016-07-01 12:42:35 +0200376 * perf events, posix CPU timers, ...
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200377 */
Frederic Weisbeckerde734f82015-06-11 18:07:12 +0200378void __tick_nohz_task_switch(void)
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200379{
380 unsigned long flags;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200381 struct tick_sched *ts;
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200382
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200383 local_irq_save(flags);
384
Li Zhong6296ace2013-04-28 11:25:58 +0800385 if (!tick_nohz_full_cpu(smp_processor_id()))
386 goto out;
387
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200388 ts = this_cpu_ptr(&tick_cpu_sched);
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200389
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200390 if (ts->tick_stopped) {
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100391 if (atomic_read(&current->tick_dep_mask) ||
392 atomic_read(&current->signal->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200393 tick_nohz_full_kick();
394 }
Li Zhong6296ace2013-04-28 11:25:58 +0800395out:
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200396 local_irq_restore(flags);
397}
398
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100399/* Parse the boot-time nohz CPU list from the kernel parameters. */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200400static int __init tick_nohz_full_setup(char *str)
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100401{
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200402 alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
403 if (cpulist_parse(str, tick_nohz_full_mask) < 0) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700404 pr_warn("NO_HZ: Incorrect nohz_full cpumask\n");
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200405 free_bootmem_cpumask_var(tick_nohz_full_mask);
Frederic Weisbecker0453b432013-03-27 02:18:34 +0100406 return 1;
407 }
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200408 tick_nohz_full_running = true;
Frederic Weisbecker0453b432013-03-27 02:18:34 +0100409
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100410 return 1;
411}
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200412__setup("nohz_full=", tick_nohz_full_setup);
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100413
Paul Gortmaker0db06282013-06-19 14:53:51 -0400414static int tick_nohz_cpu_down_callback(struct notifier_block *nfb,
Frederic Weisbecker7c8bb6c2015-09-01 16:51:00 +0200415 unsigned long action,
416 void *hcpu)
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100417{
418 unsigned int cpu = (unsigned long)hcpu;
419
420 switch (action & ~CPU_TASKS_FROZEN) {
421 case CPU_DOWN_PREPARE:
422 /*
Frederic Weisbecker7c8bb6c2015-09-01 16:51:00 +0200423 * The boot CPU handles housekeeping duty (unbound timers,
424 * workqueues, timekeeping, ...) on behalf of full dynticks
425 * CPUs. It must remain online when nohz full is enabled.
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100426 */
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200427 if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
Li Zhong1a7f8292013-05-17 16:44:04 +0800428 return NOTIFY_BAD;
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100429 break;
430 }
431 return NOTIFY_OK;
432}
433
Frederic Weisbeckerf98823a2013-03-27 01:17:22 +0100434static int tick_nohz_init_all(void)
435{
436 int err = -1;
437
438#ifdef CONFIG_NO_HZ_FULL_ALL
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200439 if (!alloc_cpumask_var(&tick_nohz_full_mask, GFP_KERNEL)) {
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200440 WARN(1, "NO_HZ: Can't allocate full dynticks cpumask\n");
Paul E. McKenneyc0f489d2014-06-04 13:46:03 -0700441 return err;
442 }
Frederic Weisbeckerf98823a2013-03-27 01:17:22 +0100443 err = 0;
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200444 cpumask_setall(tick_nohz_full_mask);
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200445 tick_nohz_full_running = true;
Frederic Weisbeckerf98823a2013-03-27 01:17:22 +0100446#endif
447 return err;
448}
449
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100450void __init tick_nohz_init(void)
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100451{
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100452 int cpu;
453
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200454 if (!tick_nohz_full_running) {
Frederic Weisbeckerf98823a2013-03-27 01:17:22 +0100455 if (tick_nohz_init_all() < 0)
456 return;
457 }
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100458
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200459 if (!alloc_cpumask_var(&housekeeping_mask, GFP_KERNEL)) {
460 WARN(1, "NO_HZ: Can't allocate not-full dynticks cpumask\n");
461 cpumask_clear(tick_nohz_full_mask);
462 tick_nohz_full_running = false;
463 return;
464 }
465
Frederic Weisbecker9b01f5b2014-08-18 01:36:07 +0200466 /*
467 * Full dynticks uses irq work to drive the tick rescheduling on safe
468 * locking contexts. But then we need irq work to raise its own
469 * interrupts to avoid circular dependency on the tick
470 */
471 if (!arch_irq_work_has_interrupt()) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700472 pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
Frederic Weisbecker9b01f5b2014-08-18 01:36:07 +0200473 cpumask_clear(tick_nohz_full_mask);
474 cpumask_copy(housekeeping_mask, cpu_possible_mask);
475 tick_nohz_full_running = false;
476 return;
477 }
478
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200479 cpu = smp_processor_id();
480
481 if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700482 pr_warn("NO_HZ: Clearing %d from nohz_full range for timekeeping\n",
483 cpu);
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200484 cpumask_clear_cpu(cpu, tick_nohz_full_mask);
485 }
486
487 cpumask_andnot(housekeeping_mask,
488 cpu_possible_mask, tick_nohz_full_mask);
489
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200490 for_each_cpu(cpu, tick_nohz_full_mask)
Frederic Weisbecker2e709332013-07-10 00:55:25 +0200491 context_tracking_cpu_set(cpu);
492
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100493 cpu_notifier(tick_nohz_cpu_down_callback, 0);
Tejun Heoffda22c2015-02-13 14:37:31 -0800494 pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
495 cpumask_pr_args(tick_nohz_full_mask));
Frederic Weisbecker7c8bb6c2015-09-01 16:51:00 +0200496
497 /*
498 * We need at least one CPU to handle housekeeping work such
499 * as timekeeping, unbound timers, workqueues, ...
500 */
501 WARN_ON_ONCE(cpumask_empty(housekeeping_mask));
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100502}
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100503#endif
504
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800505/*
506 * NOHZ - aka dynamic tick functionality
507 */
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200508#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800509/*
510 * NO HZ enabled ?
511 */
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700512bool tick_nohz_enabled __read_mostly = true;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000513unsigned long tick_nohz_active __read_mostly;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800514/*
515 * Enable / Disable tickless mode
516 */
517static int __init setup_tick_nohz(char *str)
518{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700519 return (kstrtobool(str, &tick_nohz_enabled) == 0);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800520}
521
522__setup("nohz=", setup_tick_nohz);
523
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100524int tick_nohz_tick_stopped(void)
525{
526 return __this_cpu_read(tick_cpu_sched.tick_stopped);
527}
528
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800529/**
530 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
531 *
532 * Called from interrupt entry when the CPU was idle
533 *
534 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
535 * must be updated. Otherwise an interrupt handler could use a stale jiffy
Ingo Molnar0de76112016-07-01 12:42:35 +0200536 * value. We do this unconditionally on any CPU, as we don't know whether the
537 * CPU, which has the update task assigned is in a long sleep.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800538 */
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200539static void tick_nohz_update_jiffies(ktime_t now)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800540{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800541 unsigned long flags;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800542
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200543 __this_cpu_write(tick_cpu_sched.idle_waketime, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800544
545 local_irq_save(flags);
546 tick_do_update_jiffies64(now);
547 local_irq_restore(flags);
Ingo Molnar02ff3752008-05-12 15:43:53 +0200548
Tejun Heo03e0d462015-12-08 11:28:04 -0500549 touch_softlockup_watchdog_sched();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800550}
551
Arjan van de Ven595aac42010-05-09 08:22:45 -0700552/*
Ingo Molnar0de76112016-07-01 12:42:35 +0200553 * Updates the per-CPU time idle statistics counters
Arjan van de Ven595aac42010-05-09 08:22:45 -0700554 */
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700555static void
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200556update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
Arjan van de Ven595aac42010-05-09 08:22:45 -0700557{
558 ktime_t delta;
559
Arjan van de Ven595aac42010-05-09 08:22:45 -0700560 if (ts->idle_active) {
561 delta = ktime_sub(now, ts->idle_entrytime);
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200562 if (nr_iowait_cpu(cpu) > 0)
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700563 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
Michal Hocko6beea0c2011-08-24 09:37:48 +0200564 else
565 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
Arjan van de Ven8c7b09f2010-05-09 08:23:23 -0700566 ts->idle_entrytime = now;
Arjan van de Ven595aac42010-05-09 08:22:45 -0700567 }
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700568
Arjan van de Vene0e37c22010-05-09 08:24:39 -0700569 if (last_update_time)
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700570 *last_update_time = ktime_to_us(now);
571
Arjan van de Ven595aac42010-05-09 08:22:45 -0700572}
573
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200574static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100575{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200576 update_ts_time_stats(smp_processor_id(), ts, now, NULL);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200577 ts->idle_active = 0;
Peter Zijlstra56c74262008-09-01 16:44:23 +0200578
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200579 sched_clock_idle_wakeup_event(0);
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100580}
581
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200582static ktime_t tick_nohz_start_idle(struct tick_sched *ts)
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100583{
Michal Hocko430ee882011-12-01 17:00:22 +0100584 ktime_t now = ktime_get();
Arjan van de Ven595aac42010-05-09 08:22:45 -0700585
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100586 ts->idle_entrytime = now;
587 ts->idle_active = 1;
Peter Zijlstra56c74262008-09-01 16:44:23 +0200588 sched_clock_idle_sleep_event();
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100589 return now;
590}
591
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700592/**
Ingo Molnar0de76112016-07-01 12:42:35 +0200593 * get_cpu_idle_time_us - get the total idle time of a CPU
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700594 * @cpu: CPU number to query
Michal Hocko09a1d342011-08-24 09:39:30 +0200595 * @last_update_time: variable to store update time in. Do not update
596 * counters if NULL.
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700597 *
Wei Jiangang6168f8e2016-06-29 12:51:50 +0800598 * Return the cumulative idle time (since boot) for a given
Michal Hocko6beea0c2011-08-24 09:37:48 +0200599 * CPU, in microseconds.
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700600 *
601 * This time is measured via accounting rather than sampling,
602 * and is as accurate as ktime_get() is.
603 *
604 * This function returns -1 if NOHZ is not enabled.
605 */
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100606u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
607{
608 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
Michal Hocko09a1d342011-08-24 09:39:30 +0200609 ktime_t now, idle;
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100610
Thomas Gleixnerd689fe22013-11-13 21:01:57 +0100611 if (!tick_nohz_active)
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700612 return -1;
613
Michal Hocko09a1d342011-08-24 09:39:30 +0200614 now = ktime_get();
615 if (last_update_time) {
616 update_ts_time_stats(cpu, ts, now, last_update_time);
617 idle = ts->idle_sleeptime;
618 } else {
619 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
620 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700621
Michal Hocko09a1d342011-08-24 09:39:30 +0200622 idle = ktime_add(ts->idle_sleeptime, delta);
623 } else {
624 idle = ts->idle_sleeptime;
625 }
626 }
627
628 return ktime_to_us(idle);
629
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100630}
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700631EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100632
Michal Hocko6beea0c2011-08-24 09:37:48 +0200633/**
Ingo Molnar0de76112016-07-01 12:42:35 +0200634 * get_cpu_iowait_time_us - get the total iowait time of a CPU
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700635 * @cpu: CPU number to query
Michal Hocko09a1d342011-08-24 09:39:30 +0200636 * @last_update_time: variable to store update time in. Do not update
637 * counters if NULL.
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700638 *
Wei Jiangang6168f8e2016-06-29 12:51:50 +0800639 * Return the cumulative iowait time (since boot) for a given
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700640 * CPU, in microseconds.
641 *
642 * This time is measured via accounting rather than sampling,
643 * and is as accurate as ktime_get() is.
644 *
645 * This function returns -1 if NOHZ is not enabled.
646 */
647u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
648{
649 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
Michal Hocko09a1d342011-08-24 09:39:30 +0200650 ktime_t now, iowait;
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700651
Thomas Gleixnerd689fe22013-11-13 21:01:57 +0100652 if (!tick_nohz_active)
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700653 return -1;
654
Michal Hocko09a1d342011-08-24 09:39:30 +0200655 now = ktime_get();
656 if (last_update_time) {
657 update_ts_time_stats(cpu, ts, now, last_update_time);
658 iowait = ts->iowait_sleeptime;
659 } else {
660 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
661 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700662
Michal Hocko09a1d342011-08-24 09:39:30 +0200663 iowait = ktime_add(ts->iowait_sleeptime, delta);
664 } else {
665 iowait = ts->iowait_sleeptime;
666 }
667 }
668
669 return ktime_to_us(iowait);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700670}
671EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
672
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000673static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
674{
675 hrtimer_cancel(&ts->sched_timer);
676 hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
677
678 /* Forward the time to expire in the future */
679 hrtimer_forward(&ts->sched_timer, now, tick_period);
680
681 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
682 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
683 else
684 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
685}
686
Thomas Gleixnere8119ac2017-12-22 15:51:13 +0100687static inline bool local_timer_softirq_pending(void)
688{
Anna-Maria Gleixnerf4a9db52018-07-31 18:13:58 +0200689 return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
Thomas Gleixnere8119ac2017-12-22 15:51:13 +0100690}
691
Frederic Weisbecker84bf1bc2011-08-01 01:25:38 +0200692static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
693 ktime_t now, int cpu)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800694{
Christoph Lameter22127e92014-08-17 12:30:25 -0500695 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000696 u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
697 unsigned long seq, basejiff;
698 ktime_t tick;
Frederic Weisbecker855a0fc2013-12-17 00:16:37 +0100699
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800700 /* Read jiffies and the time when jiffies were updated last */
701 do {
John Stultzd6ad4182012-02-28 16:50:11 -0800702 seq = read_seqbegin(&jiffies_lock);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000703 basemono = last_jiffies_update.tv64;
704 basejiff = jiffies;
John Stultzd6ad4182012-02-28 16:50:11 -0800705 } while (read_seqretry(&jiffies_lock, seq));
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000706 ts->last_jiffies = basejiff;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800707
Thomas Gleixnere8119ac2017-12-22 15:51:13 +0100708 /*
709 * Keep the periodic tick, when RCU, architecture or irq_work
710 * requests it.
711 * Aside of that check whether the local timer softirq is
712 * pending. If so its a bad idea to call get_next_timer_interrupt()
713 * because there is an already expired timer, so it will request
714 * immeditate expiry, which rearms the hardware timer with a
715 * minimal delta which brings us back to this place
716 * immediately. Lather, rinse and repeat...
717 */
718 if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
719 irq_work_needs_cpu() || local_timer_softirq_pending()) {
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000720 next_tick = basemono + TICK_NSEC;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200721 } else {
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000722 /*
723 * Get the next pending timer. If high resolution
724 * timers are enabled this only takes the timer wheel
725 * timers into account. If high resolution timers are
726 * disabled this also looks at the next expiring
727 * hrtimer.
728 */
729 next_tmr = get_next_timer_interrupt(basejiff, basemono);
730 ts->next_timer = next_tmr;
731 /* Take the next rcu event into account */
732 next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200733 }
Ingo Molnar47aa8b62013-04-26 10:05:59 +0200734
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000735 /*
736 * If the tick is due in the next period, keep it ticking or
Peter Zijlstra82bbe342015-11-19 17:21:06 +0100737 * force prod the timer.
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000738 */
739 delta = next_tick - basemono;
740 if (delta <= (u64)TICK_NSEC) {
741 tick.tv64 = 0;
Thomas Gleixnera683f392016-07-04 09:50:36 +0000742
743 /*
744 * Tell the timer code that the base is not idle, i.e. undo
745 * the effect of get_next_timer_interrupt():
746 */
747 timer_clear_idle();
Peter Zijlstra82bbe342015-11-19 17:21:06 +0100748 /*
749 * We've not stopped the tick yet, and there's a timer in the
750 * next period, so no point in stopping it either, bail.
751 */
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000752 if (!ts->tick_stopped)
Woodruff, Richard00147442008-12-01 14:18:11 -0800753 goto out;
Peter Zijlstra82bbe342015-11-19 17:21:06 +0100754
755 /*
756 * If, OTOH, we did stop it, but there's a pending (expired)
757 * timer reprogram the timer hardware to fire now.
758 *
759 * We will not restart the tick proper, just prod the timer
760 * hardware into firing an interrupt to process the pending
761 * timers. Just like tick_irq_exit() will not restart the tick
762 * for 'normal' interrupts.
763 *
764 * Only once we exit the idle loop will we re-enable the tick,
765 * see tick_nohz_idle_exit().
766 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000767 if (delta == 0) {
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000768 tick_nohz_restart(ts, now);
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200769 goto out;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000770 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800771 }
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000772
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000773 /*
Ingo Molnar0de76112016-07-01 12:42:35 +0200774 * If this CPU is the one which updates jiffies, then give up
775 * the assignment and let it be taken by the CPU which runs
776 * the tick timer next, which might be this CPU as well. If we
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000777 * don't drop this here the jiffies might be stale and
778 * do_timer() never invoked. Keep track of the fact that it
Ingo Molnar0de76112016-07-01 12:42:35 +0200779 * was the one which had the do_timer() duty last. If this CPU
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000780 * is the one which had the do_timer() duty last, we limit the
Wei Jiangang6168f8e2016-06-29 12:51:50 +0800781 * sleep time to the timekeeping max_deferment value.
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000782 * Otherwise we can sleep as long as we want.
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000783 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000784 delta = timekeeping_max_deferment();
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000785 if (cpu == tick_do_timer_cpu) {
786 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
787 ts->do_timer_last = 1;
788 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000789 delta = KTIME_MAX;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000790 ts->do_timer_last = 0;
791 } else if (!ts->do_timer_last) {
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000792 delta = KTIME_MAX;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000793 }
794
795#ifdef CONFIG_NO_HZ_FULL
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000796 /* Limit the tick delta to the maximum scheduler deferment */
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000797 if (!ts->inidle)
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000798 delta = min(delta, scheduler_tick_max_deferment());
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000799#endif
800
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000801 /* Calculate the next expiry time */
802 if (delta < (KTIME_MAX - basemono))
803 expires = basemono + delta;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000804 else
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000805 expires = KTIME_MAX;
806
807 expires = min_t(u64, expires, next_tick);
808 tick.tv64 = expires;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000809
810 /* Skip reprogram of event if its not changed */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000811 if (ts->tick_stopped && (expires == dev->next_event.tv64))
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000812 goto out;
813
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000814 /*
815 * nohz_stop_sched_tick can be called several times before
816 * the nohz_restart_sched_tick is called. This happens when
817 * interrupts arrive which do not cause a reschedule. In the
818 * first call we save the current tick time, so we can restart
819 * the scheduler tick in nohz_restart_sched_tick.
820 */
821 if (!ts->tick_stopped) {
822 nohz_balance_enter_idle(cpu);
823 calc_load_enter_idle();
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200824 cpu_load_update_nohz_start();
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000825
826 ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
827 ts->tick_stopped = 1;
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100828 trace_tick_stop(1, TICK_DEP_MASK_NONE);
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000829 }
830
831 /*
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000832 * If the expiration time == KTIME_MAX, then we simply stop
833 * the tick timer.
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000834 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000835 if (unlikely(expires == KTIME_MAX)) {
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000836 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
837 hrtimer_cancel(&ts->sched_timer);
838 goto out;
839 }
840
841 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000842 hrtimer_start(&ts->sched_timer, tick, HRTIMER_MODE_ABS_PINNED);
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000843 else
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000844 tick_program_event(tick, 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800845out:
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000846 /* Update the estimated sleep length */
Len Brown4f86d3a2007-10-03 18:58:00 -0400847 ts->sleep_length = ktime_sub(dev->next_event, now);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000848 return tick;
Frederic Weisbecker280f0672011-10-07 18:22:06 +0200849}
850
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200851static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200852{
853 /* Update jiffies first */
854 tick_do_update_jiffies64(now);
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200855 cpu_load_update_nohz_stop();
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200856
Thomas Gleixnera683f392016-07-04 09:50:36 +0000857 /*
858 * Clear the timer idle flag, so we avoid IPIs on remote queueing and
859 * the clock forward checks in the enqueue path:
860 */
861 timer_clear_idle();
862
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200863 calc_load_exit_idle();
Tejun Heo03e0d462015-12-08 11:28:04 -0500864 touch_softlockup_watchdog_sched();
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200865 /*
866 * Cancel the scheduled timer and restore the tick
867 */
868 ts->tick_stopped = 0;
869 ts->idle_exittime = now;
870
871 tick_nohz_restart(ts, now);
872}
Frederic Weisbecker73738a92015-05-27 19:22:08 +0200873
874static void tick_nohz_full_update_tick(struct tick_sched *ts)
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200875{
876#ifdef CONFIG_NO_HZ_FULL
Alex Shie9a2eb42013-11-28 14:27:11 +0800877 int cpu = smp_processor_id();
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200878
Frederic Weisbecker59449352015-05-27 15:42:42 +0200879 if (!tick_nohz_full_cpu(cpu))
Alex Shie9a2eb42013-11-28 14:27:11 +0800880 return;
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200881
Alex Shie9a2eb42013-11-28 14:27:11 +0800882 if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
883 return;
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200884
Wanpeng Li57ccdf42016-09-07 18:51:13 +0800885 if (can_stop_full_tick(cpu, ts))
Frederic Weisbecker73738a92015-05-27 19:22:08 +0200886 tick_nohz_stop_sched_tick(ts, ktime_get(), cpu);
887 else if (ts->tick_stopped)
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200888 tick_nohz_restart_sched_tick(ts, ktime_get());
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200889#endif
890}
891
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200892static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
893{
894 /*
Ingo Molnar0de76112016-07-01 12:42:35 +0200895 * If this CPU is offline and it is the one which updates
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200896 * jiffies, then give up the assignment and let it be taken by
Ingo Molnar0de76112016-07-01 12:42:35 +0200897 * the CPU which runs the tick timer next. If we don't drop
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200898 * this here the jiffies might be stale and do_timer() never
899 * invoked.
900 */
901 if (unlikely(!cpu_online(cpu))) {
902 if (cpu == tick_do_timer_cpu)
903 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
Thomas Gleixnerf7ea0fd2013-05-13 21:40:27 +0200904 return false;
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200905 }
906
Thomas Gleixner0e576acb2013-11-29 12:18:13 +0100907 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) {
908 ts->sleep_length = (ktime_t) { .tv64 = NSEC_PER_SEC/HZ };
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200909 return false;
Thomas Gleixner0e576acb2013-11-29 12:18:13 +0100910 }
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200911
912 if (need_resched())
913 return false;
914
915 if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
916 static int ratelimit;
917
Paul E. McKenney803b0eb2012-08-23 08:34:07 -0700918 if (ratelimit < 10 &&
919 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
Rado Vrbovskycfea7d72013-02-08 12:37:30 -0500920 pr_warn("NOHZ: local_softirq_pending %02x\n",
921 (unsigned int) local_softirq_pending());
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200922 ratelimit++;
923 }
924 return false;
925 }
926
Frederic Weisbecker460775d2013-07-24 23:52:27 +0200927 if (tick_nohz_full_enabled()) {
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100928 /*
929 * Keep the tick alive to guarantee timekeeping progression
930 * if there are full dynticks CPUs around
931 */
932 if (tick_do_timer_cpu == cpu)
933 return false;
934 /*
935 * Boot safety: make sure the timekeeping duty has been
936 * assigned before entering dyntick-idle mode,
937 */
938 if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
939 return false;
940 }
941
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200942 return true;
943}
944
Frederic Weisbecker19f5f732011-07-27 17:29:28 +0200945static void __tick_nohz_idle_enter(struct tick_sched *ts)
946{
Frederic Weisbecker84bf1bc2011-08-01 01:25:38 +0200947 ktime_t now, expires;
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200948 int cpu = smp_processor_id();
Frederic Weisbecker19f5f732011-07-27 17:29:28 +0200949
Wanpeng Li08d072592016-09-02 14:38:23 +0800950 now = tick_nohz_start_idle(ts);
951
Prasad Sodagudi602c4e22017-05-17 23:26:09 -0700952#ifdef CONFIG_SMP
953 if (check_pending_deferrable_timers(cpu))
954 raise_softirq_irqoff(TIMER_SOFTIRQ);
955#endif
956
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200957 if (can_stop_idle_tick(cpu, ts)) {
958 int was_stopped = ts->tick_stopped;
959
960 ts->idle_calls++;
Frederic Weisbecker84bf1bc2011-08-01 01:25:38 +0200961
962 expires = tick_nohz_stop_sched_tick(ts, now, cpu);
963 if (expires.tv64 > 0LL) {
964 ts->idle_sleeps++;
965 ts->idle_expires = expires;
966 }
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200967
968 if (!was_stopped && ts->tick_stopped)
969 ts->idle_jiffies = ts->last_jiffies;
970 }
Frederic Weisbecker280f0672011-10-07 18:22:06 +0200971}
972
973/**
974 * tick_nohz_idle_enter - stop the idle tick from the idle task
975 *
976 * When the next event is more than a tick into the future, stop the idle tick
977 * Called when we start the idle loop.
Frederic Weisbecker2bbb6812011-10-08 16:01:00 +0200978 *
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100979 * The arch is responsible of calling:
Frederic Weisbecker2bbb6812011-10-08 16:01:00 +0200980 *
981 * - rcu_idle_enter() after its last use of RCU before the CPU is put
982 * to sleep.
983 * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
Frederic Weisbecker280f0672011-10-07 18:22:06 +0200984 */
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100985void tick_nohz_idle_enter(void)
Frederic Weisbecker280f0672011-10-07 18:22:06 +0200986{
987 struct tick_sched *ts;
988
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100989 WARN_ON_ONCE(irqs_disabled());
990
Linus Torvalds0db49b72012-01-06 08:33:28 -0800991 /*
Ingo Molnar0de76112016-07-01 12:42:35 +0200992 * Update the idle state in the scheduler domain hierarchy
993 * when tick_nohz_stop_sched_tick() is called from the idle loop.
994 * State will be updated to busy during the first busy tick after
995 * exiting idle.
996 */
Linus Torvalds0db49b72012-01-06 08:33:28 -0800997 set_cpu_sd_state_idle();
998
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +0100999 local_irq_disable();
1000
Christoph Lameter22127e92014-08-17 12:30:25 -05001001 ts = this_cpu_ptr(&tick_cpu_sched);
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001002 ts->inidle = 1;
Frederic Weisbecker19f5f732011-07-27 17:29:28 +02001003 __tick_nohz_idle_enter(ts);
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +01001004
1005 local_irq_enable();
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001006}
1007
1008/**
1009 * tick_nohz_irq_exit - update next tick event from interrupt exit
1010 *
1011 * When an interrupt fires while we are idle and it doesn't cause
1012 * a reschedule, it may still add, modify or delete a timer, enqueue
1013 * an RCU callback, etc...
1014 * So we need to re-calculate and reprogram the next tick event.
1015 */
1016void tick_nohz_irq_exit(void)
1017{
Christoph Lameter22127e92014-08-17 12:30:25 -05001018 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001019
Rafael J. Wysocki14851912013-07-27 01:41:34 +02001020 if (ts->inidle)
Frederic Weisbecker5811d992013-04-20 16:40:31 +02001021 __tick_nohz_idle_enter(ts);
Rafael J. Wysocki14851912013-07-27 01:41:34 +02001022 else
Frederic Weisbecker73738a92015-05-27 19:22:08 +02001023 tick_nohz_full_update_tick(ts);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001024}
1025
1026/**
Len Brown4f86d3a2007-10-03 18:58:00 -04001027 * tick_nohz_get_sleep_length - return the length of the current sleep
1028 *
1029 * Called from power state control code with interrupts disabled
1030 */
1031ktime_t tick_nohz_get_sleep_length(void)
1032{
Christoph Lameter22127e92014-08-17 12:30:25 -05001033 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Len Brown4f86d3a2007-10-03 18:58:00 -04001034
1035 return ts->sleep_length;
1036}
1037
Chris Redpath595ae4a2017-05-25 15:24:58 +01001038/**
1039 * tick_nohz_get_idle_calls - return the current idle calls counter value
1040 *
1041 * Called from the schedutil frequency scaling governor in scheduler context.
1042 */
1043unsigned long tick_nohz_get_idle_calls(void)
1044{
1045 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1046
1047 return ts->idle_calls;
1048}
1049
Frederic Weisbecker2ac0d982011-07-28 04:00:47 +02001050static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
1051{
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +02001052#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Frederic Weisbecker2ac0d982011-07-28 04:00:47 +02001053 unsigned long ticks;
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +02001054
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +01001055 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +02001056 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001057 /*
1058 * We stopped the tick in idle. Update process times would miss the
1059 * time we slept as update_process_times does only a 1 tick
1060 * accounting. Enforce that this is accounted to idle !
1061 */
1062 ticks = jiffies - ts->idle_jiffies;
1063 /*
1064 * We might be one off. Do not randomly account a huge number of ticks!
1065 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01001066 if (ticks && ticks < LONG_MAX)
1067 account_idle_ticks(ticks);
1068#endif
Frederic Weisbecker19f5f732011-07-27 17:29:28 +02001069}
1070
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001071/**
1072 * tick_nohz_idle_exit - restart the idle tick from the idle task
1073 *
1074 * Restart the idle tick when the CPU is woken up from idle
1075 * This also exit the RCU extended quiescent state. The CPU
1076 * can use RCU again after this function is called.
1077 */
1078void tick_nohz_idle_exit(void)
1079{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05001080 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001081 ktime_t now;
1082
1083 local_irq_disable();
1084
1085 WARN_ON_ONCE(!ts->inidle);
1086
1087 ts->inidle = 0;
1088
1089 if (ts->idle_active || ts->tick_stopped)
1090 now = ktime_get();
1091
1092 if (ts->idle_active)
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +02001093 tick_nohz_stop_idle(ts, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001094
Frederic Weisbecker2ac0d982011-07-28 04:00:47 +02001095 if (ts->tick_stopped) {
Frederic Weisbecker1f419062016-04-13 15:56:51 +02001096 tick_nohz_restart_sched_tick(ts, now);
Frederic Weisbecker2ac0d982011-07-28 04:00:47 +02001097 tick_nohz_account_idle_ticks(ts);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001098 }
1099
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001100 local_irq_enable();
1101}
1102
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001103/*
1104 * The nohz low res interrupt handler
1105 */
1106static void tick_nohz_handler(struct clock_event_device *dev)
1107{
Christoph Lameter22127e92014-08-17 12:30:25 -05001108 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001109 struct pt_regs *regs = get_irq_regs();
1110 ktime_t now = ktime_get();
1111
1112 dev->next_event.tv64 = KTIME_MAX;
1113
Frederic Weisbecker5bb96222012-10-15 02:03:27 +02001114 tick_sched_do_timer(now);
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +02001115 tick_sched_handle(ts, regs);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001116
Viresh Kumarb5e995e2014-06-12 16:24:41 +05301117 /* No need to reprogram if we are running tickless */
1118 if (unlikely(ts->tick_stopped))
1119 return;
1120
Thomas Gleixner0ff53d02015-04-14 21:08:54 +00001121 hrtimer_forward(&ts->sched_timer, now, tick_period);
1122 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001123}
1124
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001125static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
1126{
1127 if (!tick_nohz_enabled)
1128 return;
1129 ts->nohz_mode = mode;
1130 /* One update is enough */
1131 if (!test_and_set_bit(0, &tick_nohz_active))
Thomas Gleixner683be132015-05-26 22:50:35 +00001132 timers_update_migration(true);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001133}
1134
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001135/**
1136 * tick_nohz_switch_to_nohz - switch to nohz mode
1137 */
1138static void tick_nohz_switch_to_nohz(void)
1139{
Christoph Lameter22127e92014-08-17 12:30:25 -05001140 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001141 ktime_t next;
1142
Viresh Kumar27630532014-04-15 10:54:41 +05301143 if (!tick_nohz_enabled)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001144 return;
1145
Thomas Gleixner6b442bc2015-05-07 14:35:59 +02001146 if (tick_switch_to_oneshot(tick_nohz_handler))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001147 return;
Thomas Gleixner6b442bc2015-05-07 14:35:59 +02001148
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001149 /*
1150 * Recycle the hrtimer in ts, so we can share the
1151 * hrtimer_forward with the highres code.
1152 */
1153 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1154 /* Get the next period */
1155 next = tick_init_jiffy_update();
1156
Thomas Gleixner0ff53d02015-04-14 21:08:54 +00001157 hrtimer_set_expires(&ts->sched_timer, next);
Wanpeng Li1ca8ec52016-01-27 19:26:07 +08001158 hrtimer_forward_now(&ts->sched_timer, tick_period);
1159 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001160 tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001161}
1162
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001163static inline void tick_nohz_irq_enter(void)
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001164{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05001165 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001166 ktime_t now;
1167
1168 if (!ts->idle_active && !ts->tick_stopped)
1169 return;
1170 now = ktime_get();
1171 if (ts->idle_active)
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +02001172 tick_nohz_stop_idle(ts, now);
Thomas Gleixnerff006732016-07-04 09:50:35 +00001173 if (ts->tick_stopped)
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001174 tick_nohz_update_jiffies(now);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001175}
1176
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001177#else
1178
1179static inline void tick_nohz_switch_to_nohz(void) { }
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001180static inline void tick_nohz_irq_enter(void) { }
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001181static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001182
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001183#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001184
1185/*
Thomas Gleixner719254f2008-10-17 09:59:47 +02001186 * Called from irq_enter to notify about the possible interruption of idle()
1187 */
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001188void tick_irq_enter(void)
Thomas Gleixner719254f2008-10-17 09:59:47 +02001189{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +02001190 tick_check_oneshot_broadcast_this_cpu();
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001191 tick_nohz_irq_enter();
Thomas Gleixner719254f2008-10-17 09:59:47 +02001192}
1193
1194/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001195 * High resolution timer specific code
1196 */
1197#ifdef CONFIG_HIGH_RES_TIMERS
Kyle Yan36d78702016-08-23 16:07:11 -07001198static void update_rq_stats(void)
1199{
1200 unsigned long jiffy_gap = 0;
Runmin Wang3ac70572017-04-12 17:26:50 -07001201 unsigned long long rq_avg = 0;
Kyle Yan36d78702016-08-23 16:07:11 -07001202 unsigned long flags = 0;
1203
1204 jiffy_gap = jiffies - rq_info.rq_poll_last_jiffy;
1205 if (jiffy_gap >= rq_info.rq_poll_jiffies) {
1206 spin_lock_irqsave(&rq_lock, flags);
1207 if (!rq_info.rq_avg)
1208 rq_info.rq_poll_total_jiffies = 0;
1209 rq_avg = nr_running() * 10;
1210 if (rq_info.rq_poll_total_jiffies) {
1211 rq_avg = (rq_avg * jiffy_gap) +
1212 (rq_info.rq_avg *
1213 rq_info.rq_poll_total_jiffies);
1214 do_div(rq_avg,
1215 rq_info.rq_poll_total_jiffies + jiffy_gap);
1216 }
1217 rq_info.rq_avg = rq_avg;
1218 rq_info.rq_poll_total_jiffies += jiffy_gap;
1219 rq_info.rq_poll_last_jiffy = jiffies;
1220 spin_unlock_irqrestore(&rq_lock, flags);
1221 }
1222}
1223static void wakeup_user(void)
1224{
1225 unsigned long jiffy_gap;
1226
1227 jiffy_gap = jiffies - rq_info.def_timer_last_jiffy;
1228 if (jiffy_gap >= rq_info.def_timer_jiffies) {
1229 rq_info.def_timer_last_jiffy = jiffies;
1230 queue_work(rq_wq, &rq_info.def_timer_work);
1231 }
1232}
1233
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001234/*
Pavel Machek4c9dc642008-01-30 13:30:00 +01001235 * We rearm the timer until we get disabled by the idle code.
Chuansheng Liu351f1812012-10-25 01:07:35 +08001236 * Called with interrupts disabled.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001237 */
1238static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
1239{
1240 struct tick_sched *ts =
1241 container_of(timer, struct tick_sched, sched_timer);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001242 struct pt_regs *regs = get_irq_regs();
1243 ktime_t now = ktime_get();
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -07001244
Frederic Weisbecker5bb96222012-10-15 02:03:27 +02001245 tick_sched_do_timer(now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001246
1247 /*
1248 * Do not call, when we are not in irq context and have
1249 * no valid regs pointer
1250 */
Kyle Yan36d78702016-08-23 16:07:11 -07001251 if (regs) {
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +02001252 tick_sched_handle(ts, regs);
Kyle Yan36d78702016-08-23 16:07:11 -07001253 if (rq_info.init == 1 &&
1254 tick_do_timer_cpu == smp_processor_id()) {
1255 /*
1256 * update run queue statistics
1257 */
1258 update_rq_stats();
1259 /*
1260 * wakeup user if needed
1261 */
1262 wakeup_user();
1263 }
1264 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001265
Viresh Kumar2a16fc92014-06-12 16:24:41 +05301266 /* No need to reprogram if we are in idle or full dynticks mode */
1267 if (unlikely(ts->tick_stopped))
1268 return HRTIMER_NORESTART;
1269
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001270 hrtimer_forward(timer, now, tick_period);
1271
1272 return HRTIMER_RESTART;
1273}
1274
Mike Galbraith5307c952012-05-08 12:20:58 +02001275static int sched_skew_tick;
1276
Thomas Gleixner62cf20b2012-05-25 14:08:57 +02001277static int __init skew_tick(char *str)
1278{
1279 get_option(&str, &sched_skew_tick);
1280
1281 return 0;
1282}
1283early_param("skew_tick", skew_tick);
1284
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001285/**
1286 * tick_setup_sched_timer - setup the tick emulation timer
1287 */
1288void tick_setup_sched_timer(void)
1289{
Christoph Lameter22127e92014-08-17 12:30:25 -05001290 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001291 ktime_t now = ktime_get();
1292
1293 /*
1294 * Emulate tick processing via per-CPU hrtimers:
1295 */
1296 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1297 ts->sched_timer.function = tick_sched_timer;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001298
Ingo Molnar0de76112016-07-01 12:42:35 +02001299 /* Get the next period (per-CPU) */
Arjan van de Vencc584b22008-09-01 15:02:30 -07001300 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001301
Thomas Gleixner9c3f9e22012-11-21 20:31:52 +01001302 /* Offset the tick to avert jiffies_lock contention. */
Mike Galbraith5307c952012-05-08 12:20:58 +02001303 if (sched_skew_tick) {
1304 u64 offset = ktime_to_ns(tick_period) >> 1;
1305 do_div(offset, num_possible_cpus());
1306 offset *= smp_processor_id();
1307 hrtimer_add_expires_ns(&ts->sched_timer, offset);
1308 }
1309
Thomas Gleixnerafc08b12015-04-14 21:08:52 +00001310 hrtimer_forward(&ts->sched_timer, now, tick_period);
1311 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001312 tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001313}
Miao Xie3c4fbe52008-08-20 16:37:38 -07001314#endif /* HIGH_RES_TIMERS */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001315
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001316#if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001317void tick_cancel_sched_timer(int cpu)
1318{
1319 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
1320
Miao Xie3c4fbe52008-08-20 16:37:38 -07001321# ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001322 if (ts->sched_timer.base)
1323 hrtimer_cancel(&ts->sched_timer);
Miao Xie3c4fbe52008-08-20 16:37:38 -07001324# endif
Karsten Wiesea7901762008-03-04 14:59:55 -08001325
Thomas Gleixner4b0c0f22013-05-03 15:02:50 +02001326 memset(ts, 0, sizeof(*ts));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001327}
Miao Xie3c4fbe52008-08-20 16:37:38 -07001328#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001329
1330/**
1331 * Async notification about clocksource changes
1332 */
1333void tick_clock_notify(void)
1334{
1335 int cpu;
1336
1337 for_each_possible_cpu(cpu)
1338 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
1339}
1340
1341/*
1342 * Async notification about clock event changes
1343 */
1344void tick_oneshot_notify(void)
1345{
Christoph Lameter22127e92014-08-17 12:30:25 -05001346 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001347
1348 set_bit(0, &ts->check_clocks);
1349}
1350
1351/**
1352 * Check, if a change happened, which makes oneshot possible.
1353 *
1354 * Called cyclic from the hrtimer softirq (driven by the timer
1355 * softirq) allow_nohz signals, that we can switch into low-res nohz
1356 * mode, because high resolution timers are disabled (either compile
Thomas Gleixner6b442bc2015-05-07 14:35:59 +02001357 * or runtime). Called with interrupts disabled.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001358 */
1359int tick_check_oneshot_change(int allow_nohz)
1360{
Christoph Lameter22127e92014-08-17 12:30:25 -05001361 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001362
1363 if (!test_and_clear_bit(0, &ts->check_clocks))
1364 return 0;
1365
1366 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
1367 return 0;
1368
Li Zefancf4fc6c2008-02-08 04:19:24 -08001369 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001370 return 0;
1371
1372 if (!allow_nohz)
1373 return 1;
1374
1375 tick_nohz_switch_to_nohz();
1376 return 0;
1377}
Mahesh Sivasubramanian930e2352017-01-26 16:37:08 -07001378
1379ktime_t *get_next_event_cpu(unsigned int cpu)
1380{
1381 return &(per_cpu(tick_cpu_device, cpu).evtdev->next_event);
1382}