blob: 197a3bd882ad743223f2ec4e066910cf2255de02 [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08002/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08003 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
4 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
5 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
6 *
7 * No idle tick implementation for low and high resolution timers
8 *
9 * Started by: Thomas Gleixner and Ingo Molnar
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080010 */
11#include <linux/cpu.h>
12#include <linux/err.h>
13#include <linux/hrtimer.h>
14#include <linux/interrupt.h>
15#include <linux/kernel_stat.h>
16#include <linux/percpu.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010017#include <linux/nmi.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080018#include <linux/profile.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010019#include <linux/sched/signal.h>
Ingo Molnare6017572017-02-01 16:36:40 +010020#include <linux/sched/clock.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010021#include <linux/sched/stat.h>
Ingo Molnar370c9132017-02-08 18:51:35 +010022#include <linux/sched/nohz.h>
Yunfeng Ye896b9692020-11-17 14:19:48 +010023#include <linux/sched/loadavg.h>
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -070024#include <linux/module.h>
Frederic Weisbecker00b42952012-11-07 21:03:07 +010025#include <linux/irq_work.h>
Frederic Weisbecker9014c452013-04-20 15:43:57 +020026#include <linux/posix-timers.h>
Frederic Weisbecker2e709332013-07-10 00:55:25 +020027#include <linux/context_tracking.h>
Peter Zijlstra62cb1182017-08-29 15:07:54 +020028#include <linux/mm.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080029
David S. Miller9e203bc2007-02-24 22:10:13 -080030#include <asm/irq_regs.h>
31
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080032#include "tick-internal.h"
33
Frederic Weisbeckercb41a292013-04-20 17:35:50 +020034#include <trace/events/timer.h>
35
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080036/*
Ingo Molnar0de76112016-07-01 12:42:35 +020037 * Per-CPU nohz control structure
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080038 */
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010039static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080040
Ingo Molnar289f4802007-02-16 01:28:15 -080041struct tick_sched *tick_get_tick_sched(int cpu)
42{
43 return &per_cpu(tick_cpu_sched, cpu);
44}
45
Arnd Bergmann7809998a2016-01-25 16:41:49 +010046#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
47/*
Thomas Gleixnerc3989602020-11-17 14:19:44 +010048 * The time, when the last jiffy update happened. Write access must hold
49 * jiffies_lock and jiffies_seq. tick_nohz_next_event() needs to get a
50 * consistent view of jiffies and last_jiffies_update.
Arnd Bergmann7809998a2016-01-25 16:41:49 +010051 */
52static ktime_t last_jiffies_update;
53
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080054/*
55 * Must be called with interrupts disabled !
56 */
57static void tick_do_update_jiffies64(ktime_t now)
58{
Thomas Gleixner7a35bf22020-11-17 14:19:47 +010059 unsigned long ticks = 1;
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +010060 ktime_t delta, nextp;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080061
Ingo Molnar7a14ce12008-05-12 15:43:53 +020062 /*
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +010063 * 64bit can do a quick check without holding jiffies lock and
64 * without looking at the sequence count. The smp_load_acquire()
Thomas Gleixner372acbb2020-11-17 14:19:45 +010065 * pairs with the update done later in this function.
66 *
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +010067 * 32bit cannot do that because the store of tick_next_period
68 * consists of two 32bit stores and the first store could move it
69 * to a random point in the future.
Ingo Molnar7a14ce12008-05-12 15:43:53 +020070 */
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +010071 if (IS_ENABLED(CONFIG_64BIT)) {
72 if (ktime_before(now, smp_load_acquire(&tick_next_period)))
73 return;
74 } else {
75 unsigned int seq;
Ingo Molnar7a14ce12008-05-12 15:43:53 +020076
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +010077 /*
78 * Avoid contention on jiffies_lock and protect the quick
79 * check with the sequence count.
80 */
81 do {
82 seq = read_seqcount_begin(&jiffies_seq);
83 nextp = tick_next_period;
84 } while (read_seqcount_retry(&jiffies_seq, seq));
85
86 if (ktime_before(now, nextp))
87 return;
88 }
89
90 /* Quick check failed, i.e. update is required. */
Thomas Gleixnere5d4d172020-03-21 12:25:58 +010091 raw_spin_lock(&jiffies_lock);
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +010092 /*
93 * Reevaluate with the lock held. Another CPU might have done the
94 * update already.
95 */
Yunfeng Ye94ad2e32020-11-17 14:19:46 +010096 if (ktime_before(now, tick_next_period)) {
Thomas Gleixnere5d4d172020-03-21 12:25:58 +010097 raw_spin_unlock(&jiffies_lock);
Viresh Kumar03e6bdc2014-04-15 10:54:40 +053098 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080099 }
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100100
101 write_seqcount_begin(&jiffies_seq);
102
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100103 delta = ktime_sub(now, tick_next_period);
Thomas Gleixnerb9965442020-11-17 14:19:49 +0100104 if (unlikely(delta >= TICK_NSEC)) {
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100105 /* Slow path for long idle sleep times */
Thomas Gleixnerb9965442020-11-17 14:19:49 +0100106 s64 incr = TICK_NSEC;
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100107
Thomas Gleixner7a35bf22020-11-17 14:19:47 +0100108 ticks += ktime_divns(delta, incr);
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100109
110 last_jiffies_update = ktime_add_ns(last_jiffies_update,
111 incr * ticks);
Thomas Gleixner7a35bf22020-11-17 14:19:47 +0100112 } else {
Thomas Gleixnerb9965442020-11-17 14:19:49 +0100113 last_jiffies_update = ktime_add_ns(last_jiffies_update,
114 TICK_NSEC);
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100115 }
116
Yunfeng Ye896b9692020-11-17 14:19:48 +0100117 /* Advance jiffies to complete the jiffies_seq protected job */
118 jiffies_64 += ticks;
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100119
120 /*
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +0100121 * Keep the tick_next_period variable up to date.
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100122 */
Thomas Gleixneraa3b66f2020-12-04 11:55:19 +0100123 nextp = ktime_add_ns(last_jiffies_update, TICK_NSEC);
124
125 if (IS_ENABLED(CONFIG_64BIT)) {
126 /*
127 * Pairs with smp_load_acquire() in the lockless quick
128 * check above and ensures that the update to jiffies_64 is
129 * not reordered vs. the store to tick_next_period, neither
130 * by the compiler nor by the CPU.
131 */
132 smp_store_release(&tick_next_period, nextp);
133 } else {
134 /*
135 * A plain store is good enough on 32bit as the quick check
136 * above is protected by the sequence count.
137 */
138 tick_next_period = nextp;
139 }
Yunfeng Ye94ad2e32020-11-17 14:19:46 +0100140
Yunfeng Ye896b9692020-11-17 14:19:48 +0100141 /*
142 * Release the sequence count. calc_global_load() below is not
143 * protected by it, but jiffies_lock needs to be held to prevent
144 * concurrent invocations.
145 */
Thomas Gleixnere5d4d172020-03-21 12:25:58 +0100146 write_seqcount_end(&jiffies_seq);
Yunfeng Ye896b9692020-11-17 14:19:48 +0100147
148 calc_global_load();
149
Thomas Gleixnere5d4d172020-03-21 12:25:58 +0100150 raw_spin_unlock(&jiffies_lock);
John Stultz47a1b7962013-12-12 13:10:55 -0800151 update_wall_time();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800152}
153
154/*
155 * Initialize and return retrieve the jiffies update.
156 */
157static ktime_t tick_init_jiffy_update(void)
158{
159 ktime_t period;
160
Thomas Gleixnere5d4d172020-03-21 12:25:58 +0100161 raw_spin_lock(&jiffies_lock);
162 write_seqcount_begin(&jiffies_seq);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800163 /* Did we start the jiffies update yet ? */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100164 if (last_jiffies_update == 0)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800165 last_jiffies_update = tick_next_period;
166 period = last_jiffies_update;
Thomas Gleixnere5d4d172020-03-21 12:25:58 +0100167 write_seqcount_end(&jiffies_seq);
168 raw_spin_unlock(&jiffies_lock);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800169 return period;
170}
171
Rafael J. Wysockiff7de622018-04-06 14:59:13 +0200172static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200173{
174 int cpu = smp_processor_id();
175
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200176#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200177 /*
178 * Check if the do_timer duty was dropped. We don't care about
Ingo Molnar0de76112016-07-01 12:42:35 +0200179 * concurrency: This happens only when the CPU in charge went
180 * into a long sleep. If two CPUs happen to assign themselves to
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200181 * this duty, then the jiffies update is still serialized by
Thomas Gleixner9c3f9e22012-11-21 20:31:52 +0100182 * jiffies_lock.
Nicholas Piggin08ae95f2019-04-11 13:34:48 +1000183 *
184 * If nohz_full is enabled, this should not happen because the
185 * tick_do_timer_cpu never relinquishes.
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200186 */
Nicholas Piggin08ae95f2019-04-11 13:34:48 +1000187 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) {
188#ifdef CONFIG_NO_HZ_FULL
189 WARN_ON(tick_nohz_full_running);
190#endif
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200191 tick_do_timer_cpu = cpu;
Nicholas Piggin08ae95f2019-04-11 13:34:48 +1000192 }
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200193#endif
194
195 /* Check, if the jiffies need an update */
196 if (tick_do_timer_cpu == cpu)
197 tick_do_update_jiffies64(now);
Rafael J. Wysockiff7de622018-04-06 14:59:13 +0200198
199 if (ts->inidle)
200 ts->got_idle_tick = 1;
Frederic Weisbecker5bb96222012-10-15 02:03:27 +0200201}
202
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200203static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
204{
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200205#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200206 /*
207 * When we are idle and the tick is stopped, we have to touch
208 * the watchdog as we might not schedule for a really long
209 * time. This happens on complete idle SMP systems while
210 * waiting on the login prompt. We also increment the "start of
211 * idle" jiffy stamp so the idle accounting adjustment we do
212 * when we go busy again does not account too much ticks.
213 */
214 if (ts->tick_stopped) {
Tejun Heo03e0d462015-12-08 11:28:04 -0500215 touch_softlockup_watchdog_sched();
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200216 if (is_idle_task(current))
217 ts->idle_jiffies++;
Frederic Weisbecker411fe242017-04-21 16:00:54 +0200218 /*
219 * In case the current tick fired too early past its expected
220 * expiration, make sure we don't bypass the next clock reprogramming
221 * to the same deadline.
222 */
223 ts->next_tick = 0;
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200224 }
Frederic Weisbecker94a57142012-10-15 16:17:16 +0200225#endif
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200226 update_process_times(user_mode(regs));
227 profile_tick(CPU_PROFILING);
228}
Arnd Bergmann7809998a2016-01-25 16:41:49 +0100229#endif
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +0200230
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200231#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker460775df2013-07-24 23:52:27 +0200232cpumask_var_t tick_nohz_full_mask;
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200233bool tick_nohz_full_running;
Paul E. McKenneyae9e5572019-08-09 16:29:42 -0700234EXPORT_SYMBOL_GPL(tick_nohz_full_running);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100235static atomic_t tick_dep_mask;
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100236
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100237static bool check_tick_dependency(atomic_t *dep)
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200238{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100239 int val = atomic_read(dep);
240
241 if (val & TICK_DEP_MASK_POSIX_TIMER) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100242 trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100243 return true;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200244 }
245
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100246 if (val & TICK_DEP_MASK_PERF_EVENTS) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100247 trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100248 return true;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200249 }
250
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100251 if (val & TICK_DEP_MASK_SCHED) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100252 trace_tick_stop(0, TICK_DEP_MASK_SCHED);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100253 return true;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200254 }
255
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100256 if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100257 trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100258 return true;
259 }
260
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200261 if (val & TICK_DEP_MASK_RCU) {
262 trace_tick_stop(0, TICK_DEP_MASK_RCU);
263 return true;
264 }
265
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100266 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200267}
268
Wanpeng Li57ccdf42016-09-07 18:51:13 +0800269static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
Frederic Weisbecker9014c452013-04-20 15:43:57 +0200270{
Frederic Weisbeckerebf3adb2017-11-06 16:01:20 +0100271 lockdep_assert_irqs_disabled();
Frederic Weisbecker9014c452013-04-20 15:43:57 +0200272
Wanpeng Li57ccdf42016-09-07 18:51:13 +0800273 if (unlikely(!cpu_online(cpu)))
274 return false;
275
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100276 if (check_tick_dependency(&tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200277 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200278
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100279 if (check_tick_dependency(&ts->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200280 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200281
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100282 if (check_tick_dependency(&current->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200283 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200284
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100285 if (check_tick_dependency(&current->signal->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200286 return false;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200287
Frederic Weisbecker9014c452013-04-20 15:43:57 +0200288 return true;
289}
290
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200291static void nohz_full_kick_func(struct irq_work *work)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200292{
Frederic Weisbecker73738a92015-05-27 19:22:08 +0200293 /* Empty, the tick restart happens on tick_nohz_irq_exit() */
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200294}
295
Peter Zijlstra7a9f50a2020-06-15 11:51:29 +0200296static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) =
297 IRQ_WORK_INIT_HARD(nohz_full_kick_func);
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200298
299/*
Frederic Weisbecker40bea032014-08-13 18:50:16 +0200300 * Kick this CPU if it's full dynticks in order to force it to
301 * re-evaluate its dependency on the tick and restart it if necessary.
302 * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
303 * is NMI safe.
304 */
Frederic Weisbecker555e0c12015-07-16 17:42:29 +0200305static void tick_nohz_full_kick(void)
Frederic Weisbecker40bea032014-08-13 18:50:16 +0200306{
307 if (!tick_nohz_full_cpu(smp_processor_id()))
308 return;
309
Christoph Lameter56e4dea2014-10-27 10:49:45 -0500310 irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
Frederic Weisbecker40bea032014-08-13 18:50:16 +0200311}
312
313/*
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200314 * Kick the CPU if it's full dynticks in order to force it to
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200315 * re-evaluate its dependency on the tick and restart it if necessary.
316 */
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200317void tick_nohz_full_kick_cpu(int cpu)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200318{
Frederic Weisbecker3d36aeb2014-06-04 16:17:33 +0200319 if (!tick_nohz_full_cpu(cpu))
320 return;
321
322 irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200323}
324
Frederic Weisbecker29721b82021-05-13 01:29:20 +0200325static void tick_nohz_kick_task(struct task_struct *tsk)
326{
Marcelo Tosattia1dfb632021-05-13 01:29:22 +0200327 int cpu;
328
329 /*
330 * If the task is not running, run_posix_cpu_timers()
331 * has nothing to elapse, IPI can then be spared.
332 *
333 * activate_task() STORE p->tick_dep_mask
334 * STORE p->on_rq
335 * __schedule() (switch to task 'p') smp_mb() (atomic_fetch_or())
336 * LOCK rq->lock LOAD p->on_rq
337 * smp_mb__after_spin_lock()
338 * tick_nohz_task_switch()
339 * LOAD p->tick_dep_mask
340 */
341 if (!sched_task_on_rq(tsk))
342 return;
Frederic Weisbecker29721b82021-05-13 01:29:20 +0200343
344 /*
345 * If the task concurrently migrates to another CPU,
346 * we guarantee it sees the new tick dependency upon
347 * schedule.
348 *
Frederic Weisbecker29721b82021-05-13 01:29:20 +0200349 * set_task_cpu(p, cpu);
350 * STORE p->cpu = @cpu
351 * __schedule() (switch to task 'p')
352 * LOCK rq->lock
353 * smp_mb__after_spin_lock() STORE p->tick_dep_mask
354 * tick_nohz_task_switch() smp_mb() (atomic_fetch_or())
355 * LOAD p->tick_dep_mask LOAD p->cpu
356 */
Marcelo Tosattia1dfb632021-05-13 01:29:22 +0200357 cpu = task_cpu(tsk);
Frederic Weisbecker29721b82021-05-13 01:29:20 +0200358
359 preempt_disable();
360 if (cpu_online(cpu))
361 tick_nohz_full_kick_cpu(cpu);
362 preempt_enable();
363}
364
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200365/*
366 * Kick all full dynticks CPUs in order to force these to re-evaluate
367 * their dependency on the tick and restart it if necessary.
368 */
Frederic Weisbeckerb7878302015-07-17 22:25:49 +0200369static void tick_nohz_full_kick_all(void)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200370{
Frederic Weisbecker8537bb92015-12-07 16:55:23 +0100371 int cpu;
372
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200373 if (!tick_nohz_full_running)
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200374 return;
375
376 preempt_disable();
Frederic Weisbecker8537bb92015-12-07 16:55:23 +0100377 for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
378 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker76c24fb2013-04-18 00:15:40 +0200379 preempt_enable();
380}
381
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100382static void tick_nohz_dep_set_all(atomic_t *dep,
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200383 enum tick_dep_bits bit)
384{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100385 int prev;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200386
Peter Zijlstraa1cc5bc2016-04-21 20:35:25 +0200387 prev = atomic_fetch_or(BIT(bit), dep);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200388 if (!prev)
389 tick_nohz_full_kick_all();
390}
391
392/*
393 * Set a global tick dependency. Used by perf events that rely on freq and
394 * by unstable clock.
395 */
396void tick_nohz_dep_set(enum tick_dep_bits bit)
397{
398 tick_nohz_dep_set_all(&tick_dep_mask, bit);
399}
400
401void tick_nohz_dep_clear(enum tick_dep_bits bit)
402{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100403 atomic_andnot(BIT(bit), &tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200404}
405
406/*
407 * Set per-CPU tick dependency. Used by scheduler and perf events in order to
408 * manage events throttling.
409 */
410void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
411{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100412 int prev;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200413 struct tick_sched *ts;
414
415 ts = per_cpu_ptr(&tick_cpu_sched, cpu);
416
Peter Zijlstraa1cc5bc2016-04-21 20:35:25 +0200417 prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200418 if (!prev) {
419 preempt_disable();
420 /* Perf needs local kick that is NMI safe */
421 if (cpu == smp_processor_id()) {
422 tick_nohz_full_kick();
423 } else {
424 /* Remote irq work not NMI-safe */
425 if (!WARN_ON_ONCE(in_nmi()))
426 tick_nohz_full_kick_cpu(cpu);
427 }
428 preempt_enable();
429 }
430}
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200431EXPORT_SYMBOL_GPL(tick_nohz_dep_set_cpu);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200432
433void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
434{
435 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
436
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100437 atomic_andnot(BIT(bit), &ts->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200438}
Frederic Weisbecker01b4c392019-07-24 15:22:59 +0200439EXPORT_SYMBOL_GPL(tick_nohz_dep_clear_cpu);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200440
441/*
Frederic Weisbecker3c8920e2020-05-15 02:34:29 +0200442 * Set a per-task tick dependency. RCU need this. Also posix CPU timers
443 * in order to elapse per task timers.
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200444 */
445void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
446{
Frederic Weisbecker29721b82021-05-13 01:29:20 +0200447 if (!atomic_fetch_or(BIT(bit), &tsk->tick_dep_mask))
448 tick_nohz_kick_task(tsk);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200449}
Paul E. McKenneyae9e5572019-08-09 16:29:42 -0700450EXPORT_SYMBOL_GPL(tick_nohz_dep_set_task);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200451
452void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
453{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100454 atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200455}
Paul E. McKenneyae9e5572019-08-09 16:29:42 -0700456EXPORT_SYMBOL_GPL(tick_nohz_dep_clear_task);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200457
458/*
459 * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
460 * per process timers.
461 */
Marcelo Tosatti1e4ca262021-05-13 01:29:21 +0200462void tick_nohz_dep_set_signal(struct task_struct *tsk,
463 enum tick_dep_bits bit)
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200464{
Marcelo Tosatti1e4ca262021-05-13 01:29:21 +0200465 int prev;
466 struct signal_struct *sig = tsk->signal;
467
468 prev = atomic_fetch_or(BIT(bit), &sig->tick_dep_mask);
469 if (!prev) {
470 struct task_struct *t;
471
472 lockdep_assert_held(&tsk->sighand->siglock);
473 __for_each_thread(sig, t)
474 tick_nohz_kick_task(t);
475 }
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200476}
477
478void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
479{
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100480 atomic_andnot(BIT(bit), &sig->tick_dep_mask);
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200481}
482
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200483/*
484 * Re-evaluate the need for the tick as we switch the current task.
485 * It might need the tick due to per task/process properties:
Ingo Molnar0de76112016-07-01 12:42:35 +0200486 * perf events, posix CPU timers, ...
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200487 */
Frederic Weisbeckerde734f82015-06-11 18:07:12 +0200488void __tick_nohz_task_switch(void)
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200489{
490 unsigned long flags;
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200491 struct tick_sched *ts;
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200492
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200493 local_irq_save(flags);
494
Li Zhong6296ace2013-04-28 11:25:58 +0800495 if (!tick_nohz_full_cpu(smp_processor_id()))
496 goto out;
497
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200498 ts = this_cpu_ptr(&tick_cpu_sched);
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200499
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200500 if (ts->tick_stopped) {
Frederic Weisbeckerf009a7a2016-03-24 15:38:00 +0100501 if (atomic_read(&current->tick_dep_mask) ||
502 atomic_read(&current->signal->tick_dep_mask))
Frederic Weisbeckerd027d452015-06-07 15:54:30 +0200503 tick_nohz_full_kick();
504 }
Li Zhong6296ace2013-04-28 11:25:58 +0800505out:
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +0200506 local_irq_restore(flags);
507}
508
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200509/* Get the boot-time nohz CPU list from the kernel parameters. */
510void __init tick_nohz_full_setup(cpumask_var_t cpumask)
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100511{
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200512 alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200513 cpumask_copy(tick_nohz_full_mask, cpumask);
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200514 tick_nohz_full_running = true;
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100515}
Paul E. McKenneyae9e5572019-08-09 16:29:42 -0700516EXPORT_SYMBOL_GPL(tick_nohz_full_setup);
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100517
Sebastian Andrzej Siewior31eff2432016-11-17 19:35:34 +0100518static int tick_nohz_cpu_down(unsigned int cpu)
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100519{
Sebastian Andrzej Siewior31eff2432016-11-17 19:35:34 +0100520 /*
Nicholas Piggin08ae95f2019-04-11 13:34:48 +1000521 * The tick_do_timer_cpu CPU handles housekeeping duty (unbound
522 * timers, workqueues, timekeeping, ...) on behalf of full dynticks
Sebastian Andrzej Siewior31eff2432016-11-17 19:35:34 +0100523 * CPUs. It must remain online when nohz full is enabled.
524 */
525 if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
526 return -EBUSY;
527 return 0;
Frederic Weisbeckera382bf92012-12-18 18:24:35 +0100528}
529
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100530void __init tick_nohz_init(void)
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100531{
Sebastian Andrzej Siewior31eff2432016-11-17 19:35:34 +0100532 int cpu, ret;
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100533
Paul E. McKenneya7c86552017-11-30 15:36:35 -0800534 if (!tick_nohz_full_running)
535 return;
Frederic Weisbeckerd1e43fa2013-03-26 23:47:24 +0100536
Frederic Weisbecker9b01f5b2014-08-18 01:36:07 +0200537 /*
538 * Full dynticks uses irq work to drive the tick rescheduling on safe
539 * locking contexts. But then we need irq work to raise its own
540 * interrupts to avoid circular dependency on the tick
541 */
542 if (!arch_irq_work_has_interrupt()) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700543 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 +0200544 cpumask_clear(tick_nohz_full_mask);
Frederic Weisbecker9b01f5b2014-08-18 01:36:07 +0200545 tick_nohz_full_running = false;
546 return;
547 }
548
Nicholas Piggin08ae95f2019-04-11 13:34:48 +1000549 if (IS_ENABLED(CONFIG_PM_SLEEP_SMP) &&
550 !IS_ENABLED(CONFIG_PM_SLEEP_SMP_NONZERO_CPU)) {
551 cpu = smp_processor_id();
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200552
Nicholas Piggin08ae95f2019-04-11 13:34:48 +1000553 if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
554 pr_warn("NO_HZ: Clearing %d from nohz_full range "
555 "for timekeeping\n", cpu);
556 cpumask_clear_cpu(cpu, tick_nohz_full_mask);
557 }
Frederic Weisbecker4327b152014-08-17 22:02:55 +0200558 }
559
Frederic Weisbecker73867dc2013-07-24 23:31:00 +0200560 for_each_cpu(cpu, tick_nohz_full_mask)
Frederic Weisbecker2e709332013-07-10 00:55:25 +0200561 context_tracking_cpu_set(cpu);
562
Sebastian Andrzej Siewior31eff2432016-11-17 19:35:34 +0100563 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
564 "kernel/nohz:predown", NULL,
565 tick_nohz_cpu_down);
566 WARN_ON(ret < 0);
Tejun Heoffda22c2015-02-13 14:37:31 -0800567 pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
568 cpumask_pr_args(tick_nohz_full_mask));
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100569}
Frederic Weisbeckera8318812012-12-18 17:32:19 +0100570#endif
571
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800572/*
573 * NOHZ - aka dynamic tick functionality
574 */
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200575#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800576/*
577 * NO HZ enabled ?
578 */
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700579bool tick_nohz_enabled __read_mostly = true;
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000580unsigned long tick_nohz_active __read_mostly;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800581/*
582 * Enable / Disable tickless mode
583 */
584static int __init setup_tick_nohz(char *str)
585{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700586 return (kstrtobool(str, &tick_nohz_enabled) == 0);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800587}
588
589__setup("nohz=", setup_tick_nohz);
590
Frederic Weisbeckera3642982018-02-21 05:17:24 +0100591bool tick_nohz_tick_stopped(void)
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100592{
Frederic Weisbecker2bc629a2018-04-06 04:32:37 +0200593 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
594
595 return ts->tick_stopped;
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +0100596}
597
Frederic Weisbecker22ab8bc2018-02-21 05:17:25 +0100598bool tick_nohz_tick_stopped_cpu(int cpu)
599{
600 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
601
602 return ts->tick_stopped;
603}
604
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800605/**
606 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
607 *
608 * Called from interrupt entry when the CPU was idle
609 *
610 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
611 * must be updated. Otherwise an interrupt handler could use a stale jiffy
Ingo Molnar0de76112016-07-01 12:42:35 +0200612 * value. We do this unconditionally on any CPU, as we don't know whether the
613 * CPU, which has the update task assigned is in a long sleep.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800614 */
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200615static void tick_nohz_update_jiffies(ktime_t now)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800616{
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800617 unsigned long flags;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800618
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200619 __this_cpu_write(tick_cpu_sched.idle_waketime, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800620
621 local_irq_save(flags);
622 tick_do_update_jiffies64(now);
623 local_irq_restore(flags);
Ingo Molnar02ff3752008-05-12 15:43:53 +0200624
Tejun Heo03e0d462015-12-08 11:28:04 -0500625 touch_softlockup_watchdog_sched();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800626}
627
Arjan van de Ven595aac42010-05-09 08:22:45 -0700628/*
Ingo Molnar0de76112016-07-01 12:42:35 +0200629 * Updates the per-CPU time idle statistics counters
Arjan van de Ven595aac42010-05-09 08:22:45 -0700630 */
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700631static void
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200632update_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 -0700633{
634 ktime_t delta;
635
Arjan van de Ven595aac42010-05-09 08:22:45 -0700636 if (ts->idle_active) {
637 delta = ktime_sub(now, ts->idle_entrytime);
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200638 if (nr_iowait_cpu(cpu) > 0)
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700639 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
Michal Hocko6beea0c2011-08-24 09:37:48 +0200640 else
641 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
Arjan van de Ven8c7b09f2010-05-09 08:23:23 -0700642 ts->idle_entrytime = now;
Arjan van de Ven595aac42010-05-09 08:22:45 -0700643 }
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700644
Arjan van de Vene0e37c22010-05-09 08:24:39 -0700645 if (last_update_time)
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700646 *last_update_time = ktime_to_us(now);
647
Arjan van de Ven595aac42010-05-09 08:22:45 -0700648}
649
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200650static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100651{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200652 update_ts_time_stats(smp_processor_id(), ts, now, NULL);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200653 ts->idle_active = 0;
Peter Zijlstra56c74262008-09-01 16:44:23 +0200654
Peter Zijlstraac1e8432017-04-21 12:26:23 +0200655 sched_clock_idle_wakeup_event();
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100656}
657
Rafael J. Wysocki0e776762018-04-05 18:58:27 +0200658static void tick_nohz_start_idle(struct tick_sched *ts)
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100659{
Rafael J. Wysocki0e776762018-04-05 18:58:27 +0200660 ts->idle_entrytime = ktime_get();
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100661 ts->idle_active = 1;
Peter Zijlstra56c74262008-09-01 16:44:23 +0200662 sched_clock_idle_sleep_event();
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100663}
664
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700665/**
Ingo Molnar0de76112016-07-01 12:42:35 +0200666 * get_cpu_idle_time_us - get the total idle time of a CPU
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700667 * @cpu: CPU number to query
Michal Hocko09a1d342011-08-24 09:39:30 +0200668 * @last_update_time: variable to store update time in. Do not update
669 * counters if NULL.
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700670 *
Wei Jiangang6168f8e2016-06-29 12:51:50 +0800671 * Return the cumulative idle time (since boot) for a given
Michal Hocko6beea0c2011-08-24 09:37:48 +0200672 * CPU, in microseconds.
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700673 *
674 * This time is measured via accounting rather than sampling,
675 * and is as accurate as ktime_get() is.
676 *
677 * This function returns -1 if NOHZ is not enabled.
678 */
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100679u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
680{
681 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
Michal Hocko09a1d342011-08-24 09:39:30 +0200682 ktime_t now, idle;
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100683
Thomas Gleixnerd689fe22013-11-13 21:01:57 +0100684 if (!tick_nohz_active)
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700685 return -1;
686
Michal Hocko09a1d342011-08-24 09:39:30 +0200687 now = ktime_get();
688 if (last_update_time) {
689 update_ts_time_stats(cpu, ts, now, last_update_time);
690 idle = ts->idle_sleeptime;
691 } else {
692 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
693 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700694
Michal Hocko09a1d342011-08-24 09:39:30 +0200695 idle = ktime_add(ts->idle_sleeptime, delta);
696 } else {
697 idle = ts->idle_sleeptime;
698 }
699 }
700
701 return ktime_to_us(idle);
702
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100703}
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700704EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100705
Michal Hocko6beea0c2011-08-24 09:37:48 +0200706/**
Ingo Molnar0de76112016-07-01 12:42:35 +0200707 * get_cpu_iowait_time_us - get the total iowait time of a CPU
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700708 * @cpu: CPU number to query
Michal Hocko09a1d342011-08-24 09:39:30 +0200709 * @last_update_time: variable to store update time in. Do not update
710 * counters if NULL.
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700711 *
Wei Jiangang6168f8e2016-06-29 12:51:50 +0800712 * Return the cumulative iowait time (since boot) for a given
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700713 * CPU, in microseconds.
714 *
715 * This time is measured via accounting rather than sampling,
716 * and is as accurate as ktime_get() is.
717 *
718 * This function returns -1 if NOHZ is not enabled.
719 */
720u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
721{
722 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
Michal Hocko09a1d342011-08-24 09:39:30 +0200723 ktime_t now, iowait;
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700724
Thomas Gleixnerd689fe22013-11-13 21:01:57 +0100725 if (!tick_nohz_active)
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700726 return -1;
727
Michal Hocko09a1d342011-08-24 09:39:30 +0200728 now = ktime_get();
729 if (last_update_time) {
730 update_ts_time_stats(cpu, ts, now, last_update_time);
731 iowait = ts->iowait_sleeptime;
732 } else {
733 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
734 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700735
Michal Hocko09a1d342011-08-24 09:39:30 +0200736 iowait = ktime_add(ts->iowait_sleeptime, delta);
737 } else {
738 iowait = ts->iowait_sleeptime;
739 }
740 }
741
742 return ktime_to_us(iowait);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700743}
744EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
745
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000746static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
747{
748 hrtimer_cancel(&ts->sched_timer);
749 hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
750
751 /* Forward the time to expire in the future */
Thomas Gleixnerb9965442020-11-17 14:19:49 +0100752 hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000753
Sebastian Andrzej Siewior902a9f92019-07-26 20:30:56 +0200754 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
755 hrtimer_start_expires(&ts->sched_timer,
756 HRTIMER_MODE_ABS_PINNED_HARD);
757 } else {
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000758 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
Sebastian Andrzej Siewior902a9f92019-07-26 20:30:56 +0200759 }
Frederic Weisbecker411fe242017-04-21 16:00:54 +0200760
761 /*
762 * Reset to make sure next tick stop doesn't get fooled by past
763 * cached clock deadline.
764 */
765 ts->next_tick = 0;
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000766}
767
Thomas Gleixner5d62c182017-12-22 15:51:13 +0100768static inline bool local_timer_softirq_pending(void)
769{
Anna-Maria Gleixner80d20d32018-07-31 18:13:58 +0200770 return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
Thomas Gleixner5d62c182017-12-22 15:51:13 +0100771}
772
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200773static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800774{
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000775 u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
Rasmus Villemoese1e41b62019-03-18 20:55:56 +0100776 unsigned long basejiff;
777 unsigned int seq;
Frederic Weisbecker855a0fc2013-12-17 00:16:37 +0100778
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800779 /* Read jiffies and the time when jiffies were updated last */
780 do {
Thomas Gleixnere5d4d172020-03-21 12:25:58 +0100781 seq = read_seqcount_begin(&jiffies_seq);
Thomas Gleixner2456e852016-12-25 11:38:40 +0100782 basemono = last_jiffies_update;
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000783 basejiff = jiffies;
Thomas Gleixnere5d4d172020-03-21 12:25:58 +0100784 } while (read_seqcount_retry(&jiffies_seq, seq));
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000785 ts->last_jiffies = basejiff;
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200786 ts->timer_expires_base = basemono;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800787
Thomas Gleixner5d62c182017-12-22 15:51:13 +0100788 /*
789 * Keep the periodic tick, when RCU, architecture or irq_work
790 * requests it.
791 * Aside of that check whether the local timer softirq is
792 * pending. If so its a bad idea to call get_next_timer_interrupt()
793 * because there is an already expired timer, so it will request
Ingo Molnar4bf07f62021-03-22 22:39:03 +0100794 * immediate expiry, which rearms the hardware timer with a
Thomas Gleixner5d62c182017-12-22 15:51:13 +0100795 * minimal delta which brings us back to this place
796 * immediately. Lather, rinse and repeat...
797 */
798 if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
799 irq_work_needs_cpu() || local_timer_softirq_pending()) {
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000800 next_tick = basemono + TICK_NSEC;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200801 } else {
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000802 /*
803 * Get the next pending timer. If high resolution
804 * timers are enabled this only takes the timer wheel
805 * timers into account. If high resolution timers are
806 * disabled this also looks at the next expiring
807 * hrtimer.
808 */
809 next_tmr = get_next_timer_interrupt(basejiff, basemono);
810 ts->next_timer = next_tmr;
811 /* Take the next rcu event into account */
812 next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200813 }
Ingo Molnar47aa8b62013-04-26 10:05:59 +0200814
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000815 /*
816 * If the tick is due in the next period, keep it ticking or
Peter Zijlstra82bbe342015-11-19 17:21:06 +0100817 * force prod the timer.
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000818 */
819 delta = next_tick - basemono;
820 if (delta <= (u64)TICK_NSEC) {
Thomas Gleixnera683f392016-07-04 09:50:36 +0000821 /*
822 * Tell the timer code that the base is not idle, i.e. undo
823 * the effect of get_next_timer_interrupt():
824 */
825 timer_clear_idle();
Peter Zijlstra82bbe342015-11-19 17:21:06 +0100826 /*
827 * We've not stopped the tick yet, and there's a timer in the
828 * next period, so no point in stopping it either, bail.
829 */
Frederic Weisbeckerf99973e2017-06-01 16:47:09 +0200830 if (!ts->tick_stopped) {
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200831 ts->timer_expires = 0;
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200832 goto out;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000833 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800834 }
Thomas Gleixner0ff53d02015-04-14 21:08:54 +0000835
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000836 /*
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200837 * If this CPU is the one which had the do_timer() duty last, we limit
838 * the sleep time to the timekeeping max_deferment value.
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000839 * Otherwise we can sleep as long as we want.
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000840 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000841 delta = timekeeping_max_deferment();
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200842 if (cpu != tick_do_timer_cpu &&
843 (tick_do_timer_cpu != TICK_DO_TIMER_NONE || !ts->do_timer_last))
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000844 delta = KTIME_MAX;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000845
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000846 /* Calculate the next expiry time */
847 if (delta < (KTIME_MAX - basemono))
848 expires = basemono + delta;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000849 else
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000850 expires = KTIME_MAX;
851
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200852 ts->timer_expires = min_t(u64, expires, next_tick);
853
854out:
855 return ts->timer_expires;
856}
857
858static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
859{
860 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
861 u64 basemono = ts->timer_expires_base;
862 u64 expires = ts->timer_expires;
863 ktime_t tick = expires;
864
865 /* Make sure we won't be trying to stop it twice in a row. */
866 ts->timer_expires_base = 0;
867
868 /*
869 * If this CPU is the one which updates jiffies, then give up
870 * the assignment and let it be taken by the CPU which runs
871 * the tick timer next, which might be this CPU as well. If we
872 * don't drop this here the jiffies might be stale and
873 * do_timer() never invoked. Keep track of the fact that it
874 * was the one which had the do_timer() duty last.
875 */
876 if (cpu == tick_do_timer_cpu) {
877 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
878 ts->do_timer_last = 1;
879 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
880 ts->do_timer_last = 0;
881 }
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000882
883 /* Skip reprogram of event if its not changed */
Frederic Weisbecker411fe242017-04-21 16:00:54 +0200884 if (ts->tick_stopped && (expires == ts->next_tick)) {
885 /* Sanity check: make sure clockevent is actually programmed */
Frederic Weisbeckerd4af6d92017-06-13 06:04:14 +0200886 if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200887 return;
Frederic Weisbecker411fe242017-04-21 16:00:54 +0200888
889 WARN_ON_ONCE(1);
890 printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
891 basemono, ts->next_tick, dev->next_event,
892 hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
Frederic Weisbeckerce6cf9a2017-05-11 16:36:19 +0200893 }
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000894
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000895 /*
896 * nohz_stop_sched_tick can be called several times before
897 * the nohz_restart_sched_tick is called. This happens when
898 * interrupts arrive which do not cause a reschedule. In the
899 * first call we save the current tick time, so we can restart
900 * the scheduler tick in nohz_restart_sched_tick.
901 */
902 if (!ts->tick_stopped) {
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200903 calc_load_nohz_start();
Peter Zijlstra62cb1182017-08-29 15:07:54 +0200904 quiet_vmstat();
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000905
906 ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
907 ts->tick_stopped = 1;
Frederic Weisbeckere6e6cc22015-12-11 03:27:25 +0100908 trace_tick_stop(1, TICK_DEP_MASK_NONE);
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000909 }
910
Frederic Weisbecker411fe242017-04-21 16:00:54 +0200911 ts->next_tick = tick;
912
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000913 /*
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000914 * If the expiration time == KTIME_MAX, then we simply stop
915 * the tick timer.
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000916 */
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000917 if (unlikely(expires == KTIME_MAX)) {
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000918 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
919 hrtimer_cancel(&ts->sched_timer);
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200920 return;
Thomas Gleixner157d29e2015-04-14 21:08:56 +0000921 }
922
Thomas Gleixner1f71add2018-04-24 21:22:18 +0200923 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
Sebastian Andrzej Siewior902a9f92019-07-26 20:30:56 +0200924 hrtimer_start(&ts->sched_timer, tick,
925 HRTIMER_MODE_ABS_PINNED_HARD);
Thomas Gleixner1f71add2018-04-24 21:22:18 +0200926 } else {
927 hrtimer_set_expires(&ts->sched_timer, tick);
Thomas Gleixnerc1ad3482015-04-14 21:08:58 +0000928 tick_program_event(tick, 1);
Thomas Gleixner1f71add2018-04-24 21:22:18 +0200929 }
Frederic Weisbecker280f0672011-10-07 18:22:06 +0200930}
931
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +0200932static void tick_nohz_retain_tick(struct tick_sched *ts)
933{
934 ts->timer_expires_base = 0;
935}
936
937#ifdef CONFIG_NO_HZ_FULL
938static void tick_nohz_stop_sched_tick(struct tick_sched *ts, int cpu)
939{
940 if (tick_nohz_next_event(ts, cpu))
941 tick_nohz_stop_tick(ts, cpu);
942 else
943 tick_nohz_retain_tick(ts);
944}
945#endif /* CONFIG_NO_HZ_FULL */
946
Frederic Weisbecker1f419062016-04-13 15:56:51 +0200947static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200948{
949 /* Update jiffies first */
950 tick_do_update_jiffies64(now);
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200951
Thomas Gleixnera683f392016-07-04 09:50:36 +0000952 /*
953 * Clear the timer idle flag, so we avoid IPIs on remote queueing and
954 * the clock forward checks in the enqueue path:
955 */
956 timer_clear_idle();
957
Frederic Weisbecker3c85d6d2017-06-19 04:12:00 +0200958 calc_load_nohz_stop();
Tejun Heo03e0d462015-12-08 11:28:04 -0500959 touch_softlockup_watchdog_sched();
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200960 /*
961 * Cancel the scheduled timer and restore the tick
962 */
963 ts->tick_stopped = 0;
Frederic Weisbecker59d2c7c2015-05-29 14:42:15 +0200964 tick_nohz_restart(ts, now);
965}
Frederic Weisbecker73738a92015-05-27 19:22:08 +0200966
Yunfeng Yea5183862021-05-13 01:29:16 +0200967static void __tick_nohz_full_update_tick(struct tick_sched *ts,
968 ktime_t now)
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200969{
970#ifdef CONFIG_NO_HZ_FULL
Alex Shie9a2eb42013-11-28 14:27:11 +0800971 int cpu = smp_processor_id();
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200972
Yunfeng Yea5183862021-05-13 01:29:16 +0200973 if (can_stop_full_tick(cpu, ts))
974 tick_nohz_stop_sched_tick(ts, cpu);
975 else if (ts->tick_stopped)
976 tick_nohz_restart_sched_tick(ts, now);
977#endif
978}
979
980static void tick_nohz_full_update_tick(struct tick_sched *ts)
981{
982 if (!tick_nohz_full_cpu(smp_processor_id()))
Alex Shie9a2eb42013-11-28 14:27:11 +0800983 return;
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200984
Alex Shie9a2eb42013-11-28 14:27:11 +0800985 if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
986 return;
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200987
Yunfeng Yea5183862021-05-13 01:29:16 +0200988 __tick_nohz_full_update_tick(ts, ktime_get());
Frederic Weisbecker5811d992013-04-20 16:40:31 +0200989}
990
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200991static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
992{
993 /*
Ingo Molnar0de76112016-07-01 12:42:35 +0200994 * If this CPU is offline and it is the one which updates
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200995 * jiffies, then give up the assignment and let it be taken by
Ingo Molnar0de76112016-07-01 12:42:35 +0200996 * the CPU which runs the tick timer next. If we don't drop
Frederic Weisbecker5b399392011-08-01 00:06:10 +0200997 * this here the jiffies might be stale and do_timer() never
998 * invoked.
999 */
1000 if (unlikely(!cpu_online(cpu))) {
1001 if (cpu == tick_do_timer_cpu)
1002 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
Frederic Weisbecker411fe242017-04-21 16:00:54 +02001003 /*
1004 * Make sure the CPU doesn't get fooled by obsolete tick
1005 * deadline if it comes back online later.
1006 */
1007 ts->next_tick = 0;
Thomas Gleixnerf7ea0fd2013-05-13 21:40:27 +02001008 return false;
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001009 }
1010
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001011 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001012 return false;
1013
1014 if (need_resched())
1015 return false;
1016
Peng Haod59e0ba2018-10-09 11:43:35 -04001017 if (unlikely(local_softirq_pending())) {
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001018 static int ratelimit;
1019
Thomas Gleixner47c218d2021-03-09 09:55:57 +01001020 if (ratelimit < 10 && !local_bh_blocked() &&
Paul E. McKenney803b0eb2012-08-23 08:34:07 -07001021 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
Paul E. McKenneybca37112020-06-26 13:39:41 -07001022 pr_warn("NOHZ tick-stop error: Non-RCU local softirq work is pending, handler #%02x!!!\n",
Rado Vrbovskycfea7d72013-02-08 12:37:30 -05001023 (unsigned int) local_softirq_pending());
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001024 ratelimit++;
1025 }
1026 return false;
1027 }
1028
Frederic Weisbecker460775df2013-07-24 23:52:27 +02001029 if (tick_nohz_full_enabled()) {
Frederic Weisbeckera382bf92012-12-18 18:24:35 +01001030 /*
1031 * Keep the tick alive to guarantee timekeeping progression
1032 * if there are full dynticks CPUs around
1033 */
1034 if (tick_do_timer_cpu == cpu)
1035 return false;
Nicholas Piggin08ae95f2019-04-11 13:34:48 +10001036
1037 /* Should not happen for nohz-full */
1038 if (WARN_ON_ONCE(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
Frederic Weisbeckera382bf92012-12-18 18:24:35 +01001039 return false;
1040 }
1041
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001042 return true;
1043}
1044
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001045static void __tick_nohz_idle_stop_tick(struct tick_sched *ts)
Frederic Weisbecker19f5f732011-07-27 17:29:28 +02001046{
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001047 ktime_t expires;
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001048 int cpu = smp_processor_id();
Frederic Weisbecker19f5f732011-07-27 17:29:28 +02001049
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001050 /*
1051 * If tick_nohz_get_sleep_length() ran tick_nohz_next_event(), the
1052 * tick timer expiration time is known already.
1053 */
1054 if (ts->timer_expires_base)
1055 expires = ts->timer_expires;
1056 else if (can_stop_idle_tick(cpu, ts))
1057 expires = tick_nohz_next_event(ts, cpu);
1058 else
1059 return;
Wanpeng Li08d072592016-09-02 14:38:23 +08001060
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001061 ts->idle_calls++;
1062
1063 if (expires > 0LL) {
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001064 int was_stopped = ts->tick_stopped;
1065
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001066 tick_nohz_stop_tick(ts, cpu);
Frederic Weisbecker84bf1bc2011-08-01 01:25:38 +02001067
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001068 ts->idle_sleeps++;
1069 ts->idle_expires = expires;
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001070
Frederic Weisbeckera0db9712017-06-19 04:12:01 +02001071 if (!was_stopped && ts->tick_stopped) {
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001072 ts->idle_jiffies = ts->last_jiffies;
Frederic Weisbeckera0db9712017-06-19 04:12:01 +02001073 nohz_balance_enter_idle(cpu);
1074 }
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001075 } else {
1076 tick_nohz_retain_tick(ts);
Frederic Weisbecker5b399392011-08-01 00:06:10 +02001077 }
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001078}
1079
1080/**
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001081 * tick_nohz_idle_stop_tick - stop the idle tick from the idle task
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001082 *
1083 * When the next event is more than a tick into the future, stop the idle tick
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001084 */
1085void tick_nohz_idle_stop_tick(void)
1086{
1087 __tick_nohz_idle_stop_tick(this_cpu_ptr(&tick_cpu_sched));
1088}
1089
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001090void tick_nohz_idle_retain_tick(void)
1091{
1092 tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched));
1093 /*
1094 * Undo the effect of get_next_timer_interrupt() called from
1095 * tick_nohz_next_event().
1096 */
1097 timer_clear_idle();
1098}
1099
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001100/**
1101 * tick_nohz_idle_enter - prepare for entering idle on the current CPU
1102 *
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001103 * Called when we start the idle loop.
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001104 */
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +01001105void tick_nohz_idle_enter(void)
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001106{
1107 struct tick_sched *ts;
1108
Frederic Weisbeckerebf3adb2017-11-06 16:01:20 +01001109 lockdep_assert_irqs_enabled();
Linus Torvalds0db49b72012-01-06 08:33:28 -08001110
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +01001111 local_irq_disable();
1112
Christoph Lameter22127e92014-08-17 12:30:25 -05001113 ts = this_cpu_ptr(&tick_cpu_sched);
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001114
1115 WARN_ON_ONCE(ts->timer_expires_base);
1116
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001117 ts->inidle = 1;
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001118 tick_nohz_start_idle(ts);
Frederic Weisbecker1268fbc2011-11-17 18:48:14 +01001119
1120 local_irq_enable();
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001121}
1122
1123/**
1124 * tick_nohz_irq_exit - update next tick event from interrupt exit
1125 *
1126 * When an interrupt fires while we are idle and it doesn't cause
1127 * a reschedule, it may still add, modify or delete a timer, enqueue
1128 * an RCU callback, etc...
1129 * So we need to re-calculate and reprogram the next tick event.
1130 */
1131void tick_nohz_irq_exit(void)
1132{
Christoph Lameter22127e92014-08-17 12:30:25 -05001133 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Frederic Weisbecker280f0672011-10-07 18:22:06 +02001134
Rafael J. Wysocki14851912013-07-27 01:41:34 +02001135 if (ts->inidle)
Rafael J. Wysocki0e776762018-04-05 18:58:27 +02001136 tick_nohz_start_idle(ts);
Rafael J. Wysocki14851912013-07-27 01:41:34 +02001137 else
Frederic Weisbecker73738a92015-05-27 19:22:08 +02001138 tick_nohz_full_update_tick(ts);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001139}
1140
1141/**
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +01001142 * tick_nohz_idle_got_tick - Check whether or not the tick handler has run
Len Brown4f86d3a2007-10-03 18:58:00 -04001143 */
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +01001144bool tick_nohz_idle_got_tick(void)
Len Brown4f86d3a2007-10-03 18:58:00 -04001145{
Christoph Lameter22127e92014-08-17 12:30:25 -05001146 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Len Brown4f86d3a2007-10-03 18:58:00 -04001147
Frederic Weisbecker2bc629a2018-04-06 04:32:37 +02001148 if (ts->got_idle_tick) {
1149 ts->got_idle_tick = 0;
Rafael J. Wysocki45f1ff52018-03-22 17:50:49 +01001150 return true;
1151 }
1152 return false;
1153}
1154
1155/**
Ulf Hansson6f9b83a2019-03-27 15:35:47 +01001156 * tick_nohz_get_next_hrtimer - return the next expiration time for the hrtimer
1157 * or the tick, whatever that expires first. Note that, if the tick has been
1158 * stopped, it returns the next hrtimer.
1159 *
1160 * Called from power state control code with interrupts disabled
1161 */
1162ktime_t tick_nohz_get_next_hrtimer(void)
1163{
1164 return __this_cpu_read(tick_cpu_device.evtdev)->next_event;
1165}
1166
1167/**
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001168 * tick_nohz_get_sleep_length - return the expected length of the current sleep
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +02001169 * @delta_next: duration until the next event if the tick cannot be stopped
Len Brown4f86d3a2007-10-03 18:58:00 -04001170 *
Rafael J. Wysocki4c81cb72021-03-29 20:13:37 +02001171 * Called from power state control code with interrupts disabled.
1172 *
1173 * The return value of this function and/or the value returned by it through the
1174 * @delta_next pointer can be negative which must be taken into account by its
1175 * callers.
Len Brown4f86d3a2007-10-03 18:58:00 -04001176 */
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +02001177ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
Len Brown4f86d3a2007-10-03 18:58:00 -04001178{
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001179 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001180 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001181 int cpu = smp_processor_id();
1182 /*
1183 * The idle entry time is expected to be a sufficient approximation of
1184 * the current time at this point.
1185 */
1186 ktime_t now = ts->idle_entrytime;
1187 ktime_t next_event;
Len Brown4f86d3a2007-10-03 18:58:00 -04001188
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001189 WARN_ON_ONCE(!ts->inidle);
1190
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +02001191 *delta_next = ktime_sub(dev->next_event, now);
1192
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001193 if (!can_stop_idle_tick(cpu, ts))
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +02001194 return *delta_next;
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001195
1196 next_event = tick_nohz_next_event(ts, cpu);
1197 if (!next_event)
Rafael J. Wysocki296bb1e2018-04-05 19:12:34 +02001198 return *delta_next;
Rafael J. Wysocki554c8aa2018-04-03 23:17:11 +02001199
1200 /*
1201 * If the next highres timer to expire is earlier than next_event, the
1202 * idle governor needs to know that.
1203 */
1204 next_event = min_t(u64, next_event,
1205 hrtimer_next_event_without(&ts->sched_timer));
1206
1207 return ktime_sub(next_event, now);
Len Brown4f86d3a2007-10-03 18:58:00 -04001208}
1209
Rafael J. Wysockib7eaf1a2017-03-22 00:08:50 +01001210/**
Joel Fernandes466a2b42017-12-21 02:22:45 +01001211 * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
1212 * for a particular CPU.
1213 *
1214 * Called from the schedutil frequency scaling governor in scheduler context.
1215 */
1216unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
1217{
1218 struct tick_sched *ts = tick_get_tick_sched(cpu);
1219
1220 return ts->idle_calls;
1221}
1222
1223/**
Rafael J. Wysockib7eaf1a2017-03-22 00:08:50 +01001224 * tick_nohz_get_idle_calls - return the current idle calls counter value
1225 *
1226 * Called from the schedutil frequency scaling governor in scheduler context.
1227 */
1228unsigned long tick_nohz_get_idle_calls(void)
1229{
1230 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1231
1232 return ts->idle_calls;
1233}
1234
Yunfeng Ye96c9b902021-05-13 01:29:18 +02001235static void tick_nohz_account_idle_time(struct tick_sched *ts,
1236 ktime_t now)
Frederic Weisbecker2ac0d982011-07-28 04:00:47 +02001237{
Frederic Weisbecker2ac0d982011-07-28 04:00:47 +02001238 unsigned long ticks;
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +02001239
Yunfeng Ye96c9b902021-05-13 01:29:18 +02001240 ts->idle_exittime = now;
1241
Frederic Weisbeckere44fcb42019-10-16 04:56:54 +02001242 if (vtime_accounting_enabled_this_cpu())
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +02001243 return;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001244 /*
1245 * We stopped the tick in idle. Update process times would miss the
1246 * time we slept as update_process_times does only a 1 tick
1247 * accounting. Enforce that this is accounted to idle !
1248 */
1249 ticks = jiffies - ts->idle_jiffies;
1250 /*
1251 * We might be one off. Do not randomly account a huge number of ticks!
1252 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01001253 if (ticks && ticks < LONG_MAX)
1254 account_idle_ticks(ticks);
Frederic Weisbecker19f5f732011-07-27 17:29:28 +02001255}
1256
Rafael J. Wysocki2aaf7092018-03-15 23:05:50 +01001257void tick_nohz_idle_restart_tick(void)
1258{
1259 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1260
Yunfeng Yea5183862021-05-13 01:29:16 +02001261 if (ts->tick_stopped) {
Yunfeng Ye96c9b902021-05-13 01:29:18 +02001262 ktime_t now = ktime_get();
1263 tick_nohz_restart_sched_tick(ts, now);
1264 tick_nohz_account_idle_time(ts, now);
Yunfeng Yea5183862021-05-13 01:29:16 +02001265 }
1266}
1267
1268static void tick_nohz_idle_update_tick(struct tick_sched *ts, ktime_t now)
1269{
1270 if (tick_nohz_full_cpu(smp_processor_id()))
1271 __tick_nohz_full_update_tick(ts, now);
1272 else
1273 tick_nohz_restart_sched_tick(ts, now);
1274
Yunfeng Ye96c9b902021-05-13 01:29:18 +02001275 tick_nohz_account_idle_time(ts, now);
Rafael J. Wysocki2aaf7092018-03-15 23:05:50 +01001276}
1277
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001278/**
1279 * tick_nohz_idle_exit - restart the idle tick from the idle task
1280 *
1281 * Restart the idle tick when the CPU is woken up from idle
1282 * This also exit the RCU extended quiescent state. The CPU
1283 * can use RCU again after this function is called.
1284 */
1285void tick_nohz_idle_exit(void)
1286{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05001287 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Arnd Bergmannbbe9a702018-04-09 14:23:33 +02001288 bool idle_active, tick_stopped;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001289 ktime_t now;
1290
1291 local_irq_disable();
1292
1293 WARN_ON_ONCE(!ts->inidle);
Rafael J. Wysocki23a8d882018-04-05 19:07:57 +02001294 WARN_ON_ONCE(ts->timer_expires_base);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001295
1296 ts->inidle = 0;
Arnd Bergmannbbe9a702018-04-09 14:23:33 +02001297 idle_active = ts->idle_active;
1298 tick_stopped = ts->tick_stopped;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001299
Arnd Bergmannbbe9a702018-04-09 14:23:33 +02001300 if (idle_active || tick_stopped)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001301 now = ktime_get();
1302
Arnd Bergmannbbe9a702018-04-09 14:23:33 +02001303 if (idle_active)
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +02001304 tick_nohz_stop_idle(ts, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001305
Arnd Bergmannbbe9a702018-04-09 14:23:33 +02001306 if (tick_stopped)
Yunfeng Yea5183862021-05-13 01:29:16 +02001307 tick_nohz_idle_update_tick(ts, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001308
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001309 local_irq_enable();
1310}
1311
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001312/*
1313 * The nohz low res interrupt handler
1314 */
1315static void tick_nohz_handler(struct clock_event_device *dev)
1316{
Christoph Lameter22127e92014-08-17 12:30:25 -05001317 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001318 struct pt_regs *regs = get_irq_regs();
1319 ktime_t now = ktime_get();
1320
Thomas Gleixner2456e852016-12-25 11:38:40 +01001321 dev->next_event = KTIME_MAX;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001322
Rafael J. Wysockiff7de622018-04-06 14:59:13 +02001323 tick_sched_do_timer(ts, now);
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +02001324 tick_sched_handle(ts, regs);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001325
Viresh Kumarb5e995e2014-06-12 16:24:41 +05301326 /* No need to reprogram if we are running tickless */
1327 if (unlikely(ts->tick_stopped))
1328 return;
1329
Thomas Gleixnerb9965442020-11-17 14:19:49 +01001330 hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
Thomas Gleixner0ff53d02015-04-14 21:08:54 +00001331 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001332}
1333
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001334static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
1335{
1336 if (!tick_nohz_enabled)
1337 return;
1338 ts->nohz_mode = mode;
1339 /* One update is enough */
1340 if (!test_and_set_bit(0, &tick_nohz_active))
Thomas Gleixnerae67bad2018-01-14 23:30:51 +01001341 timers_update_nohz();
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001342}
1343
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001344/**
1345 * tick_nohz_switch_to_nohz - switch to nohz mode
1346 */
1347static void tick_nohz_switch_to_nohz(void)
1348{
Christoph Lameter22127e92014-08-17 12:30:25 -05001349 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001350 ktime_t next;
1351
Viresh Kumar27630532014-04-15 10:54:41 +05301352 if (!tick_nohz_enabled)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001353 return;
1354
Thomas Gleixner6b442bc2015-05-07 14:35:59 +02001355 if (tick_switch_to_oneshot(tick_nohz_handler))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001356 return;
Thomas Gleixner6b442bc2015-05-07 14:35:59 +02001357
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001358 /*
1359 * Recycle the hrtimer in ts, so we can share the
1360 * hrtimer_forward with the highres code.
1361 */
Sebastian Andrzej Siewior71fed982019-08-23 13:38:45 +02001362 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001363 /* Get the next period */
1364 next = tick_init_jiffy_update();
1365
Thomas Gleixner0ff53d02015-04-14 21:08:54 +00001366 hrtimer_set_expires(&ts->sched_timer, next);
Thomas Gleixnerb9965442020-11-17 14:19:49 +01001367 hrtimer_forward_now(&ts->sched_timer, TICK_NSEC);
Wanpeng Li1ca8ec52016-01-27 19:26:07 +08001368 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001369 tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001370}
1371
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001372static inline void tick_nohz_irq_enter(void)
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001373{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05001374 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001375 ktime_t now;
1376
1377 if (!ts->idle_active && !ts->tick_stopped)
1378 return;
1379 now = ktime_get();
1380 if (ts->idle_active)
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +02001381 tick_nohz_stop_idle(ts, now);
Thomas Gleixnerff006732016-07-04 09:50:35 +00001382 if (ts->tick_stopped)
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001383 tick_nohz_update_jiffies(now);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +02001384}
1385
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001386#else
1387
1388static inline void tick_nohz_switch_to_nohz(void) { }
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001389static inline void tick_nohz_irq_enter(void) { }
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001390static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001391
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001392#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001393
1394/*
Thomas Gleixner719254fa2008-10-17 09:59:47 +02001395 * Called from irq_enter to notify about the possible interruption of idle()
1396 */
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001397void tick_irq_enter(void)
Thomas Gleixner719254fa2008-10-17 09:59:47 +02001398{
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +02001399 tick_check_oneshot_broadcast_this_cpu();
Frederic Weisbecker5acac1b2013-12-04 18:28:20 +01001400 tick_nohz_irq_enter();
Thomas Gleixner719254fa2008-10-17 09:59:47 +02001401}
1402
1403/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001404 * High resolution timer specific code
1405 */
1406#ifdef CONFIG_HIGH_RES_TIMERS
1407/*
Pavel Machek4c9dc642008-01-30 13:30:00 +01001408 * We rearm the timer until we get disabled by the idle code.
Chuansheng Liu351f1812012-10-25 01:07:35 +08001409 * Called with interrupts disabled.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001410 */
1411static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
1412{
1413 struct tick_sched *ts =
1414 container_of(timer, struct tick_sched, sched_timer);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001415 struct pt_regs *regs = get_irq_regs();
1416 ktime_t now = ktime_get();
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -07001417
Rafael J. Wysockiff7de622018-04-06 14:59:13 +02001418 tick_sched_do_timer(ts, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001419
1420 /*
1421 * Do not call, when we are not in irq context and have
1422 * no valid regs pointer
1423 */
Frederic Weisbecker9e8f5592012-10-15 02:43:03 +02001424 if (regs)
1425 tick_sched_handle(ts, regs);
Frederic Weisbecker7c259042017-05-15 14:56:50 +02001426 else
1427 ts->next_tick = 0;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001428
Viresh Kumar2a16fc92014-06-12 16:24:41 +05301429 /* No need to reprogram if we are in idle or full dynticks mode */
1430 if (unlikely(ts->tick_stopped))
1431 return HRTIMER_NORESTART;
1432
Thomas Gleixnerb9965442020-11-17 14:19:49 +01001433 hrtimer_forward(timer, now, TICK_NSEC);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001434
1435 return HRTIMER_RESTART;
1436}
1437
Mike Galbraith5307c952012-05-08 12:20:58 +02001438static int sched_skew_tick;
1439
Thomas Gleixner62cf20b2012-05-25 14:08:57 +02001440static int __init skew_tick(char *str)
1441{
1442 get_option(&str, &sched_skew_tick);
1443
1444 return 0;
1445}
1446early_param("skew_tick", skew_tick);
1447
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001448/**
1449 * tick_setup_sched_timer - setup the tick emulation timer
1450 */
1451void tick_setup_sched_timer(void)
1452{
Christoph Lameter22127e92014-08-17 12:30:25 -05001453 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001454 ktime_t now = ktime_get();
1455
1456 /*
1457 * Emulate tick processing via per-CPU hrtimers:
1458 */
Sebastian Andrzej Siewior902a9f92019-07-26 20:30:56 +02001459 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001460 ts->sched_timer.function = tick_sched_timer;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001461
Ingo Molnar0de76112016-07-01 12:42:35 +02001462 /* Get the next period (per-CPU) */
Arjan van de Vencc584b22008-09-01 15:02:30 -07001463 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001464
Thomas Gleixner9c3f9e22012-11-21 20:31:52 +01001465 /* Offset the tick to avert jiffies_lock contention. */
Mike Galbraith5307c952012-05-08 12:20:58 +02001466 if (sched_skew_tick) {
Thomas Gleixnerb9965442020-11-17 14:19:49 +01001467 u64 offset = TICK_NSEC >> 1;
Mike Galbraith5307c952012-05-08 12:20:58 +02001468 do_div(offset, num_possible_cpus());
1469 offset *= smp_processor_id();
1470 hrtimer_add_expires_ns(&ts->sched_timer, offset);
1471 }
1472
Thomas Gleixnerb9965442020-11-17 14:19:49 +01001473 hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
Sebastian Andrzej Siewior902a9f92019-07-26 20:30:56 +02001474 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD);
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +00001475 tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001476}
Miao Xie3c4fbe52008-08-20 16:37:38 -07001477#endif /* HIGH_RES_TIMERS */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001478
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001479#if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001480void tick_cancel_sched_timer(int cpu)
1481{
1482 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
1483
Miao Xie3c4fbe52008-08-20 16:37:38 -07001484# ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001485 if (ts->sched_timer.base)
1486 hrtimer_cancel(&ts->sched_timer);
Miao Xie3c4fbe52008-08-20 16:37:38 -07001487# endif
Karsten Wiesea7901762008-03-04 14:59:55 -08001488
Thomas Gleixner4b0c0f22013-05-03 15:02:50 +02001489 memset(ts, 0, sizeof(*ts));
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001490}
Miao Xie3c4fbe52008-08-20 16:37:38 -07001491#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001492
1493/**
1494 * Async notification about clocksource changes
1495 */
1496void tick_clock_notify(void)
1497{
1498 int cpu;
1499
1500 for_each_possible_cpu(cpu)
1501 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
1502}
1503
1504/*
1505 * Async notification about clock event changes
1506 */
1507void tick_oneshot_notify(void)
1508{
Christoph Lameter22127e92014-08-17 12:30:25 -05001509 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001510
1511 set_bit(0, &ts->check_clocks);
1512}
1513
1514/**
1515 * Check, if a change happened, which makes oneshot possible.
1516 *
1517 * Called cyclic from the hrtimer softirq (driven by the timer
1518 * softirq) allow_nohz signals, that we can switch into low-res nohz
1519 * mode, because high resolution timers are disabled (either compile
Thomas Gleixner6b442bc2015-05-07 14:35:59 +02001520 * or runtime). Called with interrupts disabled.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001521 */
1522int tick_check_oneshot_change(int allow_nohz)
1523{
Christoph Lameter22127e92014-08-17 12:30:25 -05001524 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001525
1526 if (!test_and_clear_bit(0, &ts->check_clocks))
1527 return 0;
1528
1529 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
1530 return 0;
1531
Li Zefancf4fc6c2008-02-08 04:19:24 -08001532 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001533 return 0;
1534
1535 if (!allow_nohz)
1536 return 1;
1537
1538 tick_nohz_switch_to_nohz();
1539 return 0;
1540}