Skip to contentSkip to conversation

Regression on colibri-imx7 due to commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()")

DISCUSSION Emanuele Ghidoli 3 participants 9 messages Jul 23 – Jul 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi,

I found that Colibri iMX7 no longer boots when commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") is applied. Nothing is printed on the serial.

I bisected the issue: booting the parent commit works fine, and reverting this patch on top of current main also restores boot. It's not yet obvious to me what in this change causes the regression.

Would it make sense to revert it until the root cause is found?

Thanks,
Emanuele

#1 lore raw

On 7/23/26 4:53 PM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 10 lines

I found that Colibri iMX7 no longer boots when commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") is applied. Nothing is printed on the serial.

I bisected the issue: booting the parent commit works fine, and reverting this patch on top of current main also restores boot. It's not yet obvious to me what in this change causes the regression.

Would it make sense to revert it until the root cause is found?

Give Patrice a bit of time to analyze the problem.

Signature · 2 lines
Best regards,
Marek Vasut
#2 lore raw
Quoted 8 lines · Marek Vasut

On 7/23/26 4:53 PM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 4 lines

I found that Colibri iMX7 no longer boots when commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") is applied. Nothing is printed on the serial.

Quoted 9 lines
Quoted 6 lines

I bisected the issue: booting the parent commit works fine, and reverting this patch on top of current main also restores boot. It's not yet obvious to me what in this change causes the regression.

Would it make sense to revert it until the root cause is found?

Give Patrice a bit of time to analyze the problem.

Hello,

after the regression, get_timer_us() is called on every cyclic_run() call, even when the cyclic list is empty. On imx7 this happens earlier than before, when the timer has not yet been initialized: get_timer_us() calls get_tbclk(), whose value is zero, and uses it as a divisor in tick_to_time_us(), leading to a division by zero.

I would fix this by ensuring get_tbclk() returns at least 1 on imx7, but we could hide bugs in this way. However, this bug could also be present on other architectures, so I would just guard against entering the cyclic loop by checking whether the list is empty before doing anything else, so get_timer_us() is called "at the same point in time" as before. Something like:

if (hlist_empty(cyclic_get_list())) {
	gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
	return;
}

Kind regards,
Emanuele

#3 lore raw
Quoted 35 lines · Emanuele Ghidoli
Quoted 8 lines · Marek Vasut

On 7/23/26 4:53 PM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 4 lines

I found that Colibri iMX7 no longer boots when commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") is applied. Nothing is printed on the serial.

Quoted 9 lines
Quoted 6 lines

I bisected the issue: booting the parent commit works fine, and reverting this patch on top of current main also restores boot. It's not yet obvious to me what in this change causes the regression.

Would it make sense to revert it until the root cause is found?

Give Patrice a bit of time to analyze the problem.

Hello,

after the regression, get_timer_us() is called on every cyclic_run() call, even when the cyclic list is empty. On imx7 this happens earlier than before, when the timer has not yet been initialized: get_timer_us() calls get_tbclk(), whose value is zero, and uses it as a divisor in tick_to_time_us(), leading to a division by zero.

I would fix this by ensuring get_tbclk() returns at least 1 on imx7, but we could hide bugs in this way. However, this bug could also be present on other architectures, so I would just guard against entering the cyclic loop by checking whether the list is empty before doing anything else, so get_timer_us() is called "at the same point in time" as before. Something like:

Quoted 4 lines
if (hlist_empty(cyclic_get_list())) {
	gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
	return;
}

It looks like there are two topics -- optimize the cyclic run and fix the MX7 timer.

I agree the cyclic run should exit if the list is empty, but please wait for input from Rasmus on that.

Regarding the timer, how come the timer is initialized so late on MX7 ?

The Cortex-A7 should have its own ARM timer, that should be available right from the beginning. Is that ARM timer in use on your system, or does your system use GPT timer ?

Which board is this ?

+CC Peng

#4 lore raw
Quoted 40 lines · Marek Vasut
Quoted 35 lines · Emanuele Ghidoli
Quoted 8 lines · Marek Vasut

On 7/23/26 4:53 PM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 4 lines

I found that Colibri iMX7 no longer boots when commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") is applied. Nothing is printed on the serial.

Quoted 9 lines
Quoted 6 lines

I bisected the issue: booting the parent commit works fine, and reverting this patch on top of current main also restores boot. It's not yet obvious to me what in this change causes the regression.

Would it make sense to revert it until the root cause is found?

Give Patrice a bit of time to analyze the problem.

