Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1418248 > unrolled thread

[PATCH] pvclock: introduce seqcount-like API

Started byPaolo Bonzini <pbonzini@redhat.com>
First post2016-06-09 13:30 +0200
Last post2016-06-09 20:10 +0200
Articles 7 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] pvclock: introduce seqcount-like API Paolo Bonzini <pbonzini@redhat.com> - 2016-06-09 13:30 +0200
    Re: [PATCH] pvclock: introduce seqcount-like API Paolo Bonzini <pbonzini@redhat.com> - 2016-06-09 14:50 +0200
      Re: [PATCH] pvclock: introduce seqcount-like API Roman Kagan <rkagan@virtuozzo.com> - 2016-06-09 15:40 +0200
        Re: [PATCH] pvclock: introduce seqcount-like API Paolo Bonzini <pbonzini@redhat.com> - 2016-06-09 15:50 +0200
          Re: [PATCH] pvclock: introduce seqcount-like API Andy Lutomirski <luto@amacapital.net> - 2016-06-09 19:20 +0200
            Re: [PATCH] pvclock: introduce seqcount-like API Paolo Bonzini <pbonzini@redhat.com> - 2016-06-09 20:10 +0200
              Re: [PATCH] pvclock: introduce seqcount-like API Andy Lutomirski <luto@amacapital.net> - 2016-06-09 20:10 +0200

#1418248 — [PATCH] pvclock: introduce seqcount-like API

FromPaolo Bonzini <pbonzini@redhat.com>
Date2016-06-09 13:30 +0200
Subject[PATCH] pvclock: introduce seqcount-like API
Message-ID<rI6jT-5Fr-3@gated-at.bofh.it>
The version field in struct pvclock_vcpu_time_info basically implements
a seqcount.  Wrap it with the usual read_begin and read_retry functions,
and use these APIs instead of peppering the code with smp_rmb()s.
While at it, change it to the more pedantically correct virt_rmb().

With this change, __pvclock_read_cycles can be simplified noticeably.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/entry/vdso/vclock_gettime.c | 25 +++++------------------
 arch/x86/include/asm/pvclock.h       | 39 +++++++++++++++++++++---------------
 arch/x86/kernel/pvclock.c            | 17 ++++++----------
 3 files changed, 34 insertions(+), 47 deletions(-)

diff --git a/arch/x86/entry/vdso/vclock_gettime.c b/arch/x86/entry/vdso/vclock_gettime.c
index 2f02d23a05ef..94d54d0defa7 100644
--- a/arch/x86/entry/vdso/vclock_gettime.c
+++ b/arch/x86/entry/vdso/vclock_gettime.c
@@ -96,9 +96,8 @@ static notrace cycle_t vread_pvclock(int *mode)
 {
 	const struct pvclock_vcpu_time_info *pvti = &get_pvti0()->pvti;
 	cycle_t ret;
-	u64 tsc, pvti_tsc;
-	u64 last, delta, pvti_system_time;
-	u32 version, pvti_tsc_to_system_mul, pvti_tsc_shift;
+	u64 last;
+	u32 version;
 
 	/*
 	 * Note: The kernel and hypervisor must guarantee that cpu ID
@@ -123,29 +122,15 @@ static notrace cycle_t vread_pvclock(int *mode)
 	 */
 
 	do {
-		version = pvti->version;
-
-		smp_rmb();
+		version = pvclock_read_begin(pvti);
 
 		if (unlikely(!(pvti->flags & PVCLOCK_TSC_STABLE_BIT))) {
 			*mode = VCLOCK_NONE;
 			return 0;
 		}
 
-		tsc = rdtsc_ordered();
-		pvti_tsc_to_system_mul = pvti->tsc_to_system_mul;
-		pvti_tsc_shift = pvti->tsc_shift;
-		pvti_system_time = pvti->system_time;
-		pvti_tsc = pvti->tsc_timestamp;
-
-		/* Make sure that the version double-check is last. */
-		smp_rmb();
-	} while (unlikely((version & 1) || version != pvti->version));
-
-	delta = tsc - pvti_tsc;
-	ret = pvti_system_time +
-		pvclock_scale_delta(delta, pvti_tsc_to_system_mul,
-				    pvti_tsc_shift);
+		ret = __pvclock_read_cycles(pvti);
+	} while (pvclock_read_retry(pvti, version));
 
 	/* refer to vread_tsc() comment for rationale */
 	last = gtod->cycle_last;
