History

clocksource: Reduce clocksource-skew threshold

Currently, WATCHDOG_THRESHOLD is set to detect a 62.5-millisecond skew in a 500-millisecond WATCHDOG_INTERVAL. This requires that clocks be skewed by more than 12.5% in order to be marked unstable. Except that a clock that is skewed by that much is probably destroying unsuspecting software right and left. And given that there are now checks for false-positive skews due to delays between reading the two clocks, it should be possible to greatly decrease WATCHDOG_THRESHOLD, at least for fine-grained clocks such as TSC. Therefore, add a new uncertainty_margin field to the clocksource structure that contains the maximum uncertainty in nanoseconds for the corresponding clock. This field may be initialized manually, as it is for clocksource_tsc_early and clocksource_jiffies, which is copied to refined_jiffies. If the field is not initialized manually, it will be computed at clock-registry time as the period of the clock in question based on the scale and freq parameters to __clocksource_update_freq_scale() function. If either of those two parameters are zero, the tens-of-milliseconds WATCHDOG_THRESHOLD is used as a cowardly alternative to dividing by zero. No matter how the uncertainty_margin field is calculated, it is bounded below by twice WATCHDOG_MAX_SKEW, that is, by 100 microseconds. Note that manually initialized uncertainty_margin fields are not adjusted, but there is a WARN_ON_ONCE() that triggers if any such field is less than twice WATCHDOG_MAX_SKEW. This WARN_ON_ONCE() is intended to discourage production use of the one-nanosecond uncertainty_margin values that are used to test the clock-skew code itself. The actual clock-skew check uses the sum of the uncertainty_margin fields of the two clocksource structures being compared. Integer overflow is avoided because the largest computed value of the uncertainty_margin fields is one billion (10^9), and double that value fits into an unsigned int. However, if someone manually specifies (say) UINT_MAX, they will get what they deserve. Note that the refined_jiffies uncertainty_margin field is initialized to TICK_NSEC, which means that skew checks involving this clocksource will be sufficently forgiving. In a similar vein, the clocksource_tsc_early uncertainty_margin field is initialized to 32*NSEC_PER_MSEC, which replicates the current behavior and allows custom setting if needed in order to address the rare skews detected for this clocksource in current mainline.

Paul E. McKenney committed 5 years ago

Commit 2e27e791 parent fa218f14 files · +50 −17Browse files

Unified · Split

4 files
  1. arch/x86/kernel/tsc.c +1 −0
  2. include/linux/clocksource.h +3 −0
  3. kernel/time/clocksource.c +38 −10
  4. kernel/time/jiffies.c +8 −7
