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