mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: "Radim Krčmář" <rkrcmar@redhat.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	Andy Lutomirski <luto@amacapital.net>,
	stable@vger.kernel.org
Subject: Re: [PATCH] x86: vdso: fix pvclock races with task migration
Date: Mon, 6 Apr 2015 17:57:16 -0300	[thread overview]
Message-ID: <20150406205716.GA1896@amt.cnet> (raw)
In-Reply-To: <1428000263-11892-1-git-send-email-rkrcmar@redhat.com>

On Thu, Apr 02, 2015 at 08:44:23PM +0200, Radim Krčmář wrote:
> If we were migrated right after __getcpu, but before reading the
> migration_count, we wouldn't notice that we read TSC of a different
> VCPU, nor that KVM's bug made pvti invalid, as only migration_count
> on source VCPU is increased.
> 
> Change vdso instead of updating migration_count on destination.
> 
> Fixes: 0a4e6be9ca17 ("x86: kvm: Revert "remove sched notifier for cross-cpu migrations"")
> Cc: stable@vger.kernel.org
> Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
> ---
>  Because it we'll get a complete rewrite, this series does not
>  - remove the outdated 'TODO: We can put [...]' comment
>  - use a proper encapsulation for the inner do-while loop
>  - optimize the outer do-while loop
>    (no need to re-read cpu id on version mismatch)
> 
>  arch/x86/vdso/vclock_gettime.c | 20 ++++++++++++--------
>  1 file changed, 12 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/x86/vdso/vclock_gettime.c b/arch/x86/vdso/vclock_gettime.c
> index 30933760ee5f..40d2473836c9 100644
> --- a/arch/x86/vdso/vclock_gettime.c
> +++ b/arch/x86/vdso/vclock_gettime.c
> @@ -99,21 +99,25 @@ static notrace cycle_t vread_pvclock(int *mode)
>  		 * __getcpu() calls (Gleb).
>  		 */
>  
> -		pvti = get_pvti(cpu);
> +		/* Make sure migrate_count will change if we leave the VCPU. */
> +		do {
> +			pvti = get_pvti(cpu);
> +			migrate_count = pvti->migrate_count;
>  
> -		migrate_count = pvti->migrate_count;
> +			cpu1 = cpu;
> +			cpu = __getcpu() & VGETCPU_CPU_MASK;
> +		} while (unlikely(cpu != cpu1));
>  
>  		version = __pvclock_read_cycles(&pvti->pvti, &ret, &flags);
>  
>  		/*
>  		 * Test we're still on the cpu as well as the version.
> -		 * We could have been migrated just after the first
> -		 * vgetcpu but before fetching the version, so we
> -		 * wouldn't notice a version change.
> +		 * - We must read TSC of pvti's VCPU.
> +		 * - KVM doesn't follow the versioning protocol, so data could
> +		 *   change before version if we left the VCPU.
>  		 */
> -		cpu1 = __getcpu() & VGETCPU_CPU_MASK;
> -	} while (unlikely(cpu != cpu1 ||
> -			  (pvti->pvti.version & 1) ||
> +		smp_rmb();
> +	} while (unlikely((pvti->pvti.version & 1) ||
>  			  pvti->pvti.version != version ||
>  			  pvti->migrate_count != migrate_count));
>  
> -- 
> 2.3.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reviewed-by: Marcelo Tosatti <mtosatti@redhat.com>


  parent reply	other threads:[~2015-04-06 20:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-02 18:44 Radim Krčmář
2015-04-02 18:59 ` Andy Lutomirski
2015-04-06 20:07   ` Andy Lutomirski
2015-04-06 22:04     ` Paolo Bonzini
2015-04-06 20:57 ` Marcelo Tosatti [this message]
2015-04-07 11:11 ` Paolo Bonzini
2015-04-07 12:47   ` Radim Krčmář
2015-04-07 13:57     ` 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=20150406205716.GA1896@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=pbonzini@redhat.com \
    --cc=rkrcmar@redhat.com \
    --cc=stable@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

Powered by JetHome