nohz: Ensure full dynticks CPUs are RCU nocbs
[linux-2.6-block.git] / kernel / time / tick-sched.c
CommitLineData
79bf2bb3
TG
1/*
2 * linux/kernel/time/tick-sched.c
3 *
4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
7 *
8 * No idle tick implementation for low and high resolution timers
9 *
10 * Started by: Thomas Gleixner and Ingo Molnar
11 *
b10db7f0 12 * Distribute under GPLv2.
79bf2bb3
TG
13 */
14#include <linux/cpu.h>
15#include <linux/err.h>
16#include <linux/hrtimer.h>
17#include <linux/interrupt.h>
18#include <linux/kernel_stat.h>
19#include <linux/percpu.h>
20#include <linux/profile.h>
21#include <linux/sched.h>
8083e4ad 22#include <linux/module.h>
00b42959 23#include <linux/irq_work.h>
79bf2bb3 24
9e203bcc
DM
25#include <asm/irq_regs.h>
26
79bf2bb3
TG
27#include "tick-internal.h"
28
29/*
30 * Per cpu nohz control structure
31 */
33a5f626 32DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
79bf2bb3
TG
33
34/*
d6ad4187 35 * The time, when the last jiffy update happened. Protected by jiffies_lock.
79bf2bb3
TG
36 */
37static ktime_t last_jiffies_update;
38
289f480a
IM
39struct tick_sched *tick_get_tick_sched(int cpu)
40{
41 return &per_cpu(tick_cpu_sched, cpu);
42}
43
79bf2bb3
TG
44/*
45 * Must be called with interrupts disabled !
46 */
47static void tick_do_update_jiffies64(ktime_t now)
48{
49 unsigned long ticks = 0;
50 ktime_t delta;
51
7a14ce1d 52 /*
d6ad4187 53 * Do a quick check without holding jiffies_lock:
7a14ce1d
IM
54 */
55 delta = ktime_sub(now, last_jiffies_update);
56 if (delta.tv64 < tick_period.tv64)
57 return;
58
d6ad4187
JS
59 /* Reevalute with jiffies_lock held */
60 write_seqlock(&jiffies_lock);
79bf2bb3
TG
61
62 delta = ktime_sub(now, last_jiffies_update);
63 if (delta.tv64 >= tick_period.tv64) {
64
65 delta = ktime_sub(delta, tick_period);
66 last_jiffies_update = ktime_add(last_jiffies_update,
67 tick_period);
68
69 /* Slow path for long timeouts */
70 if (unlikely(delta.tv64 >= tick_period.tv64)) {
71 s64 incr = ktime_to_ns(tick_period);
72
73 ticks = ktime_divns(delta, incr);
74
75 last_jiffies_update = ktime_add_ns(last_jiffies_update,
76 incr * ticks);
77 }
78 do_timer(++ticks);
49d670fb
TG
79
80 /* Keep the tick_next_period variable up to date */
81 tick_next_period = ktime_add(last_jiffies_update, tick_period);
79bf2bb3 82 }
d6ad4187 83 write_sequnlock(&jiffies_lock);
79bf2bb3
TG
84}
85
86/*
87 * Initialize and return retrieve the jiffies update.
88 */
89static ktime_t tick_init_jiffy_update(void)
90{
91 ktime_t period;
92
d6ad4187 93 write_seqlock(&jiffies_lock);
79bf2bb3
TG
94 /* Did we start the jiffies update yet ? */
95 if (last_jiffies_update.tv64 == 0)
96 last_jiffies_update = tick_next_period;
97 period = last_jiffies_update;
d6ad4187 98 write_sequnlock(&jiffies_lock);
79bf2bb3
TG
99 return period;
100}
101
5bb96226
FW
102
103static void tick_sched_do_timer(ktime_t now)
104{
105 int cpu = smp_processor_id();
106
3451d024 107#ifdef CONFIG_NO_HZ_COMMON
5bb96226
FW
108 /*
109 * Check if the do_timer duty was dropped. We don't care about
110 * concurrency: This happens only when the cpu in charge went
111 * into a long sleep. If two cpus happen to assign themself to
112 * this duty, then the jiffies update is still serialized by
9c3f9e28 113 * jiffies_lock.
5bb96226 114 */
a382bf93 115 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)
c5bfece2 116 && !tick_nohz_full_cpu(cpu))
5bb96226
FW
117 tick_do_timer_cpu = cpu;
118#endif
119
120 /* Check, if the jiffies need an update */
121 if (tick_do_timer_cpu == cpu)
122 tick_do_update_jiffies64(now);
123}
124
9e8f559b
FW
125static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
126{
3451d024 127#ifdef CONFIG_NO_HZ_COMMON
9e8f559b
FW
128 /*
129 * When we are idle and the tick is stopped, we have to touch
130 * the watchdog as we might not schedule for a really long
131 * time. This happens on complete idle SMP systems while
132 * waiting on the login prompt. We also increment the "start of
133 * idle" jiffy stamp so the idle accounting adjustment we do
134 * when we go busy again does not account too much ticks.
135 */
136 if (ts->tick_stopped) {
137 touch_softlockup_watchdog();
138 if (is_idle_task(current))
139 ts->idle_jiffies++;
140 }
94a57140 141#endif
9e8f559b
FW
142 update_process_times(user_mode(regs));
143 profile_tick(CPU_PROFILING);
144}
145
c5bfece2
FW
146#ifdef CONFIG_NO_HZ_FULL
147static cpumask_var_t nohz_full_mask;
148bool have_nohz_full_mask;
a831881b 149
c5bfece2 150int tick_nohz_full_cpu(int cpu)
a831881b 151{
c5bfece2 152 if (!have_nohz_full_mask)
a831881b
FW
153 return 0;
154
c5bfece2 155 return cpumask_test_cpu(cpu, nohz_full_mask);
a831881b
FW
156}
157
158/* Parse the boot-time nohz CPU list from the kernel parameters. */
c5bfece2 159static int __init tick_nohz_full_setup(char *str)
a831881b 160{
0453b435
FW
161 int cpu;
162
c5bfece2 163 alloc_bootmem_cpumask_var(&nohz_full_mask);
0453b435 164 if (cpulist_parse(str, nohz_full_mask) < 0) {
c5bfece2 165 pr_warning("NOHZ: Incorrect nohz_full cpumask\n");
0453b435
FW
166 return 1;
167 }
168
169 cpu = smp_processor_id();
170 if (cpumask_test_cpu(cpu, nohz_full_mask)) {
171 pr_warning("NO_HZ: Clearing %d from nohz_full range for timekeeping\n", cpu);
172 cpumask_clear_cpu(cpu, nohz_full_mask);
173 }
174 have_nohz_full_mask = true;
175
a831881b
FW
176 return 1;
177}
c5bfece2 178__setup("nohz_full=", tick_nohz_full_setup);
a831881b 179
a382bf93
FW
180static int __cpuinit tick_nohz_cpu_down_callback(struct notifier_block *nfb,
181 unsigned long action,
182 void *hcpu)
183{
184 unsigned int cpu = (unsigned long)hcpu;
185
186 switch (action & ~CPU_TASKS_FROZEN) {
187 case CPU_DOWN_PREPARE:
188 /*
189 * If we handle the timekeeping duty for full dynticks CPUs,
190 * we can't safely shutdown that CPU.
191 */
c5bfece2 192 if (have_nohz_full_mask && tick_do_timer_cpu == cpu)
a382bf93
FW
193 return -EINVAL;
194 break;
195 }
196 return NOTIFY_OK;
197}
198
1034fc2f
FW
199/*
200 * Worst case string length in chunks of CPU range seems 2 steps
201 * separations: 0,2,4,6,...
202 * This is NR_CPUS + sizeof('\0')
203 */
c5bfece2 204static char __initdata nohz_full_buf[NR_CPUS + 1];
1034fc2f 205
d1e43fa5 206void __init tick_nohz_init(void)
a831881b 207{
d1e43fa5
FW
208 int cpu;
209
210 if (!have_nohz_full_mask)
211 return;
212
213 cpu_notifier(tick_nohz_cpu_down_callback, 0);
214
215 /* Make sure full dynticks CPU are also RCU nocbs */
216 for_each_cpu(cpu, nohz_full_mask) {
217 if (!rcu_is_nocb_cpu(cpu)) {
218 pr_warning("NO_HZ: CPU %d is not RCU nocb: "
219 "cleared from nohz_full range", cpu);
220 cpumask_clear_cpu(cpu, nohz_full_mask);
221 }
222 }
a831881b 223
c5bfece2
FW
224 cpulist_scnprintf(nohz_full_buf, sizeof(nohz_full_buf), nohz_full_mask);
225 pr_info("NO_HZ: Full dynticks CPUs: %s.\n", nohz_full_buf);
a831881b 226}
a831881b 227#else
c5bfece2 228#define have_nohz_full_mask (0)
a831881b
FW
229#endif
230
79bf2bb3
TG
231/*
232 * NOHZ - aka dynamic tick functionality
233 */
3451d024 234#ifdef CONFIG_NO_HZ_COMMON
79bf2bb3
TG
235/*
236 * NO HZ enabled ?
237 */
9d2ad243 238int tick_nohz_enabled __read_mostly = 1;
79bf2bb3
TG
239
240/*
241 * Enable / Disable tickless mode
242 */
243static int __init setup_tick_nohz(char *str)
244{
245 if (!strcmp(str, "off"))
246 tick_nohz_enabled = 0;
247 else if (!strcmp(str, "on"))
248 tick_nohz_enabled = 1;
249 else
250 return 0;
251 return 1;
252}
253
254__setup("nohz=", setup_tick_nohz);
255
256/**
257 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
258 *
259 * Called from interrupt entry when the CPU was idle
260 *
261 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
262 * must be updated. Otherwise an interrupt handler could use a stale jiffy
263 * value. We do this unconditionally on any cpu, as we don't know whether the
264 * cpu, which has the update task assigned is in a long sleep.
265 */
eed3b9cf 266static void tick_nohz_update_jiffies(ktime_t now)
79bf2bb3
TG
267{
268 int cpu = smp_processor_id();
269 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
270 unsigned long flags;
79bf2bb3 271
5df7fa1c 272 ts->idle_waketime = now;
79bf2bb3
TG
273
274 local_irq_save(flags);
275 tick_do_update_jiffies64(now);
276 local_irq_restore(flags);
02ff3755
IM
277
278 touch_softlockup_watchdog();
79bf2bb3
TG
279}
280
595aac48
AV
281/*
282 * Updates the per cpu time idle statistics counters
283 */
8d63bf94 284static void
8c215bd3 285update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
6378ddb5 286{
eed3b9cf 287 ktime_t delta;
6378ddb5 288
595aac48
AV
289 if (ts->idle_active) {
290 delta = ktime_sub(now, ts->idle_entrytime);
8c215bd3 291 if (nr_iowait_cpu(cpu) > 0)
0224cf4c 292 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
6beea0cd
MH
293 else
294 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
8c7b09f4 295 ts->idle_entrytime = now;
595aac48 296 }
8d63bf94 297
e0e37c20 298 if (last_update_time)
8d63bf94
AV
299 *last_update_time = ktime_to_us(now);
300
595aac48
AV
301}
302
303static void tick_nohz_stop_idle(int cpu, ktime_t now)
304{
305 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
306
8c215bd3 307 update_ts_time_stats(cpu, ts, now, NULL);
eed3b9cf 308 ts->idle_active = 0;
56c7426b 309
eed3b9cf 310 sched_clock_idle_wakeup_event(0);
6378ddb5
VP
311}
312
8c215bd3 313static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
6378ddb5 314{
430ee881 315 ktime_t now = ktime_get();
595aac48 316
6378ddb5
VP
317 ts->idle_entrytime = now;
318 ts->idle_active = 1;
56c7426b 319 sched_clock_idle_sleep_event();
6378ddb5
VP
320 return now;
321}
322
b1f724c3
AV
323/**
324 * get_cpu_idle_time_us - get the total idle time of a cpu
325 * @cpu: CPU number to query
09a1d34f
MH
326 * @last_update_time: variable to store update time in. Do not update
327 * counters if NULL.
b1f724c3
AV
328 *
329 * Return the cummulative idle time (since boot) for a given
6beea0cd 330 * CPU, in microseconds.
b1f724c3
AV
331 *
332 * This time is measured via accounting rather than sampling,
333 * and is as accurate as ktime_get() is.
334 *
335 * This function returns -1 if NOHZ is not enabled.
336 */
6378ddb5
VP
337u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
338{
339 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
09a1d34f 340 ktime_t now, idle;
6378ddb5 341
8083e4ad 342 if (!tick_nohz_enabled)
343 return -1;
344
09a1d34f
MH
345 now = ktime_get();
346 if (last_update_time) {
347 update_ts_time_stats(cpu, ts, now, last_update_time);
348 idle = ts->idle_sleeptime;
349 } else {
350 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
351 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
352
353 idle = ktime_add(ts->idle_sleeptime, delta);
354 } else {
355 idle = ts->idle_sleeptime;
356 }
357 }
358
359 return ktime_to_us(idle);
8083e4ad 360
6378ddb5 361}
8083e4ad 362EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
6378ddb5 363
6beea0cd 364/**
0224cf4c
AV
365 * get_cpu_iowait_time_us - get the total iowait time of a cpu
366 * @cpu: CPU number to query
09a1d34f
MH
367 * @last_update_time: variable to store update time in. Do not update
368 * counters if NULL.
0224cf4c
AV
369 *
370 * Return the cummulative iowait time (since boot) for a given
371 * CPU, in microseconds.
372 *
373 * This time is measured via accounting rather than sampling,
374 * and is as accurate as ktime_get() is.
375 *
376 * This function returns -1 if NOHZ is not enabled.
377 */
378u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
379{
380 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
09a1d34f 381 ktime_t now, iowait;
0224cf4c
AV
382
383 if (!tick_nohz_enabled)
384 return -1;
385
09a1d34f
MH
386 now = ktime_get();
387 if (last_update_time) {
388 update_ts_time_stats(cpu, ts, now, last_update_time);
389 iowait = ts->iowait_sleeptime;
390 } else {
391 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
392 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
0224cf4c 393
09a1d34f
MH
394 iowait = ktime_add(ts->iowait_sleeptime, delta);
395 } else {
396 iowait = ts->iowait_sleeptime;
397 }
398 }
0224cf4c 399
09a1d34f 400 return ktime_to_us(iowait);
0224cf4c
AV
401}
402EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
403
84bf1bcc
FW
404static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
405 ktime_t now, int cpu)
79bf2bb3 406{
280f0677 407 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
84bf1bcc 408 ktime_t last_update, expires, ret = { .tv64 = 0 };
aa9b1630 409 unsigned long rcu_delta_jiffies;
4f86d3a8 410 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
98962465 411 u64 time_delta;
79bf2bb3 412
79bf2bb3
TG
413 /* Read jiffies and the time when jiffies were updated last */
414 do {
d6ad4187 415 seq = read_seqbegin(&jiffies_lock);
79bf2bb3
TG
416 last_update = last_jiffies_update;
417 last_jiffies = jiffies;
27185016 418 time_delta = timekeeping_max_deferment();
d6ad4187 419 } while (read_seqretry(&jiffies_lock, seq));
79bf2bb3 420
74876a98 421 if (rcu_needs_cpu(cpu, &rcu_delta_jiffies) ||
00b42959 422 arch_needs_cpu(cpu) || irq_work_needs_cpu()) {
3c5d92a0 423 next_jiffies = last_jiffies + 1;
6ba9b346 424 delta_jiffies = 1;
3c5d92a0
MS
425 } else {
426 /* Get the next timer wheel timer */
427 next_jiffies = get_next_timer_interrupt(last_jiffies);
428 delta_jiffies = next_jiffies - last_jiffies;
aa9b1630
PM
429 if (rcu_delta_jiffies < delta_jiffies) {
430 next_jiffies = last_jiffies + rcu_delta_jiffies;
431 delta_jiffies = rcu_delta_jiffies;
432 }
3c5d92a0 433 }
79bf2bb3
TG
434 /*
435 * Do not stop the tick, if we are only one off
436 * or if the cpu is required for rcu
437 */
6ba9b346 438 if (!ts->tick_stopped && delta_jiffies == 1)
79bf2bb3
TG
439 goto out;
440
441 /* Schedule the tick, if we are at least one jiffie off */
442 if ((long)delta_jiffies >= 1) {
443
00147449
WR
444 /*
445 * If this cpu is the one which updates jiffies, then
446 * give up the assignment and let it be taken by the
447 * cpu which runs the tick timer next, which might be
448 * this cpu as well. If we don't drop this here the
449 * jiffies might be stale and do_timer() never
27185016
TG
450 * invoked. Keep track of the fact that it was the one
451 * which had the do_timer() duty last. If this cpu is
452 * the one which had the do_timer() duty last, we
453 * limit the sleep time to the timekeeping
454 * max_deferement value which we retrieved
455 * above. Otherwise we can sleep as long as we want.
00147449 456 */
27185016 457 if (cpu == tick_do_timer_cpu) {
00147449 458 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
27185016
TG
459 ts->do_timer_last = 1;
460 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
461 time_delta = KTIME_MAX;
462 ts->do_timer_last = 0;
463 } else if (!ts->do_timer_last) {
464 time_delta = KTIME_MAX;
465 }
466
00147449 467 /*
98962465
JH
468 * calculate the expiry time for the next timer wheel
469 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
470 * that there is no timer pending or at least extremely
471 * far into the future (12 days for HZ=1000). In this
472 * case we set the expiry to the end of time.
473 */
474 if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
475 /*
476 * Calculate the time delta for the next timer event.
477 * If the time delta exceeds the maximum time delta
478 * permitted by the current clocksource then adjust
479 * the time delta accordingly to ensure the
480 * clocksource does not wrap.
481 */
482 time_delta = min_t(u64, time_delta,
483 tick_period.tv64 * delta_jiffies);
98962465 484 }
00147449 485
27185016
TG
486 if (time_delta < KTIME_MAX)
487 expires = ktime_add_ns(last_update, time_delta);
488 else
489 expires.tv64 = KTIME_MAX;
00147449 490
00147449
WR
491 /* Skip reprogram of event if its not changed */
492 if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
493 goto out;
494
84bf1bcc
FW
495 ret = expires;
496
79bf2bb3
TG
497 /*
498 * nohz_stop_sched_tick can be called several times before
499 * the nohz_restart_sched_tick is called. This happens when
500 * interrupts arrive which do not cause a reschedule. In the
501 * first call we save the current tick time, so we can restart
502 * the scheduler tick in nohz_restart_sched_tick.
503 */
504 if (!ts->tick_stopped) {
c1cc017c 505 nohz_balance_enter_idle(cpu);
5167e8d5 506 calc_load_enter_idle();
46cb4b7c 507
f5d411c9 508 ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
79bf2bb3 509 ts->tick_stopped = 1;
79bf2bb3 510 }
d3ed7824 511
eaad084b 512 /*
98962465
JH
513 * If the expiration time == KTIME_MAX, then
514 * in this case we simply stop the tick timer.
eaad084b 515 */
98962465 516 if (unlikely(expires.tv64 == KTIME_MAX)) {
eaad084b
TG
517 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
518 hrtimer_cancel(&ts->sched_timer);
519 goto out;
520 }
521
79bf2bb3
TG
522 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
523 hrtimer_start(&ts->sched_timer, expires,
5c333864 524 HRTIMER_MODE_ABS_PINNED);
79bf2bb3
TG
525 /* Check, if the timer was already in the past */
526 if (hrtimer_active(&ts->sched_timer))
527 goto out;
4c9dc641 528 } else if (!tick_program_event(expires, 0))
79bf2bb3
TG
529 goto out;
530 /*
531 * We are past the event already. So we crossed a
532 * jiffie boundary. Update jiffies and raise the
533 * softirq.
534 */
535 tick_do_update_jiffies64(ktime_get());
79bf2bb3
TG
536 }
537 raise_softirq_irqoff(TIMER_SOFTIRQ);
538out:
539 ts->next_jiffies = next_jiffies;
540 ts->last_jiffies = last_jiffies;
4f86d3a8 541 ts->sleep_length = ktime_sub(dev->next_event, now);
84bf1bcc
FW
542
543 return ret;
280f0677
FW
544}
545
5b39939a
FW
546static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
547{
548 /*
549 * If this cpu is offline and it is the one which updates
550 * jiffies, then give up the assignment and let it be taken by
551 * the cpu which runs the tick timer next. If we don't drop
552 * this here the jiffies might be stale and do_timer() never
553 * invoked.
554 */
555 if (unlikely(!cpu_online(cpu))) {
556 if (cpu == tick_do_timer_cpu)
557 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
558 }
559
560 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
561 return false;
562
563 if (need_resched())
564 return false;
565
566 if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
567 static int ratelimit;
568
803b0eba
PM
569 if (ratelimit < 10 &&
570 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK)) {
5b39939a
FW
571 printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
572 (unsigned int) local_softirq_pending());
573 ratelimit++;
574 }
575 return false;
576 }
577
c5bfece2 578 if (have_nohz_full_mask) {
a382bf93
FW
579 /*
580 * Keep the tick alive to guarantee timekeeping progression
581 * if there are full dynticks CPUs around
582 */
583 if (tick_do_timer_cpu == cpu)
584 return false;
585 /*
586 * Boot safety: make sure the timekeeping duty has been
587 * assigned before entering dyntick-idle mode,
588 */
589 if (tick_do_timer_cpu == TICK_DO_TIMER_NONE)
590 return false;
591 }
592
5b39939a
FW
593 return true;
594}
595
19f5f736
FW
596static void __tick_nohz_idle_enter(struct tick_sched *ts)
597{
84bf1bcc 598 ktime_t now, expires;
5b39939a 599 int cpu = smp_processor_id();
19f5f736 600
5b39939a 601 now = tick_nohz_start_idle(cpu, ts);
2ac0d98f 602
5b39939a
FW
603 if (can_stop_idle_tick(cpu, ts)) {
604 int was_stopped = ts->tick_stopped;
605
606 ts->idle_calls++;
84bf1bcc
FW
607
608 expires = tick_nohz_stop_sched_tick(ts, now, cpu);
609 if (expires.tv64 > 0LL) {
610 ts->idle_sleeps++;
611 ts->idle_expires = expires;
612 }
5b39939a
FW
613
614 if (!was_stopped && ts->tick_stopped)
615 ts->idle_jiffies = ts->last_jiffies;
616 }
280f0677
FW
617}
618
619/**
620 * tick_nohz_idle_enter - stop the idle tick from the idle task
621 *
622 * When the next event is more than a tick into the future, stop the idle tick
623 * Called when we start the idle loop.
2bbb6817 624 *
1268fbc7 625 * The arch is responsible of calling:
2bbb6817
FW
626 *
627 * - rcu_idle_enter() after its last use of RCU before the CPU is put
628 * to sleep.
629 * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
280f0677 630 */
1268fbc7 631void tick_nohz_idle_enter(void)
280f0677
FW
632{
633 struct tick_sched *ts;
634
1268fbc7
FW
635 WARN_ON_ONCE(irqs_disabled());
636
0db49b72
LT
637 /*
638 * Update the idle state in the scheduler domain hierarchy
639 * when tick_nohz_stop_sched_tick() is called from the idle loop.
640 * State will be updated to busy during the first busy tick after
641 * exiting idle.
642 */
643 set_cpu_sd_state_idle();
644
1268fbc7
FW
645 local_irq_disable();
646
280f0677
FW
647 ts = &__get_cpu_var(tick_cpu_sched);
648 /*
649 * set ts->inidle unconditionally. even if the system did not
650 * switch to nohz mode the cpu frequency governers rely on the
651 * update of the idle time accounting in tick_nohz_start_idle().
652 */
653 ts->inidle = 1;
19f5f736 654 __tick_nohz_idle_enter(ts);
1268fbc7
FW
655
656 local_irq_enable();
280f0677 657}
4dbd2771 658EXPORT_SYMBOL_GPL(tick_nohz_idle_enter);
280f0677
FW
659
660/**
661 * tick_nohz_irq_exit - update next tick event from interrupt exit
662 *
663 * When an interrupt fires while we are idle and it doesn't cause
664 * a reschedule, it may still add, modify or delete a timer, enqueue
665 * an RCU callback, etc...
666 * So we need to re-calculate and reprogram the next tick event.
667 */
668void tick_nohz_irq_exit(void)
669{
670 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
671
672 if (!ts->inidle)
673 return;
674
69a37bea
YS
675 /* Cancel the timer because CPU already waken up from the C-states*/
676 menu_hrtimer_cancel();
19f5f736 677 __tick_nohz_idle_enter(ts);
79bf2bb3
TG
678}
679
4f86d3a8
LB
680/**
681 * tick_nohz_get_sleep_length - return the length of the current sleep
682 *
683 * Called from power state control code with interrupts disabled
684 */
685ktime_t tick_nohz_get_sleep_length(void)
686{
687 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
688
689 return ts->sleep_length;
690}
691
c34bec5a
TG
692static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
693{
694 hrtimer_cancel(&ts->sched_timer);
f5d411c9 695 hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
c34bec5a
TG
696
697 while (1) {
698 /* Forward the time to expire in the future */
699 hrtimer_forward(&ts->sched_timer, now, tick_period);
700
701 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
268a3dcf 702 hrtimer_start_expires(&ts->sched_timer,
5c333864 703 HRTIMER_MODE_ABS_PINNED);
c34bec5a
TG
704 /* Check, if the timer was already in the past */
705 if (hrtimer_active(&ts->sched_timer))
706 break;
707 } else {
268a3dcf
TG
708 if (!tick_program_event(
709 hrtimer_get_expires(&ts->sched_timer), 0))
c34bec5a
TG
710 break;
711 }
6f103929 712 /* Reread time and update jiffies */
c34bec5a 713 now = ktime_get();
6f103929 714 tick_do_update_jiffies64(now);
c34bec5a
TG
715 }
716}
717
19f5f736 718static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
79bf2bb3 719{
79bf2bb3 720 /* Update jiffies first */
79bf2bb3 721 tick_do_update_jiffies64(now);
5aaa0b7a 722 update_cpu_load_nohz();
79bf2bb3 723
749c8814 724 calc_load_exit_idle();
2ac0d98f
FW
725 touch_softlockup_watchdog();
726 /*
727 * Cancel the scheduled timer and restore the tick
728 */
729 ts->tick_stopped = 0;
730 ts->idle_exittime = now;
731
732 tick_nohz_restart(ts, now);
733}
734
735static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
736{
3f4724ea 737#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
2ac0d98f 738 unsigned long ticks;
3f4724ea
FW
739
740 if (vtime_accounting_enabled())
741 return;
79bf2bb3
TG
742 /*
743 * We stopped the tick in idle. Update process times would miss the
744 * time we slept as update_process_times does only a 1 tick
745 * accounting. Enforce that this is accounted to idle !
746 */
747 ticks = jiffies - ts->idle_jiffies;
748 /*
749 * We might be one off. Do not randomly account a huge number of ticks!
750 */
79741dd3
MS
751 if (ticks && ticks < LONG_MAX)
752 account_idle_ticks(ticks);
753#endif
19f5f736
FW
754}
755
79bf2bb3 756/**
280f0677 757 * tick_nohz_idle_exit - restart the idle tick from the idle task
79bf2bb3
TG
758 *
759 * Restart the idle tick when the CPU is woken up from idle
280f0677
FW
760 * This also exit the RCU extended quiescent state. The CPU
761 * can use RCU again after this function is called.
79bf2bb3 762 */
280f0677 763void tick_nohz_idle_exit(void)
79bf2bb3
TG
764{
765 int cpu = smp_processor_id();
766 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
6378ddb5 767 ktime_t now;
79bf2bb3 768
6378ddb5 769 local_irq_disable();
2bbb6817 770
15f827be
FW
771 WARN_ON_ONCE(!ts->inidle);
772
773 ts->inidle = 0;
774
69a37bea
YS
775 /* Cancel the timer because CPU already waken up from the C-states*/
776 menu_hrtimer_cancel();
15f827be 777 if (ts->idle_active || ts->tick_stopped)
eed3b9cf
MS
778 now = ktime_get();
779
780 if (ts->idle_active)
781 tick_nohz_stop_idle(cpu, now);
6378ddb5 782
2ac0d98f 783 if (ts->tick_stopped) {
19f5f736 784 tick_nohz_restart_sched_tick(ts, now);
2ac0d98f 785 tick_nohz_account_idle_ticks(ts);
6378ddb5 786 }
79bf2bb3 787
79bf2bb3
TG
788 local_irq_enable();
789}
4dbd2771 790EXPORT_SYMBOL_GPL(tick_nohz_idle_exit);
79bf2bb3
TG
791
792static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
793{
794 hrtimer_forward(&ts->sched_timer, now, tick_period);
cc584b21 795 return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
79bf2bb3
TG
796}
797
798/*
799 * The nohz low res interrupt handler
800 */
801static void tick_nohz_handler(struct clock_event_device *dev)
802{
803 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
804 struct pt_regs *regs = get_irq_regs();
805 ktime_t now = ktime_get();
806
807 dev->next_event.tv64 = KTIME_MAX;
808
5bb96226 809 tick_sched_do_timer(now);
9e8f559b 810 tick_sched_handle(ts, regs);
79bf2bb3 811
79bf2bb3
TG
812 while (tick_nohz_reprogram(ts, now)) {
813 now = ktime_get();
814 tick_do_update_jiffies64(now);
815 }
816}
817
818/**
819 * tick_nohz_switch_to_nohz - switch to nohz mode
820 */
821static void tick_nohz_switch_to_nohz(void)
822{
823 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
824 ktime_t next;
825
826 if (!tick_nohz_enabled)
827 return;
828
829 local_irq_disable();
830 if (tick_switch_to_oneshot(tick_nohz_handler)) {
831 local_irq_enable();
832 return;
833 }
834
835 ts->nohz_mode = NOHZ_MODE_LOWRES;
836
837 /*
838 * Recycle the hrtimer in ts, so we can share the
839 * hrtimer_forward with the highres code.
840 */
841 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
842 /* Get the next period */
843 next = tick_init_jiffy_update();
844
845 for (;;) {
cc584b21 846 hrtimer_set_expires(&ts->sched_timer, next);
79bf2bb3
TG
847 if (!tick_program_event(next, 0))
848 break;
849 next = ktime_add(next, tick_period);
850 }
851 local_irq_enable();
79bf2bb3
TG
852}
853
fb02fbc1
TG
854/*
855 * When NOHZ is enabled and the tick is stopped, we need to kick the
856 * tick timer from irq_enter() so that the jiffies update is kept
857 * alive during long running softirqs. That's ugly as hell, but
858 * correctness is key even if we need to fix the offending softirq in
859 * the first place.
860 *
861 * Note, this is different to tick_nohz_restart. We just kick the
862 * timer and do not touch the other magic bits which need to be done
863 * when idle is left.
864 */
eed3b9cf 865static void tick_nohz_kick_tick(int cpu, ktime_t now)
fb02fbc1 866{
ae99286b
TG
867#if 0
868 /* Switch back to 2.6.27 behaviour */
869
fb02fbc1 870 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
eed3b9cf 871 ktime_t delta;
fb02fbc1 872
c4bd822e
TG
873 /*
874 * Do not touch the tick device, when the next expiry is either
875 * already reached or less/equal than the tick period.
876 */
268a3dcf 877 delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
c4bd822e
TG
878 if (delta.tv64 <= tick_period.tv64)
879 return;
880
881 tick_nohz_restart(ts, now);
ae99286b 882#endif
fb02fbc1
TG
883}
884
eed3b9cf
MS
885static inline void tick_check_nohz(int cpu)
886{
887 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
888 ktime_t now;
889
890 if (!ts->idle_active && !ts->tick_stopped)
891 return;
892 now = ktime_get();
893 if (ts->idle_active)
894 tick_nohz_stop_idle(cpu, now);
895 if (ts->tick_stopped) {
896 tick_nohz_update_jiffies(now);
897 tick_nohz_kick_tick(cpu, now);
898 }
899}
900
79bf2bb3
TG
901#else
902
903static inline void tick_nohz_switch_to_nohz(void) { }
eed3b9cf 904static inline void tick_check_nohz(int cpu) { }
79bf2bb3 905
3451d024 906#endif /* CONFIG_NO_HZ_COMMON */
79bf2bb3 907
719254fa
TG
908/*
909 * Called from irq_enter to notify about the possible interruption of idle()
910 */
911void tick_check_idle(int cpu)
912{
fb02fbc1 913 tick_check_oneshot_broadcast(cpu);
eed3b9cf 914 tick_check_nohz(cpu);
719254fa
TG
915}
916
79bf2bb3
TG
917/*
918 * High resolution timer specific code
919 */
920#ifdef CONFIG_HIGH_RES_TIMERS
921/*
4c9dc641 922 * We rearm the timer until we get disabled by the idle code.
351f181f 923 * Called with interrupts disabled.
79bf2bb3
TG
924 */
925static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
926{
927 struct tick_sched *ts =
928 container_of(timer, struct tick_sched, sched_timer);
79bf2bb3
TG
929 struct pt_regs *regs = get_irq_regs();
930 ktime_t now = ktime_get();
d3ed7824 931
5bb96226 932 tick_sched_do_timer(now);
79bf2bb3
TG
933
934 /*
935 * Do not call, when we are not in irq context and have
936 * no valid regs pointer
937 */
9e8f559b
FW
938 if (regs)
939 tick_sched_handle(ts, regs);
79bf2bb3 940
79bf2bb3
TG
941 hrtimer_forward(timer, now, tick_period);
942
943 return HRTIMER_RESTART;
944}
945
5307c955
MG
946static int sched_skew_tick;
947
62cf20b3
TG
948static int __init skew_tick(char *str)
949{
950 get_option(&str, &sched_skew_tick);
951
952 return 0;
953}
954early_param("skew_tick", skew_tick);
955
79bf2bb3
TG
956/**
957 * tick_setup_sched_timer - setup the tick emulation timer
958 */
959void tick_setup_sched_timer(void)
960{
961 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
962 ktime_t now = ktime_get();
963
964 /*
965 * Emulate tick processing via per-CPU hrtimers:
966 */
967 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
968 ts->sched_timer.function = tick_sched_timer;
79bf2bb3 969
3704540b 970 /* Get the next period (per cpu) */
cc584b21 971 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
79bf2bb3 972
9c3f9e28 973 /* Offset the tick to avert jiffies_lock contention. */
5307c955
MG
974 if (sched_skew_tick) {
975 u64 offset = ktime_to_ns(tick_period) >> 1;
976 do_div(offset, num_possible_cpus());
977 offset *= smp_processor_id();
978 hrtimer_add_expires_ns(&ts->sched_timer, offset);
979 }
980
79bf2bb3
TG
981 for (;;) {
982 hrtimer_forward(&ts->sched_timer, now, tick_period);
5c333864
AB
983 hrtimer_start_expires(&ts->sched_timer,
984 HRTIMER_MODE_ABS_PINNED);
79bf2bb3
TG
985 /* Check, if the timer was already in the past */
986 if (hrtimer_active(&ts->sched_timer))
987 break;
988 now = ktime_get();
989 }
990
3451d024 991#ifdef CONFIG_NO_HZ_COMMON
29c158e8 992 if (tick_nohz_enabled)
79bf2bb3
TG
993 ts->nohz_mode = NOHZ_MODE_HIGHRES;
994#endif
995}
3c4fbe5e 996#endif /* HIGH_RES_TIMERS */
79bf2bb3 997
3451d024 998#if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
79bf2bb3
TG
999void tick_cancel_sched_timer(int cpu)
1000{
1001 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
1002
3c4fbe5e 1003# ifdef CONFIG_HIGH_RES_TIMERS
79bf2bb3
TG
1004 if (ts->sched_timer.base)
1005 hrtimer_cancel(&ts->sched_timer);
3c4fbe5e 1006# endif
a7901766 1007
79bf2bb3
TG
1008 ts->nohz_mode = NOHZ_MODE_INACTIVE;
1009}
3c4fbe5e 1010#endif
79bf2bb3
TG
1011
1012/**
1013 * Async notification about clocksource changes
1014 */
1015void tick_clock_notify(void)
1016{
1017 int cpu;
1018
1019 for_each_possible_cpu(cpu)
1020 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
1021}
1022
1023/*
1024 * Async notification about clock event changes
1025 */
1026void tick_oneshot_notify(void)
1027{
1028 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
1029
1030 set_bit(0, &ts->check_clocks);
1031}
1032
1033/**
1034 * Check, if a change happened, which makes oneshot possible.
1035 *
1036 * Called cyclic from the hrtimer softirq (driven by the timer
1037 * softirq) allow_nohz signals, that we can switch into low-res nohz
1038 * mode, because high resolution timers are disabled (either compile
1039 * or runtime).
1040 */
1041int tick_check_oneshot_change(int allow_nohz)
1042{
1043 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
1044
1045 if (!test_and_clear_bit(0, &ts->check_clocks))
1046 return 0;
1047
1048 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
1049 return 0;
1050
cf4fc6cb 1051 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
79bf2bb3
TG
1052 return 0;
1053
1054 if (!allow_nohz)
1055 return 1;
1056
1057 tick_nohz_switch_to_nohz();
1058 return 0;
1059}