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.
Unified · Split
| @@ -1128,6 +1128,7 @@ static int tsc_cs_enable(struct clocksource *cs) · 1127 unmodified lines | |||
| 1128 | static struct clocksource clocksource_tsc_early = { | 1128 | static struct clocksource clocksource_tsc_early = { |
| 1129 | .name = "tsc-early", | 1129 | .name = "tsc-early", |
| 1130 | .rating = 299, | 1130 | .rating = 299, |
| 1131 | .uncertainty_margin = 32 * NSEC_PER_MSEC, | ||
| 1131 | .read = read_tsc, | 1132 | .read = read_tsc, |
| 1132 | .mask = CLOCKSOURCE_MASK(64), | 1133 | .mask = CLOCKSOURCE_MASK(64), |
| 1133 | .flags = CLOCK_SOURCE_IS_CONTINUOUS | | 1134 | .flags = CLOCK_SOURCE_IS_CONTINUOUS | |
| @@ -43,6 +43,8 @@ struct module; · 42 unmodified lines | |||
| 43 | * @shift: Cycle to nanosecond divisor (power of two) | 43 | * @shift: Cycle to nanosecond divisor (power of two) |
| 44 | * @max_idle_ns: Maximum idle time permitted by the clocksource (nsecs) | 44 | * @max_idle_ns: Maximum idle time permitted by the clocksource (nsecs) |
| 45 | * @maxadj: Maximum adjustment value to mult (~11%) | 45 | * @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. | ||
| 46 | * @archdata: Optional arch-specific data | 48 | * @archdata: Optional arch-specific data |
| 47 | * @max_cycles: Maximum safe cycle value which won't overflow on | 49 | * @max_cycles: Maximum safe cycle value which won't overflow on |
| 48 | * multiplication | 50 | * multiplication |
| @@ -98,6 +100,7 @@ struct clocksource { · 49 unmodified lines | |||
| 98 | u32 shift; | 100 | u32 shift; |
| 99 | u64 max_idle_ns; | 101 | u64 max_idle_ns; |
| 100 | u32 maxadj; | 102 | u32 maxadj; |
| 103 | u32 uncertainty_margin; | ||
| 101 | #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA | 104 | #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA |
| 102 | struct arch_clocksource_data archdata; | 105 | struct arch_clocksource_data archdata; |
| 103 | #endif | 106 | #endif |
| @@ -95,6 +95,20 @@ static char override_name[CS_NAME_LEN]; · 94 unmodified lines | |||
| 95 | static int finished_booting; | 95 | static int finished_booting; |
| 96 | static u64 suspend_start; | 96 | static u64 suspend_start; |
| 97 | 97 | ||
| 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 | |||
| 98 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG | 112 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG |
| 99 | static void clocksource_watchdog_work(struct work_struct *work); | 113 | static void clocksource_watchdog_work(struct work_struct *work); |
| 100 | static void clocksource_select(void); | 114 | static void clocksource_select(void); |
| @@ -121,17 +135,9 @@ static int clocksource_watchdog_kthread(void *data); · 20 unmodified lines | |||
| 121 | static void __clocksource_change_rating(struct clocksource *cs, int rating); | 135 | static void __clocksource_change_rating(struct clocksource *cs, int rating); |
| 122 | 136 | ||
| 123 | /* | 137 | /* |
| 124 | * Interval: 0.5sec Threshold: 0.0625s | 138 | * Interval: 0.5sec. |
| 125 | */ | 139 | */ |
| 126 | #define WATCHDOG_INTERVAL (HZ >> 1) | 140 | #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) | ||
| 135 | 141 | ||
| 136 | static void clocksource_watchdog_work(struct work_struct *work) | 142 | static void clocksource_watchdog_work(struct work_struct *work) |
| 137 | { | 143 | { |
| @@ -348,6 +354,7 @@ static void clocksource_watchdog(struct timer_list *unused) · 210 unmodified lines | |||
| 348 | int next_cpu, reset_pending; | 354 | int next_cpu, reset_pending; |
| 349 | int64_t wd_nsec, cs_nsec; | 355 | int64_t wd_nsec, cs_nsec; |
| 350 | struct clocksource *cs; | 356 | struct clocksource *cs; |
| 357 | u32 md; | ||
| 351 | 358 | ||
| 352 | spin_lock(&watchdog_lock); | 359 | spin_lock(&watchdog_lock); |
| 353 | if (!watchdog_running) | 360 | if (!watchdog_running) |
| @@ -394,7 +401,8 @@ static void clocksource_watchdog(struct timer_list *unused) · 40 unmodified lines | |||
| 394 | continue; | 401 | continue; |
| 395 | 402 | ||
| 396 | /* Check the deviation from the watchdog clocksource. */ | 403 | /* 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) { | ||
| 398 | pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n", | 406 | pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n", |
| 399 | smp_processor_id(), cs->name); | 407 | smp_processor_id(), cs->name); |
| 400 | pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n", | 408 | 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 | |||
| 1047 | clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, | 1055 | clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, |
| 1048 | NSEC_PER_SEC / scale, sec * scale); | 1056 | NSEC_PER_SEC / scale, sec * scale); |
| 1049 | } | 1057 | } |
| 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 | |||
| 1050 | /* | 1078 | /* |
| 1051 | * Ensure clocksources that have large 'mult' values don't overflow | 1079 | * Ensure clocksources that have large 'mult' values don't overflow |
| 1052 | * when adjusted. | 1080 | * when adjusted. |
| @@ -49,13 +49,14 @@ static u64 jiffies_read(struct clocksource *cs) · 48 unmodified lines | |||
| 49 | * for "tick-less" systems. | 49 | * for "tick-less" systems. |
| 50 | */ | 50 | */ |
| 51 | static struct clocksource clocksource_jiffies = { | 51 | static struct clocksource clocksource_jiffies = { |
| 52 | .name = "jiffies", | 52 | .name = "jiffies", |
| 53 | .rating = 1, /* lowest valid rating*/ | 53 | .rating = 1, /* lowest valid rating*/ |
| 54 | .read = jiffies_read, | 54 | .uncertainty_margin = 32 * NSEC_PER_MSEC, |
| 55 | .mask = CLOCKSOURCE_MASK(32), | 55 | .read = jiffies_read, |
| 56 | .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */ | 56 | .mask = CLOCKSOURCE_MASK(32), |
| 57 | .shift = JIFFIES_SHIFT, | 57 | .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */ |
| 58 | .max_cycles = 10, | 58 | .shift = JIFFIES_SHIFT, |
| 59 | .max_cycles = 10, | ||
| 59 | }; | 60 | }; |
| 60 | 61 | ||
| 61 | __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock); | 62 | __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock); |