Skip to contentSkip to conversation

time: Fix get_ticks being non-monotonic

PATCH Superseded Sean Anderson 4 participants 7 messages Sep 9, 2020 – Mar 1, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

get_ticks does not always succeed. Sometimes it can be called before the timer has been initialized. If it does, it returns a negative errno. This causes the timer to appear non-monotonic, because the value will become much smaller after the timer is initialized.

No users of get_ticks which I checked handle errors of this kind. Further, functions like tick_to_time mangle the result of get_ticks, making it very unlikely that one could check for an error without suggesting a patch such as this one.

This patch panics if we ever get an error. There are two cases in which this can occur. The first is if we couldn't find/probe the timer for some reason. One reason for this is if the timer is not available so early. This likely indicates misconfiguration. Another reason is that the timer has an invalid/missing device tree binding. In this case, panicing is also correct. The second case covers errors calling get_count. This can only occur if the timer is missing a get_count function (or on RISC-V, but that should be fixed soon).

Fixes c8a7ba9e6a5

---

Changes in v2:
- Panic on error

 lib/time.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/time.c +2 -2
index 47f8c84327..88bc50405f 100644
@@ -91,13 +91,13 @@ uint64_t notrace get_ticks(void)
 
 		ret = dm_timer_init();
 		if (ret)
-			return ret;
+			panic("Could not initialize timer (err %d)\n", ret);
 #endif
 	}
 
 	ret = timer_get_count(gd->timer, &count);
 	if (ret)
-		return ret;
+		panic("Could not read count from timer (err %d)\n", ret);
 
 	return count;
 }
Signature · 1 line
2.28.0
#1 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

Quoted 22 lines · Sean Anderson

get_ticks does not always succeed. Sometimes it can be called before the timer has been initialized. If it does, it returns a negative errno. This causes the timer to appear non-monotonic, because the value will become much smaller after the timer is initialized.

No users of get_ticks which I checked handle errors of this kind. Further, functions like tick_to_time mangle the result of get_ticks, making it very unlikely that one could check for an error without suggesting a patch such as this one.

This patch panics if we ever get an error. There are two cases in which this can occur. The first is if we couldn't find/probe the timer for some reason. One reason for this is if the timer is not available so early. This likely indicates misconfiguration. Another reason is that the timer has an invalid/missing device tree binding. In this case, panicing is also correct. The second case covers errors calling get_count. This can only occur if the timer is missing a get_count function (or on RISC-V, but that should be fixed soon).

Fixes c8a7ba9e6a5
Quoted 7 lines

---

Changes in v2:
- Panic on error

 lib/time.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#2 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

In reply to Sean Anderson
Quoted 19 lines · Sean Anderson

get_ticks does not always succeed. Sometimes it can be called before the timer has been initialized. If it does, it returns a negative errno. This causes the timer to appear non-monotonic, because the value will become much smaller after the timer is initialized.

No users of get_ticks which I checked handle errors of this kind. Further, functions like tick_to_time mangle the result of get_ticks, making it very unlikely that one could check for an error without suggesting a patch such as this one.

This patch panics if we ever get an error. There are two cases in which this can occur. The first is if we couldn't find/probe the timer for some reason. One reason for this is if the timer is not available so early. This likely indicates misconfiguration. Another reason is that the timer has an invalid/missing device tree binding. In this case, panicing is also correct. The second case covers errors calling get_count. This can only occur if the timer is missing a get_count function (or on RISC-V, but that should be fixed soon).

Quoted 3 lines

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201014/8c0d9833/attachment.sig>
#3 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

Hi,

Sorry, no messaging quoting, I was not subscribed to the list at that time.

Merging this change into master actually broke the SPL on Atmel/Microchip SAMA5D3, at least booting from MMC:

RomBOOT

<debug_uart>
Could not initialize timer (err -11)

Could not initialize timer (err -11)

Could not initialize timer (err -11)
...

I'll look for a fix, but suggestions are welcome!

Cheers,

Michael.

Signature · 3 lines
Michael Opdenacker, CEO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

Hi Michael,

Quoted 13 lines · Michael Opdenacker

Hi,

Sorry, no messaging quoting, I was not subscribed to the list at that time.

Merging this change into master actually broke the SPL on Atmel/Microchip SAMA5D3, at least booting from MMC:

RomBOOT

<debug_uart>
Could not initialize timer (err -11)

Quoted 6 lines

Could not initialize timer (err -11)

Could not initialize timer (err -11)
...

