Skip to contentSkip to conversation

powerpc: time: Prevent overflow in usec2ticks()

PATCH NAK Bradley Morgan 1 participant 2 messages Nov 7 – Nov 8 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: time: Prevent overflow in usec2ticks()

In usec2ticks(), the author of this file indicated that this was only intended for "short delays" for example, it would have overflowed at 10 seconds at 400 MHz, which is quite a hack.

To fix this, use lldiv() and 64 bit math in order to compute the tick count without the overflow, then clamp the output to ULONG_MAX so it does not break 32 bit PowerPC boards

---
 arch/powerpc/lib/time.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
arch/powerpc/lib/time.c +5 -11
index 0a0e75e726b..120f1fcd0d7 100644
@@ -6,27 +6,21 @@
 
 #include <init.h>
 #include <time.h>
+#include <div64.h>
+#include <linux/kernel.h>
 #include <asm/io.h>
 #include <linux/delay.h>
 
 /* ------------------------------------------------------------------------- */
 
 /*
- * This function is intended for SHORT delays only.
- * It will overflow at around 10 seconds @ 400MHz,
- * or 20 seconds @ 200MHz.
+ * Convert microseconds to timebase ticks without overflow.
  */
 unsigned long usec2ticks(unsigned long usec)
 {
-	ulong ticks;
+	u64 ticks = lldiv((u64)usec * get_tbclk() + 500000, 1000000);
 
-	if (usec < 1000) {
-		ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
-	} else {
-		ticks = ((usec / 10) * (get_tbclk() / 100000));
-	}
-
-	return (ticks);
+	return ticks > ULONG_MAX ? ULONG_MAX : ticks;
 }
 
 /* ------------------------------------------------------------------------- */
Signature · 1 line
2.53.0
author NAK
#1 lore raw
Quoted 41 lines · Bradley Morgan

In usec2ticks(), the author of this file indicated that this was only intended for "short delays" for example, it would have overflowed at 10 seconds at 400 MHz, which is quite a hack.

To fix this, use lldiv() and 64 bit math in order to compute the tick count without the overflow, then clamp the output to ULONG_MAX so it does not break 32 bit PowerPC boards

---
arch/powerpc/lib/time.c | 16 +++++-----------

1 file changed, 5 insertions(+), 11 deletions(-)
arch/powerpc/lib/time.c +3 -5
index 0a0e75e726b..120f1fcd0d7 100644
@@ -6,27 +6,21 @@
 
 #include <init.h>
 #include <time.h>
+#include <div64.h>
+#include <linux/kernel.h>
 #include <asm/io.h>
 #include <linux/delay.h>
 
 /*
-------------------------------------------------------------------------
 */
 
 /*
- * This function is intended for SHORT delays only.
- * It will overflow at around 10 seconds @ 400MHz,
- * or 20 seconds @ 200MHz.
+ * Convert microseconds to timebase ticks without overflow.
 */
 unsigned long usec2ticks(unsigned long usec)
 {
-	ulong ticks;

arch/powerpc/lib/time.c line 24

Quoted 10 lines
arch/powerpc/lib/time.c +2 -7
+	u64 ticks = lldiv((u64)usec * get_tbclk() + 500000, 1000000);
 
-	if (usec < 1000) {
-		ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
-	} else {
-		ticks = ((usec / 10) * (get_tbclk() / 100000));
-	}
-
-	return (ticks);
+	return ticks > ULONG_MAX ? ULONG_MAX : ticks;

NAK, marex suggested that this would cause additional overhead, so id reckon this wouldn't need to be fixed, but I'll keep a eye on this and see if anything blows up

Quoted 6 lines
arch/powerpc/lib/time.c +0 -0
 }

/*
-------------------------------------------------------------------------
*/

--- Thanks!