diff --git a/arch/x86/include/asm/pvclock.h b/arch/x86/include/asm/pvclock.h
index 7c1c89598688..0ee92db1e9f3 100644
--- a/arch/x86/include/asm/pvclock.h
+++ b/arch/x86/include/asm/pvclock.h
@@ -25,6 +25,24 @@ void pvclock_resume(void);
 
 void pvclock_touch_watchdogs(void);
 
+static __always_inline
+unsigned pvclock_read_begin(const struct pvclock_vcpu_time_info *src)
+{
+	unsigned version = src->version & ~1;
+	/* Make sure that the version is read before the data. */
+	virt_rmb();
+	return version;
+}
+
+static __always_inline
+bool pvclock_read_retry(const struct pvclock_vcpu_time_info *src,
+			unsigned version)
+{
+	/* Make sure that the version is re-read after the data. */
+	virt_rmb();
+	return version != src->version;
+}
+
 /*
  * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
  * yielding a 64-bit result.
@@ -69,23 +87,12 @@ static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
 }
 
 static __always_inline
-unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
-			       cycle_t *cycles, u8 *flags)
+cycle_t __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src)
 {
-	unsigned version;
-	cycle_t offset;
-	u64 delta;
-
-	version = src->version;
-	/* Make the latest version visible */
-	smp_rmb();
-
-	delta = rdtsc_ordered() - src->tsc_timestamp;
-	offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
-				   src->tsc_shift);
-	*cycles = src->system_time + offset;
-	*flags = src->flags;
-	return version;
+	u64 delta = rdtsc_ordered() - src->tsc_timestamp;
+	cycle_t offset = pvclock_scale_delta(delta, src->tsc_to_system_mul,
+					     src->tsc_shift);
+	return src->system_time + offset;
 }
 
 struct pvclock_vsyscall_time_info {
diff --git a/arch/x86/kernel/pvclock.c b/arch/x86/kernel/pvclock.c
index 06c58ce46762..3599404e3089 100644
--- a/arch/x86/kernel/pvclock.c
+++ b/arch/x86/kernel/pvclock.c
@@ -64,14 +64,9 @@ u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
 	u8 flags;
 
 	do {
-		version = src->version;
-		/* Make the latest version visible */
-		smp_rmb();
-
+		version = pvclock_read_begin(src);
 		flags = src->flags;
-		/* Make sure that the version double-check is last. */
-		smp_rmb();
-	} while ((src->version & 1) || version != src->version);
+	} while (pvclock_read_retry(src, version));
 
 	return flags & valid_flags;
 }
