[PATCH v2] net: stmmac: add explicit check and error on invalid PTP clock rate

Maxime Chevallier maxime.chevallier at bootlin.com
Tue May 27 09:31:05 PDT 2025


Hi Alexis,

On Tue, 27 May 2025 08:33:44 +0200
Alexis Lothoré <alexis.lothore at bootlin.com> wrote:

> The stmmac platform drivers that do not open-code the clk_ptp_rate value
> after having retrieved the default one from the device-tree can end up
> with 0 in clk_ptp_rate (as clk_get_rate can return 0). It will
> eventually propagate up to PTP initialization when bringing up the
> interface, leading to a divide by 0:
> 
>  Division by zero in kernel.
>  CPU: 1 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.30-00001-g48313bd5768a #22
>  Hardware name: STM32 (Device Tree Support)
>  Call trace:
>   unwind_backtrace from show_stack+0x18/0x1c
>   show_stack from dump_stack_lvl+0x6c/0x8c
>   dump_stack_lvl from Ldiv0_64+0x8/0x18
>   Ldiv0_64 from stmmac_init_tstamp_counter+0x190/0x1a4
>   stmmac_init_tstamp_counter from stmmac_hw_setup+0xc1c/0x111c
>   stmmac_hw_setup from __stmmac_open+0x18c/0x434
>   __stmmac_open from stmmac_open+0x3c/0xbc
>   stmmac_open from __dev_open+0xf4/0x1ac
>   __dev_open from __dev_change_flags+0x1cc/0x224
>   __dev_change_flags from dev_change_flags+0x24/0x60
>   dev_change_flags from ip_auto_config+0x2e8/0x11a0
>   ip_auto_config from do_one_initcall+0x84/0x33c
>   do_one_initcall from kernel_init_freeable+0x1b8/0x214
>   kernel_init_freeable from kernel_init+0x24/0x140
>   kernel_init from ret_from_fork+0x14/0x28
>  Exception stack(0xe0815fb0 to 0xe0815ff8)
> 
> Prevent this division by 0 by adding an explicit check and error log
> about the actual issue.
> 
> Fixes: 19d857c9038e ("stmmac: Fix calculations for ptp counters when clock input = 50Mhz.")
> Signed-off-by: Alexis Lothoré <alexis.lothore at bootlin.com>
> ---
> Changes in v2:
> - Add Fixes tag
> - Reword commit message to clarify the triggering cause of the issue
> - Link to v1: https://lore.kernel.org/r/20250523-stmmac_tstamp_div-v1-1-bca8a5a3a477@bootlin.com
> ---
>  drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> index 918d7f2e8ba992208d7d6521a1e9dba01086058f..f68e3ece919cc88d0bf199a394bc7e44b5dee095 100644
> --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
> @@ -835,6 +835,11 @@ int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
>  	if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
>  		return -EOPNOTSUPP;
>  
> +	if (!priv->plat->clk_ptp_rate) {
> +		netdev_err(priv->dev, "Invalid PTP clock rate");
> +		return -EINVAL;
> +	}
> +
>  	stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags);
>  	priv->systime_flags = systime_flags;

This may be some nitpick that can be addressed at a later point, but we
now have a guarantee that when stmmac_ptp_register() gets called,
priv->ptp_clk_rate is non-zero, right ? If so, we can drop the test in
said function :

	if (priv->plat->has_gmac4 && priv->plat->clk_ptp_rate)
		priv->plat->cdc_error_adj = (2 * NSEC_PER_SEC) / priv->plat->clk_ptp_rate;

There is another spot in the code, like in the EST handling, where we
divide by priv->plat->ptp_clk_rate :

stmmac_adjust_time(...)
	stmmac_est_configure(priv, priv, priv->est,
			     priv->plat->clk_ptp_rate)
		.est_configure()
			ctrl |= ((NSEC_PER_SEC / ptp_rate) [...]

Maybe we should fail EST configuration as well if ptp_clk_rate is 0
(probably in stmmac_tc.c's tc_taprio_configure or in the
.est_configure). That can be a step for later as well, as I don't know
if the setup you found this bug on even supports taprio/EST, and setups
that do didn't seem to encounter the bug yet.

Besides all that,

Reviewed-by: Maxime Chevallier <maxime.chevallier at bootlin.com>

Thanks,

Maxime



More information about the linux-arm-kernel mailing list