nohz: Make idle/iowait counter update conditional
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / time / tick-sched.c
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  *
12  *  Distribute under GPLv2.
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>
22 #include <linux/module.h>
23
24 #include <asm/irq_regs.h>
25
26 #include "tick-internal.h"
27
28 /*
29  * Per cpu nohz control structure
30  */
31 static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
32
33 /*
34  * The time, when the last jiffy update happened. Protected by xtime_lock.
35  */
36 static ktime_t last_jiffies_update;
37
38 struct tick_sched *tick_get_tick_sched(int cpu)
39 {
40         return &per_cpu(tick_cpu_sched, cpu);
41 }
42
43 /*
44  * Must be called with interrupts disabled !
45  */
46 static void tick_do_update_jiffies64(ktime_t now)
47 {
48         unsigned long ticks = 0;
49         ktime_t delta;
50
51         /*
52          * Do a quick check without holding xtime_lock:
53          */
54         delta = ktime_sub(now, last_jiffies_update);
55         if (delta.tv64 < tick_period.tv64)
56                 return;
57
58         /* Reevalute with xtime_lock held */
59         write_seqlock(&xtime_lock);
60
61         delta = ktime_sub(now, last_jiffies_update);
62         if (delta.tv64 >= tick_period.tv64) {
63
64                 delta = ktime_sub(delta, tick_period);
65                 last_jiffies_update = ktime_add(last_jiffies_update,
66                                                 tick_period);
67
68                 /* Slow path for long timeouts */
69                 if (unlikely(delta.tv64 >= tick_period.tv64)) {
70                         s64 incr = ktime_to_ns(tick_period);
71
72                         ticks = ktime_divns(delta, incr);
73
74                         last_jiffies_update = ktime_add_ns(last_jiffies_update,
75                                                            incr * ticks);
76                 }
77                 do_timer(++ticks);
78
79                 /* Keep the tick_next_period variable up to date */
80                 tick_next_period = ktime_add(last_jiffies_update, tick_period);
81         }
82         write_sequnlock(&xtime_lock);
83 }
84
85 /*
86  * Initialize and return retrieve the jiffies update.
87  */
88 static ktime_t tick_init_jiffy_update(void)
89 {
90         ktime_t period;
91
92         write_seqlock(&xtime_lock);
93         /* Did we start the jiffies update yet ? */
94         if (last_jiffies_update.tv64 == 0)
95                 last_jiffies_update = tick_next_period;
96         period = last_jiffies_update;
97         write_sequnlock(&xtime_lock);
98         return period;
99 }
100
101 /*
102  * NOHZ - aka dynamic tick functionality
103  */
104 #ifdef CONFIG_NO_HZ
105 /*
106  * NO HZ enabled ?
107  */
108 static int tick_nohz_enabled __read_mostly  = 1;
109
110 /*
111  * Enable / Disable tickless mode
112  */
113 static int __init setup_tick_nohz(char *str)
114 {
115         if (!strcmp(str, "off"))
116                 tick_nohz_enabled = 0;
117         else if (!strcmp(str, "on"))
118                 tick_nohz_enabled = 1;
119         else
120                 return 0;
121         return 1;
122 }
123
124 __setup("nohz=", setup_tick_nohz);
125
126 /**
127  * tick_nohz_update_jiffies - update jiffies when idle was interrupted
128  *
129  * Called from interrupt entry when the CPU was idle
130  *
131  * In case the sched_tick was stopped on this CPU, we have to check if jiffies
132  * must be updated. Otherwise an interrupt handler could use a stale jiffy
133  * value. We do this unconditionally on any cpu, as we don't know whether the
134  * cpu, which has the update task assigned is in a long sleep.
135  */
136 static void tick_nohz_update_jiffies(ktime_t now)
137 {
138         int cpu = smp_processor_id();
139         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
140         unsigned long flags;
141
142         cpumask_clear_cpu(cpu, nohz_cpu_mask);
143         ts->idle_waketime = now;
144
145         local_irq_save(flags);
146         tick_do_update_jiffies64(now);
147         local_irq_restore(flags);
148
149         touch_softlockup_watchdog();
150 }
151
152 /*
153  * Updates the per cpu time idle statistics counters
154  */
155 static void
156 update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
157 {
158         ktime_t delta;
159
160         if (ts->idle_active) {
161                 delta = ktime_sub(now, ts->idle_entrytime);
162                 if (nr_iowait_cpu(cpu) > 0)
163                         ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
164                 else
165                         ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
166                 ts->idle_entrytime = now;
167         }
168
169         if (last_update_time)
170                 *last_update_time = ktime_to_us(now);
171
172 }
173
174 static void tick_nohz_stop_idle(int cpu, ktime_t now)
175 {
176         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
177
178         update_ts_time_stats(cpu, ts, now, NULL);
179         ts->idle_active = 0;
180
181         sched_clock_idle_wakeup_event(0);
182 }
183
184 static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
185 {
186         ktime_t now;
187
188         now = ktime_get();
189
190         update_ts_time_stats(cpu, ts, now, NULL);
191
192         ts->idle_entrytime = now;
193         ts->idle_active = 1;
194         sched_clock_idle_sleep_event();
195         return now;
196 }
197
198 /**
199  * get_cpu_idle_time_us - get the total idle time of a cpu
200  * @cpu: CPU number to query
201  * @last_update_time: variable to store update time in. Do not update
202  * counters if NULL.
203  *
204  * Return the cummulative idle time (since boot) for a given
205  * CPU, in microseconds.
206  *
207  * This time is measured via accounting rather than sampling,
208  * and is as accurate as ktime_get() is.
209  *
210  * This function returns -1 if NOHZ is not enabled.
211  */
212 u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
213 {
214         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
215         ktime_t now, idle;
216
217         if (!tick_nohz_enabled)
218                 return -1;
219
220         now = ktime_get();
221         if (last_update_time) {
222                 update_ts_time_stats(cpu, ts, now, last_update_time);
223                 idle = ts->idle_sleeptime;
224         } else {
225                 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
226                         ktime_t delta = ktime_sub(now, ts->idle_entrytime);
227
228                         idle = ktime_add(ts->idle_sleeptime, delta);
229                 } else {
230                         idle = ts->idle_sleeptime;
231                 }
232         }
233
234         return ktime_to_us(idle);
235
236 }
237 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
238
239 /**
240  * get_cpu_iowait_time_us - get the total iowait time of a cpu
241  * @cpu: CPU number to query
242  * @last_update_time: variable to store update time in. Do not update
243  * counters if NULL.
244  *
245  * Return the cummulative iowait time (since boot) for a given
246  * CPU, in microseconds.
247  *
248  * This time is measured via accounting rather than sampling,
249  * and is as accurate as ktime_get() is.
250  *
251  * This function returns -1 if NOHZ is not enabled.
252  */
253 u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
254 {
255         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
256         ktime_t now, iowait;
257
258         if (!tick_nohz_enabled)
259                 return -1;
260
261         now = ktime_get();
262         if (last_update_time) {
263                 update_ts_time_stats(cpu, ts, now, last_update_time);
264                 iowait = ts->iowait_sleeptime;
265         } else {
266                 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
267                         ktime_t delta = ktime_sub(now, ts->idle_entrytime);
268
269                         iowait = ktime_add(ts->iowait_sleeptime, delta);
270                 } else {
271                         iowait = ts->iowait_sleeptime;
272                 }
273         }
274
275         return ktime_to_us(iowait);
276 }
277 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
278
279 /**
280  * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
281  *
282  * When the next event is more than a tick into the future, stop the idle tick
283  * Called either from the idle loop or from irq_exit() when an idle period was
284  * just interrupted by an interrupt which did not cause a reschedule.
285  */
286 void tick_nohz_stop_sched_tick(int inidle)
287 {
288         unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
289         struct tick_sched *ts;
290         ktime_t last_update, expires, now;
291         struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
292         u64 time_delta;
293         int cpu;
294
295         local_irq_save(flags);
296
297         cpu = smp_processor_id();
298         ts = &per_cpu(tick_cpu_sched, cpu);
299
300         /*
301          * Call to tick_nohz_start_idle stops the last_update_time from being
302          * updated. Thus, it must not be called in the event we are called from
303          * irq_exit() with the prior state different than idle.
304          */
305         if (!inidle && !ts->inidle)
306                 goto end;
307
308         /*
309          * Set ts->inidle unconditionally. Even if the system did not
310          * switch to NOHZ mode the cpu frequency governers rely on the
311          * update of the idle time accounting in tick_nohz_start_idle().
312          */
313         ts->inidle = 1;
314
315         now = tick_nohz_start_idle(cpu, ts);
316
317         /*
318          * If this cpu is offline and it is the one which updates
319          * jiffies, then give up the assignment and let it be taken by
320          * the cpu which runs the tick timer next. If we don't drop
321          * this here the jiffies might be stale and do_timer() never
322          * invoked.
323          */
324         if (unlikely(!cpu_online(cpu))) {
325                 if (cpu == tick_do_timer_cpu)
326                         tick_do_timer_cpu = TICK_DO_TIMER_NONE;
327         }
328
329         if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
330                 goto end;
331
332         if (need_resched())
333                 goto end;
334
335         if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
336                 static int ratelimit;
337
338                 if (ratelimit < 10) {
339                         printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
340                                (unsigned int) local_softirq_pending());
341                         ratelimit++;
342                 }
343                 goto end;
344         }
345
346         ts->idle_calls++;
347         /* Read jiffies and the time when jiffies were updated last */
348         do {
349                 seq = read_seqbegin(&xtime_lock);
350                 last_update = last_jiffies_update;
351                 last_jiffies = jiffies;
352                 time_delta = timekeeping_max_deferment();
353         } while (read_seqretry(&xtime_lock, seq));
354
355         if (rcu_needs_cpu(cpu) || printk_needs_cpu(cpu) ||
356             arch_needs_cpu(cpu)) {
357                 next_jiffies = last_jiffies + 1;
358                 delta_jiffies = 1;
359         } else {
360                 /* Get the next timer wheel timer */
361                 next_jiffies = get_next_timer_interrupt(last_jiffies);
362                 delta_jiffies = next_jiffies - last_jiffies;
363         }
364         /*
365          * Do not stop the tick, if we are only one off
366          * or if the cpu is required for rcu
367          */
368         if (!ts->tick_stopped && delta_jiffies == 1)
369                 goto out;
370
371         /* Schedule the tick, if we are at least one jiffie off */
372         if ((long)delta_jiffies >= 1) {
373
374                 /*
375                  * If this cpu is the one which updates jiffies, then
376                  * give up the assignment and let it be taken by the
377                  * cpu which runs the tick timer next, which might be
378                  * this cpu as well. If we don't drop this here the
379                  * jiffies might be stale and do_timer() never
380                  * invoked. Keep track of the fact that it was the one
381                  * which had the do_timer() duty last. If this cpu is
382                  * the one which had the do_timer() duty last, we
383                  * limit the sleep time to the timekeeping
384                  * max_deferement value which we retrieved
385                  * above. Otherwise we can sleep as long as we want.
386                  */
387                 if (cpu == tick_do_timer_cpu) {
388                         tick_do_timer_cpu = TICK_DO_TIMER_NONE;
389                         ts->do_timer_last = 1;
390                 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
391                         time_delta = KTIME_MAX;
392                         ts->do_timer_last = 0;
393                 } else if (!ts->do_timer_last) {
394                         time_delta = KTIME_MAX;
395                 }
396
397                 /*
398                  * calculate the expiry time for the next timer wheel
399                  * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
400                  * that there is no timer pending or at least extremely
401                  * far into the future (12 days for HZ=1000). In this
402                  * case we set the expiry to the end of time.
403                  */
404                 if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
405                         /*
406                          * Calculate the time delta for the next timer event.
407                          * If the time delta exceeds the maximum time delta
408                          * permitted by the current clocksource then adjust
409                          * the time delta accordingly to ensure the
410                          * clocksource does not wrap.
411                          */
412                         time_delta = min_t(u64, time_delta,
413                                            tick_period.tv64 * delta_jiffies);
414                 }
415
416                 if (time_delta < KTIME_MAX)
417                         expires = ktime_add_ns(last_update, time_delta);
418                 else
419                         expires.tv64 = KTIME_MAX;
420
421                 if (delta_jiffies > 1)
422                         cpumask_set_cpu(cpu, nohz_cpu_mask);
423
424                 /* Skip reprogram of event if its not changed */
425                 if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
426                         goto out;
427
428                 /*
429                  * nohz_stop_sched_tick can be called several times before
430                  * the nohz_restart_sched_tick is called. This happens when
431                  * interrupts arrive which do not cause a reschedule. In the
432                  * first call we save the current tick time, so we can restart
433                  * the scheduler tick in nohz_restart_sched_tick.
434                  */
435                 if (!ts->tick_stopped) {
436                         select_nohz_load_balancer(1);
437
438                         ts->idle_tick = hrtimer_get_expires(&ts->sched_timer);
439                         ts->tick_stopped = 1;
440                         ts->idle_jiffies = last_jiffies;
441                         rcu_enter_nohz();
442                 }
443
444                 ts->idle_sleeps++;
445
446                 /* Mark expires */
447                 ts->idle_expires = expires;
448
449                 /*
450                  * If the expiration time == KTIME_MAX, then
451                  * in this case we simply stop the tick timer.
452                  */
453                  if (unlikely(expires.tv64 == KTIME_MAX)) {
454                         if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
455                                 hrtimer_cancel(&ts->sched_timer);
456                         goto out;
457                 }
458
459                 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
460                         hrtimer_start(&ts->sched_timer, expires,
461                                       HRTIMER_MODE_ABS_PINNED);
462                         /* Check, if the timer was already in the past */
463                         if (hrtimer_active(&ts->sched_timer))
464                                 goto out;
465                 } else if (!tick_program_event(expires, 0))
466                                 goto out;
467                 /*
468                  * We are past the event already. So we crossed a
469                  * jiffie boundary. Update jiffies and raise the
470                  * softirq.
471                  */
472                 tick_do_update_jiffies64(ktime_get());
473                 cpumask_clear_cpu(cpu, nohz_cpu_mask);
474         }
475         raise_softirq_irqoff(TIMER_SOFTIRQ);
476 out:
477         ts->next_jiffies = next_jiffies;
478         ts->last_jiffies = last_jiffies;
479         ts->sleep_length = ktime_sub(dev->next_event, now);
480 end:
481         local_irq_restore(flags);
482 }
483
484 /**
485  * tick_nohz_get_sleep_length - return the length of the current sleep
486  *
487  * Called from power state control code with interrupts disabled
488  */
489 ktime_t tick_nohz_get_sleep_length(void)
490 {
491         struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
492
493         return ts->sleep_length;
494 }
495
496 static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
497 {
498         hrtimer_cancel(&ts->sched_timer);
499         hrtimer_set_expires(&ts->sched_timer, ts->idle_tick);
500
501         while (1) {
502                 /* Forward the time to expire in the future */
503                 hrtimer_forward(&ts->sched_timer, now, tick_period);
504
505                 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
506                         hrtimer_start_expires(&ts->sched_timer,
507                                               HRTIMER_MODE_ABS_PINNED);
508                         /* Check, if the timer was already in the past */
509                         if (hrtimer_active(&ts->sched_timer))
510                                 break;
511                 } else {
512                         if (!tick_program_event(
513                                 hrtimer_get_expires(&ts->sched_timer), 0))
514                                 break;
515                 }
516                 /* Update jiffies and reread time */
517                 tick_do_update_jiffies64(now);
518                 now = ktime_get();
519         }
520 }
521
522 /**
523  * tick_nohz_restart_sched_tick - restart the idle tick from the idle task
524  *
525  * Restart the idle tick when the CPU is woken up from idle
526  */
527 void tick_nohz_restart_sched_tick(void)
528 {
529         int cpu = smp_processor_id();
530         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
531 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
532         unsigned long ticks;
533 #endif
534         ktime_t now;
535
536         local_irq_disable();
537         if (ts->idle_active || (ts->inidle && ts->tick_stopped))
538                 now = ktime_get();
539
540         if (ts->idle_active)
541                 tick_nohz_stop_idle(cpu, now);
542
543         if (!ts->inidle || !ts->tick_stopped) {
544                 ts->inidle = 0;
545                 local_irq_enable();
546                 return;
547         }
548
549         ts->inidle = 0;
550
551         rcu_exit_nohz();
552
553         /* Update jiffies first */
554         select_nohz_load_balancer(0);
555         tick_do_update_jiffies64(now);
556         cpumask_clear_cpu(cpu, nohz_cpu_mask);
557
558 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
559         /*
560          * We stopped the tick in idle. Update process times would miss the
561          * time we slept as update_process_times does only a 1 tick
562          * accounting. Enforce that this is accounted to idle !
563          */
564         ticks = jiffies - ts->idle_jiffies;
565         /*
566          * We might be one off. Do not randomly account a huge number of ticks!
567          */
568         if (ticks && ticks < LONG_MAX)
569                 account_idle_ticks(ticks);
570 #endif
571
572         touch_softlockup_watchdog();
573         /*
574          * Cancel the scheduled timer and restore the tick
575          */
576         ts->tick_stopped  = 0;
577         ts->idle_exittime = now;
578
579         tick_nohz_restart(ts, now);
580
581         local_irq_enable();
582 }
583
584 static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
585 {
586         hrtimer_forward(&ts->sched_timer, now, tick_period);
587         return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
588 }
589
590 /*
591  * The nohz low res interrupt handler
592  */
593 static void tick_nohz_handler(struct clock_event_device *dev)
594 {
595         struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
596         struct pt_regs *regs = get_irq_regs();
597         int cpu = smp_processor_id();
598         ktime_t now = ktime_get();
599
600         dev->next_event.tv64 = KTIME_MAX;
601
602         /*
603          * Check if the do_timer duty was dropped. We don't care about
604          * concurrency: This happens only when the cpu in charge went
605          * into a long sleep. If two cpus happen to assign themself to
606          * this duty, then the jiffies update is still serialized by
607          * xtime_lock.
608          */
609         if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
610                 tick_do_timer_cpu = cpu;
611
612         /* Check, if the jiffies need an update */
613         if (tick_do_timer_cpu == cpu)
614                 tick_do_update_jiffies64(now);
615
616         /*
617          * When we are idle and the tick is stopped, we have to touch
618          * the watchdog as we might not schedule for a really long
619          * time. This happens on complete idle SMP systems while
620          * waiting on the login prompt. We also increment the "start
621          * of idle" jiffy stamp so the idle accounting adjustment we
622          * do when we go busy again does not account too much ticks.
623          */
624         if (ts->tick_stopped) {
625                 touch_softlockup_watchdog();
626                 ts->idle_jiffies++;
627         }
628
629         update_process_times(user_mode(regs));
630         profile_tick(CPU_PROFILING);
631
632         while (tick_nohz_reprogram(ts, now)) {
633                 now = ktime_get();
634                 tick_do_update_jiffies64(now);
635         }
636 }
637
638 /**
639  * tick_nohz_switch_to_nohz - switch to nohz mode
640  */
641 static void tick_nohz_switch_to_nohz(void)
642 {
643         struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
644         ktime_t next;
645
646         if (!tick_nohz_enabled)
647                 return;
648
649         local_irq_disable();
650         if (tick_switch_to_oneshot(tick_nohz_handler)) {
651                 local_irq_enable();
652                 return;
653         }
654
655         ts->nohz_mode = NOHZ_MODE_LOWRES;
656
657         /*
658          * Recycle the hrtimer in ts, so we can share the
659          * hrtimer_forward with the highres code.
660          */
661         hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
662         /* Get the next period */
663         next = tick_init_jiffy_update();
664
665         for (;;) {
666                 hrtimer_set_expires(&ts->sched_timer, next);
667                 if (!tick_program_event(next, 0))
668                         break;
669                 next = ktime_add(next, tick_period);
670         }
671         local_irq_enable();
672
673         printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
674 }
675
676 /*
677  * When NOHZ is enabled and the tick is stopped, we need to kick the
678  * tick timer from irq_enter() so that the jiffies update is kept
679  * alive during long running softirqs. That's ugly as hell, but
680  * correctness is key even if we need to fix the offending softirq in
681  * the first place.
682  *
683  * Note, this is different to tick_nohz_restart. We just kick the
684  * timer and do not touch the other magic bits which need to be done
685  * when idle is left.
686  */
687 static void tick_nohz_kick_tick(int cpu, ktime_t now)
688 {
689 #if 0
690         /* Switch back to 2.6.27 behaviour */
691
692         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
693         ktime_t delta;
694
695         /*
696          * Do not touch the tick device, when the next expiry is either
697          * already reached or less/equal than the tick period.
698          */
699         delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
700         if (delta.tv64 <= tick_period.tv64)
701                 return;
702
703         tick_nohz_restart(ts, now);
704 #endif
705 }
706
707 static inline void tick_check_nohz(int cpu)
708 {
709         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
710         ktime_t now;
711
712         if (!ts->idle_active && !ts->tick_stopped)
713                 return;
714         now = ktime_get();
715         if (ts->idle_active)
716                 tick_nohz_stop_idle(cpu, now);
717         if (ts->tick_stopped) {
718                 tick_nohz_update_jiffies(now);
719                 tick_nohz_kick_tick(cpu, now);
720         }
721 }
722
723 #else
724
725 static inline void tick_nohz_switch_to_nohz(void) { }
726 static inline void tick_check_nohz(int cpu) { }
727
728 #endif /* NO_HZ */
729
730 /*
731  * Called from irq_enter to notify about the possible interruption of idle()
732  */
733 void tick_check_idle(int cpu)
734 {
735         tick_check_oneshot_broadcast(cpu);
736         tick_check_nohz(cpu);
737 }
738
739 /*
740  * High resolution timer specific code
741  */
742 #ifdef CONFIG_HIGH_RES_TIMERS
743 /*
744  * We rearm the timer until we get disabled by the idle code.
745  * Called with interrupts disabled and timer->base->cpu_base->lock held.
746  */
747 static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
748 {
749         struct tick_sched *ts =
750                 container_of(timer, struct tick_sched, sched_timer);
751         struct pt_regs *regs = get_irq_regs();
752         ktime_t now = ktime_get();
753         int cpu = smp_processor_id();
754
755 #ifdef CONFIG_NO_HZ
756         /*
757          * Check if the do_timer duty was dropped. We don't care about
758          * concurrency: This happens only when the cpu in charge went
759          * into a long sleep. If two cpus happen to assign themself to
760          * this duty, then the jiffies update is still serialized by
761          * xtime_lock.
762          */
763         if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
764                 tick_do_timer_cpu = cpu;
765 #endif
766
767         /* Check, if the jiffies need an update */
768         if (tick_do_timer_cpu == cpu)
769                 tick_do_update_jiffies64(now);
770
771         /*
772          * Do not call, when we are not in irq context and have
773          * no valid regs pointer
774          */
775         if (regs) {
776                 /*
777                  * When we are idle and the tick is stopped, we have to touch
778                  * the watchdog as we might not schedule for a really long
779                  * time. This happens on complete idle SMP systems while
780                  * waiting on the login prompt. We also increment the "start of
781                  * idle" jiffy stamp so the idle accounting adjustment we do
782                  * when we go busy again does not account too much ticks.
783                  */
784                 if (ts->tick_stopped) {
785                         touch_softlockup_watchdog();
786                         ts->idle_jiffies++;
787                 }
788                 update_process_times(user_mode(regs));
789                 profile_tick(CPU_PROFILING);
790         }
791
792         hrtimer_forward(timer, now, tick_period);
793
794         return HRTIMER_RESTART;
795 }
796
797 /**
798  * tick_setup_sched_timer - setup the tick emulation timer
799  */
800 void tick_setup_sched_timer(void)
801 {
802         struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
803         ktime_t now = ktime_get();
804
805         /*
806          * Emulate tick processing via per-CPU hrtimers:
807          */
808         hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
809         ts->sched_timer.function = tick_sched_timer;
810
811         /* Get the next period (per cpu) */
812         hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
813
814         for (;;) {
815                 hrtimer_forward(&ts->sched_timer, now, tick_period);
816                 hrtimer_start_expires(&ts->sched_timer,
817                                       HRTIMER_MODE_ABS_PINNED);
818                 /* Check, if the timer was already in the past */
819                 if (hrtimer_active(&ts->sched_timer))
820                         break;
821                 now = ktime_get();
822         }
823
824 #ifdef CONFIG_NO_HZ
825         if (tick_nohz_enabled) {
826                 ts->nohz_mode = NOHZ_MODE_HIGHRES;
827                 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
828         }
829 #endif
830 }
831 #endif /* HIGH_RES_TIMERS */
832
833 #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
834 void tick_cancel_sched_timer(int cpu)
835 {
836         struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
837
838 # ifdef CONFIG_HIGH_RES_TIMERS
839         if (ts->sched_timer.base)
840                 hrtimer_cancel(&ts->sched_timer);
841 # endif
842
843         ts->nohz_mode = NOHZ_MODE_INACTIVE;
844 }
845 #endif
846
847 /**
848  * Async notification about clocksource changes
849  */
850 void tick_clock_notify(void)
851 {
852         int cpu;
853
854         for_each_possible_cpu(cpu)
855                 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
856 }
857
858 /*
859  * Async notification about clock event changes
860  */
861 void tick_oneshot_notify(void)
862 {
863         struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
864
865         set_bit(0, &ts->check_clocks);
866 }
867
868 /**
869  * Check, if a change happened, which makes oneshot possible.
870  *
871  * Called cyclic from the hrtimer softirq (driven by the timer
872  * softirq) allow_nohz signals, that we can switch into low-res nohz
873  * mode, because high resolution timers are disabled (either compile
874  * or runtime).
875  */
876 int tick_check_oneshot_change(int allow_nohz)
877 {
878         struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
879
880         if (!test_and_clear_bit(0, &ts->check_clocks))
881                 return 0;
882
883         if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
884                 return 0;
885
886         if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
887                 return 0;
888
889         if (!allow_nohz)
890                 return 1;
891
892         tick_nohz_switch_to_nohz();
893         return 0;
894 }