From: Igor Mammedov <imammedo@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: kvm@vger.kernel.org, pbonzini@redhat.com, x86@kernel.org,
mtosatti@redhat.com
Subject: [PATCH] ensure guest's kvmclock never goes backwards when TSC jumps backward
Date: Wed, 16 Jul 2014 09:52:48 +0000 [thread overview]
Message-ID: <1405504368-5581-1-git-send-email-imammedo@redhat.com> (raw)
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@redhat.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
next reply other threads:[~2014-07-16 9:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-16 9:52 Igor Mammedov [this message]
2014-07-16 10:18 ` [PATCH] ensure guest's kvmclock never goes backwards when TSC jumps backward Paolo Bonzini
2014-07-16 11:41 ` Marcelo Tosatti
2014-07-16 13:55 ` Igor Mammedov
2014-07-16 14:16 ` Paolo Bonzini
2014-07-16 14:51 ` Igor Mammedov
2014-07-16 14:55 ` Paolo Bonzini
2014-07-16 15:18 ` Igor Mammedov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1405504368-5581-1-git-send-email-imammedo@redhat.com \
--to=imammedo@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox