All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] time: Add locking to xtime access in get_seconds()
@ 2011-05-04  3:11 John Stultz
  2011-05-04  3:52 ` Andi Kleen
  2011-05-04 16:51 ` Max Asbock
  0 siblings, 2 replies; 50+ messages in thread
From: John Stultz @ 2011-05-04  3:11 UTC (permalink / raw)
  To: lkml
  Cc: John Stultz, Paul Mackerras, Paul E. McKenney, Anton Blanchard,
	Thomas Gleixner

From: John Stultz <johnstul@us.ibm.com>

So get_seconds() has always been lock free, with the assumption
that accessing a long will be atomic.

However, recently I came across an odd bug where time() access could
occasionally be inconsistent, but only on power7 hardware. The
same code paths on power6 or x86 could not reproduce the issue.

After adding careful debugging checks to any xtime manipulation, and
not seeing any inconsistencies on the kernel side, I realized that
with no locking in the get_seconds path, its could be that two
sequential calls to time() could be executed out of order on newer
hardware, causing the inconsistency to appear in userland.

After adding the following locking, the issue cannot be reproduced.

Wanted to run this by the power guys to make sure the theory above
sounds sane.

CC: Paul Mackerras <paulus@samba.org>
CC: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
CC: Anton Blanchard <anton@samba.org>
CC: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Stultz <johnstul@us.ibm.com>
---
 kernel/time/timekeeping.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 8ad5d57..89c7582 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -975,7 +975,15 @@ EXPORT_SYMBOL_GPL(monotonic_to_bootbased);
 
 unsigned long get_seconds(void)
 {
-	return xtime.tv_sec;
+	unsigned long seq, now;
+
+	do {
+		seq = read_seqbegin(&xtime_lock);
+
+		now = xtime.tv_sec;
+	} while (read_seqretry(&xtime_lock, seq));
+
+	return now;
 }
 EXPORT_SYMBOL(get_seconds);
 
-- 
1.7.3.2.146.gca209


^ permalink raw reply related	[flat|nested] 50+ messages in thread

end of thread, other threads:[~2011-05-12 14:34 UTC | newest]

Thread overview: 50+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-04  3:11 [PATCH] time: Add locking to xtime access in get_seconds() John Stultz
2011-05-04  3:52 ` Andi Kleen
2011-05-05  2:54   ` john stultz
2011-05-05  5:44     ` Eric Dumazet
2011-05-05  6:21       ` john stultz
2011-05-05  6:50         ` Eric Dumazet
2011-05-05  8:14         ` Paul E. McKenney
2011-05-05 18:51           ` john stultz
2011-05-05 14:04         ` [RFC] time: xtime_lock is held too long Eric Dumazet
2011-05-05 14:39           ` Thomas Gleixner
2011-05-05 15:08             ` Eric Dumazet
2011-05-05 15:59               ` Thomas Gleixner
2011-05-05 21:01                 ` Andi Kleen
2011-05-06  1:41                   ` Eric Dumazet
2011-05-06  6:55                     ` Andi Kleen
2011-05-06 10:18                   ` Thomas Gleixner
2011-05-06 10:22                     ` Ingo Molnar
2011-05-06 16:53                       ` Andi Kleen
2011-05-07  8:20                         ` Ingo Molnar
2011-05-06 16:59                     ` Andi Kleen
2011-05-06 17:09                       ` Eric Dumazet
2011-05-06 17:17                         ` Andi Kleen
2011-05-06 17:42                       ` Eric Dumazet
2011-05-06 17:50                         ` Andi Kleen
2011-05-06 19:26                           ` Eric Dumazet
2011-05-06 20:04                             ` Eric Dumazet
2011-05-06 20:24                               ` john stultz
2011-05-06 22:30                                 ` Eric Dumazet
2011-05-06 22:46                                   ` john stultz
2011-05-06 23:00                                     ` Eric Dumazet
2011-05-06 23:28                                       ` john stultz
2011-05-07  5:02                                         ` Eric Dumazet
2011-05-07  7:11                                           ` Henrik Rydberg
2011-05-09  8:40                                         ` Thomas Gleixner
2011-05-12  9:13                                         ` [PATCH] seqlock: don't smp_rmb in seqlock reader spin loop, [PATCH] seqlock: don't smp_rmb in seqlock reader spin loop Milton Miller
2011-05-12  9:13                                           ` Milton Miller
2011-05-12  9:35                                           ` Eric Dumazet
2011-05-12  9:35                                             ` Eric Dumazet
2011-05-12 14:08                                           ` Andi Kleen
2011-05-12 14:08                                             ` Andi Kleen
2011-05-06 20:18                         ` [RFC] time: xtime_lock is held too long john stultz
2011-05-05 17:57     ` [PATCH] time: Add locking to xtime access in get_seconds() Andi Kleen
2011-05-05 20:17       ` john stultz
2011-05-05 20:24         ` Eric Dumazet
2011-05-05 20:40           ` john stultz
2011-05-05 20:43             ` Eric Dumazet
2011-05-05 20:56         ` Andi Kleen
2011-05-04 16:51 ` Max Asbock
2011-05-04 21:05   ` Andi Kleen
2011-05-04 23:05   ` john stultz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.