I'll look for a fix, but suggestions are welcome!

The board might need CONFIG_TIMER_EARLY, but otherwise I think it is a case of figuring out why the timer is used before it is available.

You can use dm_dump_all() to print out available devices and whether they are probed.

Regards,
Simon

#5 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

Hi Simon,

I'm back working on this old issue. Thanks for your help with this!

So, SPL support for Atmel/Microchip SAMA5D3 is still broken in the latest mainline, at least for the Xplained board with the MMC configuration.

My comments and further questions below...

Quoted 22 lines · Simon Glass

Hi Michael,

Quoted 18 lines · Michael Opdenacker

Hi,

Sorry, no messaging quoting, I was not subscribed to the list at that time.

Merging this change into master actually broke the SPL on Atmel/Microchip SAMA5D3, at least booting from MMC:

RomBOOT

<debug_uart>
Could not initialize timer (err -11)

Could not initialize timer (err -11)

Could not initialize timer (err -11)
...

I'll look for a fix, but suggestions are welcome!

Now, it's:

Could not initialize timer (err -19)
(-19 is -ENODEV? /* No such device */)

Quoted 2 lines

The board might need CONFIG_TIMER_EARLY, but otherwise I think it is a case of figuring out why the timer is used before it is available.

I tried to enable CONFIG_TIMER_EARLY but it fails at link time:

? LD????? u-boot
arm-linux-ld.bfd: lib/built-in.o: in function `get_tbclk':
/home/mike/work/git/git.denx.de/u-boot/lib/time.c:70: undefined reference to `timer_early_get_rate'
arm-linux-ld.bfd: lib/built-in.o: in function `get_ticks':
/home/mike/work/git/git.denx.de/u-boot/lib/time.c:90: undefined reference to `timer_early_get_count'
make: *** [Makefile:1765: u-boot] Error 1

This is not a surprise, as according to
https://elixir.bootlin.com/u-boot/latest/C/ident/timer_early_get_rate,?
timer_early_get_rate() is not implemented on ARM, only on sandbox (drivers/timer/sandbox_timer.c) and on x86 (drivers/timer/tsc_timer.c).

So, I'm moving to your second suggestion...

Quoted 3 lines

You can use dm_dump_all() to print out available devices and whether they are probed.

Done, I added dm_dump_all to lib/time.c right before the panic() message:

?Class???? Index? Probed? Driver??????????????? Name
-----------------------------------------------------------
?root? 0? [ + ]?? root_driver? root_driver
?simple_bus? 0? [?? ]?? simple_bus? `-- ahb
?simple_bus? 1? [?? ]?? simple_bus????? `-- apb
?mmc? 0? [?? ]?? atmel-mci????????? |-- mmc at f0000000
?blk? 0? [?? ]?? mmc_blk????????? |?? `-- mmc at f0000000.blk ?mmc? 1? [?? ]?? atmel-mci????????? |-- mmc at f8000000
?blk? 1? [?? ]?? mmc_blk????????? |?? `-- mmc at f8000000.blk ?serial? 0? [?? ]?? serial_atmel????????? |-- serial at ffffee00 ?pinctrl? 0? [?? ]?? atmel_sama5d3_pinctrl????????? |-- pinctrl at fffff200