@@ -84,10 +79,10 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
 	u8 flags;
 
 	do {
-		version = __pvclock_read_cycles(src, &ret, &flags);
-		/* Make sure that the version double-check is last. */
-		smp_rmb();
-	} while ((src->version & 1) || version != src->version);
+		version = pvclock_read_begin(src);
+		ret = __pvclock_read_cycles(src);
+		flags = src->flags;
+	} while (pvclock_read_retry(src, version));
 
 	if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) {
 		src->flags &= ~PVCLOCK_GUEST_STOPPED;
-- 
1.8.3.1

[toc] | [next] | [standalone]


#1418294

FromPaolo Bonzini <pbonzini@redhat.com>
Date2016-06-09 14:50 +0200
Message-ID<rI7zk-6pj-23@gated-at.bofh.it>
In reply to#1418248

On 09/06/2016 14:43, Roman Kagan wrote:
> On Thu, Jun 09, 2016 at 01:23:23PM +0200, Paolo Bonzini wrote:
>> The version field in struct pvclock_vcpu_time_info basically implements
>> a seqcount.  Wrap it with the usual read_begin and read_retry functions,
>> and use these APIs instead of peppering the code with smp_rmb()s.
>> While at it, change it to the more pedantically correct virt_rmb().
>>
>> With this change, __pvclock_read_cycles can be simplified noticeably.
>>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>>  arch/x86/entry/vdso/vclock_gettime.c | 25 +++++------------------
>>  arch/x86/include/asm/pvclock.h       | 39 +++++++++++++++++++++---------------
>>  arch/x86/kernel/pvclock.c            | 17 ++++++----------
>>  3 files changed, 34 insertions(+), 47 deletions(-)
> [...]
>> --- a/arch/x86/include/asm/pvclock.h
>> +++ b/arch/x86/include/asm/pvclock.h
> [...]
>> @@ -69,23 +87,12 @@ static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
>>  }
>>  
>>  static __always_inline
>> -unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
>> -			       cycle_t *cycles, u8 *flags)
>> +cycle_t __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src)
>>  {
>> -	unsigned version;
>> -	cycle_t offset;
>> -	u64 delta;
>> -
>> -	version = src->version;
>> -	/* Make the latest version visible */
>> -	smp_rmb();
> 
> This is on top of Minfei's patch, right?  It isn't in Linus' tree yet so
> I wonder if it makes sense to merge the two patches into one.
> 
> Will you post it to stable, too?

Not this one, because Minfei's patch is enough to fix the bug, but I do
plan on including it in 4.7 to simplify the merging (kvm/next has
already been branched off Linus's tree).

Paolo

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

[toc] | [prev] | [next] | [standalone]


#1418328

FromRoman Kagan <rkagan@virtuozzo.com>
Date2016-06-09 15:40 +0200
Message-ID<rI8lI-6YM-21@gated-at.bofh.it>
In reply to#1418294
On Thu, Jun 09, 2016 at 02:47:54PM +0200, Paolo Bonzini wrote:
> On 09/06/2016 14:43, Roman Kagan wrote:
> > On Thu, Jun 09, 2016 at 01:23:23PM +0200, Paolo Bonzini wrote:
> >> The version field in struct pvclock_vcpu_time_info basically implements
> >> a seqcount.  Wrap it with the usual read_begin and read_retry functions,
> >> and use these APIs instead of peppering the code with smp_rmb()s.
> >> While at it, change it to the more pedantically correct virt_rmb().
> >>
> >> With this change, __pvclock_read_cycles can be simplified noticeably.
> >>
> >> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> >> ---
> >>  arch/x86/entry/vdso/vclock_gettime.c | 25 +++++------------------
> >>  arch/x86/include/asm/pvclock.h       | 39 +++++++++++++++++++++---------------
> >>  arch/x86/kernel/pvclock.c            | 17 ++++++----------
> >>  3 files changed, 34 insertions(+), 47 deletions(-)
> > [...]
> >> --- a/arch/x86/include/asm/pvclock.h
> >> +++ b/arch/x86/include/asm/pvclock.h
> > [...]
> >> @@ -69,23 +87,12 @@ static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
> >>  }
> >>  
> >>  static __always_inline
> >> -unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
> >> -			       cycle_t *cycles, u8 *flags)
> >> +cycle_t __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src)
> >>  {
> >> -	unsigned version;
> >> -	cycle_t offset;
> >> -	u64 delta;
> >> -
> >> -	version = src->version;
> >> -	/* Make the latest version visible */
> >> -	smp_rmb();
> > 
> > This is on top of Minfei's patch, right?  It isn't in Linus' tree yet so
> > I wonder if it makes sense to merge the two patches into one.
> > 
> > Will you post it to stable, too?
> 
> Not this one, because Minfei's patch is enough to fix the bug, but I do

Has it landed in any public tree?  I'm unable to find any.  There
appears to be another version of the patch on the list, so I'm confused.

Roman.

[toc] | [prev] | [next] | [standalone]


#1418343

FromPaolo Bonzini <pbonzini@redhat.com>
Date2016-06-09 15:50 +0200
Message-ID<rI8vo-72W-29@gated-at.bofh.it>
In reply to#1418328

On 09/06/2016 15:35, Roman Kagan wrote:
> On Thu, Jun 09, 2016 at 02:47:54PM +0200, Paolo Bonzini wrote:
>> On 09/06/2016 14:43, Roman Kagan wrote:
>>> On Thu, Jun 09, 2016 at 01:23:23PM +0200, Paolo Bonzini wrote:
>>>> The version field in struct pvclock_vcpu_time_info basically implements
>>>> a seqcount.  Wrap it with the usual read_begin and read_retry functions,
>>>> and use these APIs instead of peppering the code with smp_rmb()s.
>>>> While at it, change it to the more pedantically correct virt_rmb().
>>>>
>>>> With this change, __pvclock_read_cycles can be simplified noticeably.
>>>>
>>>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>>>> ---
>>>>  arch/x86/entry/vdso/vclock_gettime.c | 25 +++++------------------
>>>>  arch/x86/include/asm/pvclock.h       | 39 +++++++++++++++++++++---------------
>>>>  arch/x86/kernel/pvclock.c            | 17 ++++++----------
>>>>  3 files changed, 34 insertions(+), 47 deletions(-)
>>> [...]
>>>> --- a/arch/x86/include/asm/pvclock.h
>>>> +++ b/arch/x86/include/asm/pvclock.h
>>> [...]
>>>> @@ -69,23 +87,12 @@ static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
>>>>  }
>>>>  
>>>>  static __always_inline
>>>> -unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
>>>> -			       cycle_t *cycles, u8 *flags)
>>>> +cycle_t __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src)
>>>>  {
>>>> -	unsigned version;
>>>> -	cycle_t offset;
>>>> -	u64 delta;
>>>> -
>>>> -	version = src->version;
>>>> -	/* Make the latest version visible */
>>>> -	smp_rmb();
>>>
>>> This is on top of Minfei's patch, right?  It isn't in Linus' tree yet so
>>> I wonder if it makes sense to merge the two patches into one.
>>>
>>> Will you post it to stable, too?
>>
>> Not this one, because Minfei's patch is enough to fix the bug, but I do
> 
> Has it landed in any public tree?  I'm unable to find any.  There
> appears to be another version of the patch on the list, so I'm confused.

I'm about to push it to kvm/master.

Paolo

[toc] | [prev] | [next] | [standalone]


#1418503

FromAndy Lutomirski <luto@amacapital.net>
Date2016-06-09 19:20 +0200
Message-ID<rIbMB-Wg-3@gated-at.bofh.it>
In reply to#1418343
On Thu, Jun 9, 2016 at 6:45 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
>
> On 09/06/2016 15:35, Roman Kagan wrote:
>> On Thu, Jun 09, 2016 at 02:47:54PM +0200, Paolo Bonzini wrote:
>>> On 09/06/2016 14:43, Roman Kagan wrote:
>>>> On Thu, Jun 09, 2016 at 01:23:23PM +0200, Paolo Bonzini wrote:
>>>>> The version field in struct pvclock_vcpu_time_info basically implements
>>>>> a seqcount.  Wrap it with the usual read_begin and read_retry functions,
>>>>> and use these APIs instead of peppering the code with smp_rmb()s.
>>>>> While at it, change it to the more pedantically correct virt_rmb().
>>>>>
>>>>> With this change, __pvclock_read_cycles can be simplified noticeably.
>>>>>
>>>>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>>>>> ---
>>>>>  arch/x86/entry/vdso/vclock_gettime.c | 25 +++++------------------
>>>>>  arch/x86/include/asm/pvclock.h       | 39 +++++++++++++++++++++---------------
>>>>>  arch/x86/kernel/pvclock.c            | 17 ++++++----------
>>>>>  3 files changed, 34 insertions(+), 47 deletions(-)
>>>> [...]
>>>>> --- a/arch/x86/include/asm/pvclock.h
>>>>> +++ b/arch/x86/include/asm/pvclock.h
>>>> [...]
>>>>> @@ -69,23 +87,12 @@ static inline u64 pvclock_scale_delta(u64 delta, u32 mul_frac, int shift)
>>>>>  }
>>>>>
>>>>>  static __always_inline
>>>>> -unsigned __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src,
>>>>> -                         cycle_t *cycles, u8 *flags)
>>>>> +cycle_t __pvclock_read_cycles(const struct pvclock_vcpu_time_info *src)
>>>>>  {
>>>>> -  unsigned version;
>>>>> -  cycle_t offset;
>>>>> -  u64 delta;
>>>>> -
>>>>> -  version = src->version;
>>>>> -  /* Make the latest version visible */
>>>>> -  smp_rmb();
>>>>
>>>> This is on top of Minfei's patch, right?  It isn't in Linus' tree yet so
>>>> I wonder if it makes sense to merge the two patches into one.
>>>>
>>>> Will you post it to stable, too?
>>>
>>> Not this one, because Minfei's patch is enough to fix the bug, but I do
>>
>> Has it landed in any public tree?  I'm unable to find any.  There
>> appears to be another version of the patch on the list, so I'm confused.
>
> I'm about to push it to kvm/master.
>

Sorry for being slow.  I'm catching up.  In its current form, I don't
like this patch.  Please don't apply it.

The problem is that this makes two significant changes at once:

1. Use the new version helpers.  I like that change.

2. Use __pvclock_read_cycles.  That should be separate, and it should
come with timing numbers in the changelog.

--Andy

[toc] | [prev] | [next] | [standalone]


#1418551

FromPaolo Bonzini <pbonzini@redhat.com>
Date2016-06-09 20:10 +0200
Message-ID<rIcz0-1vR-33@gated-at.bofh.it>
In reply to#1418503

On 09/06/2016 19:12, Andy Lutomirski wrote:
> On Thu, Jun 9, 2016 at 6:45 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>> On 09/06/2016 15:35, Roman Kagan wrote:
>>> On Thu, Jun 09, 2016 at 02:47:54PM +0200, Paolo Bonzini wrote:
>>>> On 09/06/2016 14:43, Roman Kagan wrote:
>>> Has it landed in any public tree?  I'm unable to find any.  There
>>> appears to be another version of the patch on the list, so I'm confused.
>>
>> I'm about to push it to kvm/master.
> 
> Sorry for being slow.  I'm catching up.  In its current form, I don't
> like this patch.  Please don't apply it.

Sure, I was talking about Minfei's patches, not this one. :)  Of course
I need ack for this one.

> The problem is that this makes two significant changes at once:
> 
> 1. Use the new version helpers.  I like that change.
> 
> 2. Use __pvclock_read_cycles.  That should be separate, and it should
> come with timing numbers in the changelog.

__pvclock_read_cycles is pretty much the same as the code that is being
inlined.  Thus the only change is that __pvclock_read_cycles is called
inside the loop rather than outside, but the loop really is expected to
never roll so why make a copy in the first place?

I'll split the patch anyway, thanks!

Paolo

[toc] | [prev] | [next] | [standalone]


#1418553

FromAndy Lutomirski <luto@amacapital.net>
Date2016-06-09 20:10 +0200
Message-ID<rIcz0-1vR-31@gated-at.bofh.it>
In reply to#1418551
On Thu, Jun 9, 2016 at 11:03 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
>
> On 09/06/2016 19:12, Andy Lutomirski wrote:
>> On Thu, Jun 9, 2016 at 6:45 AM, Paolo Bonzini <pbonzini@redhat.com> wrote:
>>> On 09/06/2016 15:35, Roman Kagan wrote:
>>>> On Thu, Jun 09, 2016 at 02:47:54PM +0200, Paolo Bonzini wrote:
>>>>> On 09/06/2016 14:43, Roman Kagan wrote:
>>>> Has it landed in any public tree?  I'm unable to find any.  There
>>>> appears to be another version of the patch on the list, so I'm confused.
>>>
>>> I'm about to push it to kvm/master.
>>
>> Sorry for being slow.  I'm catching up.  In its current form, I don't
>> like this patch.  Please don't apply it.
>
> Sure, I was talking about Minfei's patches, not this one. :)  Of course
> I need ack for this one.
>
>> The problem is that this makes two significant changes at once:
>>
>> 1. Use the new version helpers.  I like that change.
>>
>> 2. Use __pvclock_read_cycles.  That should be separate, and it should
>> come with timing numbers in the changelog.
>
> __pvclock_read_cycles is pretty much the same as the code that is being
> inlined.  Thus the only change is that __pvclock_read_cycles is called
> inside the loop rather than outside, but the loop really is expected to
> never roll so why make a copy in the first place?

I feel like I had a reason, but I don't remember what it was.

--Andy

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web