Skip to contentSkip to conversation

net/mlx4_en: Prevent potential integer overflow calculating Hz

PATCH Superseded Dan Carpenter 2 participants 3 messages May 27, 2025

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 type for the mutliplication to prevent that.

Fixes 31c128b66e5b ("net/mlx4_en: Choose time-stamping shift value according to HW frequency")
---
 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..7abd6a7c9ebe 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 * 1000UL * 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
#1 lore raw

Hi,

Quoted 17 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 type for the mutliplication to prevent that.

Fixes 31c128b66e5b ("net/mlx4_en: Choose time-stamping shift value according to HW frequency")
---
 drivers/net/ethernet/mellanox/mlx4/en_clock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +0 -0
index cd754cd76bde..7abd6a7c9ebe 100644

drivers/net/ethernet/mellanox/mlx4/en_clock.c static const struct ptp_clock_info mlx4_en_ptp_clock_info = { · line 252

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx4/en_clock.c +1 -1
@@ -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 * 1000UL * MLX4_EN_WRAP_AROUND_SEC;

1000ULL would be better then.

Thanks,
Sundeep

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx4/en_clock.c +0 -0
 	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
#2 lore raw
Quoted 15 lines · Subbaraya Sundeep

Hi,

Quoted 12 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 type for the mutliplication to prevent that.

Fixes 31c128b66e5b ("net/mlx4_en: Choose time-stamping shift value according to HW frequency")
---
 drivers/net/ethernet/mellanox/mlx4/en_clock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines

drivers/net/ethernet/mellanox/mlx4/en_clock.c static const struct ptp_clock_info mlx4_en_ptp_clock_info = { · line 252

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx4/en_clock.c +1 -1
index cd754cd76bde..7abd6a7c9ebe 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 * 1000UL * MLX4_EN_WRAP_AROUND_SEC;

1000ULL would be better then.

Yeah, that's true.

regards,
dan carpenter