From 0f56c7d3a71fe46f77aaedd1f4dd4048f2515eb4 Mon Sep 17 00:00:00 2001
From: Nadav Har'El <nyh@cloudius-systems.com>
Date: Thu, 28 Nov 2013 16:04:52 +0200
Subject: [PATCH] sched: ignore backward jumps in clock

Due to an unknown bug (bug in our HPET driver?), OSv crashed on our build
machine with an assertion that the clock only goes foward. The clock
jumping backward is a bad sign, but it's not really necessary to crash the
VM when it happens, assuming it only happens rarely. This patch makes the
scheduler handle time<0 just like time==0.

Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 core/sched.cc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/core/sched.cc b/core/sched.cc
index d3175d9b1..679fedeab 100644
--- a/core/sched.cc
+++ b/core/sched.cc
@@ -193,9 +193,10 @@ void cpu::reschedule_from_interrupt(bool preempt)
 
     auto interval = now - running_since;
     running_since = now;
-    if (interval == 0) {
+    if (interval <= 0) {
         // During startup, the clock may be stuck and we get zero intervals.
         // To avoid scheduler loops, let's make it non-zero.
+        // Also ignore backward jumps in the clock.
         interval = context_switch_penalty;
     }
     thread* p = thread::current();
-- 
GitLab