Hello,

after the regression, get_timer_us() is called on every cyclic_run() call, even when the cyclic list is empty. On imx7 this happens earlier than before, when the timer has not yet been initialized: get_timer_us() calls get_tbclk(), whose value is zero, and uses it as a divisor in tick_to_time_us(), leading to a division by zero.

I would fix this by ensuring get_tbclk() returns at least 1 on imx7, but we could hide bugs in this way. However, this bug could also be present on other architectures, so I would just guard against entering the cyclic loop by checking whether the list is empty before doing anything else, so get_timer_us() is called "at the same point in time" as before. Something like:

Quoted 4 lines
if (hlist_empty(cyclic_get_list())) {
	gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
	return;
}
Quoted 6 lines

It looks like there are two topics -- optimize the cyclic run and fix the MX7 timer.

I agree the cyclic run should exit if the list is empty, but please wait for input from Rasmus on that.

Yes, we could/should definitely exit early if the list is empty, but don't mix it up with the GD_FLG_CYCLIC_RUNNING bit. Just add it as a separate early exit condition. But a future reader might read that as "just" an optimization, seeing that the loop would just do nothing, I think it also requires an explanation. Something like

common/cyclic.c +13 -0
index 573e715587d..e46346536ab 100644
@@ -69,6 +69,19 @@ static void cyclic_run(void)
        struct hlist_node *tmp;
        u64 now, after, cpu_time;
 
+       /*
+        * Nothing to do if the list is empty. Also, schedule() can be
+        * called before timer infrastructure is ready, in which case
+        * calling get_timer_us() before the (empty) loop could cause
+        * a divide-by-0 or otherwise crash the system. No clients
+        * should be registered before the timer infrastructure is up,
+        * so the check for the list being empty should be
+        * ok. Otherwise, we would need a new GD_FLG_TIMERS_READY
+        * flag.
+        */
+       if (hlist_empty(cyclic_get_list()))
+                return;
+
        /* Prevent recursion */
        if (gd->flags & GD_FLG_CYCLIC_RUNNING)
                return;

Completely unrelated, but now that I see a new use of that cyclic_get_list() helper, I think it's time to get rid of that and just spell it &gd->cyclic_list. It was only needed back when gd was volatile-qualified.

Rasmus

#5 lore raw
In reply to Marek Vasut via U-Boot
Quoted 40 lines · Marek Vasut
Quoted 29 lines · Emanuele Ghidoli
Quoted 8 lines · Marek Vasut

On 7/23/26 4:53 PM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 4 lines

I found that Colibri iMX7 no longer boots when commit 9c1b13b3fd27 ("cyclic: reduce get_timer_us() calls inside hlist_for_each_entry_safe()") is applied. Nothing is printed on the serial.

Quoted 9 lines
Quoted 6 lines

I bisected the issue: booting the parent commit works fine, and reverting this patch on top of current main also restores boot. It's not yet obvious to me what in this change causes the regression.

Would it make sense to revert it until the root cause is found?

Give Patrice a bit of time to analyze the problem.

Hello,

after the regression, get_timer_us() is called on every cyclic_run() call, even when the cyclic list is empty. On imx7 this happens earlier than before, when the timer has not yet been initialized: get_timer_us() calls get_tbclk(), whose value is zero, and uses it as a divisor in tick_to_time_us(), leading to a division by zero.

Quoted 10 lines

I would fix this by ensuring get_tbclk() returns at least 1 on imx7, but we could hide bugs in this way. However, this bug could also be present on other architectures, so I would just guard against entering the cyclic loop by checking whether the list is empty before doing anything else, so get_timer_us() is called "at the same point in time" as before. Something like:
if (hlist_empty(cyclic_get_list())) {
    gd->flags &= ~GD_FLG_CYCLIC_RUNNING;
    return;
}

Quoted 7 lines

It looks like there are two topics -- optimize the cyclic run and fix the MX7 timer.

I agree the cyclic run should exit if the list is empty, but please wait for input from Rasmus on that.

Regarding the timer, how come the timer is initialized so late on MX7 ?

timer_init() called in initcall_run_f().>

Quoted 3 lines

The Cortex-A7 should have its own ARM timer, that should be available right from the beginning. Is that ARM timer in use on your system, or does your system use GPT timer ?

iMX7 uses arch/arm/mach-imx/syscounter.c.

udelay() is called in initcall_run_f(), before timer_init(). arch_cpu_init() (arch/arm/mach-imx/mx7/soc.c) -> imx_gpcv2_init -> udelay (which runs schedule/cyclic).

