[PATCH 1/2] cyclic: Fix rollover every 72 min on 32 bits platforms

Patrice CHOTARD patrice.chotard at foss.st.com
Tue Jan 14 14:19:06 CET 2025



On 1/9/25 08:56, Stefan Roese wrote:
> On 08.01.25 16:09, Patrice Chotard wrote:
>> On 32 bits platforms, timer_get_us() returns an unsigned long which
>> is a 32 bits. timer_get_us() wraps around every 72 minutes
>> (2 ^ 32 / 1000000 =~ 4295 sec =~ 72 min).
>>
>> So the test "if time_after_eq64(now, cyclic->next_call)" is no more
>> true when cyclic->next_call becomes above 32 bits max value (4294967295).
>>
>> At this point after 72 min, no more cyclic function are
>> executed included watchdog one.
>>
>> Instead of using timer_get_us(), use get_timer_us() which returns a
>> uint64_t, this allows a rollover every 584942 years.
>>
>> Signed-off-by: Patrice Chotard <patrice.chotard at foss.st.com>
>> ---
>>
>>   common/cyclic.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/common/cyclic.c b/common/cyclic.c
>> index 196797fd61e..e3f03a19d55 100644
>> --- a/common/cyclic.c
>> +++ b/common/cyclic.c
>> @@ -61,7 +61,7 @@ static void cyclic_run(void)
>>            * Check if this cyclic function needs to get called, e.g.
>>            * do not call the cyclic func too often
>>            */
>> -        now = timer_get_us();
>> +        now = get_timer_us(0);
>>           if (time_after_eq64(now, cyclic->next_call)) {
>>               /* Call cyclic function and account it's cpu-time */
>>               cyclic->next_call = now + cyclic->delay_us;
> 
> Reviewed-by: Stefan Roese <sr at denx.de>
> 
> Thanks,
> Stefan
> 
Hi Stefan

I found another issue regarding cyclic.
I didn't notice it due to debug trace i activated to find this issue, but
after 72 min , i got :

ERROR: cyclic function watchdog took too long: -4294967293us vs 1000us max

This is due to remaining usage of timer_get_us() in cyclic_register() and in cyclic_run().
they also need to be replaced by get_timer_us(0).

I will send a v2 for that.

Patrice


More information about the U-Boot mailing list