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