And this is interesting, udelay() before timer_init() leads to a 0 us delay, so there is another bug.

I have verified that removing this udelay(65) the board boots.

Quoted 2 lines

Which board is this ?

board/toradex/colibri_imx7/

Quoted 2 lines

+CC Peng

Kind regards,
Emanuele

#6 lore raw

On 7/24/26 9:36 AM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 10 lines
Quoted 1 line

Regarding the timer, how come the timer is initialized so late on MX7 ?

timer_init() called in initcall_run_f().>

Quoted 3 lines

The Cortex-A7 should have its own ARM timer, that should be available right from the beginning. Is that ARM timer in use on your system, or does your system use GPT timer ?

iMX7 uses arch/arm/mach-imx/syscounter.c.

udelay() is called in initcall_run_f(), before timer_init(). arch_cpu_init() (arch/arm/mach-imx/mx7/soc.c) -> imx_gpcv2_init -> udelay (which runs schedule/cyclic).

Yikes.

Quoted 4 lines

And this is interesting, udelay() before timer_init() leads to a 0 us delay, so there is another bug.

I have verified that removing this udelay(65) the board boots.

Can we start the syscounter sooner ? Something like this:

arch/arm/include/asm/arch-mx6/sys_proto.h +2 -0
index 7845fa8e569..5f6759a3306 100644
@@ -36,4 +36,6 @@ static inline void iomuxc_set_rgmii_io_voltage(int io_vol)
 	__raw_writel(io_vol, IOMUXC_SW_PAD_CTL_GRP_DDR_TYPE_RGMII);
 }
 
+void syscounter_init(void);
+
 #endif /* __SYS_PROTO_IMX6_ */
arch/arm/include/asm/arch-mx7/sys_proto.h +2 -0
index 5da0037b2c6..765764cf1bc 100644
@@ -11,4 +11,6 @@ struct wdog_regs;
 
 void set_wdog_reset(struct wdog_regs *wdog);
 
+void syscounter_init(void);
+
 #endif /* __SYS_PROTO_IMX7_ */
arch/arm/mach-imx/mx6/soc.c +2 -0
index 02df86156d4..641b5d813ea 100644
@@ -437,6 +437,8 @@ int arch_cpu_init(void)
 		 */
 		if (mxc_get_clock(MXC_ARM_CLK) == 396000000)
 			set_ahb_rate(132000000);
+	} else {
+		syscounter_init();
 	}
 
 	if (is_mx6ul()) {
arch/arm/mach-imx/mx7/soc.c +2 -0
index e504c1fd52a..82d851b7817 100644
@@ -331,6 +331,8 @@ int arch_cpu_init(void)
 
 	init_snvs();
 
+	syscounter_init();
+
 	imx_gpcv2_init();
 
 	enable_ca7_smp();
arch/arm/mach-imx/syscounter.c +7 -1
index 96fe2c7c17b..7b902540737 100644
@@ -60,7 +60,7 @@ static inline unsigned long long us_to_tick(unsigned long long usec)
 }
 
 #if !CONFIG_IS_ENABLED(SKIP_LOWLEVEL_INIT) || IS_ENABLED(CONFIG_XPL_BUILD)
-int timer_init(void)
+void syscounter_init(void)
 {
 	struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
 	unsigned long val, freq;
@@ -80,6 +80,12 @@ int timer_init(void)
 	gd->arch.tbu = 0;
 
 	gd->arch.timer_rate_hz = freq;
+
+	return 0;
+}
+
+int timer_init(void)
+{
 	return 0;
 }
 #endif
#7 lore raw
Quoted 90 lines · Marek Vasut

On 7/24/26 9:36 AM, Emanuele Ghidoli wrote:

Hello everyone,

Quoted 10 lines
Quoted 1 line

Regarding the timer, how come the timer is initialized so late on MX7 ?

timer_init() called in initcall_run_f().>

Quoted 3 lines

The Cortex-A7 should have its own ARM timer, that should be available right from the beginning. Is that ARM timer in use on your system, or does your system use GPT timer ?

iMX7 uses arch/arm/mach-imx/syscounter.c.

udelay() is called in initcall_run_f(), before timer_init(). arch_cpu_init() (arch/arm/mach-imx/mx7/soc.c) -> imx_gpcv2_init -> udelay (which runs schedule/cyclic).

Yikes.

Quoted 4 lines

And this is interesting, udelay() before timer_init() leads to a 0 us delay, so there is another bug.

