tick/sched: Optimize tick_do_update_jiffies64() further
[linux-2.6-block.git] / kernel / time / tick-sched.c
CommitLineData
35728b82 1// SPDX-License-Identifier: GPL-2.0
79bf2bb3 2/*
79bf2bb3
TG
3 * 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
79bf2bb3
TG
10 */
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>
38b8d208 17#include <linux/nmi.h>
79bf2bb3 18#include <linux/profile.h>
3f07c014 19#include <linux/sched/signal.h>
e6017571 20#include <linux/sched/clock.h>
03441a34 21#include <linux/sched/stat.h>
370c9135 22#include <linux/sched/nohz.h>
8083e4ad 23#include <linux/module.h>
00b42959 24#include <linux/irq_work.h>
9014c45d 25#include <linux/posix-timers.h>
2e709338 26#include <linux/context_tracking.h>
62cb1188 27#include <linux/mm.h>
79bf2bb3 28
9e203bcc
DM
29#include <asm/irq_regs.h>
30
79bf2bb3
TG
31#include "tick-internal.h"
32
cb41a290
FW
33#include <trace/events/timer.h>
34
79bf2bb3 35/*
0de7611a 36 * Per-CPU nohz control structure
79bf2bb3 37 */
c1797baf 38static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
79bf2bb3 39
289f480a
IM
40struct tick_sched *tick_get_tick_sched(int cpu)
41{
42 return &per_cpu(tick_cpu_sched, cpu);
43}
44
7809998a
AB
45#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
46/*
c398960c
TG
47 * The time, when the last jiffy update happened. Write access must hold
48 * jiffies_lock and jiffies_seq. tick_nohz_next_event() needs to get a
49 * consistent view of jiffies and last_jiffies_update.
7809998a
AB
50 */
51static ktime_t last_jiffies_update;
52
79bf2bb3
TG
53/*
54 * Must be called with interrupts disabled !
55 */
56static void tick_do_update_jiffies64(ktime_t now)
57{
7a35bf2a 58 unsigned long ticks = 1;
79bf2bb3
TG
59 ktime_t delta;
60
7a14ce1d 61 /*
372acbba
TG
62 * Do a quick check without holding jiffies_lock. The READ_ONCE()
63 * pairs with the update done later in this function.
64 *
65 * This is also an intentional data race which is even safe on
66 * 32bit in theory. If there is a concurrent update then the check
67 * might give a random answer. It does not matter because if it
68 * returns then the concurrent update is already taking care, if it
69 * falls through then it will pointlessly contend on jiffies_lock.
70 *
71 * Though there is one nasty case on 32bit due to store tearing of
72 * the 64bit value. If the first 32bit store makes the quick check
73 * return on all other CPUs and the writing CPU context gets
74 * delayed to complete the second store (scheduled out on virt)
75 * then jiffies can become stale for up to ~2^32 nanoseconds
76 * without noticing. After that point all CPUs will wait for
77 * jiffies lock.
78 *
79 * OTOH, this is not any different than the situation with NOHZ=off
80 * where one CPU is responsible for updating jiffies and
81 * timekeeping. If that CPU goes out for lunch then all other CPUs
82 * will operate on stale jiffies until it decides to come back.
7a14ce1d 83 */
372acbba 84 if (ktime_before(now, READ_ONCE(tick_next_period)))
7a14ce1d
IM
85 return;
86
6168f8ed 87 /* Reevaluate with jiffies_lock held */
e5d4d175 88 raw_spin_lock(&jiffies_lock);
94ad2e3c 89 if (ktime_before(now, tick_next_period)) {
e5d4d175 90 raw_spin_unlock(&jiffies_lock);
03e6bdc5 91 return;
79bf2bb3 92 }
94ad2e3c
YY
93
94 write_seqcount_begin(&jiffies_seq);
95
94ad2e3c
YY
96 delta = ktime_sub(now, tick_next_period);
97 if (unlikely(delta >= tick_period)) {
98 /* Slow path for long idle sleep times */
99 s64 incr = ktime_to_ns(tick_period);
100
7a35bf2a 101 ticks += ktime_divns(delta, incr);
94ad2e3c
YY
102
103 last_jiffies_update = ktime_add_ns(last_jiffies_update,
104 incr * ticks);
7a35bf2a
TG
105 } else {
106 last_jiffies_update = ktime_add(last_jiffies_update,
107 tick_period);
94ad2e3c
YY
108 }
109
7a35bf2a 110 do_timer(ticks);
94ad2e3c
YY
111
112 /*
113 * Keep the tick_next_period variable up to date. WRITE_ONCE()
114 * pairs with the READ_ONCE() in the lockless quick check above.
115 */
116 WRITE_ONCE(tick_next_period,
117 ktime_add(last_jiffies_update, tick_period));
118
e5d4d175
TG
119 write_seqcount_end(&jiffies_seq);
120 raw_spin_unlock(&jiffies_lock);
47a1b796 121 update_wall_time();
79bf2bb3
TG
122}
123
124/*
125 * Initialize and return retrieve the jiffies update.
126 */
127static ktime_t tick_init_jiffy_update(void)
128{
129 ktime_t period;
130
e5d4d175
TG
131 raw_spin_lock(&jiffies_lock);
132 write_seqcount_begin(&jiffies_seq);
79bf2bb3 133 /* Did we start the jiffies update yet ? */
2456e855 134 if (last_jiffies_update == 0)
79bf2bb3
TG
135 last_jiffies_update = tick_next_period;
136 period = last_jiffies_update;
e5d4d175
TG
137 write_seqcount_end(&jiffies_seq);
138 raw_spin_unlock(&jiffies_lock);
79bf2bb3
TG
139 return period;
140}
141
ff7de620 142static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
5bb96226
FW
143{
144 int cpu = smp_processor_id();
145
3451d024 146#ifdef CONFIG_NO_HZ_COMMON
5bb96226
FW
147 /*
148 * Check if the do_timer duty was dropped. We don't care about
0de7611a
IM
149 * concurrency: This happens only when the CPU in charge went
150 * into a long sleep. If two CPUs happen to assign themselves to
5bb96226 151 * this duty, then the jiffies update is still serialized by
9c3f9e28 152 * jiffies_lock.
08ae95f4
NP
153 *
154 * If nohz_full is enabled, this should not happen because the
155 * tick_do_timer_cpu never relinquishes.
5bb96226 156 */
08ae95f4
NP
157 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) {
158#ifdef CONFIG_NO_HZ_FULL
159 WARN_ON(tick_nohz_full_running);
160#endif
5bb96226 161 tick_do_timer_cpu = cpu;
08ae95f4 162 }
5bb96226
FW
163#endif
164
165 /* Check, if the jiffies need an update */
166 if (tick_do_timer_cpu == cpu)
167 tick_do_update_jiffies64(now);
ff7de620
RW
168
169 if (ts->inidle)
170 ts->got_idle_tick = 1;
5bb96226
FW
171}
172
9e8f559b
FW
173static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
174{
3451d024 175#ifdef CONFIG_NO_HZ_COMMON
9e8f559b
FW
176 /*
177 * When we are idle and the tick is stopped, we have to touch
178 * the watchdog as we might not schedule for a really long
179 * time. This happens on complete idle SMP systems while
180 * waiting on the login prompt. We also increment the "start of
181 * idle" jiffy stamp so the idle accounting adjustment we do
182 * when we go busy again does not account too much ticks.
183 */
184 if (ts->tick_stopped) {
03e0d461 185 touch_softlockup_watchdog_sched();
9e8f559b
FW
186 if (is_idle_task(current))
187 ts->idle_jiffies++;
411fe24e
FW
188 /*
189 * In case the current tick fired too early past its expected
190 * expiration, make sure we don't bypass the next clock reprogramming
191 * to the same deadline.
192 */
193 ts->next_tick = 0;
9e8f559b 194 }
94a57140 195#endif
9e8f559b
FW
196 update_process_times(user_mode(regs));
197 profile_tick(CPU_PROFILING);
198}
7809998a 199#endif
9e8f559b 200
c5bfece2 201#ifdef CONFIG_NO_HZ_FULL
460775df 202cpumask_var_t tick_nohz_full_mask;
73867dcd 203bool tick_nohz_full_running;
ae9e557b 204EXPORT_SYMBOL_GPL(tick_nohz_full_running);
f009a7a7 205static atomic_t tick_dep_mask;
a831881b 206
f009a7a7 207static bool check_tick_dependency(atomic_t *dep)
d027d45d 208{
f009a7a7
FW
209 int val = atomic_read(dep);
210
211 if (val & TICK_DEP_MASK_POSIX_TIMER) {
e6e6cc22 212 trace_tick_stop(0, TICK_DEP_MASK_POSIX_TIMER);
f009a7a7 213 return true;
d027d45d
FW
214 }
215
f009a7a7 216 if (val & TICK_DEP_MASK_PERF_EVENTS) {
e6e6cc22 217 trace_tick_stop(0, TICK_DEP_MASK_PERF_EVENTS);
f009a7a7 218 return true;
d027d45d
FW
219 }
220
f009a7a7 221 if (val & TICK_DEP_MASK_SCHED) {
e6e6cc22 222 trace_tick_stop(0, TICK_DEP_MASK_SCHED);
f009a7a7 223 return true;
d027d45d
FW
224 }
225
f009a7a7 226 if (val & TICK_DEP_MASK_CLOCK_UNSTABLE) {
e6e6cc22 227 trace_tick_stop(0, TICK_DEP_MASK_CLOCK_UNSTABLE);
f009a7a7
FW
228 return true;
229 }
230
01b4c399
FW
231 if (val & TICK_DEP_MASK_RCU) {
232 trace_tick_stop(0, TICK_DEP_MASK_RCU);
233 return true;
234 }
235
f009a7a7 236 return false;
d027d45d
FW
237}
238
57ccdf44 239static bool can_stop_full_tick(int cpu, struct tick_sched *ts)
9014c45d 240{
ebf3adba 241 lockdep_assert_irqs_disabled();
9014c45d 242
57ccdf44
WL
243 if (unlikely(!cpu_online(cpu)))
244 return false;
245
f009a7a7 246 if (check_tick_dependency(&tick_dep_mask))
d027d45d 247 return false;
d027d45d 248
f009a7a7 249 if (check_tick_dependency(&ts->tick_dep_mask))
d027d45d 250 return false;
d027d45d 251
f009a7a7 252 if (check_tick_dependency(&current->tick_dep_mask))
d027d45d 253 return false;
d027d45d 254
f009a7a7 255 if (check_tick_dependency(&current->signal->tick_dep_mask))
d027d45d 256 return false;
d027d45d 257
9014c45d
FW
258 return true;
259}
260
d027d45d 261static void nohz_full_kick_func(struct irq_work *work)
76c24fb0 262{
73738a95 263 /* Empty, the tick restart happens on tick_nohz_irq_exit() */
76c24fb0
FW
264}
265
266static DEFINE_PER_CPU(struct irq_work, nohz_full_kick_work) = {
d027d45d 267 .func = nohz_full_kick_func,
49915ac3 268 .flags = ATOMIC_INIT(IRQ_WORK_HARD_IRQ),
76c24fb0
FW
269};
270
40bea039
FW
271/*
272 * Kick this CPU if it's full dynticks in order to force it to
273 * re-evaluate its dependency on the tick and restart it if necessary.
274 * This kick, unlike tick_nohz_full_kick_cpu() and tick_nohz_full_kick_all(),
275 * is NMI safe.
276 */
555e0c1e 277static void tick_nohz_full_kick(void)
40bea039
FW
278{
279 if (!tick_nohz_full_cpu(smp_processor_id()))
280 return;
281
56e4dea8 282 irq_work_queue(this_cpu_ptr(&nohz_full_kick_work));
40bea039
FW
283}
284
76c24fb0 285/*
3d36aebc 286 * Kick the CPU if it's full dynticks in order to force it to
76c24fb0
FW
287 * re-evaluate its dependency on the tick and restart it if necessary.
288 */
3d36aebc 289void tick_nohz_full_kick_cpu(int cpu)
76c24fb0 290{
3d36aebc
FW
291 if (!tick_nohz_full_cpu(cpu))
292 return;
293
294 irq_work_queue_on(&per_cpu(nohz_full_kick_work, cpu), cpu);
76c24fb0
FW
295}
296
76c24fb0
FW
297/*
298 * Kick all full dynticks CPUs in order to force these to re-evaluate
299 * their dependency on the tick and restart it if necessary.
300 */
b7878300 301static void tick_nohz_full_kick_all(void)
76c24fb0 302{
8537bb95
FW
303 int cpu;
304
73867dcd 305 if (!tick_nohz_full_running)
76c24fb0
FW
306 return;
307
308 preempt_disable();
8537bb95
FW
309 for_each_cpu_and(cpu, tick_nohz_full_mask, cpu_online_mask)
310 tick_nohz_full_kick_cpu(cpu);
76c24fb0
FW
311 preempt_enable();
312}
313
f009a7a7 314static void tick_nohz_dep_set_all(atomic_t *dep,
d027d45d
FW
315 enum tick_dep_bits bit)
316{
f009a7a7 317 int prev;
d027d45d 318
a1cc5bcf 319 prev = atomic_fetch_or(BIT(bit), dep);
d027d45d
FW
320 if (!prev)
321 tick_nohz_full_kick_all();
322}
323
324/*
325 * Set a global tick dependency. Used by perf events that rely on freq and
326 * by unstable clock.
327 */
328void tick_nohz_dep_set(enum tick_dep_bits bit)
329{
330 tick_nohz_dep_set_all(&tick_dep_mask, bit);
331}
332
333void tick_nohz_dep_clear(enum tick_dep_bits bit)
334{
f009a7a7 335 atomic_andnot(BIT(bit), &tick_dep_mask);
d027d45d
FW
336}
337
338/*
339 * Set per-CPU tick dependency. Used by scheduler and perf events in order to
340 * manage events throttling.
341 */
342void tick_nohz_dep_set_cpu(int cpu, enum tick_dep_bits bit)
343{
f009a7a7 344 int prev;
d027d45d
FW
345 struct tick_sched *ts;
346
347 ts = per_cpu_ptr(&tick_cpu_sched, cpu);
348
a1cc5bcf 349 prev = atomic_fetch_or(BIT(bit), &ts->tick_dep_mask);
d027d45d
FW
350 if (!prev) {
351 preempt_disable();
352 /* Perf needs local kick that is NMI safe */
353 if (cpu == smp_processor_id()) {
354 tick_nohz_full_kick();
355 } else {
356 /* Remote irq work not NMI-safe */
357 if (!WARN_ON_ONCE(in_nmi()))
358 tick_nohz_full_kick_cpu(cpu);
359 }
360 preempt_enable();
361 }
362}
01b4c399 363EXPORT_SYMBOL_GPL(tick_nohz_dep_set_cpu);
d027d45d
FW
364
365void tick_nohz_dep_clear_cpu(int cpu, enum tick_dep_bits bit)
366{
367 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
368
f009a7a7 369 atomic_andnot(BIT(bit), &ts->tick_dep_mask);
d027d45d 370}
01b4c399 371EXPORT_SYMBOL_GPL(tick_nohz_dep_clear_cpu);
d027d45d
FW
372
373/*
3c8920e2
FW
374 * Set a per-task tick dependency. RCU need this. Also posix CPU timers
375 * in order to elapse per task timers.
d027d45d
FW
376 */
377void tick_nohz_dep_set_task(struct task_struct *tsk, enum tick_dep_bits bit)
378{
3c8920e2
FW
379 if (!atomic_fetch_or(BIT(bit), &tsk->tick_dep_mask)) {
380 if (tsk == current) {
381 preempt_disable();
382 tick_nohz_full_kick();
383 preempt_enable();
384 } else {
385 /*
386 * Some future tick_nohz_full_kick_task()
387 * should optimize this.
388 */
389 tick_nohz_full_kick_all();
390 }
391 }
d027d45d 392}
ae9e557b 393EXPORT_SYMBOL_GPL(tick_nohz_dep_set_task);
d027d45d
FW
394
395void tick_nohz_dep_clear_task(struct task_struct *tsk, enum tick_dep_bits bit)
396{
f009a7a7 397 atomic_andnot(BIT(bit), &tsk->tick_dep_mask);
d027d45d 398}
ae9e557b 399EXPORT_SYMBOL_GPL(tick_nohz_dep_clear_task);
d027d45d
FW
400
401/*
402 * Set a per-taskgroup tick dependency. Posix CPU timers need this in order to elapse
403 * per process timers.
404 */
405void tick_nohz_dep_set_signal(struct signal_struct *sig, enum tick_dep_bits bit)
406{
407 tick_nohz_dep_set_all(&sig->tick_dep_mask, bit);
408}
409
410void tick_nohz_dep_clear_signal(struct signal_struct *sig, enum tick_dep_bits bit)
411{
f009a7a7 412 atomic_andnot(BIT(bit), &sig->tick_dep_mask);
d027d45d
FW
413}
414
99e5ada9
FW
415/*
416 * Re-evaluate the need for the tick as we switch the current task.
417 * It might need the tick due to per task/process properties:
0de7611a 418 * perf events, posix CPU timers, ...
99e5ada9 419 */
de734f89 420void __tick_nohz_task_switch(void)
99e5ada9
FW
421{
422 unsigned long flags;
d027d45d 423 struct tick_sched *ts;
99e5ada9 424
99e5ada9
FW
425 local_irq_save(flags);
426
6296ace4
LZ
427 if (!tick_nohz_full_cpu(smp_processor_id()))
428 goto out;
429
d027d45d 430 ts = this_cpu_ptr(&tick_cpu_sched);
99e5ada9 431
d027d45d 432 if (ts->tick_stopped) {
f009a7a7
FW
433 if (atomic_read(&current->tick_dep_mask) ||
434 atomic_read(&current->signal->tick_dep_mask))
d027d45d
FW
435 tick_nohz_full_kick();
436 }
6296ace4 437out:
99e5ada9
FW
438 local_irq_restore(flags);
439}
440
6f1982fe
FW
441/* Get the boot-time nohz CPU list from the kernel parameters. */
442void __init tick_nohz_full_setup(cpumask_var_t cpumask)
a831881b 443{
73867dcd 444 alloc_bootmem_cpumask_var(&tick_nohz_full_mask);
6f1982fe 445 cpumask_copy(tick_nohz_full_mask, cpumask);
73867dcd 446 tick_nohz_full_running = true;
a831881b 447}
ae9e557b 448EXPORT_SYMBOL_GPL(tick_nohz_full_setup);
a831881b 449
31eff243 450static int tick_nohz_cpu_down(unsigned int cpu)
a382bf93 451{
31eff243 452 /*
08ae95f4
NP
453 * The tick_do_timer_cpu CPU handles housekeeping duty (unbound
454 * timers, workqueues, timekeeping, ...) on behalf of full dynticks
31eff243
SAS
455 * CPUs. It must remain online when nohz full is enabled.
456 */
457 if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
458 return -EBUSY;
459 return 0;
a382bf93
FW
460}
461
d1e43fa5 462void __init tick_nohz_init(void)
a831881b 463{
31eff243 464 int cpu, ret;
d1e43fa5 465
a7c8655b
PM
466 if (!tick_nohz_full_running)
467 return;
d1e43fa5 468
9b01f5bf
FW
469 /*
470 * Full dynticks uses irq work to drive the tick rescheduling on safe
471 * locking contexts. But then we need irq work to raise its own
472 * interrupts to avoid circular dependency on the tick
473 */
474 if (!arch_irq_work_has_interrupt()) {
a395d6a7 475 pr_warn("NO_HZ: Can't run full dynticks because arch doesn't support irq work self-IPIs\n");
9b01f5bf 476 cpumask_clear(tick_nohz_full_mask);
9b01f5bf
FW
477 tick_nohz_full_running = false;
478 return;
479 }
480
08ae95f4
NP
481 if (IS_ENABLED(CONFIG_PM_SLEEP_SMP) &&
482 !IS_ENABLED(CONFIG_PM_SLEEP_SMP_NONZERO_CPU)) {
483 cpu = smp_processor_id();
4327b15f 484
08ae95f4
NP
485 if (cpumask_test_cpu(cpu, tick_nohz_full_mask)) {
486 pr_warn("NO_HZ: Clearing %d from nohz_full range "
487 "for timekeeping\n", cpu);
488 cpumask_clear_cpu(cpu, tick_nohz_full_mask);
489 }
4327b15f
FW
490 }
491
73867dcd 492 for_each_cpu(cpu, tick_nohz_full_mask)
2e709338
FW
493 context_tracking_cpu_set(cpu);
494
31eff243
SAS
495 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
496 "kernel/nohz:predown", NULL,
497 tick_nohz_cpu_down);
498 WARN_ON(ret < 0);
ffda22c1
TH
499 pr_info("NO_HZ: Full dynticks CPUs: %*pbl.\n",
500 cpumask_pr_args(tick_nohz_full_mask));
a831881b 501}
a831881b
FW
502#endif
503
79bf2bb3
TG
504/*
505 * NOHZ - aka dynamic tick functionality
506 */
3451d024 507#ifdef CONFIG_NO_HZ_COMMON
79bf2bb3
TG
508/*
509 * NO HZ enabled ?
510 */
4cc7ecb7 511bool tick_nohz_enabled __read_mostly = true;
bc7a34b8 512unsigned long tick_nohz_active __read_mostly;
79bf2bb3
TG
513/*
514 * Enable / Disable tickless mode
515 */
516static int __init setup_tick_nohz(char *str)
517{
4cc7ecb7 518 return (kstrtobool(str, &tick_nohz_enabled) == 0);
79bf2bb3
TG
519}
520
521__setup("nohz=", setup_tick_nohz);
522
a3642983 523bool tick_nohz_tick_stopped(void)
c1797baf 524{
2bc629a6
FW
525 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
526
527 return ts->tick_stopped;
c1797baf
TG
528}
529
22ab8bc0
FW
530bool tick_nohz_tick_stopped_cpu(int cpu)
531{
532 struct tick_sched *ts = per_cpu_ptr(&tick_cpu_sched, cpu);
533
534 return ts->tick_stopped;
535}
536
79bf2bb3
TG
537/**
538 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
539 *
540 * Called from interrupt entry when the CPU was idle
541 *
542 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
543 * must be updated. Otherwise an interrupt handler could use a stale jiffy
0de7611a
IM
544 * value. We do this unconditionally on any CPU, as we don't know whether the
545 * CPU, which has the update task assigned is in a long sleep.
79bf2bb3 546 */
eed3b9cf 547static void tick_nohz_update_jiffies(ktime_t now)
79bf2bb3 548{
79bf2bb3 549 unsigned long flags;
79bf2bb3 550
e8fcaa5c 551 __this_cpu_write(tick_cpu_sched.idle_waketime, now);
79bf2bb3
TG
552
553 local_irq_save(flags);
554 tick_do_update_jiffies64(now);
555 local_irq_restore(flags);
02ff3755 556
03e0d461 557 touch_softlockup_watchdog_sched();
79bf2bb3
TG
558}
559
595aac48 560/*
0de7611a 561 * Updates the per-CPU time idle statistics counters
595aac48 562 */
8d63bf94 563static void
8c215bd3 564update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
6378ddb5 565{
eed3b9cf 566 ktime_t delta;
6378ddb5 567
595aac48
AV
568 if (ts->idle_active) {
569 delta = ktime_sub(now, ts->idle_entrytime);
8c215bd3 570 if (nr_iowait_cpu(cpu) > 0)
0224cf4c 571 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
6beea0cd
MH
572 else
573 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
8c7b09f4 574 ts->idle_entrytime = now;
595aac48 575 }
8d63bf94 576
e0e37c20 577 if (last_update_time)
8d63bf94
AV
578 *last_update_time = ktime_to_us(now);
579
595aac48
AV
580}
581
e8fcaa5c 582static void tick_nohz_stop_idle(struct tick_sched *ts, ktime_t now)
595aac48 583{
e8fcaa5c 584 update_ts_time_stats(smp_processor_id(), ts, now, NULL);
eed3b9cf 585 ts->idle_active = 0;
56c7426b 586
ac1e843f 587 sched_clock_idle_wakeup_event();
6378ddb5
VP
588}
589
0e776768 590static void tick_nohz_start_idle(struct tick_sched *ts)
6378ddb5 591{
0e776768 592 ts->idle_entrytime = ktime_get();
6378ddb5 593 ts->idle_active = 1;
56c7426b 594 sched_clock_idle_sleep_event();
6378ddb5
VP
595}
596
b1f724c3 597/**
0de7611a 598 * get_cpu_idle_time_us - get the total idle time of a CPU
b1f724c3 599 * @cpu: CPU number to query
09a1d34f
MH
600 * @last_update_time: variable to store update time in. Do not update
601 * counters if NULL.
b1f724c3 602 *
6168f8ed 603 * Return the cumulative idle time (since boot) for a given
6beea0cd 604 * CPU, in microseconds.
b1f724c3
AV
605 *
606 * This time is measured via accounting rather than sampling,
607 * and is as accurate as ktime_get() is.
608 *
609 * This function returns -1 if NOHZ is not enabled.
610 */
6378ddb5
VP
611u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
612{
613 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
09a1d34f 614 ktime_t now, idle;
6378ddb5 615
d689fe22 616 if (!tick_nohz_active)
8083e4ad 617 return -1;
618
09a1d34f
MH
619 now = ktime_get();
620 if (last_update_time) {
621 update_ts_time_stats(cpu, ts, now, last_update_time);
622 idle = ts->idle_sleeptime;
623 } else {
624 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
625 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
626
627 idle = ktime_add(ts->idle_sleeptime, delta);
628 } else {
629 idle = ts->idle_sleeptime;
630 }
631 }
632
633 return ktime_to_us(idle);
8083e4ad 634
6378ddb5 635}
8083e4ad 636EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
6378ddb5 637
6beea0cd 638/**
0de7611a 639 * get_cpu_iowait_time_us - get the total iowait time of a CPU
0224cf4c 640 * @cpu: CPU number to query
09a1d34f
MH
641 * @last_update_time: variable to store update time in. Do not update
642 * counters if NULL.
0224cf4c 643 *
6168f8ed 644 * Return the cumulative iowait time (since boot) for a given
0224cf4c
AV
645 * CPU, in microseconds.
646 *
647 * This time is measured via accounting rather than sampling,
648 * and is as accurate as ktime_get() is.
649 *
650 * This function returns -1 if NOHZ is not enabled.
651 */
652u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
653{
654 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
09a1d34f 655 ktime_t now, iowait;
0224cf4c 656
d689fe22 657 if (!tick_nohz_active)
0224cf4c
AV
658 return -1;
659
09a1d34f
MH
660 now = ktime_get();
661 if (last_update_time) {
662 update_ts_time_stats(cpu, ts, now, last_update_time);
663 iowait = ts->iowait_sleeptime;
664 } else {
665 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
666 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
0224cf4c 667
09a1d34f
MH
668 iowait = ktime_add(ts->iowait_sleeptime, delta);
669 } else {
670 iowait = ts->iowait_sleeptime;
671 }
672 }
0224cf4c 673
09a1d34f 674 return ktime_to_us(iowait);
0224cf4c
AV
675}
676EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
677
0ff53d09
TG
678static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
679{
680 hrtimer_cancel(&ts->sched_timer);
681 hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
682
683 /* Forward the time to expire in the future */
684 hrtimer_forward(&ts->sched_timer, now, tick_period);
685
902a9f9c
SAS
686 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
687 hrtimer_start_expires(&ts->sched_timer,
688 HRTIMER_MODE_ABS_PINNED_HARD);
689 } else {
0ff53d09 690 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
902a9f9c 691 }
411fe24e
FW
692
693 /*
694 * Reset to make sure next tick stop doesn't get fooled by past
695 * cached clock deadline.
696 */
697 ts->next_tick = 0;
0ff53d09
TG
698}
699
5d62c183
TG
700static inline bool local_timer_softirq_pending(void)
701{
80d20d35 702 return local_softirq_pending() & BIT(TIMER_SOFTIRQ);
5d62c183
TG
703}
704
23a8d888 705static ktime_t tick_nohz_next_event(struct tick_sched *ts, int cpu)
79bf2bb3 706{
c1ad348b 707 u64 basemono, next_tick, next_tmr, next_rcu, delta, expires;
e1e41b6c
RV
708 unsigned long basejiff;
709 unsigned int seq;
855a0fc3 710
79bf2bb3
TG
711 /* Read jiffies and the time when jiffies were updated last */
712 do {
e5d4d175 713 seq = read_seqcount_begin(&jiffies_seq);
2456e855 714 basemono = last_jiffies_update;
c1ad348b 715 basejiff = jiffies;
e5d4d175 716 } while (read_seqcount_retry(&jiffies_seq, seq));
c1ad348b 717 ts->last_jiffies = basejiff;
23a8d888 718 ts->timer_expires_base = basemono;
79bf2bb3 719
5d62c183
TG
720 /*
721 * Keep the periodic tick, when RCU, architecture or irq_work
722 * requests it.
723 * Aside of that check whether the local timer softirq is
724 * pending. If so its a bad idea to call get_next_timer_interrupt()
725 * because there is an already expired timer, so it will request
726 * immeditate expiry, which rearms the hardware timer with a
727 * minimal delta which brings us back to this place
728 * immediately. Lather, rinse and repeat...
729 */
730 if (rcu_needs_cpu(basemono, &next_rcu) || arch_needs_cpu() ||
731 irq_work_needs_cpu() || local_timer_softirq_pending()) {
c1ad348b 732 next_tick = basemono + TICK_NSEC;
3c5d92a0 733 } else {
c1ad348b
TG
734 /*
735 * Get the next pending timer. If high resolution
736 * timers are enabled this only takes the timer wheel
737 * timers into account. If high resolution timers are
738 * disabled this also looks at the next expiring
739 * hrtimer.
740 */
741 next_tmr = get_next_timer_interrupt(basejiff, basemono);
742 ts->next_timer = next_tmr;
743 /* Take the next rcu event into account */
744 next_tick = next_rcu < next_tmr ? next_rcu : next_tmr;
3c5d92a0 745 }
47aa8b6c 746
c1ad348b
TG
747 /*
748 * If the tick is due in the next period, keep it ticking or
82bbe34b 749 * force prod the timer.
c1ad348b
TG
750 */
751 delta = next_tick - basemono;
752 if (delta <= (u64)TICK_NSEC) {
a683f390
TG
753 /*
754 * Tell the timer code that the base is not idle, i.e. undo
755 * the effect of get_next_timer_interrupt():
756 */
757 timer_clear_idle();
82bbe34b
PZ
758 /*
759 * We've not stopped the tick yet, and there's a timer in the
760 * next period, so no point in stopping it either, bail.
761 */
f99973e1 762 if (!ts->tick_stopped) {
23a8d888 763 ts->timer_expires = 0;
157d29e1
TG
764 goto out;
765 }
766 }
767
23a8d888
RW
768 /*
769 * If this CPU is the one which had the do_timer() duty last, we limit
770 * the sleep time to the timekeeping max_deferment value.
771 * Otherwise we can sleep as long as we want.
772 */
773 delta = timekeeping_max_deferment();
774 if (cpu != tick_do_timer_cpu &&
775 (tick_do_timer_cpu != TICK_DO_TIMER_NONE || !ts->do_timer_last))
776 delta = KTIME_MAX;
777
778 /* Calculate the next expiry time */
779 if (delta < (KTIME_MAX - basemono))
780 expires = basemono + delta;
781 else
782 expires = KTIME_MAX;
783
784 ts->timer_expires = min_t(u64, expires, next_tick);
785
786out:
787 return ts->timer_expires;
788}
789
790static void tick_nohz_stop_tick(struct tick_sched *ts, int cpu)
791{
792 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
793 u64 basemono = ts->timer_expires_base;
794 u64 expires = ts->timer_expires;
795 ktime_t tick = expires;
796
797 /* Make sure we won't be trying to stop it twice in a row. */
798 ts->timer_expires_base = 0;
799
79bf2bb3 800 /*
0de7611a
IM
801 * If this CPU is the one which updates jiffies, then give up
802 * the assignment and let it be taken by the CPU which runs
803 * the tick timer next, which might be this CPU as well. If we
157d29e1
TG
804 * don't drop this here the jiffies might be stale and
805 * do_timer() never invoked. Keep track of the fact that it
23a8d888 806 * was the one which had the do_timer() duty last.
79bf2bb3 807 */
157d29e1
TG
808 if (cpu == tick_do_timer_cpu) {
809 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
810 ts->do_timer_last = 1;
811 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
157d29e1 812 ts->do_timer_last = 0;
157d29e1 813 }
27185016 814
157d29e1 815 /* Skip reprogram of event if its not changed */
411fe24e
FW
816 if (ts->tick_stopped && (expires == ts->next_tick)) {
817 /* Sanity check: make sure clockevent is actually programmed */
d4af6d93 818 if (tick == KTIME_MAX || ts->next_tick == hrtimer_get_expires(&ts->sched_timer))
23a8d888 819 return;
411fe24e
FW
820
821 WARN_ON_ONCE(1);
822 printk_once("basemono: %llu ts->next_tick: %llu dev->next_event: %llu timer->active: %d timer->expires: %llu\n",
823 basemono, ts->next_tick, dev->next_event,
824 hrtimer_active(&ts->sched_timer), hrtimer_get_expires(&ts->sched_timer));
ce6cf9a1 825 }
84bf1bcc 826
157d29e1
TG
827 /*
828 * nohz_stop_sched_tick can be called several times before
829 * the nohz_restart_sched_tick is called. This happens when
830 * interrupts arrive which do not cause a reschedule. In the
831 * first call we save the current tick time, so we can restart
832 * the scheduler tick in nohz_restart_sched_tick.
833 */
834 if (!ts->tick_stopped) {
3c85d6db 835 calc_load_nohz_start();
62cb1188 836 quiet_vmstat();
d3ed7824 837
157d29e1
TG
838 ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
839 ts->tick_stopped = 1;
e6e6cc22 840 trace_tick_stop(1, TICK_DEP_MASK_NONE);
157d29e1 841 }
eaad084b 842
411fe24e
FW
843 ts->next_tick = tick;
844
157d29e1 845 /*
c1ad348b
TG
846 * If the expiration time == KTIME_MAX, then we simply stop
847 * the tick timer.
157d29e1 848 */
c1ad348b 849 if (unlikely(expires == KTIME_MAX)) {
157d29e1
TG
850 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
851 hrtimer_cancel(&ts->sched_timer);
23a8d888 852 return;
79bf2bb3 853 }
0ff53d09 854
1f71addd 855 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
902a9f9c
SAS
856 hrtimer_start(&ts->sched_timer, tick,
857 HRTIMER_MODE_ABS_PINNED_HARD);
1f71addd
TG
858 } else {
859 hrtimer_set_expires(&ts->sched_timer, tick);
c1ad348b 860 tick_program_event(tick, 1);
1f71addd 861 }
280f0677
FW
862}
863
23a8d888
RW
864static void tick_nohz_retain_tick(struct tick_sched *ts)
865{
866 ts->timer_expires_base = 0;
867}
868
869#ifdef CONFIG_NO_HZ_FULL
870static void tick_nohz_stop_sched_tick(struct tick_sched *ts, int cpu)
871{
872 if (tick_nohz_next_event(ts, cpu))
873 tick_nohz_stop_tick(ts, cpu);
874 else
875 tick_nohz_retain_tick(ts);
876}
877#endif /* CONFIG_NO_HZ_FULL */
878
1f41906a 879static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
59d2c7ca
FW
880{
881 /* Update jiffies first */
882 tick_do_update_jiffies64(now);
59d2c7ca 883
a683f390
TG
884 /*
885 * Clear the timer idle flag, so we avoid IPIs on remote queueing and
886 * the clock forward checks in the enqueue path:
887 */
888 timer_clear_idle();
889
3c85d6db 890 calc_load_nohz_stop();
03e0d461 891 touch_softlockup_watchdog_sched();
59d2c7ca
FW
892 /*
893 * Cancel the scheduled timer and restore the tick
894 */
895 ts->tick_stopped = 0;
896 ts->idle_exittime = now;
897
898 tick_nohz_restart(ts, now);
899}
73738a95
FW
900
901static void tick_nohz_full_update_tick(struct tick_sched *ts)
5811d996
FW
902{
903#ifdef CONFIG_NO_HZ_FULL
e9a2eb40 904 int cpu = smp_processor_id();
5811d996 905
59449359 906 if (!tick_nohz_full_cpu(cpu))
e9a2eb40 907 return;
5811d996 908
e9a2eb40
AS
909 if (!ts->tick_stopped && ts->nohz_mode == NOHZ_MODE_INACTIVE)
910 return;
5811d996 911
57ccdf44 912 if (can_stop_full_tick(cpu, ts))
23a8d888 913 tick_nohz_stop_sched_tick(ts, cpu);
73738a95 914 else if (ts->tick_stopped)
1f41906a 915 tick_nohz_restart_sched_tick(ts, ktime_get());
5811d996
FW
916#endif
917}
918
5b39939a
FW
919static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
920{
921 /*
0de7611a 922 * If this CPU is offline and it is the one which updates
5b39939a 923 * jiffies, then give up the assignment and let it be taken by
0de7611a 924 * the CPU which runs the tick timer next. If we don't drop
5b39939a
FW
925 * this here the jiffies might be stale and do_timer() never
926 * invoked.
927 */
928 if (unlikely(!cpu_online(cpu))) {
929 if (cpu == tick_do_timer_cpu)
930 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
411fe24e
FW
931 /*
932 * Make sure the CPU doesn't get fooled by obsolete tick
933 * deadline if it comes back online later.
934 */
935 ts->next_tick = 0;
f7ea0fd6 936 return false;
5b39939a
FW
937 }
938
23a8d888 939 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
5b39939a
FW
940 return false;
941
942 if (need_resched())
943 return false;
944
d59e0ba1 945 if (unlikely(local_softirq_pending())) {
5b39939a
FW
946 static int ratelimit;
947
803b0eba
PM
948 if (ratelimit < 10 &&
949 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
bca37119 950 pr_warn("NOHZ tick-stop error: Non-RCU local softirq work is pending, handler #%02x!!!\n",
cfea7d7e 951 (unsigned int) local_softirq_pending());
5b39939a
FW
952 ratelimit++;
953 }
954 return false;
955 }
956
460775df 957 if (tick_nohz_full_enabled()) {
a382bf93
FW
958 /*
959 * Keep the tick alive to guarantee timekeeping progression
960 * if there are full dynticks CPUs around
961 */
962 if (tick_do_timer_cpu == cpu)
963 return false;
964 /*
965 * Boot safety: make sure the timekeeping duty has been
966 * assigned before entering dyntick-idle mode,
08ae95f4 967 * tick_do_timer_cpu is TICK_DO_TIMER_BOOT
a382bf93 968 */
08ae95f4
NP
969 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_BOOT))
970 return false;
971
972 /* Should not happen for nohz-full */
973 if (WARN_ON_ONCE(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
a382bf93
FW
974 return false;
975 }
976
5b39939a
FW
977 return true;
978}
979
0e776768 980static void __tick_nohz_idle_stop_tick(struct tick_sched *ts)
19f5f736 981{
0e776768 982 ktime_t expires;
5b39939a 983 int cpu = smp_processor_id();
19f5f736 984
554c8aa8
RW
985 /*
986 * If tick_nohz_get_sleep_length() ran tick_nohz_next_event(), the
987 * tick timer expiration time is known already.
988 */
989 if (ts->timer_expires_base)
990 expires = ts->timer_expires;
991 else if (can_stop_idle_tick(cpu, ts))
992 expires = tick_nohz_next_event(ts, cpu);
993 else
994 return;
23a8d888
RW
995
996 ts->idle_calls++;
08d07259 997
23a8d888 998 if (expires > 0LL) {
5b39939a
FW
999 int was_stopped = ts->tick_stopped;
1000
23a8d888 1001 tick_nohz_stop_tick(ts, cpu);
84bf1bcc 1002
23a8d888
RW
1003 ts->idle_sleeps++;
1004 ts->idle_expires = expires;
5b39939a 1005
a0db971e 1006 if (!was_stopped && ts->tick_stopped) {
5b39939a 1007 ts->idle_jiffies = ts->last_jiffies;
a0db971e
FW
1008 nohz_balance_enter_idle(cpu);
1009 }
23a8d888
RW
1010 } else {
1011 tick_nohz_retain_tick(ts);
5b39939a 1012 }
280f0677
FW
1013}
1014
1015/**
0e776768 1016 * tick_nohz_idle_stop_tick - stop the idle tick from the idle task
280f0677
FW
1017 *
1018 * When the next event is more than a tick into the future, stop the idle tick
0e776768
RW
1019 */
1020void tick_nohz_idle_stop_tick(void)
1021{
1022 __tick_nohz_idle_stop_tick(this_cpu_ptr(&tick_cpu_sched));
1023}
1024
554c8aa8
RW
1025void tick_nohz_idle_retain_tick(void)
1026{
1027 tick_nohz_retain_tick(this_cpu_ptr(&tick_cpu_sched));
1028 /*
1029 * Undo the effect of get_next_timer_interrupt() called from
1030 * tick_nohz_next_event().
1031 */
1032 timer_clear_idle();
1033}
1034
0e776768
RW
1035/**
1036 * tick_nohz_idle_enter - prepare for entering idle on the current CPU
2bbb6817 1037 *
0e776768 1038 * Called when we start the idle loop.
280f0677 1039 */
1268fbc7 1040void tick_nohz_idle_enter(void)
280f0677
FW
1041{
1042 struct tick_sched *ts;
1043
ebf3adba 1044 lockdep_assert_irqs_enabled();
0db49b72 1045
1268fbc7
FW
1046 local_irq_disable();
1047
22127e93 1048 ts = this_cpu_ptr(&tick_cpu_sched);
23a8d888
RW
1049
1050 WARN_ON_ONCE(ts->timer_expires_base);
1051
280f0677 1052 ts->inidle = 1;
0e776768 1053 tick_nohz_start_idle(ts);
1268fbc7
FW
1054
1055 local_irq_enable();
280f0677
FW
1056}
1057
1058/**
1059 * tick_nohz_irq_exit - update next tick event from interrupt exit
1060 *
1061 * When an interrupt fires while we are idle and it doesn't cause
1062 * a reschedule, it may still add, modify or delete a timer, enqueue
1063 * an RCU callback, etc...
1064 * So we need to re-calculate and reprogram the next tick event.
1065 */
1066void tick_nohz_irq_exit(void)
1067{
22127e93 1068 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
280f0677 1069
14851912 1070 if (ts->inidle)
0e776768 1071 tick_nohz_start_idle(ts);
14851912 1072 else
73738a95 1073 tick_nohz_full_update_tick(ts);
79bf2bb3
TG
1074}
1075
4f86d3a8 1076/**
45f1ff59
RW
1077 * tick_nohz_idle_got_tick - Check whether or not the tick handler has run
1078 */
1079bool tick_nohz_idle_got_tick(void)
1080{
1081 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1082
2bc629a6
FW
1083 if (ts->got_idle_tick) {
1084 ts->got_idle_tick = 0;
45f1ff59
RW
1085 return true;
1086 }
1087 return false;
1088}
1089
6f9b83ac
UH
1090/**
1091 * tick_nohz_get_next_hrtimer - return the next expiration time for the hrtimer
1092 * or the tick, whatever that expires first. Note that, if the tick has been
1093 * stopped, it returns the next hrtimer.
1094 *
1095 * Called from power state control code with interrupts disabled
1096 */
1097ktime_t tick_nohz_get_next_hrtimer(void)
1098{
1099 return __this_cpu_read(tick_cpu_device.evtdev)->next_event;
1100}
1101
4f86d3a8 1102/**
554c8aa8 1103 * tick_nohz_get_sleep_length - return the expected length of the current sleep
296bb1e5 1104 * @delta_next: duration until the next event if the tick cannot be stopped
4f86d3a8
LB
1105 *
1106 * Called from power state control code with interrupts disabled
1107 */
296bb1e5 1108ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
4f86d3a8 1109{
554c8aa8 1110 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
22127e93 1111 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
554c8aa8
RW
1112 int cpu = smp_processor_id();
1113 /*
1114 * The idle entry time is expected to be a sufficient approximation of
1115 * the current time at this point.
1116 */
1117 ktime_t now = ts->idle_entrytime;
1118 ktime_t next_event;
1119
1120 WARN_ON_ONCE(!ts->inidle);
1121
296bb1e5
RW
1122 *delta_next = ktime_sub(dev->next_event, now);
1123
554c8aa8 1124 if (!can_stop_idle_tick(cpu, ts))
296bb1e5 1125 return *delta_next;
554c8aa8
RW
1126
1127 next_event = tick_nohz_next_event(ts, cpu);
1128 if (!next_event)
296bb1e5 1129 return *delta_next;
554c8aa8
RW
1130
1131 /*
1132 * If the next highres timer to expire is earlier than next_event, the
1133 * idle governor needs to know that.
1134 */
1135 next_event = min_t(u64, next_event,
1136 hrtimer_next_event_without(&ts->sched_timer));
4f86d3a8 1137
554c8aa8 1138 return ktime_sub(next_event, now);
4f86d3a8
LB
1139}
1140
466a2b42
JF
1141/**
1142 * tick_nohz_get_idle_calls_cpu - return the current idle calls counter value
1143 * for a particular CPU.
1144 *
1145 * Called from the schedutil frequency scaling governor in scheduler context.
1146 */
1147unsigned long tick_nohz_get_idle_calls_cpu(int cpu)
1148{
1149 struct tick_sched *ts = tick_get_tick_sched(cpu);
1150
1151 return ts->idle_calls;
1152}
1153
b7eaf1aa
RW
1154/**
1155 * tick_nohz_get_idle_calls - return the current idle calls counter value
1156 *
1157 * Called from the schedutil frequency scaling governor in scheduler context.
1158 */
1159unsigned long tick_nohz_get_idle_calls(void)
1160{
1161 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1162
1163 return ts->idle_calls;
1164}
1165
2ac0d98f
FW
1166static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
1167{
3f4724ea 1168#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
2ac0d98f 1169 unsigned long ticks;
3f4724ea 1170
e44fcb4b 1171 if (vtime_accounting_enabled_this_cpu())
3f4724ea 1172 return;
79bf2bb3
TG
1173 /*
1174 * We stopped the tick in idle. Update process times would miss the
1175 * time we slept as update_process_times does only a 1 tick
1176 * accounting. Enforce that this is accounted to idle !
1177 */
1178 ticks = jiffies - ts->idle_jiffies;
1179 /*
1180 * We might be one off. Do not randomly account a huge number of ticks!
1181 */
79741dd3
MS
1182 if (ticks && ticks < LONG_MAX)
1183 account_idle_ticks(ticks);
1184#endif
19f5f736
FW
1185}
1186
2aaf709a
RW
1187static void __tick_nohz_idle_restart_tick(struct tick_sched *ts, ktime_t now)
1188{
1189 tick_nohz_restart_sched_tick(ts, now);
1190 tick_nohz_account_idle_ticks(ts);
1191}
1192
1193void tick_nohz_idle_restart_tick(void)
1194{
1195 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
1196
1197 if (ts->tick_stopped)
1198 __tick_nohz_idle_restart_tick(ts, ktime_get());
1199}
1200
79bf2bb3 1201/**
280f0677 1202 * tick_nohz_idle_exit - restart the idle tick from the idle task
79bf2bb3
TG
1203 *
1204 * Restart the idle tick when the CPU is woken up from idle
280f0677
FW
1205 * This also exit the RCU extended quiescent state. The CPU
1206 * can use RCU again after this function is called.
79bf2bb3 1207 */
280f0677 1208void tick_nohz_idle_exit(void)
79bf2bb3 1209{
4a32fea9 1210 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
bbe9a70a 1211 bool idle_active, tick_stopped;
6378ddb5 1212 ktime_t now;
79bf2bb3 1213
6378ddb5 1214 local_irq_disable();
2bbb6817 1215
15f827be 1216 WARN_ON_ONCE(!ts->inidle);
23a8d888 1217 WARN_ON_ONCE(ts->timer_expires_base);
15f827be
FW
1218
1219 ts->inidle = 0;
bbe9a70a
AB
1220 idle_active = ts->idle_active;
1221 tick_stopped = ts->tick_stopped;
15f827be 1222
bbe9a70a 1223 if (idle_active || tick_stopped)
eed3b9cf
MS
1224 now = ktime_get();
1225
bbe9a70a 1226 if (idle_active)
e8fcaa5c 1227 tick_nohz_stop_idle(ts, now);
6378ddb5 1228
bbe9a70a 1229 if (tick_stopped)
2aaf709a 1230 __tick_nohz_idle_restart_tick(ts, now);
79bf2bb3 1231
79bf2bb3
TG
1232 local_irq_enable();
1233}
1234
79bf2bb3
TG
1235/*
1236 * The nohz low res interrupt handler
1237 */
1238static void tick_nohz_handler(struct clock_event_device *dev)
1239{
22127e93 1240 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
79bf2bb3
TG
1241 struct pt_regs *regs = get_irq_regs();
1242 ktime_t now = ktime_get();
1243
2456e855 1244 dev->next_event = KTIME_MAX;
79bf2bb3 1245
ff7de620 1246 tick_sched_do_timer(ts, now);
9e8f559b 1247 tick_sched_handle(ts, regs);
79bf2bb3 1248
b5e995e6
VK
1249 /* No need to reprogram if we are running tickless */
1250 if (unlikely(ts->tick_stopped))
1251 return;
1252
0ff53d09
TG
1253 hrtimer_forward(&ts->sched_timer, now, tick_period);
1254 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
79bf2bb3
TG
1255}
1256
bc7a34b8
TG
1257static inline void tick_nohz_activate(struct tick_sched *ts, int mode)
1258{
1259 if (!tick_nohz_enabled)
1260 return;
1261 ts->nohz_mode = mode;
1262 /* One update is enough */
1263 if (!test_and_set_bit(0, &tick_nohz_active))
ae67bada 1264 timers_update_nohz();
bc7a34b8
TG
1265}
1266
79bf2bb3
TG
1267/**
1268 * tick_nohz_switch_to_nohz - switch to nohz mode
1269 */
1270static void tick_nohz_switch_to_nohz(void)
1271{
22127e93 1272 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
79bf2bb3
TG
1273 ktime_t next;
1274
27630532 1275 if (!tick_nohz_enabled)
79bf2bb3
TG
1276 return;
1277
6b442bc8 1278 if (tick_switch_to_oneshot(tick_nohz_handler))
79bf2bb3 1279 return;
6b442bc8 1280
79bf2bb3
TG
1281 /*
1282 * Recycle the hrtimer in ts, so we can share the
1283 * hrtimer_forward with the highres code.
1284 */
71fed982 1285 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
79bf2bb3
TG
1286 /* Get the next period */
1287 next = tick_init_jiffy_update();
1288
0ff53d09 1289 hrtimer_set_expires(&ts->sched_timer, next);
1ca8ec53
WL
1290 hrtimer_forward_now(&ts->sched_timer, tick_period);
1291 tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
bc7a34b8 1292 tick_nohz_activate(ts, NOHZ_MODE_LOWRES);
79bf2bb3
TG
1293}
1294
5acac1be 1295static inline void tick_nohz_irq_enter(void)
eed3b9cf 1296{
4a32fea9 1297 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
eed3b9cf
MS
1298 ktime_t now;
1299
1300 if (!ts->idle_active && !ts->tick_stopped)
1301 return;
1302 now = ktime_get();
1303 if (ts->idle_active)
e8fcaa5c 1304 tick_nohz_stop_idle(ts, now);
ff006732 1305 if (ts->tick_stopped)
eed3b9cf 1306 tick_nohz_update_jiffies(now);
eed3b9cf
MS
1307}
1308
79bf2bb3
TG
1309#else
1310
1311static inline void tick_nohz_switch_to_nohz(void) { }
5acac1be 1312static inline void tick_nohz_irq_enter(void) { }
bc7a34b8 1313static inline void tick_nohz_activate(struct tick_sched *ts, int mode) { }
79bf2bb3 1314
3451d024 1315#endif /* CONFIG_NO_HZ_COMMON */
79bf2bb3 1316
719254fa
TG
1317/*
1318 * Called from irq_enter to notify about the possible interruption of idle()
1319 */
5acac1be 1320void tick_irq_enter(void)
719254fa 1321{
e8fcaa5c 1322 tick_check_oneshot_broadcast_this_cpu();
5acac1be 1323 tick_nohz_irq_enter();
719254fa
TG
1324}
1325
79bf2bb3
TG
1326/*
1327 * High resolution timer specific code
1328 */
1329#ifdef CONFIG_HIGH_RES_TIMERS
1330/*
4c9dc641 1331 * We rearm the timer until we get disabled by the idle code.
351f181f 1332 * Called with interrupts disabled.
79bf2bb3
TG
1333 */
1334static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
1335{
1336 struct tick_sched *ts =
1337 container_of(timer, struct tick_sched, sched_timer);
79bf2bb3
TG
1338 struct pt_regs *regs = get_irq_regs();
1339 ktime_t now = ktime_get();
d3ed7824 1340
ff7de620 1341 tick_sched_do_timer(ts, now);
79bf2bb3
TG
1342
1343 /*
1344 * Do not call, when we are not in irq context and have
1345 * no valid regs pointer
1346 */
9e8f559b
FW
1347 if (regs)
1348 tick_sched_handle(ts, regs);
7c259045
FW
1349 else
1350 ts->next_tick = 0;
79bf2bb3 1351
2a16fc93
VK
1352 /* No need to reprogram if we are in idle or full dynticks mode */
1353 if (unlikely(ts->tick_stopped))
1354 return HRTIMER_NORESTART;
1355
79bf2bb3
TG
1356 hrtimer_forward(timer, now, tick_period);
1357
1358 return HRTIMER_RESTART;
1359}
1360
5307c955
MG
1361static int sched_skew_tick;
1362
62cf20b3
TG
1363static int __init skew_tick(char *str)
1364{
1365 get_option(&str, &sched_skew_tick);
1366
1367 return 0;
1368}
1369early_param("skew_tick", skew_tick);
1370
79bf2bb3
TG
1371/**
1372 * tick_setup_sched_timer - setup the tick emulation timer
1373 */
1374void tick_setup_sched_timer(void)
1375{
22127e93 1376 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
79bf2bb3
TG
1377 ktime_t now = ktime_get();
1378
1379 /*
1380 * Emulate tick processing via per-CPU hrtimers:
1381 */
902a9f9c 1382 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_HARD);
79bf2bb3 1383 ts->sched_timer.function = tick_sched_timer;
79bf2bb3 1384
0de7611a 1385 /* Get the next period (per-CPU) */
cc584b21 1386 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
79bf2bb3 1387
9c3f9e28 1388 /* Offset the tick to avert jiffies_lock contention. */
5307c955
MG
1389 if (sched_skew_tick) {
1390 u64 offset = ktime_to_ns(tick_period) >> 1;
1391 do_div(offset, num_possible_cpus());
1392 offset *= smp_processor_id();
1393 hrtimer_add_expires_ns(&ts->sched_timer, offset);
1394 }
1395
afc08b15 1396 hrtimer_forward(&ts->sched_timer, now, tick_period);
902a9f9c 1397 hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD);
bc7a34b8 1398 tick_nohz_activate(ts, NOHZ_MODE_HIGHRES);
79bf2bb3 1399}
3c4fbe5e 1400#endif /* HIGH_RES_TIMERS */
79bf2bb3 1401
3451d024 1402#if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
79bf2bb3
TG
1403void tick_cancel_sched_timer(int cpu)
1404{
1405 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
1406
3c4fbe5e 1407# ifdef CONFIG_HIGH_RES_TIMERS
79bf2bb3
TG
1408 if (ts->sched_timer.base)
1409 hrtimer_cancel(&ts->sched_timer);
3c4fbe5e 1410# endif
a7901766 1411
4b0c0f29 1412 memset(ts, 0, sizeof(*ts));
79bf2bb3 1413}
3c4fbe5e 1414#endif
79bf2bb3
TG
1415
1416/**
1417 * Async notification about clocksource changes
1418 */
1419void tick_clock_notify(void)
1420{
1421 int cpu;
1422
1423 for_each_possible_cpu(cpu)
1424 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
1425}
1426
1427/*
1428 * Async notification about clock event changes
1429 */
1430void tick_oneshot_notify(void)
1431{
22127e93 1432 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
79bf2bb3
TG
1433
1434 set_bit(0, &ts->check_clocks);
1435}
1436
1437/**
1438 * Check, if a change happened, which makes oneshot possible.
1439 *
1440 * Called cyclic from the hrtimer softirq (driven by the timer
1441 * softirq) allow_nohz signals, that we can switch into low-res nohz
1442 * mode, because high resolution timers are disabled (either compile
6b442bc8 1443 * or runtime). Called with interrupts disabled.
79bf2bb3
TG
1444 */
1445int tick_check_oneshot_change(int allow_nohz)
1446{
22127e93 1447 struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
79bf2bb3
TG
1448
1449 if (!test_and_clear_bit(0, &ts->check_clocks))
1450 return 0;
1451
1452 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
1453 return 0;
1454
cf4fc6cb 1455 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
79bf2bb3
TG
1456 return 0;
1457
1458 if (!allow_nohz)
1459 return 1;
1460
1461 tick_nohz_switch_to_nohz();
1462 return 0;
1463}