Skip to contentSkip to conversation

clocksource: delayed watchdog can mark TSC unstable

REGRESSION Applied Karl Mehltretter 3 participants 6 messages Oct 10 – Oct 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Since the watchdog rewrite in v7.1, the TSC can be marked unstable after a delayed watchdog run under QEMU TCG. Without injected stalls, this happened on 5 of 12 boots on a loaded host and 0 of 6 on an idle host.

The delayed run re-arms the timer with an expiry already in the past. The watchdog then runs again almost immediately and rejects the TSC based on a roughly 75 us measurement interval.

#regzbot introduced: 763aacf86f1b

v7.3-rc5, 4 vCPUs, after vCPU 0 was stopped for 0.8 s:

clocksource: Marking clocksource tsc unstable due to frequency skew
clocksource: Watchdog hpet interval: 74940ns
clocksource: Clocksource tsc interval: 74251ns
clocksource: Switched to clocksource hpet

The intervals are 75 us, not the 500 ms watchdog period. A function trace of clocksource_watchdog() from the same boot:

busybox-79 [000] ..s1. 15.168486: clocksource_watchdog <-call_timer_fn busybox-79 [000] .Ns.. 16.269726: clocksource_watchdog <-call_timer_fn busybox-79 [000] .Ns.. 16.269820: clocksource_watchdog <-call_timer_fn

The second call came 1.10 s after the first, the third 94 us after the second. Re-arming with expires += WATCHDOG_INTERVAL leaves the next expiry in the past when a run is more than one interval late.

For a 75 us interval, the 500 ppm allowance is only about 37 ns, plus the readout time. The measured difference was 689 ns.

Before 763aacf86f1b an interval above WATCHDOG_INTERVAL_MAX_NS was skipped with "Long readout interval, skipping watchdog check" and the timer re-armed from jiffies, so there was no immediate second run, and the skew was compared against a fixed margin of at least
2 * WATCHDOG_MAX_SKEW. The rewrite dropped both.

Reproducer: a v7.3-rc5 guest with allnoconfig plus the fragment below, 4 vCPUs, qemu-system-x86_64 -accel tcg, the HPET as watchdog. PTRACE_ATTACH on the "CPU 0/TCG" thread for 0.8 s, detach, six times 12 s apart. The TSC was marked unstable on the first stall in 2 of 2 runs. Stopping the whole QEMU process did not reproduce it. The guest clocks stopped with it.

The same stall against other kernels, same config:

TSC marked unstable

  v7.3-rc5                                   2 of 2 runs
  v7.3-rc5 + the check below                 0 of 2
  763aacf86f1b (the rewrite)                 3 of 4
  763aacf86f1b^                              0 of 2
  v7.0                                       0 of 2

763aacf86f1b^ and v7.0 print the "Long readout interval" line for the same stalls and keep the TSC. 763aacf86f1b marked it unstable on a 170 us window, hpet 169720 ns against tsc 168974 ns.

The added check in watchdog_check_freq() skips the frequency check and resets the watchdog when the measured interval is shorter than a quarter of the period:

	if (max_delta < WATCHDOG_INTERVAL * TICK_NSEC / 4)
		goto reset;

Re-arming from jiffies instead of from the previous expiry would avoid the second run altogether.

An LLM agent helped me with the test setup and the trace analysis. I ran the tests and checked the results.

The relevant part of the config fragment on top of allnoconfig:
CONFIG_64BIT=y
CONFIG_EXPERT=y
CONFIG_SMP=y
CONFIG_NR_CPUS=4
CONFIG_PREEMPT=y
CONFIG_HIGH_RES_TIMERS=y
CONFIG_NO_HZ_FULL=y
CONFIG_HZ_250=y
CONFIG_ACPI=y
CONFIG_PCI=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_CONSOLE=y
CONFIG_BLK_DEV_INITRD=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y
CONFIG_FTRACE=y
CONFIG_FUNCTION_TRACER=y CONFIG_IRQ_TIME_ACCOUNTING=y
CONFIG_PROVE_LOCKING=y
CONFIG_DEBUG_PREEMPT=y

