History

tick/common: Make tick_periodic() check for missing ticks

The tick_periodic() function is used at the beginning part of the bootup process for time keeping while the other clock sources are being initialized. The current code assumes that all the timer interrupts are handled in a timely manner with no missing ticks. That is not actually true. Some ticks are missed and there are some discrepancies between the tick time (jiffies) and the timestamp reported in the kernel log. Some systems, however, are more prone to missing ticks than the others. In the extreme case, the discrepancy can actually cause a soft lockup message to be printed by the watchdog kthread. For example, on a Cavium ThunderX2 Sabre arm64 system: [ 25.496379] watchdog: BUG: soft lockup - CPU#14 stuck for 22s! On that system, the missing ticks are especially prevalent during the smp_init() phase of the boot process. With an instrumented kernel, it was found that it took about 24s as reported by the timestamp for the tick to accumulate 4s of time. Investigation and bisection done by others seemed to point to the commit 73f381660959 ("arm64: Advertise mitigation of Spectre-v2, or lack thereof") as the culprit. It could also be a firmware issue as new firmware was promised that would fix the issue. To properly address this problem, stop assuming that there will be no missing tick in tick_periodic(). Modify it to follow the example of tick_do_update_jiffies64() by using another reference clock to check for missing ticks. Since the watchdog timer uses running_clock(), it is used here as the reference. With this applied, the soft lockup problem in the affected arm64 system is gone and tick time tracks much more closely to the timestamp time.

Waiman Long committed 6 years ago

Commit d441dce1 parent 38f7b0b1 file · +33 −3Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/time/tick-common.c+33−3
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616 #include <linux/profile.h>
1717 #include <linux/sched.h>
1818 #include <linux/module.h>
19+#include <linux/sched/clock.h>
1920 #include <trace/events/power.h>
2021
2122 #include <asm/irq_regs.h>
@@ -84,12 +85,41 @@ int tick_is_oneshot_available(void) · 62 unmodified lines
8485 static void tick_periodic(int cpu)
8586 {
8687 if (tick_do_timer_cpu == cpu) {
88+ /*
89+ * Use running_clock() as reference to check for missing ticks.
90+ */
91+ static ktime_t last_update;
92+ ktime_t now;
93+ int ticks = 1;
94+
95+ now = ns_to_ktime(running_clock());
8796 write_seqlock(&jiffies_lock);
8897
89− /* Keep track of the next tick event */
90− tick_next_period = ktime_add(tick_next_period, tick_period);
98+ if (last_update) {
99+ u64 delta = ktime_sub(now, last_update);
91100
92− do_timer(1);
101+ /*
102+ * Check for eventually missed ticks
103+ *
104+ * There is likely a persistent delta between
105+ * last_update and tick_next_period. So they are
106+ * updated separately.
107+ */
108+ if (delta >= 2 * tick_period) {
109+ s64 period = ktime_to_ns(tick_period);
110+
111+ ticks = ktime_divns(delta, period);
112+ }
113+ last_update = ktime_add(last_update,
114+ ticks * tick_period);
115+ } else {
116+ last_update = now;
117+ }
118+
119+ /* Keep track of the next tick event */
120+ tick_next_period = ktime_add(tick_next_period,
121+ ticks * tick_period);
122+ do_timer(ticks);
93123 write_sequnlock(&jiffies_lock);
94124 update_wall_time();
95125 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t