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