linux-stable/kernel/time
john stultz af79350f15 Fix time() inconsistencies caused by intermediate xtime_cache values being read
Currently with 2.6.32-longterm, its possible for time() to occasionally
return values one second earlier then the previous time() call.

This happens because update_xtime_cache() does:
	xtime_cache = xtime;
	timespec_add_ns(&xtime_cache, nsec);

Its possible that xtime is 1sec,999msecs, and nsecs is 1ms, resulting in
a xtime_cache that is 2sec,0ms.

get_seconds() (which is used by sys_time()) does not take the
xtime_lock, which is ok as the xtime.tv_sec value is a long and can be
atomically read safely.

The problem occurs the next call to update_xtime_cache() if xtime has
not increased:
	/* This sets xtime_cache back to 1sec, 999msec */
	xtime_cache = xtime;
	/* get_seconds, calls here, and sees a 1second inconsistency */
	timespec_add_ns(&xtime_cache, nsec);

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.

The xtime_cache manipulations were removed with 2.6.35, so that kernel
and later do not need this change.

In 2.6.33 and 2.6.34 the logarithmic accumulation should make it so
xtime is updated each tick, so it is unlikely that two updates to
xtime_cache could occur while the difference between xtime and
xtime_cache crosses the second boundary. However, the paranoid might
want to pull this into 2.6.33/34-longterm just to be sure.

Thanks to Stephen for helping finally narrow down the root cause and
many hours of help with testing and validation. Also thanks to Max,
Andi, Eric and Paul for review of earlier attempts and helping clarify
what is possible with regard to out of order execution.

Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: John Stultz <johnstul@us.ibm.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Willy Tarreau <w@1wt.eu>
2012-02-11 15:40:48 +01:00
..
clockevents.c clockevent: Prevent dead lock on clockevents_lock 2010-05-26 14:27:07 -07:00
clocksource.c Merge branch 'cpus4096-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip 2008-07-23 18:37:44 -07:00
jiffies.c [PATCH] fix jiffies clocksource inittime 2007-04-04 21:12:47 -07:00
Kconfig clockevents: Allow build w/o run-tine usage for migration purposes 2007-10-12 23:04:05 +02:00
Makefile clockevents: Allow build w/o run-tine usage for migration purposes 2007-10-12 23:04:05 +02:00
ntp.c ntp: fix calculation of the next jiffie to trigger RTC sync 2008-09-06 15:31:48 +02:00
tick-broadcast.c clockevent: Prevent dead lock on clockevents_lock 2010-05-26 14:27:07 -07:00
tick-common.c clockevents: prevent endless loop in tick_handle_periodic() 2009-05-08 14:54:36 -07:00
tick-internal.h timers: fix build error in !oneshot case 2008-09-23 12:57:00 +02:00
tick-oneshot.c clockevents: remove WARN_ON which was used to gather information 2008-09-09 22:20:01 +02:00
tick-sched.c printk: robustify printk 2009-11-09 16:52:43 -08:00
timekeeping.c Fix time() inconsistencies caused by intermediate xtime_cache values being read 2012-02-11 15:40:48 +01:00
timer_list.c kernel: use non-racy method for proc entries creation 2008-04-29 08:06:22 -07:00
timer_stats.c kernel: use non-racy method for proc entries creation 2008-04-29 08:06:22 -07:00