shithub: riscv

Download patch

ref: 773b57b676526975059e5ba3858cfde6d8b50ceb
parent: ce0b77e2b9bd4669844d0356f8e49d2f4878bac8
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Aug 16 17:04:41 EDT 2014

kernel: fix todfix() race

we have to recheck the condition under tod lock, otherwise
another process can come in and updated tod.last and
tod.off and once we have the lock, we would make time
jump backwards.

--- a/sys/src/9/port/tod.c
+++ b/sys/src/9/port/tod.c
@@ -198,11 +198,13 @@
 	uvlong x;
 
 	ticks = fastticks(nil);
+	diff = ticks - tod.last;
+	if(diff <= tod.hz)
+		return;
 
+	ilock(&tod);
 	diff = ticks - tod.last;
 	if(diff > tod.hz){
-		ilock(&tod);
-
 		/* convert to epoch */
 		mul64fract(&x, diff, tod.multiplier);
 if(x > 30000000000ULL) iprint("todfix %llud\n", x);
@@ -211,9 +213,8 @@
 		/* protect against overflows */
 		tod.last = ticks;
 		tod.off = x;
-
-		iunlock(&tod);
 	}
+	iunlock(&tod);
 }
 
 long