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