public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@redhat.com>
To: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Andy Lutomirski <luto@amacapital.net>,
	kvm list <kvm@vger.kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: KVM: x86: fix kvmclock write race (v2)
Date: Mon, 20 Apr 2015 14:54:22 +0200	[thread overview]
Message-ID: <20150420125422.GA26491@potion.brq.redhat.com> (raw)
In-Reply-To: <20150418002310.GA23898@amt.cnet>

2015-04-17 21:23-0300, Marcelo Tosatti:
> 
> From: Radim Krčmář <rkrcmar@redhat.com>
> 
> As noted by Andy Lutomirski, kvm does not follow the documented version
> protocol. Fix it.
> 
> Note: this bug results in a race which can occur if the following three
> conditions are met:
> 
> 1) There is KVM guest time update (there is one every 5 minutes).
> 
> 2) Which races with a thread in the guest in the following way:
> The execution of these 29 instructions has to take at _least_
> 2 seconds (rebalance interval is 1 second).
> 
> lsl    %r9w,%esi
> mov    %esi,%r8d
> and    $0x3f,%esi
> and    $0xfff,%r8d
> test   $0xfc0,%r8d
> jne    0xa12 <vread_pvclock+210>
> shl    $0x6,%rsi
> mov    -0xa01000(%rsi),%r10d
> data32 xchg %ax,%ax
> data32 xchg %ax,%ax
> rdtsc  
> shl    $0x20,%rdx
> mov    %eax,%eax
> movsbl -0xa00fe4(%rsi),%ecx
> or     %rax,%rdx
> sub    -0xa00ff8(%rsi),%rdx
> mov    -0xa00fe8(%rsi),%r11d
> mov    %rdx,%rax
> shl    %cl,%rax
> test   %ecx,%ecx
> js     0xa08 <vread_pvclock+200>
> mov    %r11d,%edx
> movzbl -0xa00fe3(%rsi),%ecx
> mov    -0xa00ff0(%rsi),%r11
> mul    %rdx
> shrd   $0x20,%rdx,%rax
> data32 xchg %ax,%ax
> data32 xchg %ax,%ax
> lsl    %r9w,%edx
> 
> 3) Scheduler moves the task, while executing these 29 instructions, to a
> destination processor, then back to the source processor.
> 
> 4) Source processor, after has been moved back from destination,
> perceives data out of order as written by processor performing guest
> time update (item 1), with string mov.
> 
> Given the rarity of this condition, and the fact it was never observed
> or reported, reverting pvclock vsyscall on systems whose host is
> susceptible to the race, seems an excessive measure.
> 
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> Cc: stable@kernel.org

Thanks.

Reviewed-or-Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>

Like most code, I would have written it differently now :)

> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> +	kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
> +				&guest_hv_clock,
> +				sizeof(guest_hv_clock));

The easiest optimization is replacing sizeof(guest_hv_clock) with
  offsetof(typeof(guest_hv_clock), version) + sizeof(guest_hv_clock.version)
because kvm_write_guest_cached() allows writing of prefixes.
This still won't get optimized to a simple MOV at compile time, but
saves few mov bytes.

(Offset of version is 0 now, so using 'sizeof guest_hv_clock.version' is
 just a minor offence sand saves some hard to read code.)

      reply	other threads:[~2015-04-20 12:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-17 23:38 KVM: x86: fix kvmclock write race Marcelo Tosatti
2015-04-18  0:04 ` Andy Lutomirski
2015-04-18  0:12   ` Marcelo Tosatti
2015-04-18  0:20   ` Marcelo Tosatti
2015-04-18  0:23     ` KVM: x86: fix kvmclock write race (v2) Marcelo Tosatti
2015-04-20 12:54       ` Radim Krčmář [this message]

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=20150420125422.GA26491@potion.brq.redhat.com \
    --to=rkrcmar@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    /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