Thanks,
Karl

maintainer
#1 lore raw
Quoted 18 lines · Karl Mehltretter

Since the watchdog rewrite in v7.1, the TSC can be marked unstable after a delayed watchdog run under QEMU TCG. Without injected stalls, this happened on 5 of 12 boots on a loaded host and 0 of 6 on an idle host.

The delayed run re-arms the timer with an expiry already in the past. The watchdog then runs again almost immediately and rejects the TSC based on a roughly 75 us measurement interval.

#regzbot introduced: 763aacf86f1b

v7.3-rc5, 4 vCPUs, after vCPU 0 was stopped for 0.8 s:

clocksource: Marking clocksource tsc unstable due to frequency skew
clocksource: Watchdog hpet interval: 74940ns
clocksource: Clocksource tsc interval: 74251ns
clocksource: Switched to clocksource hpet

Quoted 6 lines

The intervals are 75 us, not the 500 ms watchdog period. A function trace of clocksource_watchdog() from the same boot:

busybox-79 [000] ..s1. 15.168486: clocksource_watchdog <-call_timer_fn busybox-79 [000] .Ns.. 16.269726: clocksource_watchdog <-call_timer_fn busybox-79 [000] .Ns.. 16.269820: clocksource_watchdog <-call_timer_fn

I did not think about that scenario obvioulsy. The code is resilient against long intervals between two measurements, so the easy and straight forward fix is indeed what you (or the LLM) suggested.

Thanks,

tglx
---

kernel/time/clocksource.c +1 -1
@@ -673,7 +673,7 @@ static void clocksource_watchdog(struct
 
 	/* Could have been rearmed by a stop/start cycle */
 	if (!timer_pending(&watchdog_timer)) {
-		watchdog_timer.expires += WATCHDOG_INTERVAL;
+		watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
 		add_timer_local(&watchdog_timer);
 	}
 }
#2 lore raw
Quoted 15 lines · Thomas Gleixner
Quoted 3 lines

busybox-79 [000] ..s1. 15.168486: clocksource_watchdog <-call_timer_fn busybox-79 [000] .Ns.. 16.269726: clocksource_watchdog <-call_timer_fn busybox-79 [000] .Ns.. 16.269820: clocksource_watchdog <-call_timer_fn

I did not think about that scenario obvioulsy. The code is resilient against long intervals between two measurements, so the easy and straight forward fix is indeed what you (or the LLM) suggested.

Thanks,

tglx
---

