Skip to contentSkip to conversation

PATCH mtd CFI flash: timeout calculation underflow if imprecise 1kHz timer: fix

U-Boot Renato Andreola 3 participants 4 messages Aug 6, 2009 1 message not in the archive Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

From be54cb97ca26bcbbc1a908d1f2a5447b6639dc59 Mon Sep 17 00:00:00 2001
From: Renato Andreola <[email protected]>
Date: Thu, 6 Aug 2009 11:40:52 +0200
Subject: [PATCH] drivers/mtd/cfi_flash: precision and underflow problem in tout calculation

With old configuration it could happen tout=0 if CONFIG_SYS_HZ<1000 solved using an unsigned long long

---
  drivers/mtd/cfi_flash.c |    6 ++++--
  1 files changed, 4 insertions(+), 2 deletions(-)
drivers/mtd/cfi_flash.c +0 -0
index 81ac5d3..6bcd102 100644
@@ -659,8 +659,10 @@ static int flash_status_check (flash_info_t * info, 

flash_sect_t sector,
{
ulong start;

-#if CONFIG_SYS_HZ != 1000
- tout *= CONFIG_SYS_HZ/1000;
+#if CONFIG_SYS_HZ != 1000
+ unsigned long long ull;
+ ull = tout*CONFIG_SYS_HZ + CONFIG_SYS_HZ/2;
+ tout = ull/1000; /* Compute: tout *= CONFIG_SYS_HZ/1000; */
#endif

/* Wait for command completion */

Signature · 1 line
1.5.5
#1 lore raw

Dear Renato Andreola,

Quoted 8 lines · In message <[email protected]> you wrote:

From be54cb97ca26bcbbc1a908d1f2a5447b6639dc59 Mon Sep 17 00:00:00 2001
From: Renato Andreola <[email protected]>
Date: Thu, 6 Aug 2009 11:40:52 +0200
Subject: [PATCH] drivers/mtd/cfi_flash: precision and underflow problem in tout calculation

With old configuration it could happen tout=0 if CONFIG_SYS_HZ<1000 solved using an unsigned long long

I don't like this implementation - using LL divisions just pulls in more dependencies on libgcc code and the like, and blows up the code size.

Review

Quoted 6 lines
(quoted diff) +4 -2
-#if CONFIG_SYS_HZ != 1000
-	tout *= CONFIG_SYS_HZ/1000;
+#if CONFIG_SYS_HZ != 1000	
+	unsigned long long ull;
+	ull = tout*CONFIG_SYS_HZ + CONFIG_SYS_HZ/2;
+	tout = ull/1000; /* Compute: tout *= CONFIG_SYS_HZ/1000; */

I think rounding up should be sufficient here, i. e. something like:

	tout *= DIV_ROUND_UP(CONFIG_SYS_HZ,1000);

?

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Ninety-Ninety Rule of Project Schedules:
        The first ninety percent of the task takes ninety percent of
the time, and the last ten percent takes the other ninety percent.
#2 lore raw
In reply to Renato Andreola
Quoted 2 lines

With old configuration it could happen tout=0 if CONFIG_SYS_HZ<1000 solved using an unsigned long long

I had the same problem with an ancient version of a vendor-ported u-boot. There my CFG_HZ was 100, so the timeout was 0.

I used this, which avoids the preprocessor conditional. Since the HZ value is a compile-time constant, the compiler chooses the if or else branch and doesn't spit a run-time conditional.2

        if (CFG_HZ > 100000)
                tout *= CFG_HZ/1000;  /* for a big HZ, avoid overflow */
        else
                tout = tout * CFG_HZ / 1000 + 1;

/alessandro

#4 lore raw
In reply to a message not in the archive

Dear Renato Andreola,

Quoted 3 lines · In message <[email protected]> you wrote:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<head>

Please NEVER post HTML here!!

Quoted 4 lines

I agree with Wolfgang Denk in not using the LL type (too compiler/library/machine dependent).<br>
I think that the code of Alessandro Rubini that makes the runtime decision using no extra macro definition is more system independent and

This is no extra macro definition. This macro is available by default (defined in include/common.h).

Quoted 1 line

keeps a small 1% residual error when CFG_HZ is just above 100000.<br>

Keep in mind that systems with CONFIG_SYS_HZ != 1000 are considered broken anyway.

As little effort as possible should be spent to support these - efforts should rather be focussed on fixing these systems.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
A meeting is an event at which the minutes are kept and the hours are
lost.