Skip to contentSkip to conversation

timer: Fix possible underflow in udelay

PATCH New Andrew F. Davis 2 participants 3 messages Aug 25, 2016 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 timer: Fix possible underflow in udelay

When the inputed usec is too large we process it in chunks of CONFIG_WD_PERIOD size. Subtracting this from usec until usec is zero. If usec is not an integer multiple of CONFIG_WD_PERIOD it will underflow and the condition will not become false when it should. Fix this logic.

---
 lib/time.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/time.c +5 -5
index f37150f..4ec3cb9 100644
@@ -145,14 +145,14 @@ void __weak __udelay(unsigned long usec)
 
 void udelay(unsigned long usec)
 {
-	ulong kv;
+	ulong kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
+	ulong elapsed = 0;
 
 	do {
 		WATCHDOG_RESET();
-		kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
-		__udelay (kv);
-		usec -= kv;
-	} while(usec);
+		__udelay(kv);
+		elapsed += kv;
+	} while (elapsed < usec);
 }
 
 void mdelay(unsigned long msec)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/1 timer: Fix possible underflow in udelay

Quoted 26 lines · Andrew F. Davis

When the inputed usec is too large we process it in chunks of CONFIG_WD_PERIOD size. Subtracting this from usec until usec is zero. If usec is not an integer multiple of CONFIG_WD_PERIOD it will underflow and the condition will not become false when it should. Fix this logic.

---
 lib/time.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/time.c +2 -1
index f37150f..4ec3cb9 100644
@@ -145,14 +145,14 @@ void __weak __udelay(unsigned long usec)
 
 void udelay(unsigned long usec)
 {
-	ulong kv;
+	ulong kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
+	ulong elapsed = 0;
 
 	do {
 		WATCHDOG_RESET();

lib/time.c void __weak __udelay(unsigned long usec) · line 158

Quoted 10 lines
lib/time.c +3 -4
-		kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
-		__udelay (kv);
-		usec -= kv;
-	} while(usec);
+		__udelay(kv);
+		elapsed += kv;
+	} while (elapsed < usec);
 }
 
 void mdelay(unsigned long msec)

The original code looks fine to me. Can you give an example of failure ? ie.
If udelay is passed value of CONFIG_WD_PERIOD+1, the udelay sequence will be

udelay(CONFIG_WD_PERIOD)
udelay(1)

whereas the need code does
udelay(CONFIG_WD_PERIOD)
udelay(CONFIG_WD_PERIOD)

#2 lore raw

PATCH 1/1 timer: Fix possible underflow in udelay

Quoted 42 lines · Troy Kisky
Quoted 26 lines · Andrew F. Davis

When the inputed usec is too large we process it in chunks of CONFIG_WD_PERIOD size. Subtracting this from usec until usec is zero. If usec is not an integer multiple of CONFIG_WD_PERIOD it will underflow and the condition will not become false when it should. Fix this logic.

---
 lib/time.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
lib/time.c +2 -1
index f37150f..4ec3cb9 100644
@@ -145,14 +145,14 @@ void __weak __udelay(unsigned long usec)
 
 void udelay(unsigned long usec)
 {
-	ulong kv;
+	ulong kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
+	ulong elapsed = 0;
 
 	do {
 		WATCHDOG_RESET();

lib/time.c void __weak __udelay(unsigned long usec) · line 158

Quoted 10 lines
lib/time.c +3 -4
-		kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
-		__udelay (kv);
-		usec -= kv;
-	} while(usec);
+		__udelay(kv);
+		elapsed += kv;
+	} while (elapsed < usec);
 }
 
 void mdelay(unsigned long msec)

The original code looks fine to me. Can you give an example of failure ? ie.
If udelay is passed value of CONFIG_WD_PERIOD+1, the udelay sequence will be

Quoted 7 lines

udelay(CONFIG_WD_PERIOD)
udelay(1)

whereas the need code does
udelay(CONFIG_WD_PERIOD)
udelay(CONFIG_WD_PERIOD)

Hmm, I'm not sure where I saw the problem before, I think I may have tried to optimize it and broke it myself, oh well, this patch can safely be ignored.

Thanks,
Andrew