From 548fd632641ef4dee9360565e5f162f23339d6ec Mon Sep 17 00:00:00 2001 From: Kamil Koczurek Date: Tue, 10 Sep 2024 14:12:43 +0200 Subject: [PATCH] tsc: calculate elapsed ticks using saturating_sub RDTSC readouts might not actually be monotonically increasing due to desynchronization between CPU cores, so checked_sub was causing a kernel panic. --- src/arch/x86_shared/device/tsc.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_shared/device/tsc.rs b/src/arch/x86_shared/device/tsc.rs index d4ae8ec413..030ac79908 100644 --- a/src/arch/x86_shared/device/tsc.rs +++ b/src/arch/x86_shared/device/tsc.rs @@ -69,9 +69,8 @@ pub fn monotonic_absolute() -> Option { if cur_version & 1 == 1 { continue; } - let elapsed_ticks = x86::time::rdtsc() - .checked_sub(addr_of!((*ptr).tsc_timestamp).read_volatile()) - .unwrap(); + let elapsed_ticks = + x86::time::rdtsc().saturating_sub(addr_of!((*ptr).tsc_timestamp).read_volatile()); let tsc_shift = addr_of!((*ptr).tsc_shift).read_volatile(); let elapsed = if tsc_shift >= 0 { elapsed_ticks.checked_shl(tsc_shift as u32).unwrap()