qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Denis Plotnikov <dplotnikov@virtuozzo.com>
To: pbonzini@redhat.com
Cc: qemu-devel@nongnu.org, mtosatti@redhat.com, den@virtuozzo.com,
	svt-core@lists.sw.ru
Subject: [Qemu-devel] [PATCH] kvmclock: update system_time_msr address forcibly
Date: Wed, 24 May 2017 17:07:24 +0300	[thread overview]
Message-ID: <1495634844-14777-1-git-send-email-dplotnikov@virtuozzo.com> (raw)

Do an update of system_time_msr address every time before reading
the value of tsc_timestamp from guest's kvmclock page.

It should be done in a forcible manner because there is a situation
when system_time_msr has been set by kvm but qemu doesn't aware of it.
This leads to updates of kvmclock_offset without respect of guest's
kvmclock values.

The situation appears when L2 linux guest runs over L1 linux guest and
the action inducing system_time_msr update is tpr access reporting.
Some L1 linux guests turn off processing TPR access and when L0
gets an L2 exit induced by TPR MSR access it doesn't enter L1 and
processed it by itself.
Thus, L1 kvm doesn't know about that TPR access happening and doesn't
exit to qemu which in turn doesn't set system_time_msr address.

This patch fixes this by making sure it knows the correct address every
time it is needed.

Signed-off-by: Denis Plotnikov <dplotnikov@virtuozzo.com>
---
 hw/i386/kvm/clock.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
index e713162..035196a 100644
--- a/hw/i386/kvm/clock.c
+++ b/hw/i386/kvm/clock.c
@@ -48,11 +48,38 @@ struct pvclock_vcpu_time_info {
     uint8_t    pad[2];
 } __attribute__((__packed__)); /* 32 bytes */
 
+static void update_all_system_time_msr(void)
+{
+    CPUState *cpu;
+    CPUX86State *env;
+    struct {
+        struct kvm_msrs info;
+        struct kvm_msr_entry entries[1];
+    } msr_data;
+    int ret;
+
+    msr_data.info.nmsrs = 1;
+    msr_data.entries[0].index = MSR_KVM_SYSTEM_TIME;
+
+    CPU_FOREACH(cpu) {
+        ret = kvm_vcpu_ioctl(cpu, KVM_GET_MSRS, &msr_data);
+
+        if (ret < 0) {
+            fprintf(stderr, "KVM_GET_MSRS failed: %s\n", strerror(ret));
+            abort();
+        }
+
+        assert(ret == 1);
+        env = cpu->env_ptr;
+        env->system_time_msr = msr_data.entries[0].data;
+    }
+}
+
 static uint64_t kvmclock_current_nsec(KVMClockState *s)
 {
     CPUState *cpu = first_cpu;
     CPUX86State *env = cpu->env_ptr;
-    hwaddr kvmclock_struct_pa = env->system_time_msr & ~1ULL;
+    hwaddr kvmclock_struct_pa;
     uint64_t migration_tsc = env->tsc;
     struct pvclock_vcpu_time_info time;
     uint64_t delta;
@@ -60,6 +87,9 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
     uint64_t nsec_hi;
     uint64_t nsec;
 
+    update_all_system_time_msr();
+    kvmclock_struct_pa = env->system_time_msr & ~1ULL;
+
     if (!(env->system_time_msr & 1ULL)) {
         /* KVM clock not active */
         return 0;
-- 
2.7.4

             reply	other threads:[~2017-05-24 14:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-24 14:07 Denis Plotnikov [this message]
2017-05-24 14:09 ` [Qemu-devel] [PATCH] kvmclock: update system_time_msr address forcibly Denis V. Lunev
2017-05-24 14:37   ` Denis Plotnikov
2017-05-24 15:54 ` [Qemu-devel] [svt-core] " Roman Kagan
2017-05-24 17:20   ` Marcelo Tosatti
2017-05-25  9:40     ` Roman Kagan
2017-05-25 15:50       ` Paolo Bonzini

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=1495634844-14777-1-git-send-email-dplotnikov@virtuozzo.com \
    --to=dplotnikov@virtuozzo.com \
    --cc=den@virtuozzo.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=svt-core@lists.sw.ru \
    /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;
as well as URLs for NNTP newsgroup(s).