Skip to contentSkip to conversation

sys_time-speedup-small-cleanup

PATCH In review Oleg Nesterov 2 participants 5 messages Jun 26, 2007 – Jun 27, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sys_time-speedup-small-cleanup

on top of sys_time-speedup.patch

Quoted 5 lines · Ingo Molnar

asmlinkage long sys_time(time_t __user * tloc)
{

(quoted diff) +0 -2
-	time_t i;
-	struct timeval tv;

Review

Quoted 10 lines
(quoted diff) +7 -2
+	/*
+	 * We read xtime.tv_sec atomically - it's updated
+	 * atomically by update_wall_time(), so no need to
+	 * even read-lock the xtime seqlock:
+	 */
+	time_t i = xtime.tv_sec;
 
-	do_gettimeofday(&tv);
-	i = tv.tv_sec;
+	smp_rmb(); /* sys_time() results are coherent */

Why do we need this barrier? My guess it is needed to prevent the reading of xtime.tv_sec twice, yes? In that case a simple barrier() should be enough.

kernel/time.c renamed from kernel/time.c~ +1 -1
@@ -64,7 +64,7 @@ asmlinkage long sys_time(time_t __user *
 	 */
 	time_t i = xtime.tv_sec;
 
-	smp_rmb(); /* sys_time() results are coherent */
+	barrier(); /* sys_time() results are coherent */
 
 	if (tloc) {
 		if (put_user(i, tloc))
#1 lore raw
Quoted 14 lines · Oleg Nesterov

on top of sys_time-speedup.patch

Quoted 11 lines · Ingo Molnar

asmlinkage long sys_time(time_t __user * tloc)
{

(quoted diff) +6 -2
-	time_t i;
-	struct timeval tv;
+	/*
+	 * We read xtime.tv_sec atomically - it's updated
+	 * atomically by update_wall_time(), so no need to
+	 * even read-lock the xtime seqlock:
+	 */
+	time_t i = xtime.tv_sec;
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +1 -2
-	do_gettimeofday(&tv);
-	i = tv.tv_sec;
+	smp_rmb(); /* sys_time() results are coherent */

Why do we need this barrier? My guess it is needed to prevent the reading of xtime.tv_sec twice, yes? In that case a simple barrier() should be enough.

Without the smp_rmb, you can potentially have a situation where one CPU is still reading an old value from cache while another has the new value. It's generally a rather small race window on most architectures, but very bad things can happen if time ever goes backwards, so it's worth the overhead of maintaining coherence on smp.

-- Chris

Quoted 19 lines
kernel/time.c renamed from kernel/time.c~ +1 -2
@@ -64,7 +64,7 @@ asmlinkage long sys_time(time_t __user *
 	 */
 	time_t i = xtime.tv_sec;
 
-	smp_rmb(); /* sys_time() results are coherent */
+	barrier(); /* sys_time() results are coherent */
 
 	if (tloc) {
 		if (put_user(i, tloc))
 
-

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/

#2 lore raw
Quoted 24 lines · Chris Snook
Quoted 14 lines · Oleg Nesterov

on top of sys_time-speedup.patch

Quoted 11 lines · Ingo Molnar

asmlinkage long sys_time(time_t __user * tloc)
{

(quoted diff) +6 -2
-	time_t i;
-	struct timeval tv;
+	/*
+	 * We read xtime.tv_sec atomically - it's updated
+	 * atomically by update_wall_time(), so no need to
+	 * even read-lock the xtime seqlock:
+	 */
+	time_t i = xtime.tv_sec;
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +1 -2
-	do_gettimeofday(&tv);
-	i = tv.tv_sec;
+	smp_rmb(); /* sys_time() results are coherent */

Why do we need this barrier? My guess it is needed to prevent the reading of xtime.tv_sec twice, yes? In that case a simple barrier() should be enough.

Quoted 2 lines

Without the smp_rmb, you can potentially have a situation where one CPU is still reading an old value from cache while another has the new value.

I can't understand this.

Fisrt, smp_rmb() can't help in this case. It can't influence the preceeding LOAD if it was from cache.

Even if it could, another CPU can alter the value just after the reading completes, and we have the same situation.

Could you please clarify if I am wrong?

Oleg.

#3 lore raw
Quoted 29 lines · Oleg Nesterov
Quoted 15 lines · Chris Snook
Quoted 14 lines · Oleg Nesterov

on top of sys_time-speedup.patch

Quoted 11 lines · Ingo Molnar

asmlinkage long sys_time(time_t __user * tloc)
{

(quoted diff) +6 -2
-	time_t i;
-	struct timeval tv;
+	/*
+	 * We read xtime.tv_sec atomically - it's updated
+	 * atomically by update_wall_time(), so no need to
+	 * even read-lock the xtime seqlock:
+	 */
+	time_t i = xtime.tv_sec;
Quoted 7 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +1 -2
-	do_gettimeofday(&tv);
-	i = tv.tv_sec;
+	smp_rmb(); /* sys_time() results are coherent */

Why do we need this barrier? My guess it is needed to prevent the reading of xtime.tv_sec twice, yes? In that case a simple barrier() should be enough.

Without the smp_rmb, you can potentially have a situation where one CPU is still reading an old value from cache while another has the new value.

I can't understand this.

Fisrt, smp_rmb() can't help in this case. It can't influence the preceeding LOAD if it was from cache.

Quoted 7 lines

Even if it could, another CPU can alter the value just after the reading completes, and we have the same situation.

Could you please clarify if I am wrong?

Oleg.

You're right, but so is Ingo's patch. We're not trying to enforce some notion of absolute time, just make it possible for userspace to guarantee that time cannot be *observed* to travel backwards. It's still the responsibility of the user to use proper synchronization in multithreaded apps. Without the smp_rmb() it would be possible on some architectures for the results of the race you describe to leak across other lock-prefixed instructions used to ensure monotonicity in userspace. Relativity applies to SMP timekeeping, not just space travelers, so if there's no way to prove a race occurred, it doesn't matter whether or not it occurred in some frame of reference.

-- Chris

#4 lore raw
Quoted 40 lines · Chris Snook
Quoted 30 lines · Oleg Nesterov
Quoted 15 lines · Chris Snook
Quoted 14 lines · Oleg Nesterov

on top of sys_time-speedup.patch

Quoted 11 lines · Ingo Molnar

asmlinkage long sys_time(time_t __user * tloc)
{

(quoted diff) +6 -2
-	time_t i;
-	struct timeval tv;
+	/*
+	 * We read xtime.tv_sec atomically - it's updated
+	 * atomically by update_wall_time(), so no need to
+	 * even read-lock the xtime seqlock:
+	 */
+	time_t i = xtime.tv_sec;
Quoted 8 lines
Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +1 -2
-	do_gettimeofday(&tv);
-	i = tv.tv_sec;
+	smp_rmb(); /* sys_time() results are coherent */

Why do we need this barrier? My guess it is needed to prevent the reading of xtime.tv_sec twice, yes? In that case a simple barrier() should be enough.

Without the smp_rmb, you can potentially have a situation where one CPU is still reading an old value from cache while another has the new value.

I can't understand this.

Fisrt, smp_rmb() can't help in this case. It can't influence the preceeding LOAD if it was from cache.

Quoted 7 lines

Even if it could, another CPU can alter the value just after the reading completes, and we have the same situation.

Could you please clarify if I am wrong?

Oleg.

Quoted 10 lines

You're right, but so is Ingo's patch. We're not trying to enforce some notion of absolute time, just make it possible for userspace to guarantee that time cannot be *observed* to travel backwards. It's still the responsibility of the user to use proper synchronization in multithreaded apps. Without the smp_rmb() it would be possible on some architectures for the results of the race you describe to leak across other lock-prefixed instructions used to ensure monotonicity in userspace. Relativity applies to SMP timekeeping, not just space travelers, so if there's no way to prove a race occurred, it doesn't matter whether or not it occurred in some frame of reference.

This doesn't make sense to me, sorry. Could provide more details to explain the race? Some ascii diagram?

I believe smp_rmb() can't make _any_ difference in this case.

Oleg.