Subject: [PATCH net] stmmac: fix potential division by 0

From: Piergiorgio Beruto <[email-masked]>
Date: Thu, 1 Dec 2022 01:37:08 +0100

Depending on the HW platform and configuration, the
stmmac_config_sub_second_increment() function may return 0 in the
sec_inc variable. Therefore, the subsequent div_u64 operation can Oops
the kernel because of the divisor being 0.

Signed-off-by: Piergiorgio Beruto <[email-masked]>
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
index 23ec0a9e396c..6ed1704b638d 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
@@ -848,7 +848,7 @@ int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
 	stmmac_config_sub_second_increment(priv, priv->ptpaddr,
 					   priv->plat->clk_ptp_rate,
 					   xmac, &sec_inc);
-	temp = div_u64(1000000000ULL, sec_inc);
+	temp = div_u64(1000000000ULL, max_t(u32, sec_inc, 1));
 
 	/* Store sub second increment for later use */
 	priv->sub_second_inc = sec_inc;
-- 
2.35.1