Skip to contentSkip to conversation

net/mlx4_en: Prevent potential integer overflow calculating Hz

PATCH Applied Dan Carpenter 3 participants 3 messages May 28, 2025 – May 29, 2025

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

PATCH v2 1/1 net/mlx4_en: Prevent potential integer overflow calculating Hz

The "freq" variable is in terms of MHz and "max_val_cycles" is in terms of Hz. The fact that "max_val_cycles" is a u64 suggests that support for high frequency is intended but the "freq_khz * 1000" would overflow the u32 type if we went above 4GHz. Use unsigned long long type for the mutliplication to prevent that.

Fixes 31c128b66e5b ("net/mlx4_en: Choose time-stamping shift value according to HW frequency")

---
v2: Use ULL instead UL.

 drivers/net/ethernet/mellanox/mlx4/en_clock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +1 -1
index cd754cd76bde..d73a2044dc26 100644
@@ -249,7 +249,7 @@ static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
 static u32 freq_to_shift(u16 freq)
 {
 	u32 freq_khz = freq * 1000;
-	u64 max_val_cycles = freq_khz * 1000 * MLX4_EN_WRAP_AROUND_SEC;
+	u64 max_val_cycles = freq_khz * 1000ULL * MLX4_EN_WRAP_AROUND_SEC;
 	u64 max_val_cycles_rounded = 1ULL << fls64(max_val_cycles - 1);
 	/* calculate max possible multiplier in order to fit in 64bit */
 	u64 max_mul = div64_u64(ULLONG_MAX, max_val_cycles_rounded);
Signature · 1 line
2.47.2
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Dan Carpenter

The "freq" variable is in terms of MHz and "max_val_cycles" is in terms of Hz. The fact that "max_val_cycles" is a u64 suggests that support for high frequency is intended but the "freq_khz * 1000" would overflow the u32 type if we went above 4GHz. Use unsigned long long type for the mutliplication to prevent that.

Fixes 31c128b66e5b ("net/mlx4_en: Choose time-stamping shift value according to HW frequency")
#2 lore raw
In reply to Dan Carpenter
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

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

Quoted 10 lines · On Wed, 28 May 2025 11:11:09 +0300 you wrote:

The "freq" variable is in terms of MHz and "max_val_cycles" is in terms of Hz. The fact that "max_val_cycles" is a u64 suggests that support for high frequency is intended but the "freq_khz * 1000" would overflow the u32 type if we went above 4GHz. Use unsigned long long type for the mutliplication to prevent that.

Fixes 31c128b66e5b ("net/mlx4_en: Choose time-stamping shift value according to HW frequency")

[...]

Here is the summary with links:
- [v2,net] net/mlx4_en: Prevent potential integer overflow calculating Hz

    https://git.kernel.org/netdev/net/c/54d34165b4f7

You are awesome, thank you!

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