Skip to contentSkip to conversation

sched: Fix 32bit race in sched_clock_remote()

PATCH In review Peter Zijlstra 3 participants 4 messages Apr 5, 2013 – Apr 10, 2013 Scheduler

lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

PATCH 1/1 sched: Fix 32bit race in sched_clock_remote()

Thomas spotted a nasty 32bit race in sched_clock_remote() after way too many hours of debugging weirdness.

What happens is that sched_clock_remote() does regular machine word reads of sched_clock_data::clock; this appears safe since we use cmpxchg64() to update the variable and any half-read value would trigger a retry.

Except we don't validate the new value 'val' in the same way! Thus we can propagate non-atomic read errors into the clock value.

---
 kernel/sched/clock.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
kernel/sched/clock.c +17 -2
index c685e31..7042ef7 100644
@@ -170,6 +170,21 @@ static u64 sched_clock_local(struct sched_clock_data *scd)
 	return clock;
 }
 
+#ifndef CONFIG_64BIT
+/*
+ * 32bit machines can't atomically read a u64 except using cmpxchg64()
+ */
+static inline u64 scd_read_clock(struct sched_clock_data *scd)
+{
+	return cmpxchg64(&scd->clock, 0, 0);
+}
+#else
+static inline u64 scd_read_clock(struct sched_clock_data *scd)
+{
+	return scd->clock;
+}
+#endif
+
 static u64 sched_clock_remote(struct sched_clock_data *scd)
 {
 	struct sched_clock_data *my_scd = this_scd();
@@ -178,8 +193,8 @@ static u64 sched_clock_remote(struct sched_clock_data *scd)
 
 	sched_clock_local(my_scd);
 again:
-	this_clock = my_scd->clock;
-	remote_clock = scd->clock;
+	this_clock = scd_clock_read(my_scd);
+	remote_clock = scd_clock_read(scd);
 
 	/*
 	 * Use the opportunity that we have both locks
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 24 lines · Peter Zijlstra

Thomas spotted a nasty 32bit race in sched_clock_remote() after way too many hours of debugging weirdness.

What happens is that sched_clock_remote() does regular machine word reads of sched_clock_data::clock; this appears safe since we use cmpxchg64() to update the variable and any half-read value would trigger a retry.

Except we don't validate the new value 'val' in the same way! Thus we can propagate non-atomic read errors into the clock value.

---
 kernel/sched/clock.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
kernel/sched/clock.c +0 -0
index c685e31..7042ef7 100644

kernel/sched/clock.c static u64 sched_clock_local(struct sched_clock_data *scd) · line 173

Quoted 4 lines
kernel/sched/clock.c +1 -0
@@ -170,6 +170,21 @@ static u64 sched_clock_local(struct sched_clock_data *scd)
 	return clock;
 }
 
+#ifndef CONFIG_64BIT

We need to add a Kconfig:

config 32BIT
depends on BROKEN

-- Steve

Quoted 29 lines
kernel/sched/clock.c +16 -2
+/*
+ * 32bit machines can't atomically read a u64 except using cmpxchg64()
+ */
+static inline u64 scd_read_clock(struct sched_clock_data *scd)
+{
+	return cmpxchg64(&scd->clock, 0, 0);
+}
+#else
+static inline u64 scd_read_clock(struct sched_clock_data *scd)
+{
+	return scd->clock;
+}
+#endif
+
 static u64 sched_clock_remote(struct sched_clock_data *scd)
 {
 	struct sched_clock_data *my_scd = this_scd();
@@ -178,8 +193,8 @@ static u64 sched_clock_remote(struct sched_clock_data *scd)
 
 	sched_clock_local(my_scd);
 again:
-	this_clock = my_scd->clock;
-	remote_clock = scd->clock;
+	this_clock = scd_clock_read(my_scd);
+	remote_clock = scd_clock_read(scd);
 
 	/*
 	 * Use the opportunity that we have both locks
#2 lore raw
In reply to Peter Zijlstra
Quoted 46 lines · Peter Zijlstra

Thomas spotted a nasty 32bit race in sched_clock_remote() after way too many hours of debugging weirdness.

What happens is that sched_clock_remote() does regular machine word reads of sched_clock_data::clock; this appears safe since we use cmpxchg64() to update the variable and any half-read value would trigger a retry.

Except we don't validate the new value 'val' in the same way! Thus we can propagate non-atomic read errors into the clock value.

---
 kernel/sched/clock.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
kernel/sched/clock.c +15 -0
index c685e31..7042ef7 100644
@@ -170,6 +170,21 @@ static u64 sched_clock_local(struct sched_clock_data *scd)
 	return clock;
 }
 
+#ifndef CONFIG_64BIT
+/*
+ * 32bit machines can't atomically read a u64 except using cmpxchg64()
+ */
+static inline u64 scd_read_clock(struct sched_clock_data *scd)
+{
+	return cmpxchg64(&scd->clock, 0, 0);
+}
+#else
+static inline u64 scd_read_clock(struct sched_clock_data *scd)
+{
+	return scd->clock;
+}
+#endif
+
 static u64 sched_clock_remote(struct sched_clock_data *scd)
 {
 	struct sched_clock_data *my_scd = this_scd();

kernel/sched/clock.c static u64 sched_clock_remote(struct sched_clock_data *scd) · line 197

Quoted 7 lines
kernel/sched/clock.c +2 -2
@@ -178,8 +193,8 @@ static u64 sched_clock_remote(struct sched_clock_data *scd)
 
 	sched_clock_local(my_scd);
 again:
-	this_clock = my_scd->clock;
-	remote_clock = scd->clock;
+	this_clock = scd_clock_read(my_scd);
+	remote_clock = scd_clock_read(scd);
		       ^^^^^^^^^^^^^^
		       it doesn't match the declaration: scd_read_clock().

Thanks,
Yong

Quoted 10 lines
kernel/sched/clock.c +0 -1
 	/*
 	 * Use the opportunity that we have both locks
 
 
--

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

author maintainer
#3 lore raw
Quoted 5 lines · Yong Zhang

kernel/sched/clock.c static u64 sched_clock_remote(struct sched_clock_data *scd) · line 197

Quoted 2 lines
kernel/sched/clock.c +2 -0
+     this_clock = scd_clock_read(my_scd);
+     remote_clock = scd_clock_read(scd);

^^^^^^^^^^^^^^
it doesn't match the declaration:
scd_read_clock().

Yeah, I'm a moron and forgot to compile test or somesuch :-)

Anyway, Thomas wrote a much better patch which made it in; see a1cbcaa9ea87b87a96b9fc465951dcf36e459ca2.