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