Re: [Stable-review] [05/21] Fix time() inconsistencies caused byintermediate xtime_cache values being read

From: Ben Hutchings
Date: Thu May 19 2011 - 16:33:37 EST


I couldn't see who the author of this was, but assuming John Stultz.

On Thu, May 19, 2011 at 11:23:35AM -0700, Greg KH wrote:
[...]
> In order to resolve this, we could add locking to get_seconds(), but it
> needs to be lock free, as it is called from the machine check handler,
> opening a possible deadlock.
>
> So instead, this patch introduces an intermediate value for the
> calculations, so that we only assign xtime_cache once with the correct
> time, using ACCESS_ONCE to make sure the compiler doesn't optimize out
> any intermediate values.
[...]
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -168,8 +168,15 @@ int __read_mostly timekeeping_suspended;
> static struct timespec xtime_cache __attribute__ ((aligned (16)));
> void update_xtime_cache(u64 nsec)
> {
> - xtime_cache = xtime;
> - timespec_add_ns(&xtime_cache, nsec);
> + /*
> + * Use temporary variable so get_seconds() cannot catch
> + * an intermediate xtime_cache.tv_sec value.
> + * The ACCESS_ONCE() keeps the compiler from optimizing
> + * out the intermediate value.
> + */
> + struct timespec ts = xtime;
> + timespec_add_ns(&ts, nsec);
> + ACCESS_ONCE(xtime_cache) = ts;
[...]

I think this use of ACCESS_ONCE() is bogus. What it does is to add
volatile-qualification to the write, and while we believe that has
a well-defined effect for int and long I don't think we can assume
that for structure assignment.

It probably works in practice, and I have no objection to this in
2.6.32.y, but I think it would be safer to assign each of the
structure fields separately with ACCESS_ONCE().

Ben.

--
Ben Hutchings
We get into the habit of living before acquiring the habit of thinking.
- Albert Camus
--
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/