?pinconfig? 0? [?? ]?? pinconfig????????? |?? |-- dbgu
?pinconfig? 1? [?? ]?? pinconfig????????? |?? |?? `-- dbgu-0
?pinconfig? 2? [?? ]?? pinconfig????????? |?? |-- mmc0
?pinconfig? 3? [?? ]?? pinconfig????????? |?? |?? |-- mmc0_clk_cmd_dat0
?pinconfig? 4? [?? ]?? pinconfig????????? |?? |?? |-- mmc0_dat1_3
?pinconfig? 5? [?? ]?? pinconfig????????? |?? |?? `-- mmc0_dat4_7
?pinconfig? 6? [?? ]?? pinconfig????????? |?? |-- mmc1
?pinconfig? 7? [?? ]?? pinconfig????????? |?? |?? |-- mmc1_clk_cmd_dat0
?pinconfig? 8? [?? ]?? pinconfig????????? |?? |?? `-- mmc1_dat1_3
?pinconfig? 9? [?? ]?? pinconfig????????? |?? |-- spi0
?pinconfig? 10? [?? ]?? pinconfig????????? |?? |?? `-- spi0-0
?pinconfig? 11? [?? ]?? pinconfig????????? |?? |-- spi1
?pinconfig? 12? [?? ]?? pinconfig????????? |?? |?? `-- spi1-0
?pinconfig? 13? [?? ]?? pinconfig????????? |?? `-- board
?pinconfig? 14? [?? ]?? pinconfig????????? |?????? |-- mmc0_cd
?pinconfig? 15? [?? ]?? pinconfig????????? |?????? `-- mmc1_cd
?gpio? 0? [?? ]?? atmel_at91rm9200_gpio????????? |-- gpio at fffff200
?gpio? 1? [?? ]?? atmel_at91rm9200_gpio????????? |-- gpio at fffff400
?gpio? 2? [?? ]?? atmel_at91rm9200_gpio????????? |-- gpio at fffff600
?gpio? 3? [?? ]?? atmel_at91rm9200_gpio????????? |-- gpio at fffff800
?gpio? 4? [?? ]?? atmel_at91rm9200_gpio????????? |-- gpio at fffffa00

?simple_bus? 2? [?? ]?? at91-pmc????????? -- pmc at fffffc00 ?clk? 0? [?? ]?? at91sam9x5-utmi-clk????????????? |-- utmick ?clk? 1? [?? ]?? at91-master-clk????????????? |-- masterck ?misc? 0? [?? ]?? sam9x5-periph-clk????????????? -- periphck

?clk? 2? [?? ]?? periph-clk????????????????? |-- dbgu_clk at 2
?clk? 3? [?? ]?? periph-clk????????????????? |-- pioA_clk at 6
?clk? 4? [?? ]?? periph-clk????????????????? |-- pioB_clk at 7
?clk? 5? [?? ]?? periph-clk????????????????? |-- pioC_clk at 8
?clk? 6? [?? ]?? periph-clk????????????????? |-- pioD_clk at 9
?clk? 7? [?? ]?? periph-clk????????????????? |-- pioE_clk at 10
?clk? 8? [?? ]?? periph-clk????????????????? |-- mci0_clk at 21
?clk? 9? [?? ]?? periph-clk????????????????? |-- mci1_clk at 22

?clk? 10? [?? ]?? periph-clk????????????????? |-- spi0_clk at 24 ?clk? 11? [?? ]?? periph-clk????????????????? `-- spi1_clk at 25 Could not initialize timer (err -19)

I'm not familiar enough with U-Boot yet (and with SAMA5D3 support either) to see why the timer device is missing here, but I hope our Microchip friends can give us further clues...

Thanks again,

Cheers,

Michael.

Signature · 3 lines
Michael Opdenacker, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

PATCH v2 1/1 time: Fix get_ticks being non-monotonic

Quoted 55 lines · Michael Opdenacker

Hi Simon,

I'm back working on this old issue. Thanks for your help with this!

So, SPL support for Atmel/Microchip SAMA5D3 is still broken in the latest mainline, at least for the Xplained board with the MMC configuration.

My comments and further questions below...

Quoted 22 lines · Simon Glass

Hi Michael,

Quoted 18 lines · Michael Opdenacker

Hi,

Sorry, no messaging quoting, I was not subscribed to the list at that time.

Merging this change into master actually broke the SPL on Atmel/Microchip SAMA5D3, at least booting from MMC:

RomBOOT

<debug_uart>
Could not initialize timer (err -11)

Could not initialize timer (err -11)

Could not initialize timer (err -11)
...

I'll look for a fix, but suggestions are welcome!

Now, it's:

Could not initialize timer (err -19)
(-19 is -ENODEV /* No such device */)

Quoted 2 lines

The board might need CONFIG_TIMER_EARLY, but otherwise I think it is a case of figuring out why the timer is used before it is available.

I tried to enable CONFIG_TIMER_EARLY but it fails at link time:

LD u-boot
arm-linux-ld.bfd: lib/built-in.o: in function `get_tbclk':
/home/mike/work/git/git.denx.de/u-boot/lib/time.c:70: undefined reference to `timer_early_get_rate'
arm-linux-ld.bfd: lib/built-in.o: in function `get_ticks':
/home/mike/work/git/git.denx.de/u-boot/lib/time.c:90: undefined reference to `timer_early_get_count'
make: *** [Makefile:1765: u-boot] Error 1

Quoted 4 lines

This is not a surprise, as according to
https://elixir.bootlin.com/u-boot/latest/C/ident/timer_early_get_rate,
timer_early_get_rate() is not implemented on ARM, only on sandbox (drivers/timer/sandbox_timer.c) and on x86 (drivers/timer/tsc_timer.c).

