Skip to content

Commit fd99aeb

Browse files
Waiman-Longgregkh
authored andcommitted
clocksource: Avoid accidental unstable marking of clocksources
[ Upstream commit c86ff8c ] Since commit db3a34e ("clocksource: Retry clock read if long delays detected") and commit 2e27e79 ("clocksource: Reduce clocksource-skew threshold"), it is found that tsc clocksource fallback to hpet can sometimes happen on both Intel and AMD systems especially when they are running stressful benchmarking workloads. Of the 23 systems tested with a v5.14 kernel, 10 of them have switched to hpet clock source during the test run. The result of falling back to hpet is a drastic reduction of performance when running benchmarks. For example, the fio performance tests can drop up to 70% whereas the iperf3 performance can drop up to 80%. 4 hpet fallbacks happened during bootup. They were: [ 8.749399] clocksource: timekeeping watchdog on CPU13: hpet read-back delay of 263750ns, attempt 4, marking unstable [ 12.044610] clocksource: timekeeping watchdog on CPU19: hpet read-back delay of 186166ns, attempt 4, marking unstable [ 17.336941] clocksource: timekeeping watchdog on CPU28: hpet read-back delay of 182291ns, attempt 4, marking unstable [ 17.518565] clocksource: timekeeping watchdog on CPU34: hpet read-back delay of 252196ns, attempt 4, marking unstable Other fallbacks happen when the systems were running stressful benchmarks. For example: [ 2685.867873] clocksource: timekeeping watchdog on CPU117: hpet read-back delay of 57269ns, attempt 4, marking unstable [46215.471228] clocksource: timekeeping watchdog on CPU8: hpet read-back delay of 61460ns, attempt 4, marking unstable Commit 2e27e79 ("clocksource: Reduce clocksource-skew threshold"), changed the skew margin from 100us to 50us. I think this is too small and can easily be exceeded when running some stressful workloads on a thermally stressed system. So it is switched back to 100us. Even a maximum skew margin of 100us may be too small in for some systems when booting up especially if those systems are under thermal stress. To eliminate the case that the large skew is due to the system being too busy slowing down the reading of both the watchdog and the clocksource, an extra consecutive read of watchdog clock is being done to check this. The consecutive watchdog read delay is compared against WATCHDOG_MAX_SKEW/2. If the delay exceeds the limit, we assume that the system is just too busy. A warning will be printed to the console and the clock skew check is skipped for this round. Fixes: db3a34e ("clocksource: Retry clock read if long delays detected") Fixes: 2e27e79 ("clocksource: Reduce clocksource-skew threshold") Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent cacc6c3 commit fd99aeb

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

‎kernel/time/clocksource.c‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static u64 suspend_start;
105105
* This delay could be due to SMIs, NMIs, or to VCPU preemptions. Used as
106106
* a lower bound for cs->uncertainty_margin values when registering clocks.
107107
*/
108-
#define WATCHDOG_MAX_SKEW (50 * NSEC_PER_USEC)
108+
#define WATCHDOG_MAX_SKEW (100 * NSEC_PER_USEC)
109109

110110
#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
111111
static void clocksource_watchdog_work(struct work_struct *work);
@@ -200,17 +200,24 @@ void clocksource_mark_unstable(struct clocksource *cs)
200200
static ulong max_cswd_read_retries = 3;
201201
module_param(max_cswd_read_retries, ulong, 0644);
202202

203-
static bool cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
203+
enum wd_read_status {
204+
WD_READ_SUCCESS,
205+
WD_READ_UNSTABLE,
206+
WD_READ_SKIP
207+
};
208+
209+
static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
204210
{
205211
unsigned int nretries;
206-
u64 wd_end, wd_delta;
207-
int64_t wd_delay;
212+
u64 wd_end, wd_end2, wd_delta;
213+
int64_t wd_delay, wd_seq_delay;
208214

209215
for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
210216
local_irq_disable();
211217
*wdnow = watchdog->read(watchdog);
212218
*csnow = cs->read(cs);
213219
wd_end = watchdog->read(watchdog);
220+
wd_end2 = watchdog->read(watchdog);
214221
local_irq_enable();
215222

216223
wd_delta = clocksource_delta(wd_end, *wdnow, watchdog->mask);
@@ -221,13 +228,34 @@ static bool cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
221228
pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n",
222229
smp_processor_id(), watchdog->name, nretries);
223230
}
224-
return true;
231+
return WD_READ_SUCCESS;
225232
}
233+
234+
/*
235+
* Now compute delay in consecutive watchdog read to see if
236+
* there is too much external interferences that cause
237+
* significant delay in reading both clocksource and watchdog.
238+
*
239+
* If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2,
240+
* report system busy, reinit the watchdog and skip the current
241+
* watchdog test.
242+
*/
243+
wd_delta = clocksource_delta(wd_end2, wd_end, watchdog->mask);
244+
wd_seq_delay = clocksource_cyc2ns(wd_delta, watchdog->mult, watchdog->shift);
245+
if (wd_seq_delay > WATCHDOG_MAX_SKEW/2)
246+
goto skip_test;
226247
}
227248

228249
pr_warn("timekeeping watchdog on CPU%d: %s read-back delay of %lldns, attempt %d, marking unstable\n",
229250
smp_processor_id(), watchdog->name, wd_delay, nretries);
230-
return false;
251+
return WD_READ_UNSTABLE;
252+
253+
skip_test:
254+
pr_info("timekeeping watchdog on CPU%d: %s wd-wd read-back delay of %lldns\n",
255+
smp_processor_id(), watchdog->name, wd_seq_delay);
256+
pr_info("wd-%s-wd read-back delay of %lldns, clock-skew test skipped!\n",
257+
cs->name, wd_delay);
258+
return WD_READ_SKIP;
231259
}
232260

233261
static u64 csnow_mid;
@@ -290,6 +318,7 @@ static void clocksource_watchdog(struct timer_list *unused)
290318
int next_cpu, reset_pending;
291319
int64_t wd_nsec, cs_nsec;
292320
struct clocksource *cs;
321+
enum wd_read_status read_ret;
293322
u32 md;
294323

295324
spin_lock(&watchdog_lock);
@@ -307,9 +336,12 @@ static void clocksource_watchdog(struct timer_list *unused)
307336
continue;
308337
}
309338

310-
if (!cs_watchdog_read(cs, &csnow, &wdnow)) {
311-
/* Clock readout unreliable, so give it up. */
312-
__clocksource_unstable(cs);
339+
read_ret = cs_watchdog_read(cs, &csnow, &wdnow);
340+
341+
if (read_ret != WD_READ_SUCCESS) {
342+
if (read_ret == WD_READ_UNSTABLE)
343+
/* Clock readout unreliable, so give it up. */
344+
__clocksource_unstable(cs);
313345
continue;
314346
}
315347

0 commit comments

Comments
 (0)