Skip to contentSkip to conversation

lib/time - fix "usec_to_tick" calculation for hi freq system timers

PATCH New Alexey Brodkin 2 participants 5 messages Dec 9, 2013 – Dec 13, 2013 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/time - fix "usec_to_tick" calculation for hi freq system timers

Current implementation works fine if "usec * get_tbclk()" fits in 32 bits. Otherwise result will be cut down to 32-bit.

Fix is obvious - first extend data type of either operand.

Cc Rob Herring <[email protected]>
Cc Mischa Jonker <[email protected]>
---
 lib/time.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/time.c +1 -1
index 09bb05a..80003c3 100644
@@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
 }
 static unsigned long long usec_to_tick(unsigned long usec)
 {
-	uint64_t tick = usec * get_tbclk();
+	uint64_t tick = (uint64_t)usec * get_tbclk();
 	usec *= get_tbclk();
 	do_div(tick, 1000000);
 	return tick;
Signature · 1 line
1.8.4.2
#1 lore raw

PATCH 1/1 lib/time - fix "usec_to_tick" calculation for hi freq system timers

Hi Tom,

Quoted 18 lines · Alexey Brodkin

Current implementation works fine if "usec * get_tbclk()" fits in 32 bits. Otherwise result will be cut down to 32-bit.

Fix is obvious - first extend data type of either operand.

Cc Rob Herring <[email protected]>
Cc Mischa Jonker <[email protected]>
---
 lib/time.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/time.c +0 -0
index 09bb05a..80003c3 100644

lib/time.c unsigned long __weak notrace timer_get_us(void) · line 77

Quoted 8 lines
lib/time.c +1 -1
@@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
 }
 static unsigned long long usec_to_tick(unsigned long usec)
 {
-	uint64_t tick = usec * get_tbclk();
+	uint64_t tick = (uint64_t)usec * get_tbclk();
 	usec *= get_tbclk();
 	do_div(tick, 1000000);
 	return tick;

Any chance to get it applied?

Regards,
Alexey

#2 lore raw

PATCH 1/1 lib/time - fix "usec_to_tick" calculation for hi freq system timers

Quoted 16 lines · Alexey Brodkin

Hi Tom,

Quoted 13 lines · Alexey Brodkin

Current implementation works fine if "usec * get_tbclk()" fits in 32 bits. Otherwise result will be cut down to 32-bit.

Fix is obvious - first extend data type of either operand.

Cc Rob Herring <[email protected]>
Cc Mischa Jonker <[email protected]>
---
 lib/time.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

lib/time.c unsigned long __weak notrace timer_get_us(void) · line 77

Quoted 13 lines
lib/time.c +1 -1
index 09bb05a..80003c3 100644
@@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
 }
 static unsigned long long usec_to_tick(unsigned long usec)
 {
-	uint64_t tick = usec * get_tbclk();
+	uint64_t tick = (uint64_t)usec * get_tbclk();
 	usec *= get_tbclk();
 	do_div(tick, 1000000);
 	return tick;

Any chance to get it applied?

Yes, it's on my TODO list shortly, thanks.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131213/7b1a871b/attachment.pgp>
#3 lore raw

PATCH 1/1 lib/time - fix "usec_to_tick" calculation for hi freq system timers

Quoted 34 lines · Tom Rini
Quoted 16 lines · Alexey Brodkin

Hi Tom,

Quoted 13 lines · Alexey Brodkin

Current implementation works fine if "usec * get_tbclk()" fits in 32 bits. Otherwise result will be cut down to 32-bit.

Fix is obvious - first extend data type of either operand.

Cc Rob Herring <[email protected]>
Cc Mischa Jonker <[email protected]>
---
 lib/time.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

lib/time.c unsigned long __weak notrace timer_get_us(void) · line 77

Quoted 13 lines
lib/time.c +1 -1
index 09bb05a..80003c3 100644
@@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
 }
 static unsigned long long usec_to_tick(unsigned long usec)
 {
-	uint64_t tick = usec * get_tbclk();
+	uint64_t tick = (uint64_t)usec * get_tbclk();
 	usec *= get_tbclk();
 	do_div(tick, 1000000);
 	return tick;

Any chance to get it applied?

Quoted 1 line

Yes, it's on my TODO list shortly, thanks.

Please note that I'm taking http://patchwork.ozlabs.org/patch/297361/ as the solution for this problem, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131213/048b52ad/attachment.pgp>
#4 lore raw

PATCH 1/1 lib/time - fix "usec_to_tick" calculation for hi freq system timers

Quoted 3 lines · Tom Rini

Please note that I'm taking http://patchwork.ozlabs.org/patch/297361/ as the solution for this problem, thanks!

As long as it resolves overflow issue (and from that patch I'd say it should) I'm totally fine with it.

Thanks.

P.S. funny enough this overflow issue bites multiple people these days (but earlier it seems like everybody was happy though).