Skip to content

Commit a64405b

Browse files
committed
Merge tag 'timers-clocksource-2024-09-16' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull clocksource watchdog updates from Thomas Gleixner: - Make the uncertainty margin handling more robust to prevent false positives - Clarify comments * tag 'timers-clocksource-2024-09-16' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: clocksource: Set cs_watchdog_read() checks based on .uncertainty_margin clocksource: Fix comments on WATCHDOG_THRESHOLD & WATCHDOG_MAX_SKEW clocksource: Improve comments for watchdog skew bounds
2 parents 97e17c0 + 4ac1dd3 commit a64405b

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

kernel/time/clocksource.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ static u64 suspend_start;
113113

114114
/*
115115
* Threshold: 0.0312s, when doubled: 0.0625s.
116-
* Also a default for cs->uncertainty_margin when registering clocks.
117116
*/
118117
#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5)
119118

@@ -125,13 +124,27 @@ static u64 suspend_start;
125124
*
126125
* The default of 500 parts per million is based on NTP's limits.
127126
* If a clocksource is good enough for NTP, it is good enough for us!
127+
*
128+
* In other words, by default, even if a clocksource is extremely
129+
* precise (for example, with a sub-nanosecond period), the maximum
130+
* permissible skew between the clocksource watchdog and the clocksource
131+
* under test is not permitted to go below the 500ppm minimum defined
132+
* by MAX_SKEW_USEC. This 500ppm minimum may be overridden using the
133+
* CLOCKSOURCE_WATCHDOG_MAX_SKEW_US Kconfig option.
128134
*/
129135
#ifdef CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US
130136
#define MAX_SKEW_USEC CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US
131137
#else
132138
#define MAX_SKEW_USEC (125 * WATCHDOG_INTERVAL / HZ)
133139
#endif
134140

141+
/*
142+
* Default for maximum permissible skew when cs->uncertainty_margin is
143+
* not specified, and the lower bound even when cs->uncertainty_margin
144+
* is specified. This is also the default that is used when registering
145+
* clocks with unspecifed cs->uncertainty_margin, so this macro is used
146+
* even in CONFIG_CLOCKSOURCE_WATCHDOG=n kernels.
147+
*/
135148
#define WATCHDOG_MAX_SKEW (MAX_SKEW_USEC * NSEC_PER_USEC)
136149

137150
#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
@@ -231,6 +244,7 @@ enum wd_read_status {
231244

232245
static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
233246
{
247+
int64_t md = 2 * watchdog->uncertainty_margin;
234248
unsigned int nretries, max_retries;
235249
int64_t wd_delay, wd_seq_delay;
236250
u64 wd_end, wd_end2;
@@ -245,7 +259,7 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
245259
local_irq_enable();
246260

247261
wd_delay = cycles_to_nsec_safe(watchdog, *wdnow, wd_end);
248-
if (wd_delay <= WATCHDOG_MAX_SKEW) {
262+
if (wd_delay <= md + cs->uncertainty_margin) {
249263
if (nretries > 1 && nretries >= max_retries) {
250264
pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n",
251265
smp_processor_id(), watchdog->name, nretries);
@@ -258,12 +272,12 @@ static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow,
258272
* there is too much external interferences that cause
259273
* significant delay in reading both clocksource and watchdog.
260274
*
261-
* If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2,
262-
* report system busy, reinit the watchdog and skip the current
275+
* If consecutive WD read-back delay > md, report
276+
* system busy, reinit the watchdog and skip the current
263277
* watchdog test.
264278
*/
265279
wd_seq_delay = cycles_to_nsec_safe(watchdog, wd_end, wd_end2);
266-
if (wd_seq_delay > WATCHDOG_MAX_SKEW/2)
280+
if (wd_seq_delay > md)
267281
goto skip_test;
268282
}
269283

@@ -1146,14 +1160,19 @@ void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq
11461160
}
11471161

11481162
/*
1149-
* If the uncertainty margin is not specified, calculate it.
1150-
* If both scale and freq are non-zero, calculate the clock
1151-
* period, but bound below at 2*WATCHDOG_MAX_SKEW. However,
1152-
* if either of scale or freq is zero, be very conservative and
1153-
* take the tens-of-milliseconds WATCHDOG_THRESHOLD value for the
1154-
* uncertainty margin. Allow stupidly small uncertainty margins
1155-
* to be specified by the caller for testing purposes, but warn
1156-
* to discourage production use of this capability.
1163+
* If the uncertainty margin is not specified, calculate it. If
1164+
* both scale and freq are non-zero, calculate the clock period, but
1165+
* bound below at 2*WATCHDOG_MAX_SKEW, that is, 500ppm by default.
1166+
* However, if either of scale or freq is zero, be very conservative
1167+
* and take the tens-of-milliseconds WATCHDOG_THRESHOLD value
1168+
* for the uncertainty margin. Allow stupidly small uncertainty
1169+
* margins to be specified by the caller for testing purposes,
1170+
* but warn to discourage production use of this capability.
1171+
*
1172+
* Bottom line: The sum of the uncertainty margins of the
1173+
* watchdog clocksource and the clocksource under test will be at
1174+
* least 500ppm by default. For more information, please see the
1175+
* comment preceding CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US above.
11571176
*/
11581177
if (scale && freq && !cs->uncertainty_margin) {
11591178
cs->uncertainty_margin = NSEC_PER_SEC / (scale * freq);

0 commit comments

Comments
 (0)