It is also available on most RISC-V boards, not that it helps :)

Quoted 57 lines

So, I'm moving to your second suggestion...

Quoted 3 lines

You can use dm_dump_all() to print out available devices and whether they are probed.

Done, I added dm_dump_all to lib/time.c right before the panic() message:

Class Index Probed Driver Name
-----------------------------------------------------------
root 0 [ + ] root_driver root_driver

  simple_bus  0  [   ]   simple_bus  `-- ahb
  simple_bus  1  [   ]   simple_bus      `-- apb
  mmc  0  [   ]   atmel-mci          |-- mmc at f0000000
  blk  0  [   ]   mmc_blk          |   `-- mmc at f0000000.blk
  mmc  1  [   ]   atmel-mci          |-- mmc at f8000000
  blk  1  [   ]   mmc_blk          |   `-- mmc at f8000000.blk
  serial  0  [   ]   serial_atmel          |-- serial at ffffee00
  pinctrl  0  [   ]   atmel_sama5d3_pinctrl          |-- pinctrl at fffff200
  pinconfig  0  [   ]   pinconfig          |   |-- dbgu
  pinconfig  1  [   ]   pinconfig          |   |   `-- dbgu-0
  pinconfig  2  [   ]   pinconfig          |   |-- mmc0
  pinconfig  3  [   ]   pinconfig          |   |   |-- mmc0_clk_cmd_dat0
  pinconfig  4  [   ]   pinconfig          |   |   |-- mmc0_dat1_3
  pinconfig  5  [   ]   pinconfig          |   |   `-- mmc0_dat4_7
  pinconfig  6  [   ]   pinconfig          |   |-- mmc1
  pinconfig  7  [   ]   pinconfig          |   |   |-- mmc1_clk_cmd_dat0
  pinconfig  8  [   ]   pinconfig          |   |   `-- mmc1_dat1_3
  pinconfig  9  [   ]   pinconfig          |   |-- spi0
  pinconfig  10  [   ]   pinconfig          |   |   `-- spi0-0
  pinconfig  11  [   ]   pinconfig          |   |-- spi1
  pinconfig  12  [   ]   pinconfig          |   |   `-- spi1-0
  pinconfig  13  [   ]   pinconfig          |   `-- board
  pinconfig  14  [   ]   pinconfig          |       |-- mmc0_cd
  pinconfig  15  [   ]   pinconfig          |       `-- mmc1_cd
  gpio  0  [   ]   atmel_at91rm9200_gpio          |-- gpio at fffff200
  gpio  1  [   ]   atmel_at91rm9200_gpio          |-- gpio at fffff400
  gpio  2  [   ]   atmel_at91rm9200_gpio          |-- gpio at fffff600
  gpio  3  [   ]   atmel_at91rm9200_gpio          |-- gpio at fffff800
  gpio  4  [   ]   atmel_at91rm9200_gpio          |-- gpio at fffffa00
  simple_bus  2  [   ]   at91-pmc          `-- pmc at fffffc00
  clk  0  [   ]   at91sam9x5-utmi-clk              |-- utmick
  clk  1  [   ]   at91-master-clk              |-- masterck
  misc  0  [   ]   sam9x5-periph-clk              `-- periphck
  clk  2  [   ]   periph-clk                  |-- dbgu_clk at 2
  clk  3  [   ]   periph-clk                  |-- pioA_clk at 6
  clk  4  [   ]   periph-clk                  |-- pioB_clk at 7
  clk  5  [   ]   periph-clk                  |-- pioC_clk at 8
  clk  6  [   ]   periph-clk                  |-- pioD_clk at 9
  clk  7  [   ]   periph-clk                  |-- pioE_clk at 10
  clk  8  [   ]   periph-clk                  |-- mci0_clk at 21
  clk  9  [   ]   periph-clk                  |-- mci1_clk at 22
  clk  10  [   ]   periph-clk                  |-- spi0_clk at 24
  clk  11  [   ]   periph-clk                  `-- spi1_clk at 25
Quoted 1 line

Could not initialize timer (err -19)

So nothing here is probed, but additionally nothing has UCLASS_TIMER. What do you expect the timer device to be?

--Sean

Quoted 11 lines

I'm not familiar enough with U-Boot yet (and with SAMA5D3 support either) to see why the timer device is missing here, but I hope our Microchip friends can give us further clues...

Thanks again,

Cheers,

Michael.