Re: [PATCH 3/3 V2] pvclock: Get rid of __pvclock_read_cycles in function pvclock_read_flags

From: Peter Zijlstra
Date: Thu Jun 09 2016 - 08:24:17 EST


On Thu, Jun 09, 2016 at 02:16:03PM +0200, Peter Zijlstra wrote:
> What's wrong with:
>
> u8 flags = READ_ONCE(src->flags);
>
> ?
>
> (and have the flags store be done using WRITE_ONCE() of course).
>
> Sure, if your total state is larger than one word you need the
> seqcount for integrity, but reading _one_ byte, shees.

Something like so; although given that I've never seen this code before
I can easily have missed an update site.

diff --git a/arch/x86/kernel/pvclock.c b/arch/x86/kernel/pvclock.c
index 99bfc025111d..7a4bf59aa929 100644
--- a/arch/x86/kernel/pvclock.c
+++ b/arch/x86/kernel/pvclock.c
@@ -60,15 +60,7 @@ void pvclock_resume(void)

u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)
{
- unsigned version;
- cycle_t ret;
- u8 flags;
-
- do {
- version = __pvclock_read_cycles(src, &ret, &flags);
- } while ((src->version & 1) || version != src->version);
-
- return flags & valid_flags;
+ return READ_ONCE(src->flags) & valid_flags;
}

cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
@@ -83,7 +75,8 @@ cycle_t pvclock_clocksource_read(struct pvclock_vcpu_time_info *src)
} while ((src->version & 1) || version != src->version);

if (unlikely((flags & PVCLOCK_GUEST_STOPPED) != 0)) {
- src->flags &= ~PVCLOCK_GUEST_STOPPED;
+ flags &= ~PVCLOCK_GUEST_STOPPED;
+ WRITE_ONCE(src->flags, flags);
pvclock_touch_watchdogs();
}

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 1ba3b7d3cae9..54458acce4f5 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1832,7 +1832,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
if (use_master_clock)
pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;

- vcpu->hv_clock.flags = pvclock_flags;
+ WRITE_ONCE(vcpu->hv_clock.flags, pvclock_flags);

trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);