I have verified that removing this udelay(65) the board boots.

Can we start the syscounter sooner ? Something like this:

arch/arm/include/asm/ renamed from arch/arm/include/asm/arch-mx6/sys_proto.h +0 -0

arch-mx6/sys_proto.h
index 7845fa8e569..5f6759a3306 100644

arch/arm/include/asm/arch-mx6/sys_proto.h +2 -0
@@ -36,4 +36,6 @@ static inline void iomuxc_set_rgmii_io_voltage(int io_vol)
      __raw_writel(io_vol, IOMUXC_SW_PAD_CTL_GRP_DDR_TYPE_RGMII);
  }
 
+void syscounter_init(void);
+
  #endif /* __SYS_PROTO_IMX6_ */
arch/arm/include/asm/ renamed from arch/arm/include/asm/arch-mx7/sys_proto.h +0 -0

arch-mx7/sys_proto.h
index 5da0037b2c6..765764cf1bc 100644

arch/arm/include/asm/arch-mx7/sys_proto.h +2 -0
@@ -11,4 +11,6 @@ struct wdog_regs;
 
  void set_wdog_reset(struct wdog_regs *wdog);
 
+void syscounter_init(void);
+
  #endif /* __SYS_PROTO_IMX7_ */
arch/arm/mach-imx/mx6/soc.c +2 -0
index 02df86156d4..641b5d813ea 100644
@@ -437,6 +437,8 @@ int arch_cpu_init(void)
           */
          if (mxc_get_clock(MXC_ARM_CLK) == 396000000)
              set_ahb_rate(132000000);
+    } else {
+        syscounter_init();
      }
 
      if (is_mx6ul()) {
arch/arm/mach-imx/mx7/soc.c +2 -0
index e504c1fd52a..82d851b7817 100644
@@ -331,6 +331,8 @@ int arch_cpu_init(void)
 
      init_snvs();
 
+    syscounter_init();
+
      imx_gpcv2_init();
 
      enable_ca7_smp();
arch/arm/mach-imx/syscounter.c +1 -1
index 96fe2c7c17b..7b902540737 100644
@@ -60,7 +60,7 @@ static inline unsigned long long us_to_tick(unsigned long
 long usec)
  }
 
  #if !CONFIG_IS_ENABLED(SKIP_LOWLEVEL_INIT) || IS_ENABLED(CONFIG_XPL_BUILD)
-int timer_init(void)
+void syscounter_init(void)
  {
      struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;

     unsigned long val, freq;

arch/arm/mach-imx/syscounter.c +0 -0
@@ -80,6 +80,12 @@ int timer_init(void)
      gd->arch.tbu = 0;
 

arch/arm/mach-imx/syscounter.c int timer_init(void) · line 91

Quoted 10 lines
arch/arm/mach-imx/syscounter.c +6 -0
      gd->arch.timer_rate_hz = freq;
+
+    return 0;
+}
+
+int timer_init(void)
+{
      return 0;
  }
  #endif

Hello Marek,
thanks for the suggestion.

It works, build and tested on Colibri iMX7.

But imx6 is obviously broken:
/home/emanuele/tdx/u-boot-build/u-boot/arch/arm/mach-imx/mx6/soc.c:441:
undefined reference to `syscounter_init'

Emanuele

#8 lore raw

On 7/28/26 11:46 AM, Emanuele Ghidoli wrote:

Hello Emanuele,

Quoted 21 lines
Quoted 7 lines
arch/arm/mach-imx/syscounter.c +1 -0
+void syscounter_init(void)
  {
      struct sctr_regs *sctr = (struct sctr_regs *)SCTR_BASE_ADDR;
      unsigned long val, freq;
@@ -80,6 +80,12 @@ int timer_init(void)
      gd->arch.tbu = 0;
 

arch/arm/mach-imx/syscounter.c int timer_init(void) · line 91

Quoted 10 lines
arch/arm/mach-imx/syscounter.c +6 -0
      gd->arch.timer_rate_hz = freq;
+
+    return 0;
+}
+
+int timer_init(void)
+{
      return 0;
  }
  #endif

Hello Marek,
thanks for the suggestion.

Quoted 5 lines

It works, build and tested on Colibri iMX7.

But imx6 is obviously broken:
/home/emanuele/tdx/u-boot-build/u-boot/arch/arm/mach-imx/mx6/soc.c:441:
undefined reference to `syscounter_init'

Excellent, could you please submit this timer patch as a proper fix / patch ?

Thank you !