[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <1405504368-5581-1-git-send-email-imammedo@redhat.com>
Date: Wed, 16 Jul 2014 09:52:48 +0000
From: Igor Mammedov <imammedo@...hat.com>
To: linux-kernel@...r.kernel.org
Cc: kvm@...r.kernel.org, pbonzini@...hat.com, x86@...nel.org,
mtosatti@...hat.com
Subject: [PATCH] ensure guest's kvmclock never goes backwards when TSC jumps backward
There are buggy hosts in the wild that advertise invariant
TSC and as result host uses TSC as clocksource, but TSC on
such host sometimes sporadically jumps backwards.
This causes kvmclock to go backwards if host advertises
PVCLOCK_TSC_STABLE_BIT, which turns off aggregated clock
accumulator and returns:
pvclock_vcpu_time_info.system_timestamp + offset
where 'offset' is calculated using TSC.
Since TSC is not virtualized in KVM, it makes guest see
TSC jumped backwards and leads to kvmclock going backwards
as well.
This is defensive patch that keeps per CPU last clock value
and ensures that clock will never go backwards even with
using PVCLOCK_TSC_STABLE_BIT enabled path.
Signed-off-by: Igor Mammedov <imammedo@...hat.com>
---
RHBZ: 1115795
---
arch/x86/kernel/pvclock.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kernel/pvclock.c b/arch/x86/kernel/pvclock.c
index 2f355d2..dd9df0e 100644
--- a/arch/x86/kernel/pvclock.c
+++ b/arch/x86/kernel/pvclock.c
@@ -71,11 +71,14 @@ u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
return flags & valid_flags;
}
+static DEFINE_PER_CPU(cycle_t, last_clock);
+
cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
{
unsigned version;
cycle_t ret;
- u64 last;
+ u64 last, *this_cpu_last;
+ s64 clock_delta;
u8 flags;
do {
@@ -87,6 +90,16 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
pvclock_touch_watchdogs();
}
+ this_cpu_last = &get_cpu_var(last_clock);
+ clock_delta = ret - *this_cpu_last;
+ if (likely(clock_delta > 0)) {
+ *this_cpu_last = ret;
+ } else {
+ ret = *this_cpu_last;
+ WARN_ONCE(1, "clock went backwards");
+ }
+ put_cpu_var(last_clock);
+
if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) &&
(flags & PVCLOCK_TSC_STABLE_BIT))
return ret;
--
1.8.3.1
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists