All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vitaly Kuznetsov <vkuznets@redhat.com>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: Andy Lutomirski <luto@amacapital.net>,
	"K. Y. Srinivasan" <kys@microsoft.com>, X86 ML <x86@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	Haiyang Zhang <haiyangz@microsoft.com>,
	Stephen Hemminger <sthemmin@microsoft.com>,
	Dexuan Cui <decui@microsoft.com>,
	"linux-kernel\@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	devel@linuxdriverproject.org,
	Linux Virtualization <virtualization@lists.linux-foundation.org>
Subject: Re: [PATCH v2 0/3] x86/vdso: Add Hyper-V TSC page clocksource support
Date: Fri, 17 Feb 2017 11:14:42 +0100	[thread overview]
Message-ID: <87tw7txgx9.fsf@vitty.brq.redhat.com> (raw)
In-Reply-To: <alpine.DEB.2.20.1702161832020.3543@nanos> (Thomas Gleixner's message of "Thu, 16 Feb 2017 18:50:58 +0100 (CET)")

Thomas Gleixner <tglx@linutronix.de> writes:

> On Wed, 15 Feb 2017, Vitaly Kuznetsov wrote:
>> Actually, we already have an implementation of TSC page update in KVM
>> (see arch/x86/kvm/hyperv.c, kvm_hv_setup_tsc_page()) and the update does
>> the following:
>> 
>> 0) stash seq into seq_prev
>> 1) seq = 0 making all reads from the page invalid
>> 2) smp_wmb()
>> 3) update tsc_scale, tsc_offset
>> 4) smp_wmb()
>> 5) set seq = seq_prev + 1
>
> I hope they handle the case where seq_prev overflows and becomes 0 :)
>
>> As far as I understand this helps with situations you described above as
>> guest will notice either invalid value of 0 or seq change. In case the
>> implementation in real Hyper-V is the same we're safe with compile
>> barriers only.
>
> On x86 that's correct. smp_rmb() resolves to barrier(), but you certainly
> need the smp_wmb() on the writer side.
>
> Now looking at the above your reader side code is bogus:
>
> +       while (1) {
> +               sequence = tsc_pg->tsc_sequence;
> +               if (!sequence)
> +                       break;
>
> Why would you break out of the loop when seq is 0? The 0 is just telling
> you that there is an update in progress.

Not only. As far as I understand (and I *think* K. Y. pointed this out)
when VM is migrating to another host TSC page clocksource is disabled for
extended period of time so we're better off reading from MSR than
looping here. With regards to VDSO this means reverting to doing normal
syscall.

>
> The Linux seqcount writer side is:
>
>     seq++;
>     smp_wmb();
>
>     update...
>
>     smp_wmb();
>     seq++;
>
> and it's defined that an odd sequence count, i.e. bit 0 set means update in
> progress. Which is nice, because you don't have to treat 0 special on the
> writer side and you don't need extra storage to stash seq away :)
>
> So the reader side does:
>
>     do {
>     	  while (1) {
>     	     s = READ_ONCE(seq);
> 	     if (!(s & 0x01))
> 	        break;
>      	     cpu_relax();
>          }
> 	 smp_rmb();
>
> 	 read data ...
>
> 	 smp_rmb();
>    } while (s != seq)
>
> So for that hyperv thing you want:
>
>     do {
>     	  while (1) {
>     	     s = READ_ONCE(seq);
> 	     if (s)
> 	        break;
>      	     cpu_relax();
>          }
> 	 smp_rmb();
>
> 	 read data ...
>
> 	 smp_rmb();
>    } while (s != seq)
>
> Thanks,
>
> 	tglx

-- 
  Vitaly

  parent reply	other threads:[~2017-02-17 10:14 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-14 12:44 [PATCH v2 0/3] x86/vdso: Add Hyper-V TSC page clocksource support Vitaly Kuznetsov
2017-02-14 12:44 ` Vitaly Kuznetsov
2017-02-14 12:44 ` [PATCH v2 1/3] x86/hyperv: implement hv_get_tsc_page() Vitaly Kuznetsov
2017-02-14 12:44   ` Vitaly Kuznetsov
2017-02-14 12:44 ` [PATCH v2 2/3] x86/hyperv: move TSC reading method to asm/mshyperv.h Vitaly Kuznetsov
2017-02-14 12:44   ` Vitaly Kuznetsov
2017-02-14 12:44 ` [PATCH v2 3/3] x86/vdso: Add VCLOCK_HVCLOCK vDSO clock read method Vitaly Kuznetsov
2017-02-14 12:44 ` Vitaly Kuznetsov
2017-02-14 14:46 ` [PATCH v2 0/3] x86/vdso: Add Hyper-V TSC page clocksource support KY Srinivasan via Virtualization
2017-02-14 14:46   ` KY Srinivasan
2017-02-14 14:56 ` Thomas Gleixner
2017-02-14 14:56   ` Thomas Gleixner
2017-02-14 15:50   ` Vitaly Kuznetsov
2017-02-14 15:50     ` Vitaly Kuznetsov
2017-02-14 17:34     ` Andy Lutomirski
2017-02-14 17:34       ` Andy Lutomirski
2017-02-15 14:01       ` Vitaly Kuznetsov
2017-02-15 14:01         ` Vitaly Kuznetsov
2017-02-16 17:50         ` Thomas Gleixner
2017-02-16 17:50           ` Thomas Gleixner
2017-02-17 10:14           ` Vitaly Kuznetsov
2017-02-17 10:14           ` Vitaly Kuznetsov [this message]
2017-02-17 10:35             ` Thomas Gleixner
2017-02-17 10:35               ` Thomas Gleixner
2017-02-17 17:02             ` Andy Lutomirski
2017-02-17 17:02             ` Andy Lutomirski
2017-02-17 17:55               ` Thomas Gleixner
2017-02-17 17:55               ` Thomas Gleixner

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=87tw7txgx9.fsf@vitty.brq.redhat.com \
    --to=vkuznets@redhat.com \
    --cc=decui@microsoft.com \
    --cc=devel@linuxdriverproject.org \
    --cc=haiyangz@microsoft.com \
    --cc=hpa@zytor.com \
    --cc=kys@microsoft.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mingo@redhat.com \
    --cc=sthemmin@microsoft.com \
    --cc=tglx@linutronix.de \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.