]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/time/tick-common.c
Merge tag 'trace-v5.6-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[mirror_ubuntu-jammy-kernel.git] / kernel / time / tick-common.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This file contains the base functions to manage periodic tick
4 * related events.
5 *
6 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
7 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
8 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
9 */
10 #include <linux/cpu.h>
11 #include <linux/err.h>
12 #include <linux/hrtimer.h>
13 #include <linux/interrupt.h>
14 #include <linux/nmi.h>
15 #include <linux/percpu.h>
16 #include <linux/profile.h>
17 #include <linux/sched.h>
18 #include <linux/module.h>
19 #include <trace/events/power.h>
20
21 #include <asm/irq_regs.h>
22
23 #include "tick-internal.h"
24
25 /*
26 * Tick devices
27 */
28 DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
29 /*
30 * Tick next event: keeps track of the tick time
31 */
32 ktime_t tick_next_period;
33 ktime_t tick_period;
34
35 /*
36 * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
37 * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
38 * variable has two functions:
39 *
40 * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
41 * timekeeping lock all at once. Only the CPU which is assigned to do the
42 * update is handling it.
43 *
44 * 2) Hand off the duty in the NOHZ idle case by setting the value to
45 * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
46 * at it will take over and keep the time keeping alive. The handover
47 * procedure also covers cpu hotplug.
48 */
49 int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
50 #ifdef CONFIG_NO_HZ_FULL
51 /*
52 * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns
53 * tick_do_timer_cpu and it should be taken over by an eligible secondary
54 * when one comes online.
55 */
56 static int tick_do_timer_boot_cpu __read_mostly = -1;
57 #endif
58
59 /*
60 * Debugging: see timer_list.c
61 */
62 struct tick_device *tick_get_device(int cpu)
63 {
64 return &per_cpu(tick_cpu_device, cpu);
65 }
66
67 /**
68 * tick_is_oneshot_available - check for a oneshot capable event device
69 */
70 int tick_is_oneshot_available(void)
71 {
72 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
73
74 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
75 return 0;
76 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
77 return 1;
78 return tick_broadcast_oneshot_available();
79 }
80
81 /*
82 * Periodic tick
83 */
84 static void tick_periodic(int cpu)
85 {
86 if (tick_do_timer_cpu == cpu) {
87 write_seqlock(&jiffies_lock);
88
89 /* Keep track of the next tick event */
90 tick_next_period = ktime_add(tick_next_period, tick_period);
91
92 do_timer(1);
93 write_sequnlock(&jiffies_lock);
94 update_wall_time();
95 }
96
97 update_process_times(user_mode(get_irq_regs()));
98 profile_tick(CPU_PROFILING);
99 }
100
101 /*
102 * Event handler for periodic ticks
103 */
104 void tick_handle_periodic(struct clock_event_device *dev)
105 {
106 int cpu = smp_processor_id();
107 ktime_t next = dev->next_event;
108
109 tick_periodic(cpu);
110
111 #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
112 /*
113 * The cpu might have transitioned to HIGHRES or NOHZ mode via
114 * update_process_times() -> run_local_timers() ->
115 * hrtimer_run_queues().
116 */
117 if (dev->event_handler != tick_handle_periodic)
118 return;
119 #endif
120
121 if (!clockevent_state_oneshot(dev))
122 return;
123 for (;;) {
124 /*
125 * Setup the next period for devices, which do not have
126 * periodic mode:
127 */
128 next = ktime_add(next, tick_period);
129
130 if (!clockevents_program_event(dev, next, false))
131 return;
132 /*
133 * Have to be careful here. If we're in oneshot mode,
134 * before we call tick_periodic() in a loop, we need
135 * to be sure we're using a real hardware clocksource.
136 * Otherwise we could get trapped in an infinite
137 * loop, as the tick_periodic() increments jiffies,
138 * which then will increment time, possibly causing
139 * the loop to trigger again and again.
140 */
141 if (timekeeping_valid_for_hres())
142 tick_periodic(cpu);
143 }
144 }
145
146 /*
147 * Setup the device for a periodic tick
148 */
149 void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
150 {
151 tick_set_periodic_handler(dev, broadcast);
152
153 /* Broadcast setup ? */
154 if (!tick_device_is_functional(dev))
155 return;
156
157 if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
158 !tick_broadcast_oneshot_active()) {
159 clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
160 } else {
161 unsigned int seq;
162 ktime_t next;
163
164 do {
165 seq = read_seqbegin(&jiffies_lock);
166 next = tick_next_period;
167 } while (read_seqretry(&jiffies_lock, seq));
168
169 clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
170
171 for (;;) {
172 if (!clockevents_program_event(dev, next, false))
173 return;
174 next = ktime_add(next, tick_period);
175 }
176 }
177 }
178
179 #ifdef CONFIG_NO_HZ_FULL
180 static void giveup_do_timer(void *info)
181 {
182 int cpu = *(unsigned int *)info;
183
184 WARN_ON(tick_do_timer_cpu != smp_processor_id());
185
186 tick_do_timer_cpu = cpu;
187 }
188
189 static void tick_take_do_timer_from_boot(void)
190 {
191 int cpu = smp_processor_id();
192 int from = tick_do_timer_boot_cpu;
193
194 if (from >= 0 && from != cpu)
195 smp_call_function_single(from, giveup_do_timer, &cpu, 1);
196 }
197 #endif
198
199 /*
200 * Setup the tick device
201 */
202 static void tick_setup_device(struct tick_device *td,
203 struct clock_event_device *newdev, int cpu,
204 const struct cpumask *cpumask)
205 {
206 void (*handler)(struct clock_event_device *) = NULL;
207 ktime_t next_event = 0;
208
209 /*
210 * First device setup ?
211 */
212 if (!td->evtdev) {
213 /*
214 * If no cpu took the do_timer update, assign it to
215 * this cpu:
216 */
217 if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
218 tick_do_timer_cpu = cpu;
219
220 tick_next_period = ktime_get();
221 tick_period = NSEC_PER_SEC / HZ;
222 #ifdef CONFIG_NO_HZ_FULL
223 /*
224 * The boot CPU may be nohz_full, in which case set
225 * tick_do_timer_boot_cpu so the first housekeeping
226 * secondary that comes up will take do_timer from
227 * us.
228 */
229 if (tick_nohz_full_cpu(cpu))
230 tick_do_timer_boot_cpu = cpu;
231
232 } else if (tick_do_timer_boot_cpu != -1 &&
233 !tick_nohz_full_cpu(cpu)) {
234 tick_take_do_timer_from_boot();
235 tick_do_timer_boot_cpu = -1;
236 WARN_ON(tick_do_timer_cpu != cpu);
237 #endif
238 }
239
240 /*
241 * Startup in periodic mode first.
242 */
243 td->mode = TICKDEV_MODE_PERIODIC;
244 } else {
245 handler = td->evtdev->event_handler;
246 next_event = td->evtdev->next_event;
247 td->evtdev->event_handler = clockevents_handle_noop;
248 }
249
250 td->evtdev = newdev;
251
252 /*
253 * When the device is not per cpu, pin the interrupt to the
254 * current cpu:
255 */
256 if (!cpumask_equal(newdev->cpumask, cpumask))
257 irq_set_affinity(newdev->irq, cpumask);
258
259 /*
260 * When global broadcasting is active, check if the current
261 * device is registered as a placeholder for broadcast mode.
262 * This allows us to handle this x86 misfeature in a generic
263 * way. This function also returns !=0 when we keep the
264 * current active broadcast state for this CPU.
265 */
266 if (tick_device_uses_broadcast(newdev, cpu))
267 return;
268
269 if (td->mode == TICKDEV_MODE_PERIODIC)
270 tick_setup_periodic(newdev, 0);
271 else
272 tick_setup_oneshot(newdev, handler, next_event);
273 }
274
275 void tick_install_replacement(struct clock_event_device *newdev)
276 {
277 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
278 int cpu = smp_processor_id();
279
280 clockevents_exchange_device(td->evtdev, newdev);
281 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
282 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
283 tick_oneshot_notify();
284 }
285
286 static bool tick_check_percpu(struct clock_event_device *curdev,
287 struct clock_event_device *newdev, int cpu)
288 {
289 if (!cpumask_test_cpu(cpu, newdev->cpumask))
290 return false;
291 if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
292 return true;
293 /* Check if irq affinity can be set */
294 if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
295 return false;
296 /* Prefer an existing cpu local device */
297 if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
298 return false;
299 return true;
300 }
301
302 static bool tick_check_preferred(struct clock_event_device *curdev,
303 struct clock_event_device *newdev)
304 {
305 /* Prefer oneshot capable device */
306 if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
307 if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
308 return false;
309 if (tick_oneshot_mode_active())
310 return false;
311 }
312
313 /*
314 * Use the higher rated one, but prefer a CPU local device with a lower
315 * rating than a non-CPU local device
316 */
317 return !curdev ||
318 newdev->rating > curdev->rating ||
319 !cpumask_equal(curdev->cpumask, newdev->cpumask);
320 }
321
322 /*
323 * Check whether the new device is a better fit than curdev. curdev
324 * can be NULL !
325 */
326 bool tick_check_replacement(struct clock_event_device *curdev,
327 struct clock_event_device *newdev)
328 {
329 if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
330 return false;
331
332 return tick_check_preferred(curdev, newdev);
333 }
334
335 /*
336 * Check, if the new registered device should be used. Called with
337 * clockevents_lock held and interrupts disabled.
338 */
339 void tick_check_new_device(struct clock_event_device *newdev)
340 {
341 struct clock_event_device *curdev;
342 struct tick_device *td;
343 int cpu;
344
345 cpu = smp_processor_id();
346 td = &per_cpu(tick_cpu_device, cpu);
347 curdev = td->evtdev;
348
349 /* cpu local device ? */
350 if (!tick_check_percpu(curdev, newdev, cpu))
351 goto out_bc;
352
353 /* Preference decision */
354 if (!tick_check_preferred(curdev, newdev))
355 goto out_bc;
356
357 if (!try_module_get(newdev->owner))
358 return;
359
360 /*
361 * Replace the eventually existing device by the new
362 * device. If the current device is the broadcast device, do
363 * not give it back to the clockevents layer !
364 */
365 if (tick_is_broadcast_device(curdev)) {
366 clockevents_shutdown(curdev);
367 curdev = NULL;
368 }
369 clockevents_exchange_device(curdev, newdev);
370 tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
371 if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
372 tick_oneshot_notify();
373 return;
374
375 out_bc:
376 /*
377 * Can the new device be used as a broadcast device ?
378 */
379 tick_install_broadcast_device(newdev);
380 }
381
382 /**
383 * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
384 * @state: The target state (enter/exit)
385 *
386 * The system enters/leaves a state, where affected devices might stop
387 * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
388 *
389 * Called with interrupts disabled, so clockevents_lock is not
390 * required here because the local clock event device cannot go away
391 * under us.
392 */
393 int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
394 {
395 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
396
397 if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
398 return 0;
399
400 return __tick_broadcast_oneshot_control(state);
401 }
402 EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
403
404 #ifdef CONFIG_HOTPLUG_CPU
405 /*
406 * Transfer the do_timer job away from a dying cpu.
407 *
408 * Called with interrupts disabled. Not locking required. If
409 * tick_do_timer_cpu is owned by this cpu, nothing can change it.
410 */
411 void tick_handover_do_timer(void)
412 {
413 if (tick_do_timer_cpu == smp_processor_id()) {
414 int cpu = cpumask_first(cpu_online_mask);
415
416 tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
417 TICK_DO_TIMER_NONE;
418 }
419 }
420
421 /*
422 * Shutdown an event device on a given cpu:
423 *
424 * This is called on a life CPU, when a CPU is dead. So we cannot
425 * access the hardware device itself.
426 * We just set the mode and remove it from the lists.
427 */
428 void tick_shutdown(unsigned int cpu)
429 {
430 struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
431 struct clock_event_device *dev = td->evtdev;
432
433 td->mode = TICKDEV_MODE_PERIODIC;
434 if (dev) {
435 /*
436 * Prevent that the clock events layer tries to call
437 * the set mode function!
438 */
439 clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
440 clockevents_exchange_device(dev, NULL);
441 dev->event_handler = clockevents_handle_noop;
442 td->evtdev = NULL;
443 }
444 }
445 #endif
446
447 /**
448 * tick_suspend_local - Suspend the local tick device
449 *
450 * Called from the local cpu for freeze with interrupts disabled.
451 *
452 * No locks required. Nothing can change the per cpu device.
453 */
454 void tick_suspend_local(void)
455 {
456 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
457
458 clockevents_shutdown(td->evtdev);
459 }
460
461 /**
462 * tick_resume_local - Resume the local tick device
463 *
464 * Called from the local CPU for unfreeze or XEN resume magic.
465 *
466 * No locks required. Nothing can change the per cpu device.
467 */
468 void tick_resume_local(void)
469 {
470 struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
471 bool broadcast = tick_resume_check_broadcast();
472
473 clockevents_tick_resume(td->evtdev);
474 if (!broadcast) {
475 if (td->mode == TICKDEV_MODE_PERIODIC)
476 tick_setup_periodic(td->evtdev, 0);
477 else
478 tick_resume_oneshot();
479 }
480 }
481
482 /**
483 * tick_suspend - Suspend the tick and the broadcast device
484 *
485 * Called from syscore_suspend() via timekeeping_suspend with only one
486 * CPU online and interrupts disabled or from tick_unfreeze() under
487 * tick_freeze_lock.
488 *
489 * No locks required. Nothing can change the per cpu device.
490 */
491 void tick_suspend(void)
492 {
493 tick_suspend_local();
494 tick_suspend_broadcast();
495 }
496
497 /**
498 * tick_resume - Resume the tick and the broadcast device
499 *
500 * Called from syscore_resume() via timekeeping_resume with only one
501 * CPU online and interrupts disabled.
502 *
503 * No locks required. Nothing can change the per cpu device.
504 */
505 void tick_resume(void)
506 {
507 tick_resume_broadcast();
508 tick_resume_local();
509 }
510
511 #ifdef CONFIG_SUSPEND
512 static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
513 static unsigned int tick_freeze_depth;
514
515 /**
516 * tick_freeze - Suspend the local tick and (possibly) timekeeping.
517 *
518 * Check if this is the last online CPU executing the function and if so,
519 * suspend timekeeping. Otherwise suspend the local tick.
520 *
521 * Call with interrupts disabled. Must be balanced with %tick_unfreeze().
522 * Interrupts must not be enabled before the subsequent %tick_unfreeze().
523 */
524 void tick_freeze(void)
525 {
526 raw_spin_lock(&tick_freeze_lock);
527
528 tick_freeze_depth++;
529 if (tick_freeze_depth == num_online_cpus()) {
530 trace_suspend_resume(TPS("timekeeping_freeze"),
531 smp_processor_id(), true);
532 system_state = SYSTEM_SUSPEND;
533 sched_clock_suspend();
534 timekeeping_suspend();
535 } else {
536 tick_suspend_local();
537 }
538
539 raw_spin_unlock(&tick_freeze_lock);
540 }
541
542 /**
543 * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
544 *
545 * Check if this is the first CPU executing the function and if so, resume
546 * timekeeping. Otherwise resume the local tick.
547 *
548 * Call with interrupts disabled. Must be balanced with %tick_freeze().
549 * Interrupts must not be enabled after the preceding %tick_freeze().
550 */
551 void tick_unfreeze(void)
552 {
553 raw_spin_lock(&tick_freeze_lock);
554
555 if (tick_freeze_depth == num_online_cpus()) {
556 timekeeping_resume();
557 sched_clock_resume();
558 system_state = SYSTEM_RUNNING;
559 trace_suspend_resume(TPS("timekeeping_freeze"),
560 smp_processor_id(), false);
561 } else {
562 touch_softlockup_watchdog();
563 tick_resume_local();
564 }
565
566 tick_freeze_depth--;
567
568 raw_spin_unlock(&tick_freeze_lock);
569 }
570 #endif /* CONFIG_SUSPEND */
571
572 /**
573 * tick_init - initialize the tick control
574 */
575 void __init tick_init(void)
576 {
577 tick_broadcast_init();
578 tick_nohz_init();
579 }