arch/x86/kernel/tsc.c+1−0
@@ -1128,6 +1128,7 @@ static int tsc_cs_enable(struct clocksource *cs) · 1127 unmodified lines
11281128 static struct clocksource clocksource_tsc_early = {
11291129 .name = "tsc-early",
11301130 .rating = 299,
1131+ .uncertainty_margin = 32 * NSEC_PER_MSEC,
11311132 .read = read_tsc,
11321133 .mask = CLOCKSOURCE_MASK(64),
11331134 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
include/linux/clocksource.h+3−0
@@ -43,6 +43,8 @@ struct module; · 42 unmodified lines
4343 * @shift: Cycle to nanosecond divisor (power of two)
4444 * @max_idle_ns: Maximum idle time permitted by the clocksource (nsecs)
4545 * @maxadj: Maximum adjustment value to mult (~11%)
46+ * @uncertainty_margin: Maximum uncertainty in nanoseconds per half second.
47+ * Zero says to use default WATCHDOG_THRESHOLD.
4648 * @archdata: Optional arch-specific data
4749 * @max_cycles: Maximum safe cycle value which won't overflow on
4850 * multiplication
@@ -98,6 +100,7 @@ struct clocksource { · 49 unmodified lines
98100 u32 shift;
99101 u64 max_idle_ns;
100102 u32 maxadj;
103+ u32 uncertainty_margin;
101104 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
102105 struct arch_clocksource_data archdata;
103106 #endif
kernel/time/clocksource.c+38−10
@@ -95,6 +95,20 @@ static char override_name[CS_NAME_LEN]; · 94 unmodified lines
9595 static int finished_booting;
9696 static u64 suspend_start;
9797
98+/*
99+ * Threshold: 0.0312s, when doubled: 0.0625s.
100+ * Also a default for cs->uncertainty_margin when registering clocks.
101+ */
102+#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5)
103+
104+/*
105+ * Maximum permissible delay between two readouts of the watchdog
106+ * clocksource surrounding a read of the clocksource being validated.
107+ * This delay could be due to SMIs, NMIs, or to VCPU preemptions. Used as
108+ * a lower bound for cs->uncertainty_margin values when registering clocks.
109+ */
110+#define WATCHDOG_MAX_SKEW (50 * NSEC_PER_USEC)
111+
98112 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
99113 static void clocksource_watchdog_work(struct work_struct *work);
100114 static void clocksource_select(void);
@@ -121,17 +135,9 @@ static int clocksource_watchdog_kthread(void *data); · 20 unmodified lines
121135 static void __clocksource_change_rating(struct clocksource *cs, int rating);
122136
123137 /*
124− * Interval: 0.5sec Threshold: 0.0625s
138+ * Interval: 0.5sec.
125139 */
126140 #define WATCHDOG_INTERVAL (HZ >> 1)
127−#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
128−
129−/*
130− * Maximum permissible delay between two readouts of the watchdog
131− * clocksource surrounding a read of the clocksource being validated.
132− * This delay could be due to SMIs, NMIs, or to VCPU preemptions.
133− */
134−#define WATCHDOG_MAX_SKEW (100 * NSEC_PER_USEC)
135141
136142 static void clocksource_watchdog_work(struct work_struct *work)
137143 {
@@ -348,6 +354,7 @@ static void clocksource_watchdog(struct timer_list *unused) · 210 unmodified lines
348354 int next_cpu, reset_pending;
349355 int64_t wd_nsec, cs_nsec;
350356 struct clocksource *cs;
357+ u32 md;
351358
352359 spin_lock(&watchdog_lock);
353360 if (!watchdog_running)
@@ -394,7 +401,8 @@ static void clocksource_watchdog(struct timer_list *unused) · 40 unmodified lines
394401 continue;
395402
396403 /* Check the deviation from the watchdog clocksource. */
397− if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
404+ md = cs->uncertainty_margin + watchdog->uncertainty_margin;
405+ if (abs(cs_nsec - wd_nsec) > md) {
398406 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
399407 smp_processor_id(), cs->name);
400408 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
@@ -1047,6 +1055,26 @@ void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq · 646 unmodified lines
10471055 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
10481056 NSEC_PER_SEC / scale, sec * scale);
10491057 }
1058+
1059+ /*
1060+ * If the uncertainty margin is not specified, calculate it.
1061+ * If both scale and freq are non-zero, calculate the clock
1062+ * period, but bound below at 2*WATCHDOG_MAX_SKEW. However,
1063+ * if either of scale or freq is zero, be very conservative and
1064+ * take the tens-of-milliseconds WATCHDOG_THRESHOLD value for the
1065+ * uncertainty margin. Allow stupidly small uncertainty margins
1066+ * to be specified by the caller for testing purposes, but warn
1067+ * to discourage production use of this capability.
1068+ */
1069+ if (scale && freq && !cs->uncertainty_margin) {
1070+ cs->uncertainty_margin = NSEC_PER_SEC / (scale * freq);
1071+ if (cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW)
1072+ cs->uncertainty_margin = 2 * WATCHDOG_MAX_SKEW;
1073+ } else if (!cs->uncertainty_margin) {
1074+ cs->uncertainty_margin = WATCHDOG_THRESHOLD;
1075+ }
1076+ WARN_ON_ONCE(cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW);
1077+
10501078 /*
10511079 * Ensure clocksources that have large 'mult' values don't overflow
10521080 * when adjusted.
kernel/time/jiffies.c+8−7
@@ -49,13 +49,14 @@ static u64 jiffies_read(struct clocksource *cs) · 48 unmodified lines
4949 * for "tick-less" systems.
5050 */
5151 static struct clocksource clocksource_jiffies = {
52− .name = "jiffies",
53− .rating = 1, /* lowest valid rating*/
54− .read = jiffies_read,
55− .mask = CLOCKSOURCE_MASK(32),
56− .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */
57− .shift = JIFFIES_SHIFT,
58− .max_cycles = 10,
52+ .name = "jiffies",
53+ .rating = 1, /* lowest valid rating*/
54+ .uncertainty_margin = 32 * NSEC_PER_MSEC,
55+ .read = jiffies_read,
56+ .mask = CLOCKSOURCE_MASK(32),
57+ .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */
58+ .shift = JIFFIES_SHIFT,
59+ .max_cycles = 10,
5960 };
6061
6162 __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);

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