[Qemu-devel] [PATCH v3] kvmclock: update system_time_msr address forcibly

Denis Plotnikov posted 1 patch 6 years, 10 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/1496054944-25623-1-git-send-email-dplotnikov@virtuozzo.com
Test checkpatch passed
Test docker passed
Test s390x passed
hw/i386/kvm/clock.c | 3 +++
1 file changed, 3 insertions(+)
[Qemu-devel] [PATCH v3] kvmclock: update system_time_msr address forcibly
Posted by Denis Plotnikov 6 years, 10 months ago
Do an update of system_time_msr address every time before reading
the value of tsc_timestamp from guest's kvmclock page.

There is no other code paths which ensure that qemu has an up-to-date
value of system_time_msr. So, force this update on guest's tsc_timestamp
reading.

This bug causes effect on those nested setups which turn off TPR access
interception for L2 guests and that access being intercepted by L0 doesn't
show up in L1.
Linux bootstrap initiate kvmclock before APIC initializing causing TPR access.
That's why on L1 guests, having TPR interception turned on for L2, the effect
of the bug is not revealed.

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

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

diff --git a/hw/i386/kvm/clock.c b/hw/i386/kvm/clock.c
index 13eca37..363d1b5 100644
--- a/hw/i386/kvm/clock.c
+++ b/hw/i386/kvm/clock.c
@@ -19,6 +19,7 @@
 #include "qemu/host-utils.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/kvm.h"
+#include "sysemu/hw_accel.h"
 #include "kvm_i386.h"
 #include "hw/sysbus.h"
 #include "hw/kvm/clock.h"
@@ -69,6 +70,8 @@ static uint64_t kvmclock_current_nsec(KVMClockState *s)
     uint64_t nsec_hi;
     uint64_t nsec;
 
+    cpu_synchronize_state(cpu);
+
     if (!(env->system_time_msr & 1ULL)) {
         /* KVM clock not active */
         return 0;
-- 
2.7.4


Re: [Qemu-devel] [PATCH v3] kvmclock: update system_time_msr address forcibly
Posted by Roman Kagan 6 years, 10 months ago
On Mon, May 29, 2017 at 01:49:04PM +0300, Denis Plotnikov wrote:
> Do an update of system_time_msr address every time before reading
> the value of tsc_timestamp from guest's kvmclock page.
> 
> There is no other code paths which ensure that qemu has an up-to-date
> value of system_time_msr. So, force this update on guest's tsc_timestamp
> reading.
> 
> This bug causes effect on those nested setups which turn off TPR access
> interception for L2 guests and that access being intercepted by L0 doesn't
> show up in L1.
> Linux bootstrap initiate kvmclock before APIC initializing causing TPR access.
> That's why on L1 guests, having TPR interception turned on for L2, the effect
> of the bug is not revealed.
> 
> This patch fixes this problem by making sure it knows the correct
> system_time_msr address every time it is needed.
> 
> Signed-off-by: Denis Plotnikov <dplotnikov@virtuozzo.com>
> ---
>  hw/i386/kvm/clock.c | 3 +++
>  1 file changed, 3 insertions(+)
> 

Please don't forget the changelog.

Reviewed-by: Roman Kagan <rkagan@virtuozzo.com>