kernel/time/clocksource.c static void clocksource_watchdog(struct · line 679

Quoted 8 lines
kernel/time/clocksource.c +1 -1
@@ -673,7 +673,7 @@ static void clocksource_watchdog(struct
 
 	/* Could have been rearmed by a stop/start cycle */
 	if (!timer_pending(&watchdog_timer)) {
-		watchdog_timer.expires += WATCHDOG_INTERVAL;
+		watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
 		add_timer_local(&watchdog_timer);
 	}
 }

Yes, I tested this change on v7.3-rc5 (72d3fcf802c4)

                              baseline   patched
TSC marked unstable           2/3 boots  0/3 boots
watchdog gaps below 250 ms    3/3 boots  0/3 boots
minimum traced gap            0.231 ms   505.484 ms

Each boot had four QEMU TCG vCPUs. I used PTRACE_ATTACH to stop only the vCPU 0 TCG thread for 800 ms.

Thanks,
Karl

maintainer
#3 lore raw
Quoted 23 lines · Karl Mehltretter
Quoted 11 lines · Thomas Gleixner

I did not think about that scenario obvioulsy. The code is resilient against long intervals between two measurements, so the easy and straight forward fix is indeed what you (or the LLM) suggested.

Thanks,

tglx
---

kernel/time/clocksource.c static void clocksource_watchdog(struct · line 679

Quoted 8 lines
kernel/time/clocksource.c +1 -1
@@ -673,7 +673,7 @@ static void clocksource_watchdog(struct
 
 	/* Could have been rearmed by a stop/start cycle */
 	if (!timer_pending(&watchdog_timer)) {
-		watchdog_timer.expires += WATCHDOG_INTERVAL;
+		watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
 		add_timer_local(&watchdog_timer);
 	}
 }

Yes, I tested this change on v7.3-rc5 (72d3fcf802c4)

Quoted 7 lines
                              baseline   patched
TSC marked unstable           2/3 boots  0/3 boots
watchdog gaps below 250 ms    3/3 boots  0/3 boots
minimum traced gap            0.231 ms   505.484 ms

Each boot had four QEMU TCG vCPUs. I used PTRACE_ATTACH to stop only the vCPU 0 TCG thread for 800 ms.

Care to send a formal patch? After all you decoded the problem and suggested the fix. So you should get the credit for it not me.

Thanks,

tglx

#4 lore raw

PATCH 1/1 clocksource: Prevent immediate watchdog rerun after a delay

The watchdog rewrite made the clocksource check handle long intervals between two measurements, but missed that the timer expiry needs to be rebased after such a delay.

When a watchdog invocation is delayed by more than one interval, adding WATCHDOG_INTERVAL to the previous expiry leaves the timer expiry in the past. That causes an immediate second invocation. The resulting short measurement interval can mark a clocksource unstable.

Cure this by rearming the timer based on jiffies, which places the next expiry one full watchdog interval into the future.

Fixes 763aacf86f1b ("clocksource: Rewrite watchdog code completely")
Assisted-by LLM

---
The baseline and patched builds used identical configs and initramfs images. Tested on v7.3-rc5 (72d3fcf802c4) with four QEMU 8.2.2 TCG vCPUs. PTRACE_ATTACH stopped only the vCPU 0 TCG thread for 800 ms, six times 12 seconds apart per boot.

                              baseline   patched
TSC marked unstable           2/3 boots  0/3 boots
boots with gaps below 250 ms  3/3        0/3
minimum watchdog gap          0.231 ms   505.484 ms

All 18 injected stalls completed for each kernel.

 kernel/time/clocksource.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/clocksource.c +1 -1
index f1253f5795c6e..58c0ca11cdc5a 100644
@@ -673,7 +673,7 @@ static void clocksource_watchdog(struct timer_list *unused)
 
 	/* Could have been rearmed by a stop/start cycle */
 	if (!timer_pending(&watchdog_timer)) {
-		watchdog_timer.expires += WATCHDOG_INTERVAL;
+		watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
 		add_timer_local(&watchdog_timer);
 	}
 }

base-commit: 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f

Signature · 1 line
2.39.5 (Apple Git-154)
#5 lore raw

[tip: timers/urgent] clocksource: Prevent immediate watchdog rerun after a delay

The following commit has been merged into the timers/urgent branch of tip:

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID:     f322d7f7ae56bce728ba03a387e35380bc4ad939
Gitweb:        https://git.kernel.org/tip/f322d7f7ae56bce728ba03a387e35380bc4ad939
Author:        Karl Mehltretter <[email protected]>
AuthorDate:    Mon, 26 Oct 2026 02:02:00 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 27 Oct 2026 21:26:16 +01:00

clocksource: Prevent immediate watchdog rerun after a delay

The watchdog rewrite made the clocksource check handle long intervals between two measurements, but missed that the timer expiry needs to be rebased after such a delay.

When a watchdog invocation is delayed by more than one interval, adding WATCHDOG_INTERVAL to the previous expiry leaves the timer expiry in the past. That causes an immediate second invocation. The resulting short measurement interval can mark a clocksource unstable.

Cure this by rearming the timer based on jiffies, which places the next expiry one full watchdog interval into the future.

---
 kernel/time/clocksource.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/clocksource.c +1 -1
index f1253f5..58c0ca1 100644
@@ -673,7 +673,7 @@ static void clocksource_watchdog(struct timer_list *unused)
 
 	/* Could have been rearmed by a stop/start cycle */
 	if (!timer_pending(&watchdog_timer)) {
-		watchdog_timer.expires += WATCHDOG_INTERVAL;
+		watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
 		add_timer_local(&watchdog_timer);
 	}
 }