Re: [PATCH] sched_clock: Prevent 64bit inatomicity on 32bit systems

From: Peter Zijlstra
Date: Sat Apr 06 2013 - 12:30:09 EST


On Sat, 2013-04-06 at 10:10 +0200, Thomas Gleixner wrote:
> Index: linux-stable/kernel/sched/clock.c
> ===================================================================
> --- linux.orig/kernel/sched/clock.c
> +++ linux/kernel/sched/clock.c
> @@ -176,10 +176,36 @@ static u64 sched_clock_remote(struct sch
> u64 this_clock, remote_clock;
> u64 *ptr, old_val, val;
>
> +#if BITS_PER_LONG != 64
> +again:
> + /*
> + * Careful here: The local and the remote clock values need to
> + * be read out atomic as we need to compare the values and
> + * then update either the local or the remote side. So the
> + * cmpxchg64 below only protects one readout.
> + *
> + * We must reread via sched_clock_local() in the retry case on
> + * 32bit as an NMI could use sched_clock_local() via the
> + * tracer and hit between the readout of
> + * the low32bit and the high 32bit portion.
> + */
> + this_clock = sched_clock_local(my_scd);
> + /*
> + * We must enforce atomic readout on 32bit, otherwise the
> + * update on the remote cpu can hit inbetween the readout of
> + * the low32bit and the high 32bit portion.
> + */
> + remote_clock = cmpxchg64(&scd->clock, 0, 0);
> +#else
> + /*
> + * On 64bit the read of [my]scd->clock is atomic versus the
> + * update, so we can avoid the above 32bit dance.
> + */
> sched_clock_local(my_scd);
> again:
> this_clock = my_scd->clock;
> remote_clock = scd->clock;
> +#endif

Yeah, I like your version better.. just before reading your email I
realized I could use the return value of sched_clock_local() to avoid a
cmpxchg64() :-)

That said, with this there's a subtle behavioural change between 32 and
64 bits with your patch; see how 32bit has sched_clock_local() inside
the retry loop whereas 64 bit does not.

Not sure we should worry about that though.

Thanks!

Acked-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/