From: Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com>
To: kvm@vger.kernel.org
Cc: jan.kiszka@siemens.com, glommer@redhat.com, kraxel@redhat.com,
Vasilis Liaskovitis <vasilis.liaskovitis@profitbricks.com>
Subject: [PATCH] KVM, CPU hotplug: Avoid wraparound in pvclock_get_nsec_offset
Date: Mon, 12 Dec 2011 14:37:15 +0100 [thread overview]
Message-ID: <1323697035-5957-1-git-send-email-vasilis.liaskovitis@profitbricks.com> (raw)
Hotplugging a vCPU with kvmclock enabled can cause a guest stall/hang. When
the stall happens, pvclock_clocksource_read() is called for the new vCPU and
pvclock_get_nsec_offset calculates native_read_tsc() - shadow->tsc_timestamp.
shadow->tsc_timestamp contains a value larger than native_read_tsc(), so the
result is a very large 64-bit unsigned value. The global tsc variable
last_value gets updated with this, causing system stall/freeze:
"rcu_sched_state detected stalls on CPUs/tasks ..."
The large shadow->tsc_timestamp value observed in the hanged cases is the tsc
written into the "boot clock" on VM startup.
Is the "boot clock" persistent in the guest? Can it get accessed by a vCPU
other than vCPU 0, if its own hv_clock struct has not yet been registered
or if the host has not yet updated the new hv_clock with a valid tsc_timestamp
in kvm_guest_time_update() ?
Fix temporarily by returning a zero offset if the delta in
pvclock_get_nsec_offset() is negative.
Tested on 3.0.6 guest kernel. Testing this patch requires qemu-kvm from:
git://git.kiszka.org/qemu-kvm.git queues/cpu-hotplug
---
arch/x86/kernel/pvclock.c | 11 ++++++++---
1 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/arch/x86/kernel/pvclock.c b/arch/x86/kernel/pvclock.c
index 42eb330..9d31144 100644
--- a/arch/x86/kernel/pvclock.c
+++ b/arch/x86/kernel/pvclock.c
@@ -43,9 +43,14 @@ void pvclock_set_flags(u8 flags)
static u64 pvclock_get_nsec_offset(struct pvclock_shadow_time *shadow)
{
- u64 delta = native_read_tsc() - shadow->tsc_timestamp;
- return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
- shadow->tsc_shift);
+ u64 current_read_tsc = native_read_tsc();
+ if (current_read_tsc > shadow->tsc_timestamp) {
+ u64 delta = current_read_tsc - shadow->tsc_timestamp;
+ return pvclock_scale_delta(delta, shadow->tsc_to_nsec_mul,
+ shadow->tsc_shift);
+ }
+ /* tsc value can be smaller than tsc_timestamp on a vCPU hotplug */
+ else return 0;
}
/*
--
1.7.7.3
next reply other threads:[~2011-12-12 13:36 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-12 13:37 Vasilis Liaskovitis [this message]
2011-12-12 13:53 ` [PATCH] KVM, CPU hotplug: Avoid wraparound in pvclock_get_nsec_offset Jan Kiszka
2011-12-12 14:59 ` Vasilis Liaskovitis
2011-12-16 15:27 ` Marcelo Tosatti
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=1323697035-5957-1-git-send-email-vasilis.liaskovitis@profitbricks.com \
--to=vasilis.liaskovitis@profitbricks.com \
--cc=glommer@redhat.com \
--cc=jan.kiszka@siemens.com \
--cc=kraxel@redhat.com \
--cc=kvm@vger.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