Skip to contentSkip to conversation

net: enetc: fix potential divide-by-zero when num_vsi is zero

PATCH Applied [email protected] 4 participants 4 messages Jun 24 – Jun 25

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

PATCH 1/1 net: enetc: fix potential divide-by-zero when num_vsi is zero

From: Wei Fang <[email protected]>

For i.MX94 series, all the standalone ENETCs do not support SR-IOV, so pf->caps.num_vsi is zero. This leads to a divide-by-zero in enetc4_default_rings_allocation() when distributing rings among PF and VFs.

Division by zero is undefined behavior in C. On ARM64, the UDIV/SDIV instructions silently return zero rather than raising an exception, so the issue does not cause a visible crash. However, relying on this behavior is incorrect and poses a cross-platform compatibility risk.

Add an explicit check for num_vsi == 0 and return early after the PF's rings have been configured.

Fixes 2d673b0e2f8d ("net: enetc: add standalone ENETC support for i.MX94")
---
 drivers/net/ethernet/freescale/enetc/enetc4_pf.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/ethernet/freescale/enetc/enetc4_pf.c +3 -0
index 4e771f852358..437a15bbb47b 100644
@@ -322,6 +322,9 @@ static void enetc4_default_rings_allocation(struct enetc_pf *pf)
 	val = enetc4_psicfgr0_val_construct(false, num_tx_bdr, num_rx_bdr);
 	enetc_port_wr(hw, ENETC4_PSICFGR0(0), val);
 
+	if (!pf->caps.num_vsi)
+		return;
+
 	num_rx_bdr = pf->caps.num_rx_bdr - num_rx_bdr;
 	rx_rem = num_rx_bdr % pf->caps.num_vsi;
 	num_rx_bdr = num_rx_bdr / pf->caps.num_vsi;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 26 lines · On 6/24/26 09:27, [email protected] wrote:

From: Wei Fang <[email protected]>

For i.MX94 series, all the standalone ENETCs do not support SR-IOV, so pf->caps.num_vsi is zero. This leads to a divide-by-zero in enetc4_default_rings_allocation() when distributing rings among PF and VFs.

Division by zero is undefined behavior in C. On ARM64, the UDIV/SDIV instructions silently return zero rather than raising an exception, so the issue does not cause a visible crash. However, relying on this behavior is incorrect and poses a cross-platform compatibility risk.

Add an explicit check for num_vsi == 0 and return early after the PF's rings have been configured.

Fixes 2d673b0e2f8d ("net: enetc: add standalone ENETC support for i.MX94")
---
 drivers/net/ethernet/freescale/enetc/enetc4_pf.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/ethernet/freescale/enetc/enetc4_pf.c +0 -0
index 4e771f852358..437a15bbb47b 100644

drivers/net/ethernet/freescale/enetc/enetc4_pf.c static void enetc4_default_rings_allocation(struct enetc_pf *pf) · line 330

Quoted 9 lines
drivers/net/ethernet/freescale/enetc/enetc4_pf.c +3 -0
@@ -322,6 +322,9 @@ static void enetc4_default_rings_allocation(struct enetc_pf *pf)
 	val = enetc4_psicfgr0_val_construct(false, num_tx_bdr, num_rx_bdr);
 	enetc_port_wr(hw, ENETC4_PSICFGR0(0), val);
 
+	if (!pf->caps.num_vsi)
+		return;
+
 	num_rx_bdr = pf->caps.num_rx_bdr - num_rx_bdr;
 	rx_rem = num_rx_bdr % pf->caps.num_vsi;
 	num_rx_bdr = num_rx_bdr / pf->caps.num_vsi;

Maxime

#2 lore raw
In reply to [email protected]
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Potential divide by zero in enetc_port_si_configure() when distributing remaining rings to VFs. --

commit acc8f735fcd690e1b12e85278ec996003795aeda
Author: Wei Fang <[email protected]>

net: enetc: fix potential divide-by-zero when num_vsi is zero

For i.MX94 series, standalone ENETCs do not support SR-IOV, leaving pf->caps.num_vsi at zero. This leads to a divide-by-zero in enetc4_default_rings_allocation() when distributing rings. The patch adds an explicit check for num_vsi to return early and avoid the bug.

drivers/net/ethernet/freescale/enetc/enetc4_pf.c static void enetc4_default_rings_allocation(struct enetc_pf *pf) · line 327

Quoted 11 lines
drivers/net/ethernet/freescale/enetc/enetc4_pf.c +3 -0
index 4e771f8523580..437a15bbb47ba 100644
@@ -322,6 +322,9 @@ static void enetc4_default_rings_allocation(struct enetc_pf *pf)
 	val = enetc4_psicfgr0_val_construct(false, num_tx_bdr, num_rx_bdr);
 	enetc_port_wr(hw, ENETC4_PSICFGR0(0), val);
 
+	if (!pf->caps.num_vsi)
+		return;
+

[Severity: High]
This isn't a bug introduced by this patch, but while you are fixing the divide-by-zero here, I noticed a very similar pattern in enetc_pf.c:

drivers/net/ethernet/freescale/enetc/enetc_pf.c:enetc_port_si_configure() {
...

	if (num_rings) {
		num_rings /= pf->total_vfs;
		val |= ENETC_PSICFGR0_SET_TXBDR(num_rings);
		val |= ENETC_PSICFGR0_SET_RXBDR(num_rings);
	}

...
}

Does this code also lack a check to ensure pf->total_vfs is not zero?

If the hardware advertises more than 8 rings but SR-IOV is disabled or unavailable (making pf->total_vfs zero), could this trigger the same divide-by-zero exception when distributing the remaining rings to VFs?

Quoted 3 lines
drivers/net/ethernet/freescale/enetc/enetc4_pf.c +0 -0
 	num_rx_bdr = pf->caps.num_rx_bdr - num_rx_bdr;
 	rx_rem = num_rx_bdr % pf->caps.num_vsi;
 	num_rx_bdr = num_rx_bdr / pf->caps.num_vsi;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to [email protected]
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Wed, 24 Jun 2026 15:27:26 +0800 you wrote:

From: Wei Fang <[email protected]>

For i.MX94 series, all the standalone ENETCs do not support SR-IOV, so pf->caps.num_vsi is zero. This leads to a divide-by-zero in enetc4_default_rings_allocation() when distributing rings among PF and VFs.

[...]

Here is the summary with links:
- [net] net: enetc: fix potential divide-by-zero when num_vsi is zero

    https://git.kernel.org/netdev/net/c/5da65537792b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html